From 376a2ef6de221e88e1b406376762ebde13f91758 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Sun, 29 Sep 2024 18:07:58 +1000 Subject: [PATCH] added song skipping --- buttons.cpp | 11 ++++++++--- buttons.hpp | 1 + data.cpp | 22 +++++++++++++++++++++- data.hpp | 2 ++ eeprom.cpp | 10 +++++----- eeprom.hpp | 3 ++- entry.hpp | 3 ++- scheduler.cpp | 3 +++ util.hpp | 2 +- 9 files changed, 45 insertions(+), 12 deletions(-) diff --git a/buttons.cpp b/buttons.cpp index 3a922e3..d1d7cc9 100644 --- a/buttons.cpp +++ b/buttons.cpp @@ -8,7 +8,9 @@ #include "indicator.hpp" const int PIN_NEXT = 8; -bool state_next = 1; + +static bool state_next = 1; +static uint32_t addr_next = 0; void buttons::init() { pinMode(PIN_NEXT, INPUT_PULLUP); @@ -19,6 +21,7 @@ static void on_next() { case scheduler::State::play: scheduler::state = scheduler::State::skip; indicator::reset(); + data::jump(addr_next); tones::clear_all(); dac::reset(); break; @@ -29,12 +32,10 @@ static void on_next() { } void buttons::update() { - if(!digitalRead(PIN_NEXT) && state_next) { delay(1); state_next = 0; } - else if(digitalRead(PIN_NEXT) && !state_next) { delay(1); on_next(); @@ -42,3 +43,7 @@ void buttons::update() { } } +void buttons::set_addr_next(uint32_t addr) { + addr_next = addr; +} + diff --git a/buttons.hpp b/buttons.hpp index 85e49e3..e264313 100644 --- a/buttons.hpp +++ b/buttons.hpp @@ -4,5 +4,6 @@ namespace buttons { void init(); void update(); + void set_addr_next(uint32_t addr); }; diff --git a/data.cpp b/data.cpp index b4facef..2cb782c 100644 --- a/data.cpp +++ b/data.cpp @@ -62,7 +62,7 @@ void data::update() { scheduler::reset(); break; } - } else if(mode != Mode::local) { + } else { mode = Mode::local; eeprom::jump(0); scheduler::state = scheduler::State::pause; @@ -87,3 +87,23 @@ unsigned data::read(char* data, unsigned len) { return count; } +bool data::jump(uint32_t addr) { + switch(mode) { + case Mode::local: + eeprom::jump(addr); + break; + default: + return false; + } + return true; +} + +uint32_t data::get_addr() { + switch(mode) { + case Mode::local: + return eeprom::get_addr(); + default: + return 0; + } +} + diff --git a/data.hpp b/data.hpp index 8a34cf8..1062bda 100644 --- a/data.hpp +++ b/data.hpp @@ -12,5 +12,7 @@ namespace data { void update(); unsigned read(char* data, unsigned len); + bool jump(uint32_t addr); + uint32_t get_addr(); }; diff --git a/eeprom.cpp b/eeprom.cpp index 8e58474..4799515 100644 --- a/eeprom.cpp +++ b/eeprom.cpp @@ -22,11 +22,7 @@ static void wait_for_responsive(uint8_t i2c_addr) { } static uint8_t get_i2c_addr(uint32_t at) { - uint8_t i2c_addr = ADDR; - if(at > 0xffff) { - i2c_addr |= 1; - } - return i2c_addr; + return ADDR ^ (at >> 16); } static void set_address(uint8_t i2c_addr, uint16_t at) { @@ -78,3 +74,7 @@ void eeprom::page_write(uint32_t at, const char* data) { delay(WRITE_MS); } +uint32_t eeprom::get_addr() { + return at; +} + diff --git a/eeprom.hpp b/eeprom.hpp index 2f10a05..3aef733 100644 --- a/eeprom.hpp +++ b/eeprom.hpp @@ -8,7 +8,7 @@ namespace eeprom { constexpr uint8_t PAGE_SIZE = 128; constexpr uint32_t BLOCK_SIZE = 0x10000; - constexpr uint32_t LENGTH = 0x20000; + constexpr uint32_t LENGTH = 0x80000; inline SoftTWI mem(A4, A5); @@ -16,5 +16,6 @@ namespace eeprom { void jump(uint32_t addr); void read(char* data, uint16_t len); void page_write(uint32_t at, const char* data); + uint32_t get_addr(); }; diff --git a/entry.hpp b/entry.hpp index 82e6630..7cabef9 100644 --- a/entry.hpp +++ b/entry.hpp @@ -43,6 +43,7 @@ namespace entry { struct Config { uint32_t us_per_tick; uint8_t amplitude; + uint32_t jump_addr; }; struct Generic { @@ -68,7 +69,7 @@ namespace entry { case Type::clear_ts: return 4; case Type::config: - return 5; + return 8; case Type::stop: default: return 1; diff --git a/scheduler.cpp b/scheduler.cpp index f132234..d27310c 100644 --- a/scheduler.cpp +++ b/scheduler.cpp @@ -6,6 +6,7 @@ #include "entry.hpp" #include "tones.hpp" #include "util.hpp" +#include "buttons.hpp" using namespace scheduler; @@ -52,6 +53,8 @@ static void do_next() { switch(type) { case entry::Type::config: { + buttons::set_addr_next((v & bm(24)) + data::get_addr()); + v >>= 24; config.amplitude = v & bm(8); v >>= 8; config.us_per_tick = v & bm(29); diff --git a/util.hpp b/util.hpp index 280c6fb..f969979 100644 --- a/util.hpp +++ b/util.hpp @@ -13,6 +13,6 @@ inline void swap(T& a, T& b) { a = t; } -#define bm(V) (((uint64_t)1 << V) - 1); +#define bm(V) (((uint64_t)1 << V) - 1) #define size(V) (sizeof(V) / sizeof(V[0]))