APRSuino/include/eepromedit.hpp

125 lines
3 KiB
C++
Raw Normal View History

#include <Arduino.h>
#include <EEPROM.h>
#define ALPHABET_SIZE 37
#define EEPROM_BASE 42
// 6
// 4 5
// 3
// 1 2
// 0
uint8_t dispMap[ALPHABET_SIZE+1] = { 0x00, 0x77, 0x24, 0x6b, 0x6d, 0x3c, 0x5d, 0x5f, 0x64, 0x7f, 0x7d, 0x7e, 0x1f, 0x53, 0x2f, 0x5b, 0x5a, 0x57, 0x1e, 0x12, 0x25, 0x3c, 0x13, 0x76, 0x0e, 0x0f, 0x7a, 0x7c, 0x0a, 0x55, 0x1b, 0x07, 0x37, 0x3f, 0x06, 0x3d, 0x63 };
char charMap[ALPHABET_SIZE] = { ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
uint8_t *pinMap;
uint8_t state = 0;
volatile bool advanceState = 0;
volatile bool advanceDigit = 0;
void nextState() {
advanceState = true;
}
void cycleDigit() {
advanceDigit = true;
}
void showDigit(char digit, bool dot) {
// add 0 for SSID hexdump
if (digit < 10) {
digit += '0';
} else if (digit < 16) {
digit += 'A' - 10;
}
uint8_t output = 0x08;
for (uint8_t i = 0; i < ALPHABET_SIZE; ++i) {
if (charMap[i] == digit) {
output = dispMap[i];
break;
}
}
if (dot) {
output |= 0x80;
}
for (uint8_t i = 0; i < 8; ++i) {
digitalWrite(pinMap[i], (output >> i) & 1);
}
}
void loadFromEeprom(char *str, uint8_t len, uint8_t eeprom_base) {
if (EEPROM.read(eeprom_base) != 0x42) {
for (uint8_t i = 0; i < len; ++i) {
EEPROM.update(eeprom_base+1+i, str[i]);
}
// Write EEPROM init flag last
EEPROM.write(eeprom_base, 0x42);
}
for (uint8_t i = 0; i < len; ++i) {
str[i] = EEPROM.read(eeprom_base+1+i);
}
}
void clearDisplay() {
showDigit(' ', false);
}
void display(char *str, uint8_t len) {
for (uint8_t i = 0; i < len; ++i) {
showDigit(str[i], false);
delay(1000);
}
clearDisplay();
}
void editor(char *str, uint8_t len, uint16_t eeprom_base) {
attachInterrupt(digitalPinToInterrupt(2), nextState, FALLING);
attachInterrupt(digitalPinToInterrupt(3), cycleDigit, FALLING);
while (true) {
if (advanceDigit) {
advanceDigit = false;
if (str[state] < 16) {
str[state] = (str[state] + 1) % 16;
} else {
for (uint8_t i = 0; i < ALPHABET_SIZE; ++i) {
if (charMap[i] == str[state]) {
str[state] = charMap[(i+1)%ALPHABET_SIZE];
break;
}
}
}
}
if (advanceState) {
advanceState = false;
state++;
}
if (state >= len) {
for (uint8_t i = 0; i < len; ++i) {
EEPROM.update(eeprom_base+1+i, str[i]);
}
display(str, len);
return;
} else {
showDigit(str[state], true);
}
}
}
void initEditor(char *str, uint8_t len, uint16_t eeprom_base, uint8_t *initPinMap) {
loadFromEeprom(str, len, eeprom_base);
pinMap = initPinMap;
for (uint8_t i = 0; i < 8; ++i) {
pinMode(pinMap[i], OUTPUT);
digitalWrite(pinMap[i], LOW);
}
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
delay(500);
if (!digitalRead(2)) {
while (!digitalRead(2));
editor(str, len, eeprom_base);
} else {
display(str, len);
}
}