From cc23ccbec8bb37ed78567410b60b23e8cd2603a9 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 16 Feb 2016 18:33:49 +0100 Subject: prepare state machine to receive animation messages --- src/storage.cc | 34 +++++++++++++++++++++++++++ src/storage.h | 8 ++++++- src/system.cc | 73 +++++++++++++++++++++++++++++++++++++++++++++++----------- src/system.h | 18 ++++++++++++++- 4 files changed, 117 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/storage.cc b/src/storage.cc index 4decc27..71265a8 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -46,6 +46,8 @@ void Storage::enable() */ TWSR = 0; // the lower two bits control TWPS TWBR = ((F_CPU / 100000UL) - 16) / 2; + + i2c_read(0, 1, &num_anims); } @@ -168,3 +170,35 @@ int8_t Storage::i2c_read(uint16_t pos, uint8_t len, uint8_t *data) return 0; // TODO proper return code to indicate read/write errors } + +void Storage::reset() +{ + uint8_t data = 0; + i2c_write(0, 1, &data); + first_free_page = 0; +} + +void Storage::load(uint16_t idx, uint8_t *data) +{ + uint8_t page_offset; + uint8_t header[2]; + i2c_read(1 + idx, 1, &page_offset); + + i2c_read(256 + (64 * (uint16_t)page_offset), 2, header); + i2c_read(256 + (64 * (uint16_t)page_offset) + 2, (header[0] << 4) + (header[1] >> 4), data); +} + +void Storage::save(uint16_t idx, uint8_t *data) +{ + i2c_write(1 + idx, 1, &first_free_page); + append(data); +} + +void Storage::append(uint8_t *data) +{ + // the header indicates the length of the data, but we really don't care + // - it's easier to just write the whole page and skip the trailing + // garbage when reading. + i2c_write(256 + (64 * (uint16_t)first_free_page), 64, data); + first_free_page++; +} diff --git a/src/storage.h b/src/storage.h index 011406d..4336d34 100644 --- a/src/storage.h +++ b/src/storage.h @@ -4,6 +4,8 @@ class Storage { private: + uint8_t num_anims; + uint8_t first_free_page; void i2c_start_write(void); void i2c_start_read(void); void i2c_stop(void); @@ -13,8 +15,12 @@ class Storage { int8_t i2c_write(uint16_t addr, uint8_t len, uint8_t *data); // TODO "file system" housekeeping (index of first free page) public: - Storage() {}; + Storage() { num_anims = 0xff; first_free_page = 0;}; void enable(); + void reset(); + void load(uint16_t idx, uint8_t *data); + void save(uint16_t idx, uint8_t *data); + void append(uint8_t *data); // TODO load / save methods for animations }; 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(); diff --git a/src/system.h b/src/system.h index 7c0ab80..a2aad25 100644 --- a/src/system.h +++ b/src/system.h @@ -10,8 +10,24 @@ class System { private: uint16_t want_shutdown; void shutdown(void); + void receive(void); + + enum RxExpect : uint8_t { + START1, + START2, + PATTERN1, + PATTERN2, + HEADER1, + HEADER2, + META1, + META2, + DATA + }; + + RxExpect rxExpect; + public: - System() { want_shutdown = 0; }; + System() { want_shutdown = 0; rxExpect = START1; }; /** * Initial MCU setup. Turns off unused peripherals to save power -- cgit v1.2.3