From 42e6f7c79f7b5bab196369c42454b75d8fdfa155 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Fri, 27 Sep 2024 22:46:51 +1000 Subject: [PATCH] added track skipping --- buttons.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ buttons.hpp | 8 ++++++++ data.cpp | 8 +++++++- eeprom.cpp | 8 -------- eeprom.hpp | 2 +- scheduler.cpp | 16 +++++++++------- scheduler.hpp | 19 ++++++++++++++++++- soft_twi.cpp | 5 ----- tone-generator.ino | 6 ++++-- 9 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 buttons.cpp create mode 100644 buttons.hpp diff --git a/buttons.cpp b/buttons.cpp new file mode 100644 index 0000000..3a922e3 --- /dev/null +++ b/buttons.cpp @@ -0,0 +1,44 @@ + +#include +#include "buttons.hpp" +#include "scheduler.hpp" +#include "data.hpp" +#include "tones.hpp" +#include "dac.hpp" +#include "indicator.hpp" + +const int PIN_NEXT = 8; +bool state_next = 1; + +void buttons::init() { + pinMode(PIN_NEXT, INPUT_PULLUP); +} + +static void on_next() { + switch(scheduler::state) { + case scheduler::State::play: + scheduler::state = scheduler::State::skip; + indicator::reset(); + tones::clear_all(); + dac::reset(); + break; + case scheduler::State::pause: + scheduler::state = scheduler::State::play; + break; + } +} + +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(); + state_next = 1; + } +} + diff --git a/buttons.hpp b/buttons.hpp new file mode 100644 index 0000000..85e49e3 --- /dev/null +++ b/buttons.hpp @@ -0,0 +1,8 @@ + +#pragma once + +namespace buttons { + void init(); + void update(); +}; + diff --git a/data.cpp b/data.cpp index 1b48620..b4facef 100644 --- a/data.cpp +++ b/data.cpp @@ -6,6 +6,7 @@ #include "eeprom.hpp" #include "scheduler.hpp" #include "indicator.hpp" +#include "buttons.hpp" #include "dac.hpp" using namespace data; @@ -31,6 +32,7 @@ static void flash_data() { void data::update() { static int last = 2; int now = serial::is_connected() ? 1 : 0; + buttons::update(); if(last == now) { return; } @@ -48,18 +50,22 @@ void data::update() { case Mode::flash: flash_data(); mode = Mode::none; + scheduler::state = scheduler::State::end; break; case Mode::local: eeprom::jump(0); + scheduler::state = scheduler::State::play; scheduler::reset(); break; case Mode::stream: + scheduler::state = scheduler::State::play; scheduler::reset(); break; } - } else { + } else if(mode != Mode::local) { mode = Mode::local; eeprom::jump(0); + scheduler::state = scheduler::State::pause; scheduler::reset(); } } diff --git a/eeprom.cpp b/eeprom.cpp index 01235cb..8e58474 100644 --- a/eeprom.cpp +++ b/eeprom.cpp @@ -38,14 +38,6 @@ static void set_address(uint8_t i2c_addr, uint16_t at) { void eeprom::init() { mem.begin(); - -// Wire.setClock(400000); -// Wire.begin(); -// mem.setMemorySizeBytes(eeprom::MAX_LENGTH); -// mem.setAddressBytes(2); -// mem.setPageSizeBytes(128); -// mem.setWriteTimeMs(3); -// mem.begin(); } void eeprom::jump(uint32_t p_at) { diff --git a/eeprom.hpp b/eeprom.hpp index 84e3c54..2f10a05 100644 --- a/eeprom.hpp +++ b/eeprom.hpp @@ -10,7 +10,7 @@ namespace eeprom { constexpr uint32_t BLOCK_SIZE = 0x10000; constexpr uint32_t LENGTH = 0x20000; - inline SoftTWI mem(A4, A5);//A4, A5); + inline SoftTWI mem(A4, A5); void init(); void jump(uint32_t addr); diff --git a/scheduler.cpp b/scheduler.cpp index f2615f5..f132234 100644 --- a/scheduler.cpp +++ b/scheduler.cpp @@ -24,8 +24,7 @@ struct { void scheduler::reset() { tones::clear_all(); - ts_init = micros(); - running = true; + ts_init = timestamp = micros(); } static void do_next() { @@ -33,7 +32,9 @@ static void do_next() { int i = 1; if(next.active) { - tones::set(next.index, next.frequency, next.amplitude); + if(state == State::play) { + tones::set(next.index, next.frequency, next.amplitude); + } next.active = false; } @@ -54,7 +55,8 @@ static void do_next() { config.amplitude = v & bm(8); v >>= 8; config.us_per_tick = v & bm(29); - ts_init = micros(); + state = State::play; + scheduler::reset(); break; } case entry::Type::set: { @@ -96,7 +98,7 @@ static void do_next() { break; } default: { - running = false; + state = State::end; tones::clear_all(); break; } @@ -104,9 +106,9 @@ static void do_next() { } void scheduler::do_all() { - while(running && timing::at >= timestamp) { + do { do_next(); - } + } while(should_update()); tones::recalc(); } diff --git a/scheduler.hpp b/scheduler.hpp index fbf51df..613a239 100644 --- a/scheduler.hpp +++ b/scheduler.hpp @@ -1,12 +1,29 @@ #pragma once +#include "timing.hpp" + namespace scheduler { + + enum State { + play, + skip, + pause, + end, + }; inline uint32_t timestamp = 0; - inline bool running = false; + inline State state = State::pause; void reset(); void do_all(); + + inline bool is_running() { + return state <= State::skip; + } + + inline bool should_update() { + return (state == State::play && timing::at >= timestamp) || state == State::skip; + } }; diff --git a/soft_twi.cpp b/soft_twi.cpp index 235b858..9658a08 100644 --- a/soft_twi.cpp +++ b/soft_twi.cpp @@ -2,11 +2,6 @@ #include "soft_twi.hpp" #include -//void SoftTWI::sleep(uint8_t t) { -// unsigned d = _delay * t; -// if(d) delayMicroseconds(d); -//} - void SoftTWI::set_pin(uint8_t pin, bool state) { uint8_t bitmask = digitalPinToBitMask(pin); volatile uint8_t& reg = *portModeRegister(digitalPinToPort(pin)); diff --git a/tone-generator.ino b/tone-generator.ino index c71b1ca..6b0fa7e 100644 --- a/tone-generator.ino +++ b/tone-generator.ino @@ -1,4 +1,5 @@ +#include "buttons.hpp" #include "tone.hpp" #include "tones.hpp" #include "dac.hpp" @@ -27,13 +28,14 @@ void setup() { eeprom::init(); tones::init(); indicator::init(); + buttons::init(); scheduler::reset(); } void loop() { - if(scheduler::running) { - if(timing::at >= scheduler::timestamp) { + if(scheduler::is_running()) { + if(scheduler::should_update()) { data::update(); scheduler::do_all(); indicator::update();