Reorganized file structure to allow building Arduino project

This commit is contained in:
Dejvino
2026-02-28 21:49:17 +01:00
parent ef69701878
commit aaeaa9986e
10 changed files with 391 additions and 224 deletions
+83
View File
@@ -1,9 +1,14 @@
#include <mutex>
#include "UIThread.h"
#include "SharedState.h"
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "synth_engine.h"
#include <EEPROM.h>
extern SynthEngine* globalSynth;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
@@ -53,6 +58,32 @@ void readEncoder() {
}
}
void saveGridToEEPROM() {
if (!globalSynth) return;
uint8_t buf[SynthEngine::SERIALIZED_GRID_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.commit();
}
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);
}
globalSynth->importGrid(buf);
} else {
globalSynth->loadPreset(1); // Default to preset 1
}
}
void setupUI() {
Wire.setSDA(PIN_SDA);
Wire.setSCL(PIN_SCL);
@@ -71,6 +102,22 @@ void setupUI() {
display.clearDisplay();
display.display();
// Initialize EEPROM
EEPROM.begin(512);
// Check for safety clear (Button held on startup)
if (digitalRead(PIN_ENC_SW) == LOW) {
display.setCursor(0, 0);
display.setTextColor(SSD1306_WHITE);
display.println(F("CLEARING DATA..."));
display.display();
EEPROM.write(0, 0); // Invalidate magic
EEPROM.commit();
delay(1000);
}
loadGridFromEEPROM();
}
void handleInput() {
@@ -181,8 +228,44 @@ void drawUI() {
display.display();
}
void checkSerial() {
static int state = 0; // 0: Header, 1: Data
static int headerIdx = 0;
static const char* header = "NSGRID";
static uint8_t buffer[SynthEngine::SERIALIZED_GRID_SIZE];
static int bufferIdx = 0;
while (Serial.available()) {
uint8_t b = Serial.read();
if (state == 0) {
if (b == header[headerIdx]) {
headerIdx++;
if (headerIdx == 6) {
state = 1;
bufferIdx = 0;
headerIdx = 0;
}
} else {
headerIdx = 0;
if (b == 'N') headerIdx = 1;
}
} else if (state == 1) {
buffer[bufferIdx++] = b;
if (bufferIdx == SynthEngine::SERIALIZED_GRID_SIZE) {
if (globalSynth) {
globalSynth->importGrid(buffer);
saveGridToEEPROM();
}
state = 0;
bufferIdx = 0;
}
}
}
}
void loopUI() {
handleInput();
checkSerial();
drawUI();
delay(20); // Prevent excessive screen refresh
}