added track skipping
This commit is contained in:
parent
b10b72567b
commit
42e6f7c79f
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
namespace buttons {
|
||||
void init();
|
||||
void update();
|
||||
};
|
||||
|
8
data.cpp
8
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -2,11 +2,6 @@
|
|||
#include "soft_twi.hpp"
|
||||
#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) {
|
||||
uint8_t bitmask = digitalPinToBitMask(pin);
|
||||
volatile uint8_t& reg = *portModeRegister(digitalPinToPort(pin));
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue