added track skipping

This commit is contained in:
Jay Robson 2024-09-27 22:46:51 +10:00
parent b10b72567b
commit 42e6f7c79f
9 changed files with 91 additions and 25 deletions

44
buttons.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <Arduino.h>
#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;
}
}

8
buttons.hpp Normal file
View File

@ -0,0 +1,8 @@
#pragma once
namespace buttons {
void init();
void update();
};

View File

@ -6,6 +6,7 @@
#include "eeprom.hpp" #include "eeprom.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "indicator.hpp" #include "indicator.hpp"
#include "buttons.hpp"
#include "dac.hpp" #include "dac.hpp"
using namespace data; using namespace data;
@ -31,6 +32,7 @@ static void flash_data() {
void data::update() { void data::update() {
static int last = 2; static int last = 2;
int now = serial::is_connected() ? 1 : 0; int now = serial::is_connected() ? 1 : 0;
buttons::update();
if(last == now) { if(last == now) {
return; return;
} }
@ -48,18 +50,22 @@ void data::update() {
case Mode::flash: case Mode::flash:
flash_data(); flash_data();
mode = Mode::none; mode = Mode::none;
scheduler::state = scheduler::State::end;
break; break;
case Mode::local: case Mode::local:
eeprom::jump(0); eeprom::jump(0);
scheduler::state = scheduler::State::play;
scheduler::reset(); scheduler::reset();
break; break;
case Mode::stream: case Mode::stream:
scheduler::state = scheduler::State::play;
scheduler::reset(); scheduler::reset();
break; break;
} }
} else { } else if(mode != Mode::local) {
mode = Mode::local; mode = Mode::local;
eeprom::jump(0); eeprom::jump(0);
scheduler::state = scheduler::State::pause;
scheduler::reset(); scheduler::reset();
} }
} }

View File

@ -38,14 +38,6 @@ static void set_address(uint8_t i2c_addr, uint16_t at) {
void eeprom::init() { void eeprom::init() {
mem.begin(); 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) { void eeprom::jump(uint32_t p_at) {

View File

@ -10,7 +10,7 @@ namespace eeprom {
constexpr uint32_t BLOCK_SIZE = 0x10000; constexpr uint32_t BLOCK_SIZE = 0x10000;
constexpr uint32_t LENGTH = 0x20000; constexpr uint32_t LENGTH = 0x20000;
inline SoftTWI mem(A4, A5);//A4, A5); inline SoftTWI mem(A4, A5);
void init(); void init();
void jump(uint32_t addr); void jump(uint32_t addr);

View File

@ -24,8 +24,7 @@ struct {
void scheduler::reset() { void scheduler::reset() {
tones::clear_all(); tones::clear_all();
ts_init = micros(); ts_init = timestamp = micros();
running = true;
} }
static void do_next() { static void do_next() {
@ -33,7 +32,9 @@ static void do_next() {
int i = 1; int i = 1;
if(next.active) { 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; next.active = false;
} }
@ -54,7 +55,8 @@ static void do_next() {
config.amplitude = v & bm(8); config.amplitude = v & bm(8);
v >>= 8; v >>= 8;
config.us_per_tick = v & bm(29); config.us_per_tick = v & bm(29);
ts_init = micros(); state = State::play;
scheduler::reset();
break; break;
} }
case entry::Type::set: { case entry::Type::set: {
@ -96,7 +98,7 @@ static void do_next() {
break; break;
} }
default: { default: {
running = false; state = State::end;
tones::clear_all(); tones::clear_all();
break; break;
} }
@ -104,9 +106,9 @@ static void do_next() {
} }
void scheduler::do_all() { void scheduler::do_all() {
while(running && timing::at >= timestamp) { do {
do_next(); do_next();
} } while(should_update());
tones::recalc(); tones::recalc();
} }

View File

@ -1,12 +1,29 @@
#pragma once #pragma once
#include "timing.hpp"
namespace scheduler { namespace scheduler {
enum State {
play,
skip,
pause,
end,
};
inline uint32_t timestamp = 0; inline uint32_t timestamp = 0;
inline bool running = false; inline State state = State::pause;
void reset(); void reset();
void do_all(); 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;
}
}; };

View File

@ -2,11 +2,6 @@
#include "soft_twi.hpp" #include "soft_twi.hpp"
#include <Arduino.h> #include <Arduino.h>
//void SoftTWI::sleep(uint8_t t) {
// unsigned d = _delay * t;
// if(d) delayMicroseconds(d);
//}
void SoftTWI::set_pin(uint8_t pin, bool state) { void SoftTWI::set_pin(uint8_t pin, bool state) {
uint8_t bitmask = digitalPinToBitMask(pin); uint8_t bitmask = digitalPinToBitMask(pin);
volatile uint8_t& reg = *portModeRegister(digitalPinToPort(pin)); volatile uint8_t& reg = *portModeRegister(digitalPinToPort(pin));

View File

@ -1,4 +1,5 @@
#include "buttons.hpp"
#include "tone.hpp" #include "tone.hpp"
#include "tones.hpp" #include "tones.hpp"
#include "dac.hpp" #include "dac.hpp"
@ -27,13 +28,14 @@ void setup() {
eeprom::init(); eeprom::init();
tones::init(); tones::init();
indicator::init(); indicator::init();
buttons::init();
scheduler::reset(); scheduler::reset();
} }
void loop() { void loop() {
if(scheduler::running) { if(scheduler::is_running()) {
if(timing::at >= scheduler::timestamp) { if(scheduler::should_update()) {
data::update(); data::update();
scheduler::do_all(); scheduler::do_all();
indicator::update(); indicator::update();