🎨 Misc. DGUS cleanup

This commit is contained in:
Scott Lahteine
2022-02-10 16:03:48 -06:00
parent f2d1770cee
commit 3d2b2ca790
11 changed files with 48 additions and 36 deletions

View File

@@ -46,6 +46,12 @@
DGUSDisplay dgusdisplay;
#ifdef DEBUG_DGUSLCD_COMM
#define DEBUGLCDCOMM_ECHOPGM DEBUG_ECHOPGM
#else
#define DEBUGLCDCOMM_ECHOPGM(...) NOOP
#endif
// Preamble... 2 Bytes, usually 0x5A 0xA5, but configurable
constexpr uint8_t DGUS_HEADER1 = 0x5A;
constexpr uint8_t DGUS_HEADER2 = 0xA5;
@@ -154,19 +160,19 @@ void DGUSDisplay::ProcessRx() {
case DGUS_IDLE: // Waiting for the first header byte
receivedbyte = LCD_SERIAL.read();
//DEBUG_ECHOPGM("< ",x);
//DEBUGLCDCOMM_ECHOPGM("< ", receivedbyte);
if (DGUS_HEADER1 == receivedbyte) rx_datagram_state = DGUS_HEADER1_SEEN;
break;
case DGUS_HEADER1_SEEN: // Waiting for the second header byte
receivedbyte = LCD_SERIAL.read();
//DEBUG_ECHOPGM(" ",x);
//DEBUGLCDCOMM_ECHOPGM(" ", receivedbyte);
rx_datagram_state = (DGUS_HEADER2 == receivedbyte) ? DGUS_HEADER2_SEEN : DGUS_IDLE;
break;
case DGUS_HEADER2_SEEN: // Waiting for the length byte
rx_datagram_len = LCD_SERIAL.read();
DEBUG_ECHOPGM(" (", rx_datagram_len, ") ");
//DEBUGLCDCOMM_ECHOPGM(" (", rx_datagram_len, ") ");
// Telegram min len is 3 (command and one word of payload)
rx_datagram_state = WITHIN(rx_datagram_len, 3, DGUS_RX_BUFFER_SIZE) ? DGUS_WAIT_TELEGRAM : DGUS_IDLE;
@@ -178,20 +184,20 @@ void DGUSDisplay::ProcessRx() {
Initialized = true; // We've talked to it, so we defined it as initialized.
uint8_t command = LCD_SERIAL.read();
DEBUG_ECHOPGM("# ", command);
//DEBUGLCDCOMM_ECHOPGM("# ", command);
uint8_t readlen = rx_datagram_len - 1; // command is part of len.
unsigned char tmp[rx_datagram_len - 1];
unsigned char *ptmp = tmp;
while (readlen--) {
receivedbyte = LCD_SERIAL.read();
DEBUG_ECHOPGM(" ", receivedbyte);
//DEBUGLCDCOMM_ECHOPGM(" ", receivedbyte);
*ptmp++ = receivedbyte;
}
DEBUG_ECHOPGM(" # ");
//DEBUGLCDCOMM_ECHOPGM(" # ");
// mostly we'll get this: 5A A5 03 82 4F 4B -- ACK on 0x82, so discard it.
if (command == DGUS_CMD_WRITEVAR && 'O' == tmp[0] && 'K' == tmp[1]) {
DEBUG_ECHOLNPGM(">");
//DEBUGLCDCOMM_ECHOPGM(">");
rx_datagram_state = DGUS_IDLE;
break;
}
@@ -253,16 +259,16 @@ void DGUSDisplay::loop() {
rx_datagram_state_t DGUSDisplay::rx_datagram_state = DGUS_IDLE;
uint8_t DGUSDisplay::rx_datagram_len = 0;
bool DGUSDisplay::Initialized = false;
bool DGUSDisplay::no_reentrance = false;
bool DGUSDisplay::Initialized = false,
DGUSDisplay::no_reentrance = false;
// A SW memory barrier, to ensure GCC does not overoptimize loops
#define sw_barrier() asm volatile("": : :"memory");
bool populate_VPVar(const uint16_t VP, DGUS_VP_Variable * const ramcopy) {
// DEBUG_ECHOPGM("populate_VPVar ", VP);
//DEBUG_ECHOPGM("populate_VPVar ", VP);
const DGUS_VP_Variable *pvp = DGUSLCD_FindVPVar(VP);
// DEBUG_ECHOLNPGM(" pvp ", (uint16_t )pvp);
//DEBUG_ECHOLNPGM(" pvp ", (uint16_t )pvp);
if (!pvp) return false;
memcpy_P(ramcopy, pvp, sizeof(DGUS_VP_Variable));
return true;