TektroHack/ui_interface.ino
Razvalyaev 7075f11e2b init
можно задавать с uart и бета экранчик
2025-07-24 18:17:50 +03:00

181 lines
4.9 KiB
C++

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED дисплей 128x64, I2C адрес по умолчанию — 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Кнопки
#define BTN_UP_PIN 32
#define BTN_DOWN_PIN 33
#define BTN_OK_PIN 25 // #
#define BTN_BACK_PIN 26 // *
extern uint16_t startAddr;
extern void writeStringToEEPROM(const char* str, uint16_t addr);
extern void readVersionFromEEPROM();
extern char versionStr[32];
char moduleStr[32] = {0}; // буфер для считанного модуля
extern uint8_t eepromReadByte(uint8_t addr);
extern uint16_t startAddr;
enum MenuState {
MENU_MAIN,
MENU_SELECT_MODULE,
MENU_SELECT_VERSION,
MENU_SHOW_EEPROM
};
const char* moduleOptions[] = {
"DPO4COMP", "DPO2EMBD", "DPO4EMBD", "DPO4AUTO", "DPO4AUTOMAX", "DPO4PWR",
"DPO4USB", "DPO4ENET", "DPO4LMT", "DPO4AUDIO", "MSO5204B 5RL", "MDO4MSO", "MDO4EMBD",
"MDO4AERO", "MDO4AUTO", "MDO4COMP", "MDO4FLEX", "MDO4LMT", "MDO4SA", "MDO4SA3",
"MDO4SA6", "MDO4AFG", "MDO4PWR", "MDO4BND", "MDO4SEC", "MDO4AUDIO"
};
const uint8_t moduleCount = sizeof(moduleOptions) / sizeof(moduleOptions[0]);
MenuState currentMenu = MENU_MAIN;
uint8_t cursorPos = 0;
extern char versionStr[32]; // Используется из основного кода
void readModuleFromEEPROM() {
memset(moduleStr, 0, sizeof(moduleStr));
for (size_t i = 0; i < sizeof(moduleStr) - 1; i++) {
uint8_t b = eepromReadByte(startAddr + i);
if (b == 0 || b == 0xFF) {
moduleStr[i] = 0;
break;
}
moduleStr[i] = (char)b;
}
moduleStr[sizeof(moduleStr)-1] = 0;
}
void setupUI() {
pinMode(BTN_UP_PIN, INPUT_PULLUP);
pinMode(BTN_DOWN_PIN, INPUT_PULLUP);
pinMode(BTN_OK_PIN, INPUT_PULLUP);
pinMode(BTN_BACK_PIN, INPUT_PULLUP);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 not found"));
while (true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.display();
showMainMenu();
}
void loopUI() {
static uint32_t lastPress = 0;
if (millis() - lastPress < 200) return;
if (!digitalRead(BTN_UP_PIN)) {
if (cursorPos > 0) cursorPos--;
updateMenuDisplay();
lastPress = millis();
} else if (!digitalRead(BTN_DOWN_PIN)) {
cursorPos++;
updateMenuDisplay();
lastPress = millis();
} else if (!digitalRead(BTN_OK_PIN)) {
handleOk();
lastPress = millis();
} else if (!digitalRead(BTN_BACK_PIN)) {
handleBack();
lastPress = millis();
}
}
void showMainMenu() {
currentMenu = MENU_MAIN;
cursorPos = 0;
updateMenuDisplay();
}
void updateMenuDisplay() {
display.clearDisplay();
display.setCursor(0, 0);
if (currentMenu == MENU_MAIN) {
display.setCursor(0, 0);
display.print(cursorPos == 0 ? "> Module" : " Module");
display.setCursor(0, 10);
display.print(cursorPos == 1 ? "> Version" : " Version");
display.setCursor(0, 20);
display.print(cursorPos == 2 ? "> Read" : " Read");
} else if (currentMenu == MENU_SELECT_MODULE) {
display.print("Module:");
display.setCursor(0, 10);
display.print(">");
display.print(moduleOptions[cursorPos % moduleCount]);
} else if (currentMenu == MENU_SELECT_VERSION) {
display.print("Set version via UART:");
display.setCursor(0, 10);
display.print("VER=1.23");
} else if (currentMenu == MENU_SHOW_EEPROM) {
display.setCursor(0, 0);
display.print("Module: ");
display.println(moduleStr);
display.setCursor(0, 20);
display.print("Version: ");
display.println(versionStr);
}
display.display();
}
void handleOk() {
if (currentMenu == MENU_MAIN) {
if (cursorPos == 0) {
currentMenu = MENU_SELECT_MODULE;
cursorPos = 0;
} else if (cursorPos == 1) {
currentMenu = MENU_SELECT_VERSION;
} else if (cursorPos == 2) {
// Считать из EEPROM
readVersionFromEEPROM(); // версия из 0x00
readModuleFromEEPROM(); // модуль из startAddr
currentMenu = MENU_SHOW_EEPROM;
}
}
else if (currentMenu == MENU_SELECT_MODULE) {
const char* sel = moduleOptions[cursorPos % moduleCount];
writeStringToEEPROM(sel, startAddr);
readAndDumpEEPROM();
strncpy(versionStr, sel, sizeof(versionStr) - 1);
versionStr[sizeof(versionStr) - 1] = 0;
Serial.print("[UI] Module written to EEPROM: ");
Serial.println(sel);
showMainMenu();
}
else if (currentMenu == MENU_SELECT_VERSION) {
writeStringToEEPROM("v1.23", 0);
readAndDumpEEPROM();
strncpy(versionStr, "v1.23", sizeof(versionStr) - 1);
versionStr[sizeof(versionStr) - 1] = 0;
Serial.print("[UI] Version written to EEPROM: ");
Serial.println(versionStr);
showMainMenu();
}
updateMenuDisplay();
}
void handleBack() {
showMainMenu();
}