2024-08-19 22:43:24 +10:00
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include "timing.hpp"
|
|
|
|
#include "scheduler.hpp"
|
|
|
|
#include "eeprom.hpp"
|
|
|
|
#include "entry.hpp"
|
|
|
|
#include "tones.hpp"
|
|
|
|
#include "util.hpp"
|
|
|
|
|
2024-08-25 13:55:43 +10:00
|
|
|
static uint32_t ts_init = 0;
|
|
|
|
static uint32_t ts_last = 0;
|
|
|
|
struct {
|
|
|
|
float amplitude = 1;
|
|
|
|
uint32_t us_per_tick = 1;
|
|
|
|
} config;
|
2024-08-19 22:43:24 +10:00
|
|
|
struct {
|
|
|
|
uint8_t index;
|
|
|
|
uint16_t frequency;
|
|
|
|
float amplitude;
|
|
|
|
} next;
|
|
|
|
|
2024-08-25 13:55:43 +10:00
|
|
|
constexpr uint64_t bm(int n) {
|
|
|
|
return ((uint64_t)1 << n) - 1;
|
|
|
|
}
|
|
|
|
|
2024-08-19 22:43:24 +10:00
|
|
|
void scheduler::do_next() {
|
2024-08-25 13:55:43 +10:00
|
|
|
|
|
|
|
int i = 1;
|
2024-08-19 22:43:24 +10:00
|
|
|
|
|
|
|
if(running) {
|
|
|
|
tones::set(next.index, next.frequency, next.amplitude);
|
|
|
|
} else {
|
|
|
|
eeprom::jump(0);
|
2024-08-25 13:55:43 +10:00
|
|
|
tones::clear_all();
|
|
|
|
ts_init = micros();
|
2024-08-19 22:43:24 +10:00
|
|
|
running = true;
|
|
|
|
}
|
|
|
|
|
2024-08-25 13:55:43 +10:00
|
|
|
uint64_t v = 0;
|
|
|
|
uint8_t buff[8];
|
|
|
|
eeprom::read(buff, 1);
|
|
|
|
|
|
|
|
entry::Type type = buff[0] >> 5;
|
|
|
|
int entry_size = entry::get_size(type);
|
|
|
|
eeprom::read(buff + 1, entry_size - 1);
|
|
|
|
|
|
|
|
|
|
|
|
Serial.print("LOAD (");
|
|
|
|
|
|
|
|
for(int i = 0; i < entry_size; i++) {
|
|
|
|
v = (v << 8) | buff[i];
|
|
|
|
Serial.print(buff[i]);
|
|
|
|
Serial.print(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Serial.print(") ");
|
2024-08-19 22:43:24 +10:00
|
|
|
|
|
|
|
switch(type) {
|
2024-08-25 13:55:43 +10:00
|
|
|
case entry::Type::config: {
|
|
|
|
config.amplitude = *(float*)(buff + 4);
|
|
|
|
v >>= 32;
|
|
|
|
config.us_per_tick = v & bm(29);
|
|
|
|
ts_init = micros();
|
|
|
|
|
|
|
|
Serial.print("config\t");
|
|
|
|
Serial.print(config.amplitude);
|
|
|
|
Serial.print('\t');
|
|
|
|
Serial.println(config.us_per_tick);
|
2024-08-19 22:43:24 +10:00
|
|
|
break;
|
|
|
|
}
|
2024-08-25 13:55:43 +10:00
|
|
|
case entry::Type::set: {
|
|
|
|
Serial.println("set");
|
|
|
|
v >>= 4;
|
|
|
|
next.frequency = v & bm(12);
|
|
|
|
v >>= 12;
|
|
|
|
next.index = v & bm(5);
|
|
|
|
next.amplitude = config.amplitude;
|
2024-08-19 22:43:24 +10:00
|
|
|
break;
|
|
|
|
}
|
2024-08-25 13:55:43 +10:00
|
|
|
case entry::Type::set_ts: {
|
|
|
|
Serial.println("set_ts");
|
|
|
|
next.frequency = v & bm(12);
|
|
|
|
v >>= 12;
|
|
|
|
uint32_t ticks = v & bm(20);
|
|
|
|
v >>= 20;
|
|
|
|
next.index = v & bm(5);
|
|
|
|
next.amplitude = config.amplitude;
|
|
|
|
timestamp = ticks * config.us_per_tick + ts_init;
|
2024-08-19 22:43:24 +10:00
|
|
|
break;
|
|
|
|
}
|
2024-08-25 13:55:43 +10:00
|
|
|
case entry::Type::clear: {
|
|
|
|
Serial.println("clear");
|
|
|
|
next.index = v & bm(5);
|
|
|
|
next.frequency = 0;
|
|
|
|
next.amplitude = 0;
|
2024-08-19 22:43:24 +10:00
|
|
|
break;
|
|
|
|
}
|
2024-08-25 13:55:43 +10:00
|
|
|
case entry::Type::clear_ts: {
|
|
|
|
Serial.println("clear_ts");
|
|
|
|
v >>= 4;
|
|
|
|
uint32_t ticks = v & bm(20);
|
|
|
|
v >>= 20;
|
|
|
|
next.index = v & bm(5);
|
|
|
|
next.frequency = 0;
|
|
|
|
next.amplitude = 0;
|
|
|
|
timestamp = ticks * config.us_per_tick + ts_init;
|
2024-08-19 22:43:24 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
running = false;
|
2024-08-25 13:55:43 +10:00
|
|
|
tones::clear_all();
|
|
|
|
Serial.println("stop");
|
2024-08-19 22:43:24 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-08-25 13:55:43 +10:00
|
|
|
|
|
|
|
Serial.println("DONE");
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void scheduler::clear() {
|
|
|
|
eeprom::jump(0);
|
|
|
|
ts_last = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void scheduler::add_set(uint32_t ts, uint8_t index, uint16_t freq) {
|
2024-08-25 13:55:43 +10:00
|
|
|
// if(index >= size(tones::all)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// if(ts <= ts_last) {
|
|
|
|
// entry::Set e {.type=entry::Type::SET, .index=index, .frequency=freq};
|
|
|
|
// eeprom::write(&e, sizeof(e));
|
|
|
|
// } else {
|
|
|
|
// entry::SetTS e {.type=entry::Type::SET_TS, .index=index, .ticks=ts, .frequency=freq};
|
|
|
|
// eeprom::write(&e, sizeof(e));
|
|
|
|
// ts_last = ts;
|
|
|
|
// }
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void scheduler::add_clear(uint32_t ts, uint8_t index) {
|
2024-08-25 13:55:43 +10:00
|
|
|
// if(index >= size(tones::all)) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// if(ts <= ts_last) {
|
|
|
|
// entry::Clear e {.type=entry::Type::CLEAR, .index=index};
|
|
|
|
// eeprom::write(&e, sizeof(e));
|
|
|
|
// } else {
|
|
|
|
// entry::ClearTS e {.type=entry::Type::CLEAR_TS, .index=index, .ticks=ts};
|
|
|
|
// eeprom::write(&e, sizeof(e));
|
|
|
|
// ts_last = ts;
|
|
|
|
// }
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
|
2024-08-25 13:55:43 +10:00
|
|
|
void scheduler::add_config(float amplitude, uint32_t us_per_tick) {
|
|
|
|
// entry::Config e {.type=entry::Type::CONFIG, .p=0, .us_per_tick=us_per_tick, .amplitude=amplitude};
|
|
|
|
// eeprom::write(&e, sizeof(e));
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
void scheduler::finalize() {
|
2024-08-25 13:55:43 +10:00
|
|
|
// entry::Header e {.type=entry::Type::STOP};
|
|
|
|
// eeprom::write(&e, sizeof(e));
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
|