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

194 lines
4.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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();
}