Спойлер
/*****************/
/* eeProm_.h */
/*****************/
#pragma once
//#include <Arduino.h>
#include <EEPROM.h>
struct eeprom_t {
uint8_t key[8];
char note[8];
};
const uint8_t eeDefMarkerAddress = 14; // defMarker == 0x1812
constexpr uint8_t eeFirstAddress = eeDefMarkerAddress + sizeof(uint16_t); //
const uint8_t eeCountKeys = 4; //
constexpr uint8_t eeSizeKey = sizeof(eeprom_t); //
constexpr uint8_t eeMaxAddress = (eeFirstAddress + eeSizeKey * eeCountKeys); //
eeprom_t data_rom[eeCountKeys];
const uint16_t DEF_MARKER = 0x1812U;
const uint8_t keys_main[4][8] = {
{ 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x1A },
{ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x2A },
{ 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x3A },
{ 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00, 0x4A }
};
const char* keys_name[eeCountKeys] = { "key_01_", "key_02_", "key_03_", "key_04_" };
//*********************//
void serial_eeprom_key(const uint8_t* key, bool lineFeed = true);
void serial_eeprom_item(uint8_t addr, const eeprom_t& ee_data_rom);
void serial_eeprom_item_hex(uint8_t addr, const eeprom_t& ee_data_rom);
void eeSerialAddr();
uint16_t eeReadDefMarker();
bool eeWriteDefMarker();
uint8_t eeCalcAddress(uint8_t idx);
void eeClearROM();
void eeReadROM();
void eeWriteROM();
//*********************//
// //
//*********************//
void serial_eeprom_key(const uint8_t* key, bool lineFeed) {
for (uint8_t i = 0; i < 8; i++) {
if (key[i] < 16) Serial.print(F("0"));
Serial.print(key[i], HEX);
if (i < 7) Serial.print(F(":"));
}
if (lineFeed) Serial.println();
}
//*********************//
void serial_eeprom_item(uint8_t addr, const eeprom_t& ee_data_rom) {
Serial.print(addr);
Serial.print(F(" "));
for (uint8_t i = 0; i < 8; i++) Serial.printf("%c", ee_data_rom.note[i]);
Serial.print(F(" "));
serial_eeprom_key(ee_data_rom.key);
}
//*********************//
void serial_eeprom_item_hex(uint8_t addr, const eeprom_t& ee_data_rom) {
Serial.printf("%2d ", addr);
serial_eeprom_key((uint8_t*)ee_data_rom.note, false);
Serial.print(F(" "));
serial_eeprom_key(ee_data_rom.key);
}
//*********************//
uint16_t eeReadDefMarker() {
uint16_t _1812;
EEPROM.get(eeDefMarkerAddress, _1812);
return _1812;
}
//*********************//
bool eeWriteDefMarker() {
Serial.printf("write Marker:\n%2d 0x%04X\n", eeDefMarkerAddress, DEF_MARKER);
EEPROM.put(eeDefMarkerAddress, DEF_MARKER);
return eeReadDefMarker() == DEF_MARKER;
}
//*********************//
uint8_t eeCalcAddress(uint8_t idx) {
return eeFirstAddress + (eeSizeKey * idx);
}
//*********************//
void eeClearROM() {
const uint32_t zero = 0xFFFFFFFF;
uint8_t idx = 0;
Serial.println("eeClearROM:");
for (uint8_t i = 0; i < eeMaxAddress; i += sizeof(zero)) {
EEPROM.put(i, zero);
if (idx++ == 0) Serial.printf("%2d", i);
Serial.print(" 0x");
Serial.print(zero, HEX);
if (idx == 4) {
idx = 0;
Serial.println();
}
}
}
//*********************//
void eeReadROM() {
uint8_t addr, i;
Serial.println(F("eeReadROM:"));
Serial.printf("%2d Marker 0x%04X\n", eeDefMarkerAddress, eeReadDefMarker());
for (i = 0; i < eeCountKeys; i++) { // чтение в data_rom
addr = eeCalcAddress(i);
EEPROM.get(addr, data_rom[i]);
serial_eeprom_item(addr, data_rom[i]);
}
eeprom_t _ee;
for (i = 0; i < eeMaxAddress; i += eeSizeKey) { // чтение в _ee и вывод для перестраховки
EEPROM.get(i, _ee);
serial_eeprom_item_hex(i, _ee);
}
}
//*********************//
void eeWriteROM() {
uint8_t addr;
eeprom_t ee;
Serial.println(F("eeWriteROM:"));
memset(ee.key, 0, 8);
memset(ee.note, '\0', 8);
for (uint8_t i = 0; i < eeCountKeys; i++) {
addr = eeCalcAddress(i);
memcpy(ee.key, keys_main[i], 8);
memcpy(ee.note, keys_name[i], 7);
EEPROM.put(addr, ee);
serial_eeprom_item(addr, ee);
}
}
//*********************//