From 3a02241ad1fbd89a6603fb699989cb9f3c9b200d Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Sun, 21 Feb 2016 20:19:55 +0100 Subject: add animation load function -- untested, needs I2C debugging first --- src/main.cc | 24 +-------------------- src/storage.cc | 5 +++-- src/system.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/system.h | 4 +++- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/src/main.cc b/src/main.cc index ae9ebc5..b42c12f 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,34 +1,12 @@ +#include #include -#include "display.h" -#include "font.h" -#include "storage.h" -#include "fecmodem.h" #include "system.h" -animation_t ohai; - int main (void) { - ohai.type = AnimationType::TEXT; - ohai.speed = (2 << 4) + 15; - ohai.data = (uint8_t *)" Ohai \001"; - ohai.length = 8; - ohai.delay = (0 << 4) + 0; // -> 4bit 0 remains 0 - - uint8_t anim_data[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; - - animation_t test; - test.type = AnimationType::FRAMES; - test.speed = 14; - test.length = 2*8; - test.data = anim_data; - test.delay = (1 << 4); - rocket.initialize(); - display.show(&ohai); - while (1) { // nothing to do here, go to idle to save power SMCR = _BV(SE); diff --git a/src/storage.cc b/src/storage.cc index 98a90f0..a517f53 100644 --- a/src/storage.cc +++ b/src/storage.cc @@ -186,8 +186,9 @@ uint8_t Storage::i2c_read(uint16_t pos, uint8_t len, uint8_t *data) void Storage::reset() { first_free_page = 0; + num_anims = 0xff; + i2c_write(0, 1, &num_anims); // pretend the EEPROM was never written to num_anims = 0; - i2c_write(0, 1, &num_anims); } bool Storage::hasData() @@ -205,7 +206,7 @@ void Storage::load(uint16_t idx, uint8_t *data) 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); + i2c_read(256 + (64 * (uint16_t)page_offset), header[1] + 2, data); } void Storage::save(uint8_t *data) diff --git a/src/system.cc b/src/system.cc index 3dc21d9..dd09b5a 100644 --- a/src/system.cc +++ b/src/system.cc @@ -13,9 +13,9 @@ System rocket; -extern animation_t ohai; +animation_t active_anim; -uint8_t disp_buf[128]; +uint8_t disp_buf[260]; // 4 byte header + 256 byte data uint8_t *rx_buf = disp_buf + 64; void System::initialize() @@ -34,6 +34,68 @@ void System::initialize() storage.enable(); sei(); + + if (storage.hasData()) { + current_anim_no = 0; + loadPattern(0); + } else { + active_anim.type = AnimationType::TEXT; + active_anim.speed = (2 << 4) + 15; + active_anim.data = disp_buf; + active_anim.length = 26; + active_anim.delay = (0 << 4) + 0; + + // no strcpy_P or similar to save space + disp_buf[0] = ' '; + disp_buf[1] = 1; + disp_buf[2] = ' '; + disp_buf[3] = 'O'; + disp_buf[4] = 'h'; + disp_buf[5] = 'a'; + disp_buf[6] = 'i'; + disp_buf[7] = ' '; + disp_buf[8] = '-'; + disp_buf[9] = ' '; + disp_buf[10] = 'S'; + disp_buf[11] = 't'; + disp_buf[12] = 'o'; + disp_buf[13] = 'r'; + disp_buf[14] = 'a'; + disp_buf[15] = 'g'; + disp_buf[16] = 'e'; + disp_buf[17] = ' '; + disp_buf[18] = 'i'; + disp_buf[19] = 's'; + disp_buf[20] = ' '; + disp_buf[21] = 'e'; + disp_buf[22] = 'm'; + disp_buf[23] = 'p'; + disp_buf[24] = 't'; + disp_buf[25] = 'y'; + } + + display.show(&active_anim); +} + +void System::loadPattern(uint8_t anim_no) +{ + storage.load(anim_no, disp_buf); + + active_anim.type = (AnimationType)(disp_buf[0] >> 4); + active_anim.length = disp_buf[1]; + + if (active_anim.type == AnimationType::TEXT) { + active_anim.speed = (disp_buf[2] & 0xf0) + 15; + active_anim.delay = (disp_buf[2] & 0x0f ) << 4; + active_anim.direction = disp_buf[3] >> 4; + } else if (active_anim.type == AnimationType::FRAMES) { + active_anim.speed = ((disp_buf[2] & 0x0f) << 4) + 15; + active_anim.delay = (disp_buf[2] & 0x0f) << 4; + } + + active_anim.data = disp_buf + 4; + + display.show(&active_anim); } // ! This function has not been tested yet diff --git a/src/system.h b/src/system.h index 14dc257..165ce7b 100644 --- a/src/system.h +++ b/src/system.h @@ -9,8 +9,10 @@ class System { private: uint16_t want_shutdown; + uint8_t current_anim_no; void shutdown(void); void receive(void); + void loadPattern(uint8_t anim_no); enum RxExpect : uint8_t { START1, @@ -29,7 +31,7 @@ class System { RxExpect rxExpect; public: - System() { want_shutdown = 0; rxExpect = START1; }; + System() { want_shutdown = 0; rxExpect = START1; current_anim_no = 0;}; /** * Initial MCU setup. Turns off unused peripherals to save power -- cgit v1.2.3