From bffcdb4987acb5a49e2756df355ae9d55fdb112d Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 17 Feb 2016 17:17:12 +0100 Subject: more modem receive state machine logic --- src/storage.cc | 10 ++++++---- src/storage.h | 3 +-- src/system.cc | 25 ++++++++++++++++++++++++- src/system.h | 3 ++- 4 files changed, 33 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/storage.cc b/src/storage.cc index 71265a8..4afe1d8 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -173,9 +173,9 @@ int8_t Storage::i2c_read(uint16_t pos, uint8_t len, uint8_t *data) void Storage::reset() { - uint8_t data = 0; - i2c_write(0, 1, &data); first_free_page = 0; + num_anims = 0; + i2c_write(0, 1, &num_anims); } void Storage::load(uint16_t idx, uint8_t *data) @@ -188,9 +188,11 @@ void Storage::load(uint16_t idx, uint8_t *data) 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) +void Storage::save(uint8_t *data) { - i2c_write(1 + idx, 1, &first_free_page); + num_anims++; + i2c_write(0, 1, &num_anims); + i2c_write(num_anims, 1, &first_free_page); append(data); } diff --git a/src/storage.h b/src/storage.h index 656d13e..10cdbc5 100644 --- a/src/storage.h +++ b/src/storage.h @@ -47,10 +47,9 @@ class Storage { * dattern data will be read and stored, regardless of the * pattern header. * - * @param idx pattern index (subjec to change!) * @param data pattern data. Must be at least 64 Bytes */ - void save(uint16_t idx, uint8_t *data); // TODO probably better without idx + void save(uint8_t *data); /** * Continue saving a pattern on the EEPROM. Appends 64 bytes of diff --git a/src/system.cc b/src/system.cc index 5e705a6..864f98a 100644 --- a/src/system.cc +++ b/src/system.cc @@ -36,6 +36,7 @@ void System::initialize() sei(); } +// ! This function has not been tested yet void System::receive(void) { static uint8_t rx_pos = 0; @@ -72,17 +73,39 @@ void System::receive(void) break; case HEADER1: rxExpect = HEADER2; + rx_pos = 0; + remaining_bytes = (rx_byte & 0x0f) << 8; break; case HEADER2: rxExpect = META1; + remaining_bytes += rx_byte; break; case META1: rxExpect = META2; break; case META2: - rxExpect = DATA; + rxExpect = DATA_FIRSTBLOCK; + break; + case DATA_FIRSTBLOCK: + remaining_bytes--; + if (remaining_bytes == 0) { + rxExpect = PATTERN1; // TODO or new START1 + storage.save(rx_buf); + } else if (rx_pos == 64) { + rxExpect = DATA; + rx_pos = 0; + storage.save(rx_buf); + } break; case DATA: + remaining_bytes--; + if (remaining_bytes == 0) { + rxExpect = PATTERN1; // TODO or new START1 + storage.append(rx_buf); + } else if (rx_pos == 64) { + rx_pos = 0; + storage.append(rx_buf); + } break; } diff --git a/src/system.h b/src/system.h index a2aad25..764ef95 100644 --- a/src/system.h +++ b/src/system.h @@ -21,7 +21,8 @@ class System { HEADER2, META1, META2, - DATA + DATA_FIRSTBLOCK, + DATA, }; RxExpect rxExpect; -- cgit v1.2.3