feat: add 7-segment display callsign editor; store callsign in eeprom

This commit is contained in:
s3lph 2023-04-29 00:11:10 +02:00
parent 6cc2d5e239
commit ba8924fae1
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 133 additions and 4 deletions

124
include/eepromedit.hpp Normal file
View file

@ -0,0 +1,124 @@
#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);
}
}

View file

@ -3,11 +3,15 @@
#include <util/crc16.h> #include <util/crc16.h>
#include "afsk_sinus.hpp" #include "afsk_sinus.hpp"
#include "eepromedit.hpp"
#define CALLSIGN "HB3YBQ"
#define DESTINATION "GPS " #define DESTINATION "GPS "
#define TXDELAY 32 #define TXDELAY 32
#define EDITOR_EEPROM_BASE 42
char callsign[7] = { ' ', ' ', ' ', ' ', ' ', ' ', '\0' };
uint8_t editorPinMap[8] = { 10, 11, 4, 6, 7, 8, 9, 5 };
struct ax25 { struct ax25 {
uint8_t daddr[7]; uint8_t daddr[7];
uint8_t saddr[7]; uint8_t saddr[7];
@ -99,13 +103,13 @@ void sendBell202buf(uint8_t *buf, size_t len, bool sync) {
void initFrame() { void initFrame() {
memset(&frame, 0, sizeof(frame)); memset(&frame, 0, sizeof(frame));
memcpy(&frame.daddr, DESTINATION, 6); memcpy(&frame.daddr, DESTINATION, 6);
memcpy(&frame.saddr, CALLSIGN, 6); memcpy(&frame.saddr, callsign, 6);
for (uint8_t i = 0; i < 7; ++i) { for (uint8_t i = 0; i < 7; ++i) {
frame.daddr[i] <<=1; frame.daddr[i] <<=1;
frame.saddr[i] <<=1; frame.saddr[i] <<=1;
} }
frame.daddr[6] = (NONE << 1) | 0xe0; // command bit + reserved bits (AX.25 3.12.2) frame.daddr[6] = (NONE << 1) | 0xe0; // command bit + reserved bits (AX.25 3.12.2)
frame.saddr[6] = (BICYCLE << 1) | 0xe1; // command bit + reserved bits + end bit frame.saddr[6] = (callsign[6] << 1) | 0xe1; // command bit + reserved bits + end bit
frame.ctrl = 0x03; frame.ctrl = 0x03;
frame.proto = 0xf0; frame.proto = 0xf0;
} }
@ -117,6 +121,7 @@ uint8_t mirrorByte(uint8_t byt) {
} }
void setup() { void setup() {
initEditor(callsign, 7, EDITOR_EEPROM_BASE, editorPinMap);
OUT_DDR = 0x3f; OUT_DDR = 0x3f;
setZero(); setZero();
nrzi = true; nrzi = true;
@ -168,7 +173,7 @@ void readNmeaGll() {
if (byt == '\n') { if (byt == '\n') {
state = 2; state = 2;
*sptr = 0; *sptr = 0;
if (str[3] == 'G' && str[4] == 'L' && str[5] == 'L') { if (str[3] == 'R' && str[4] == 'M' && str[5] == 'C' && *(sptr-6) == 'A') {
state = 2; state = 2;
} else { } else {
state = 0; state = 0;