diff options
author | Daniel Friesel <derf@finalrewind.org> | 2016-02-16 18:33:49 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2016-02-16 18:33:49 +0100 |
commit | cc23ccbec8bb37ed78567410b60b23e8cd2603a9 (patch) | |
tree | 691a41ccd7943098ca433592608f29c5624d14d1 /src/system.cc | |
parent | 541b0f27054f27e661a71aa465e2df6e5a1c0b86 (diff) |
prepare state machine to receive animation messages
Diffstat (limited to 'src/system.cc')
-rw-r--r-- | src/system.cc | 73 |
1 files changed, 59 insertions, 14 deletions
diff --git a/src/system.cc b/src/system.cc index 537f0b9..bee1791 100644 --- a/src/system.cc +++ b/src/system.cc @@ -16,6 +16,7 @@ System rocket; extern animation_t ohai; uint8_t disp_buf[128]; +uint8_t *rx_buf = disp_buf + 64; void System::initialize() { @@ -35,10 +36,65 @@ void System::initialize() sei(); } +void System::receive(void) +{ + static uint8_t rx_pos = 0; + static uint16_t remaining_bytes = 0; + uint8_t rx_byte = modem.buffer_get(); + + if (rxExpect > PATTERN2) { + rx_buf[rx_pos] = modem.buffer_get(); + } + + switch(rxExpect) { + case START1: + if (rx_byte == 0x99) + rxExpect = START2; + break; + case START2: + if (rx_byte == 0x99) + rxExpect = PATTERN1; + break; + case PATTERN1: + if (rx_byte == 0xa9) + rxExpect = PATTERN2; + break; + case PATTERN2: + if (rx_byte == 0xa9) + rxExpect = HEADER1; + break; + case HEADER1: + rxExpect = HEADER2; + break; + case HEADER2: + rxExpect = META1; + break; + case META1: + rxExpect = META2; + break; + case META2: + rxExpect = DATA; + break; + case DATA: + break; + } + + /* + if (i == 127) { + i = 0; + } else if (modem_byte == 0) { + if (i > 1) { // workaround for trailing double null bytes + ohai.data = disp_buf; + ohai.length = i-1; + display.show(&ohai); + } + i = 0; + } + */ +} + void System::loop() { - static uint8_t i = 0; - uint8_t modem_byte; // both buttons are pressed if ((PINC & (_BV(PC3) | _BV(PC7))) == 0) { // naptime! @@ -62,18 +118,7 @@ void System::loop() } while (modem.buffer_available()) { - modem_byte = modem.buffer_get(); - disp_buf[i++] = modem_byte; - if (i == 127) { - i = 0; - } else if (modem_byte == 0) { - if (i > 1) { // workaround for trailing double null bytes - ohai.data = disp_buf; - ohai.length = i-1; - display.show(&ohai); - } - i = 0; - } + receive(); } display.update(); |