Fix terminal export import

This commit is contained in:
Dejvino
2026-03-01 14:08:41 +01:00
parent 1047d846f9
commit 14ac4401ce
4 changed files with 178 additions and 52 deletions
+76 -14
View File
@@ -15,6 +15,7 @@
#if !defined(_WIN32)
#include <sys/select.h>
#include <unistd.h>
#include <termios.h>
#endif
// --- Configuration ---
@@ -98,37 +99,60 @@ void checkSerialInput(FILE* serialPort) {
return;
#endif
printf("Grid import maybe?\n");
static int state = 0;
static int headerIdx = 0;
static const char* header = "NSGRID";
static uint8_t buffer[SynthEngine::SERIALIZED_GRID_SIZE];
static uint8_t buffer[SynthEngine::MAX_SERIALIZED_GRID_SIZE];
static int bufferIdx = 0;
static uint8_t elementCount = 0;
for (ssize_t i = 0; i < n; ++i) {
uint8_t b = buf[i];
if (state == 0) {
if (b == header[headerIdx]) {
if (b == (uint8_t)header[headerIdx]) {
headerIdx++;
if (headerIdx == 6) {
state = 1;
state = 1; // Expect count
bufferIdx = 0;
headerIdx = 0;
printf("Grid import starting.\n");
}
} else {
if (headerIdx > 0) {
printf("Header mismatch at index %d. Received: %02X\n", headerIdx, b);
}
headerIdx = 0;
if (b == 'N') headerIdx = 1;
}
} else if (state == 1) {
buffer[bufferIdx++] = b;
if (bufferIdx == SynthEngine::SERIALIZED_GRID_SIZE) {
engine.importGrid(buffer);
printf("Grid imported from serial.\n");
} else if (state == 1) { // Count
elementCount = b;
printf("Grid element count: %d\n", elementCount);
if (1 + elementCount * 5 + 1 > sizeof(buffer)) {
state = 0;
bufferIdx = 0;
printf("ERROR: Grid too large (count: %d)\n", elementCount);
} else {
buffer[bufferIdx++] = b;
state = (elementCount == 0) ? 3 : 2;
}
} else if (state == 2) { // Data
buffer[bufferIdx++] = b;
if (bufferIdx == 1 + elementCount * 5) {
state = 3;
}
} else if (state == 3) { // End Count
buffer[bufferIdx++] = b;
printf("Grid import finishing. Total bytes: %d. End count: %d\n", bufferIdx, b);
int result = engine.importGrid(buffer, bufferIdx);
if (result != 0) {
printf("Grid import failed: CRC ERROR %d\n", result);
engine.clearGrid();
} else {
printf("Grid imported from serial successfully.\n");
}
state = 0;
bufferIdx = 0;
}
}
}
@@ -986,7 +1010,45 @@ int main(int argc, char* argv[]) {
if (argc > 1) {
serialPort = fopen(argv[1], "r+b");
if (serialPort) printf("Opened serial port: %s\n", argv[1]);
if (serialPort) {
printf("Opened serial port: %s\n", argv[1]);
#if !defined(_WIN32)
int fd = fileno(serialPort);
struct termios tty;
if (tcgetattr(fd, &tty) != 0) {
printf("Error from tcgetattr\n");
} else {
cfsetospeed(&tty, B115200);
cfsetispeed(&tty, B115200);
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; // Disable canonical mode
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_cc[VTIME] = 0; // No blocking with timeout
tty.c_cc[VMIN] = 0; // Non-blocking read
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr\n");
}
}
#endif
}
else printf("Failed to open serial port: %s\n", argv[1]);
}
@@ -1230,10 +1292,10 @@ int main(int argc, char* argv[]) {
my >= exportButtonRect.y && my <= exportButtonRect.y + exportButtonRect.h) {
if (serialPort) {
uint8_t buf[SynthEngine::SERIALIZED_GRID_SIZE];
engine.exportGrid(buf);
uint8_t buf[SynthEngine::MAX_SERIALIZED_GRID_SIZE];
size_t size = engine.exportGrid(buf);
fwrite("NSGRID", 1, 6, serialPort);
fwrite(buf, 1, sizeof(buf), serialPort);
fwrite(buf, 1, size, serialPort);
fflush(serialPort);
printf("Grid exported to serial. Waiting for response...\n");