init
можно задавать с uart и бета экранчик
This commit is contained in:
		
						commit
						7075f11e2b
					
				
							
								
								
									
										193
									
								
								TecktroHack.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										193
									
								
								TecktroHack.ino
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,193 @@
 | 
			
		||||
#include <Wire.h>
 | 
			
		||||
#include <ctype.h>
 | 
			
		||||
/*
 | 
			
		||||
   Разъем 6 пинов (2 ряда по 3 пина):
 | 
			
		||||
 | 
			
		||||
     +--------------+---------+---------+--+
 | 
			
		||||
     |--------------|  none   |   SCL   |--|  <-- верхний ряд, пины 1 и 2
 | 
			
		||||
     +--------------+---------+---------+--+
 | 
			
		||||
     |--------------|  GND    |  3.3V   |--|  <-- нижний ряд, пины 3 и 4
 | 
			
		||||
     +--------------+---------+---------+--+
 | 
			
		||||
     |--------------|  none   |   SDA   |--|  <-- нижний ряд, пины 5 и 6
 | 
			
		||||
     +--------------+---------+---------+--+
 | 
			
		||||
 | 
			
		||||
   Пояснения:
 | 
			
		||||
   - SCL  = I2C Clock pin
 | 
			
		||||
   - SDA  = I2C Data pin
 | 
			
		||||
   - GND  = общий минус
 | 
			
		||||
   - 3.3V = питание EEPROM
 | 
			
		||||
   - none = не подключен
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
#define EEPROM_SIZE 256
 | 
			
		||||
#define I2C_MIN_ADDR 0x50
 | 
			
		||||
#define I2C_MAX_ADDR 0x57
 | 
			
		||||
 | 
			
		||||
#define SDA_PIN 21
 | 
			
		||||
#define SCL_PIN 22
 | 
			
		||||
 | 
			
		||||
uint8_t eeprom_addr = 0;
 | 
			
		||||
uint16_t startAddr = 0x04;  // адрес записи строк по умолчанию
 | 
			
		||||
char versionStr[32] = {0};  // буфер для версии
 | 
			
		||||
 | 
			
		||||
bool findEEPROM() {
 | 
			
		||||
  Serial.println("Scanning for EEPROM...");
 | 
			
		||||
 | 
			
		||||
  for (uint8_t addr = I2C_MIN_ADDR; addr <= I2C_MAX_ADDR; addr++) {
 | 
			
		||||
    Wire.beginTransmission(addr);
 | 
			
		||||
    if (Wire.endTransmission() == 0) {
 | 
			
		||||
      eeprom_addr = addr;
 | 
			
		||||
      Serial.print("Found EEPROM at address 0x");
 | 
			
		||||
      Serial.println(eeprom_addr, HEX);
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Serial.println("EEPROM not found!");
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void eepromWriteByte(uint8_t addr, uint8_t data) {
 | 
			
		||||
  Wire.beginTransmission(eeprom_addr);
 | 
			
		||||
  Wire.write(addr);
 | 
			
		||||
  Wire.write(data);
 | 
			
		||||
  Wire.endTransmission();
 | 
			
		||||
  delay(5);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t eepromReadByte(uint8_t addr) {
 | 
			
		||||
  Wire.beginTransmission(eeprom_addr);
 | 
			
		||||
  Wire.write(addr);
 | 
			
		||||
  Wire.endTransmission();
 | 
			
		||||
 | 
			
		||||
  Wire.requestFrom(eeprom_addr, (uint8_t)1);
 | 
			
		||||
  if (Wire.available()) {
 | 
			
		||||
    return Wire.read();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return 0xFF;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void writeStringToEEPROM(const char* str, uint16_t addr) {
 | 
			
		||||
  size_t len = strlen(str);
 | 
			
		||||
  if (addr + len + 1 > EEPROM_SIZE)
 | 
			
		||||
    len = EEPROM_SIZE - addr - 1;
 | 
			
		||||
 | 
			
		||||
  Serial.print("Writing to EEPROM @ 0x");
 | 
			
		||||
  Serial.print(addr, HEX);
 | 
			
		||||
  Serial.print(": ");
 | 
			
		||||
  Serial.println(str);
 | 
			
		||||
 | 
			
		||||
  for (size_t i = 0; i < len; i++) {
 | 
			
		||||
    eepromWriteByte(addr + i, str[i]);
 | 
			
		||||
  }
 | 
			
		||||
  eepromWriteByte(addr + len, 0);  // null-терминатор
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void readVersionFromEEPROM() {
 | 
			
		||||
  // Читаем строку версии из EEPROM начиная с 0
 | 
			
		||||
  for (size_t i = 0; i < sizeof(versionStr) - 1; i++) {
 | 
			
		||||
    uint8_t b = eepromReadByte(i);
 | 
			
		||||
    if (b == 0 || b == 0xFF) {
 | 
			
		||||
      versionStr[i] = 0;
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    versionStr[i] = (char)b;
 | 
			
		||||
  }
 | 
			
		||||
  versionStr[sizeof(versionStr)-1] = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void readAndDumpEEPROM() {
 | 
			
		||||
  Serial.println("\nDumping full EEPROM contents:\n");
 | 
			
		||||
 | 
			
		||||
  for (uint16_t base = 0; base < EEPROM_SIZE; base += 16) {
 | 
			
		||||
    char ascii[17] = {0};
 | 
			
		||||
 | 
			
		||||
    Serial.print("0x");
 | 
			
		||||
    if (base < 0x10) Serial.print('0');
 | 
			
		||||
    Serial.print(base, HEX);
 | 
			
		||||
    Serial.print(": ");
 | 
			
		||||
 | 
			
		||||
    for (uint8_t i = 0; i < 16; i++) {
 | 
			
		||||
      uint8_t val = eepromReadByte(base + i);
 | 
			
		||||
      if (val < 0x10) Serial.print('0');
 | 
			
		||||
      Serial.print(val, HEX);
 | 
			
		||||
      Serial.print(' ');
 | 
			
		||||
 | 
			
		||||
      ascii[i] = isPrintable(val) ? (char)val : '.';
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Serial.print(" |");
 | 
			
		||||
    Serial.print(ascii);
 | 
			
		||||
    Serial.println("|");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  Serial.begin(9600);
 | 
			
		||||
  while (!Serial);
 | 
			
		||||
 | 
			
		||||
  setupUI();
 | 
			
		||||
  Wire.begin(SDA_PIN, SCL_PIN);
 | 
			
		||||
 | 
			
		||||
  bool eeprom_found = findEEPROM();
 | 
			
		||||
 | 
			
		||||
  if (eeprom_found) {
 | 
			
		||||
    readVersionFromEEPROM();
 | 
			
		||||
    readAndDumpEEPROM();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Serial.println("Ready.");
 | 
			
		||||
  Serial.println("Set start address with 'ADDR=<number>'");
 | 
			
		||||
  Serial.println("Set version with 'VER=<version_string>'");
 | 
			
		||||
  Serial.println("Then type string and press ENTER to write it.");
 | 
			
		||||
  Serial.print("Current start address = 0x");
 | 
			
		||||
  Serial.println(startAddr, HEX);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
String inputBuffer;
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
  while (Serial.available()) {
 | 
			
		||||
    char c = Serial.read();
 | 
			
		||||
    if (c == '\n' || c == '\r') {
 | 
			
		||||
      inputBuffer.trim();
 | 
			
		||||
 | 
			
		||||
      if (inputBuffer.length() > 0) {
 | 
			
		||||
        if (inputBuffer.startsWith("ADDR=")) {
 | 
			
		||||
          String valStr = inputBuffer.substring(5);
 | 
			
		||||
          int val = valStr.toInt();
 | 
			
		||||
          if (val >= 0 && val < EEPROM_SIZE) {
 | 
			
		||||
            startAddr = (uint16_t)val;
 | 
			
		||||
            Serial.print("Start address set to 0x");
 | 
			
		||||
            Serial.println(startAddr, HEX);
 | 
			
		||||
          } else {
 | 
			
		||||
            Serial.println("Invalid address! Must be 0..255");
 | 
			
		||||
          }
 | 
			
		||||
        } 
 | 
			
		||||
        else if (inputBuffer.startsWith("VER=")) {
 | 
			
		||||
          String ver = inputBuffer.substring(4);
 | 
			
		||||
          ver.trim();
 | 
			
		||||
          ver.toCharArray(versionStr, sizeof(versionStr));
 | 
			
		||||
          writeStringToEEPROM(versionStr, 0);
 | 
			
		||||
          Serial.print("Version written: ");
 | 
			
		||||
          Serial.println(versionStr);
 | 
			
		||||
        } 
 | 
			
		||||
        else {
 | 
			
		||||
          writeStringToEEPROM(inputBuffer.c_str(), startAddr);
 | 
			
		||||
          delay(100);
 | 
			
		||||
          readAndDumpEEPROM();
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      inputBuffer = "";
 | 
			
		||||
      Serial.println("\nEnter command or string:");
 | 
			
		||||
    } else {
 | 
			
		||||
      inputBuffer += c;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  loopUI();
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										180
									
								
								ui_interface.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										180
									
								
								ui_interface.ino
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,180 @@
 | 
			
		||||
#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();
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user