2024-08-26 13:02:17 +10:00
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include "data.hpp"
|
2024-08-28 13:10:57 +10:00
|
|
|
#include "serial.hpp"
|
|
|
|
#include "eeprom.hpp"
|
|
|
|
#include "scheduler.hpp"
|
|
|
|
#include "indicator.hpp"
|
2024-09-27 22:46:51 +10:00
|
|
|
#include "buttons.hpp"
|
2024-08-28 13:10:57 +10:00
|
|
|
#include "dac.hpp"
|
2024-08-26 13:02:17 +10:00
|
|
|
|
2024-08-28 13:10:57 +10:00
|
|
|
using namespace data;
|
2024-08-26 13:02:17 +10:00
|
|
|
|
2024-08-28 13:10:57 +10:00
|
|
|
static Mode mode = Mode::none;
|
2024-08-26 13:02:17 +10:00
|
|
|
|
2024-08-28 13:10:57 +10:00
|
|
|
static void flash_data() {
|
2024-08-31 01:53:25 +10:00
|
|
|
uint32_t at = 0;
|
|
|
|
uint16_t count;
|
|
|
|
char buffer[eeprom::PAGE_SIZE];
|
2024-08-26 13:02:17 +10:00
|
|
|
|
2024-08-31 01:53:25 +10:00
|
|
|
do {
|
|
|
|
count = serial::read(buffer, eeprom::PAGE_SIZE);
|
|
|
|
eeprom::page_write(at, buffer);
|
|
|
|
at += eeprom::PAGE_SIZE;
|
|
|
|
} while(count == eeprom::PAGE_SIZE);
|
2024-08-26 13:02:17 +10:00
|
|
|
|
2024-08-31 01:53:25 +10:00
|
|
|
while(serial::is_connected()) {
|
2024-08-31 02:06:10 +10:00
|
|
|
serial::read(buffer, eeprom::PAGE_SIZE);
|
2024-08-26 13:02:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-28 13:10:57 +10:00
|
|
|
void data::update() {
|
|
|
|
static int last = 2;
|
|
|
|
int now = serial::is_connected() ? 1 : 0;
|
2024-09-27 22:46:51 +10:00
|
|
|
buttons::update();
|
2024-08-28 13:10:57 +10:00
|
|
|
if(last == now) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dac::reset();
|
|
|
|
indicator::reset();
|
|
|
|
last = now;
|
|
|
|
if(now) {
|
|
|
|
while(serial::dev.available()) {
|
|
|
|
serial::get();
|
|
|
|
}
|
|
|
|
delay(10);
|
|
|
|
serial::write("\x19\xc1\x6c\x19\x5b\x6f\xd2\x44", 8);
|
|
|
|
mode = serial::get();
|
|
|
|
switch(mode) {
|
|
|
|
case Mode::flash:
|
|
|
|
flash_data();
|
|
|
|
mode = Mode::none;
|
2024-09-27 22:46:51 +10:00
|
|
|
scheduler::state = scheduler::State::end;
|
2024-08-28 13:10:57 +10:00
|
|
|
break;
|
|
|
|
case Mode::local:
|
|
|
|
eeprom::jump(0);
|
2024-09-27 22:46:51 +10:00
|
|
|
scheduler::state = scheduler::State::play;
|
2024-08-28 13:10:57 +10:00
|
|
|
scheduler::reset();
|
|
|
|
break;
|
|
|
|
case Mode::stream:
|
2024-09-27 22:46:51 +10:00
|
|
|
scheduler::state = scheduler::State::play;
|
2024-08-28 13:10:57 +10:00
|
|
|
scheduler::reset();
|
|
|
|
break;
|
|
|
|
}
|
2024-09-29 18:07:58 +10:00
|
|
|
} else {
|
2024-08-28 13:10:57 +10:00
|
|
|
mode = Mode::local;
|
|
|
|
eeprom::jump(0);
|
2024-09-27 22:46:51 +10:00
|
|
|
scheduler::state = scheduler::State::pause;
|
2024-08-28 13:10:57 +10:00
|
|
|
scheduler::reset();
|
2024-08-26 13:02:17 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-28 13:10:57 +10:00
|
|
|
unsigned data::read(char* data, unsigned len) {
|
|
|
|
unsigned count = 0;
|
2024-08-26 13:02:17 +10:00
|
|
|
switch(mode) {
|
|
|
|
case Mode::local:
|
2024-08-28 13:10:57 +10:00
|
|
|
eeprom::read(data, len);
|
|
|
|
count = len;
|
2024-08-26 13:02:17 +10:00
|
|
|
break;
|
2024-08-28 13:10:57 +10:00
|
|
|
case Mode::stream:
|
|
|
|
count = serial::read(data, len);
|
2024-08-26 13:02:17 +10:00
|
|
|
break;
|
|
|
|
}
|
2024-08-28 13:10:57 +10:00
|
|
|
for(unsigned i = count; i < len; i++) {
|
|
|
|
data[i] = 0;
|
|
|
|
}
|
|
|
|
return count;
|
2024-08-26 13:02:17 +10:00
|
|
|
}
|
|
|
|
|
2024-09-29 18:07:58 +10:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|