consolidate all repos to one for archive
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
|
||||
class serial
|
||||
{
|
||||
public:
|
||||
serial();
|
||||
~serial();
|
||||
bool init(const char *path);
|
||||
int swrite(uint8_t *data, int size);
|
||||
int sread(uint8_t *data, int size);
|
||||
int sioctl();
|
||||
|
||||
private:
|
||||
int serial_port = 0;
|
||||
};
|
@@ -0,0 +1,101 @@
|
||||
#include "serial.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <string.h>
|
||||
|
||||
void print_man()
|
||||
{
|
||||
printf("Commands:\n");
|
||||
printf(" 0: Exit\n");
|
||||
printf(" 1: LED_ON\n");
|
||||
printf(" 2: LED_OFF\n");
|
||||
printf(" 3: ANIMATE_ON\n");
|
||||
printf(" 4: ANIMATE_OFF\n");
|
||||
}
|
||||
|
||||
int print_led_num()
|
||||
{
|
||||
int led = 1;
|
||||
printf("Enter LED number [0..7] or 8 for all: ");
|
||||
std::cin >> led;
|
||||
|
||||
while (led > 8)
|
||||
{
|
||||
printf("Invalid LED number.\n");
|
||||
printf("Enter LED number [0..7] or 8 for all: ");
|
||||
std::cin >> led;
|
||||
}
|
||||
led = 1 << led;
|
||||
if (led == 256)
|
||||
led--;
|
||||
return led;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
serial s;
|
||||
if (!s.init("/dev/ttyACM0"))
|
||||
return 1;
|
||||
|
||||
uint8_t txbuff[5] = {0xAA, 0x55, 0, 0, 0};
|
||||
uint8_t rxbuff[4] = {0};
|
||||
|
||||
s.swrite(txbuff, 3);
|
||||
s.sread(rxbuff, sizeof(rxbuff));
|
||||
int r = *(int *)rxbuff;
|
||||
if (r != 1)
|
||||
{
|
||||
printf("No hand shake\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
bool running = true;
|
||||
int cmd = 0;
|
||||
int size = 0;
|
||||
|
||||
while (running)
|
||||
{
|
||||
print_man();
|
||||
printf("Enter command: ");
|
||||
std::cin >> cmd;
|
||||
switch (cmd)
|
||||
{
|
||||
case 0:
|
||||
running = false;
|
||||
txbuff[2] = 0;
|
||||
size = 3;
|
||||
break;
|
||||
case 1:
|
||||
txbuff[2] = 2;
|
||||
txbuff[3] = 1;
|
||||
txbuff[4] = print_led_num();
|
||||
size = 5;
|
||||
break;
|
||||
case 2:
|
||||
txbuff[2] = 2;
|
||||
txbuff[3] = 2;
|
||||
txbuff[4] = print_led_num();
|
||||
size = 5;
|
||||
break;
|
||||
case 3:
|
||||
txbuff[2] = 1;
|
||||
txbuff[3] = 3;
|
||||
size = 4;
|
||||
break;
|
||||
case 4:
|
||||
txbuff[2] = 1;
|
||||
txbuff[3] = 4;
|
||||
size = 4;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
s.swrite(txbuff, size);
|
||||
s.sread(rxbuff, sizeof(rxbuff));
|
||||
r = rxbuff[0];
|
||||
if (r != 1)
|
||||
printf("No ack\n");
|
||||
}
|
||||
|
||||
return 0; // success
|
||||
};
|
@@ -0,0 +1,27 @@
|
||||
CC=g++
|
||||
|
||||
CFLAGS= -std=c++23
|
||||
|
||||
INCLUDE_DIR= include
|
||||
|
||||
SRC_DIR= src
|
||||
|
||||
# Get all cpp files from src directory
|
||||
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
|
||||
|
||||
all: main run
|
||||
|
||||
main: main.cpp $(SRCS)
|
||||
$(CC) $(CFLAGS) -o3 -I$(INCLUDE_DIR) $(SRCS) main.cpp -o main
|
||||
|
||||
debug: main.cpp $(SRCS)
|
||||
$(CC) $(CFLAGS) -I$(INCLUDE_DIR) $(SRCS) -g main.cpp -o main
|
||||
|
||||
run: main
|
||||
./main
|
||||
|
||||
zip:
|
||||
zip -r main.zip main.cpp $(INCLUDE_DIR) $(SRC_DIR) makefile
|
||||
|
||||
clean:
|
||||
rm main
|
@@ -0,0 +1,97 @@
|
||||
#include "serial.h"
|
||||
|
||||
// C library headers
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Linux headers
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <errno.h> // Error integer and strerror() function
|
||||
#include <termios.h> // Contains POSIX terminal control definitions
|
||||
#include <unistd.h> // write(), read(), close()
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
serial::serial()
|
||||
{
|
||||
}
|
||||
|
||||
serial::~serial()
|
||||
{
|
||||
close(serial_port);
|
||||
}
|
||||
|
||||
bool serial::init(const char *path)
|
||||
{
|
||||
// Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device)
|
||||
serial_port = open(path, O_RDWR);
|
||||
|
||||
if (serial_port < 0)
|
||||
{
|
||||
printf("Error %i from open: %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
// Create new termios struct, we call it 'tty' for convention
|
||||
struct termios tty;
|
||||
|
||||
// Read in existing settings, and handle any error
|
||||
if (tcgetattr(serial_port, &tty) != 0)
|
||||
{
|
||||
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
|
||||
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
|
||||
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
|
||||
tty.c_cflag |= CS8; // 8 bits per byte (most common)
|
||||
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
|
||||
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
|
||||
|
||||
tty.c_lflag &= ~ICANON;
|
||||
tty.c_lflag &= ~ECHO; // Disable echo
|
||||
tty.c_lflag &= ~ECHOE; // Disable erasure
|
||||
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
||||
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
|
||||
|
||||
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
|
||||
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
||||
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
|
||||
// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)
|
||||
|
||||
tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
|
||||
tty.c_cc[VMIN] = 0;
|
||||
|
||||
// Set in/out baud rate to be 9600
|
||||
cfsetispeed(&tty, 48000);
|
||||
cfsetospeed(&tty, 48000);
|
||||
|
||||
// Save tty settings, also checking for error
|
||||
if (tcsetattr(serial_port, TCSANOW, &tty) != 0)
|
||||
{
|
||||
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
// Here we assume we received ASCII data, but you might be sending raw bytes (in that case, don't try and
|
||||
// print it to the screen like this!)
|
||||
}
|
||||
|
||||
int serial::swrite(uint8_t *data, int size)
|
||||
{
|
||||
return write(serial_port, data, size);
|
||||
}
|
||||
|
||||
int serial::sread(uint8_t *data, int size)
|
||||
{
|
||||
return read(serial_port, data, size);
|
||||
}
|
||||
|
||||
int serial::sioctl()
|
||||
{
|
||||
int bytes;
|
||||
ioctl(serial_port, FIONREAD, &bytes);
|
||||
return bytes;
|
||||
}
|
Reference in New Issue
Block a user