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
+43 -19
View File
@@ -60,13 +60,15 @@ void readEncoder() {
void saveGridToEEPROM() {
if (!globalSynth) return;
uint8_t buf[SynthEngine::SERIALIZED_GRID_SIZE];
globalSynth->exportGrid(buf);
uint8_t buf[SynthEngine::MAX_SERIALIZED_GRID_SIZE];
size_t size = globalSynth->exportGrid(buf);
EEPROM.write(0, 'N');
EEPROM.write(1, 'S');
for (size_t i = 0; i < sizeof(buf); i++) {
EEPROM.write(2 + i, buf[i]);
EEPROM.write(2, (size >> 8) & 0xFF);
EEPROM.write(3, size & 0xFF);
for (size_t i = 0; i < size; i++) {
EEPROM.write(4 + i, buf[i]);
}
EEPROM.commit();
}
@@ -74,11 +76,14 @@ void saveGridToEEPROM() {
void loadGridFromEEPROM() {
if (!globalSynth) return;
if (EEPROM.read(0) == 'N' && EEPROM.read(1) == 'S') {
uint8_t buf[SynthEngine::SERIALIZED_GRID_SIZE];
for (size_t i = 0; i < sizeof(buf); i++) {
buf[i] = EEPROM.read(2 + i);
size_t size = (EEPROM.read(2) << 8) | EEPROM.read(3);
if (size > SynthEngine::MAX_SERIALIZED_GRID_SIZE) size = SynthEngine::MAX_SERIALIZED_GRID_SIZE;
uint8_t buf[SynthEngine::MAX_SERIALIZED_GRID_SIZE];
for (size_t i = 0; i < size; i++) {
buf[i] = EEPROM.read(4 + i);
}
globalSynth->importGrid(buf);
globalSynth->importGrid(buf, size);
} else {
globalSynth->loadPreset(1); // Default to preset 1
}
@@ -104,7 +109,7 @@ void setupUI() {
display.display();
// Initialize EEPROM
EEPROM.begin(512);
EEPROM.begin(1024);
// Check for safety clear (Button held on startup)
if (digitalRead(PIN_ENC_SW) == LOW) {
@@ -269,13 +274,14 @@ void drawUI() {
}
void checkSerial() {
static int state = 0; // 0: Header, 1: Data
static int state = 0; // 0: Header, 1: Count, 2: Data, 3: EndCount
static int headerIdx = 0;
static const char* header = "NSGRID";
static int loadHeaderIdx = 0;
static const char* loadHeader = "NSLOAD";
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;
while (Serial.available()) {
uint8_t b = Serial.read();
@@ -283,7 +289,7 @@ void checkSerial() {
if (b == header[headerIdx]) {
headerIdx++;
if (headerIdx == 6) {
state = 1;
state = 1; // Expect count next
bufferIdx = 0;
headerIdx = 0;
loadHeaderIdx = 0;
@@ -298,10 +304,10 @@ void checkSerial() {
loadHeaderIdx++;
if (loadHeaderIdx == 6) {
if (globalSynth) {
uint8_t buf[SynthEngine::SERIALIZED_GRID_SIZE];
globalSynth->exportGrid(buf);
static uint8_t exportBuf[SynthEngine::MAX_SERIALIZED_GRID_SIZE];
size_t size = globalSynth->exportGrid(exportBuf);
Serial.write("NSGRID", 6);
Serial.write(buf, sizeof(buf));
Serial.write(exportBuf, size);
Serial.flush();
}
loadHeaderIdx = 0;
@@ -312,11 +318,29 @@ void checkSerial() {
if (b == 'N') loadHeaderIdx = 1;
}
}
} else if (state == 1) {
} else if (state == 1) { // Count
elementCount = b;
if (1 + elementCount * 5 > sizeof(buffer)) {
state = 0;
bufferIdx = 0;
Serial.println(F("ERROR: Grid too large"));
} else {
buffer[bufferIdx++] = b;
state = 2;
}
} else if (state == 2) { // Data
buffer[bufferIdx++] = b;
if (bufferIdx == SynthEngine::SERIALIZED_GRID_SIZE) {
if (globalSynth) {
globalSynth->importGrid(buffer);
if (bufferIdx == 1 + elementCount * 5) {
state = 3;
}
} else if (state == 3) { // End Count
buffer[bufferIdx++] = b;
if (globalSynth) {
int result = globalSynth->importGrid(buffer, bufferIdx);
if (result != 0) {
Serial.print("CRC ERROR "); Serial.println(result);
globalSynth->clearGrid();
} else {
saveGridToEEPROM();
Serial.println(F("OK: Grid Received"));
}