working play from progmem

This commit is contained in:
Jay Robson 2024-08-25 13:55:43 +10:00
parent a4fd933189
commit aa752faa5f
13 changed files with 506 additions and 99 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
sketch.yaml sketch.yaml
midi_data.hpp

View File

@ -5,6 +5,7 @@
#include "serial.hpp" #include "serial.hpp"
#include "tones.hpp" #include "tones.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "eeprom.hpp"
static void help() { static void help() {
Serial.println("OK help"); Serial.println("OK help");
@ -12,11 +13,13 @@ static void help() {
Serial.println(" c: clear :c,index,"); Serial.println(" c: clear :c,index,");
Serial.println(" g: get :g,index,"); Serial.println(" g: get :g,index,");
Serial.println(" w: write :w,[v|s|c,...]...;,"); Serial.println(" w: write :w,[v|s|c,...]...;,");
Serial.println(" v: volume v,level,"); Serial.println(" o: option v,amplitude,us_per_tick,");
Serial.println(" s: set s,timestamp,index,freq,"); Serial.println(" s: set s,timestamp,index,freq,");
Serial.println(" c: clear v,timestamp,index,"); Serial.println(" c: clear v,timestamp,index,");
Serial.println(" p: play :p,"); Serial.println(" p: play :p,");
Serial.println(" q: stop :q,");
Serial.println(" r: reset :r,"); Serial.println(" r: reset :r,");
Serial.println(" e: eeprom :e,");
Serial.println(" h: help :h,"); Serial.println(" h: help :h,");
} }
@ -46,25 +49,38 @@ static bool write() {
for(;;) { for(;;) {
serial::read_until(); serial::read_until();
switch(serial::buffer[0]) { switch(serial::buffer[0]) {
case 'v': case 'o': {
scheduler::add_volume(serial::read_float()); float amplitude = serial::read_float();
uint32_t us_per_tick = serial::read_long();
scheduler::add_config(amplitude, us_per_tick);
break; break;
case 's': }
scheduler::add_set(serial::read_int(), serial::read_int(), serial::read_int()); case 's': {
uint32_t ts = serial::read_long();
uint8_t index = serial::read_int();
uint16_t freq = serial::read_int();
scheduler::add_set(ts, index, freq);
break; break;
case 'c': }
scheduler::add_clear(serial::read_int(), serial::read_int()); case 'c': {
uint32_t ts = serial::read_long();
uint8_t index = serial::read_int();
scheduler::add_clear(ts, index);
break; break;
}
case ';': case ';':
return false; return false;
default: default:
return true; return true;
} }
Serial.println();
} }
scheduler::finalize(); scheduler::finalize();
} }
static const char HEXC[] = "0123456789abcdef";
void commands::process() { void commands::process() {
serial::read_until(); serial::read_until();
switch(serial::buffer[0]) { switch(serial::buffer[0]) {
@ -72,10 +88,14 @@ void commands::process() {
if(write()) break; if(write()) break;
Serial.println("OK"); Serial.println("OK");
return; return;
case 's': case 's': {
tones::set(serial::read_int(), serial::read_int(), serial::read_int()); uint8_t index = serial::read_int();
uint16_t freq = serial::read_int();
float amp = serial::read_float();
tones::set(index, freq, amp);
Serial.println("OK"); Serial.println("OK");
return; return;
}
case 'c': case 'c':
tones::clear(serial::read_int()); tones::clear(serial::read_int());
Serial.println("OK"); Serial.println("OK");
@ -87,7 +107,24 @@ void commands::process() {
scheduler::running = false; scheduler::running = false;
scheduler::do_next(); scheduler::do_next();
Serial.println("OK"); Serial.println("OK");
break; return;
case 'q':
scheduler::running = false;
Serial.println("OK");
return;
case 'e':
eeprom::jump(0);
Serial.println("OK serial");
for(int i = 0; i < 1024; i+=16) {
Serial.print(i);
Serial.print('\t');
eeprom::read(serial::buffer, 16);
for(int j = 0; j < 16; j++) {
Serial.print(HEXC[(serial::buffer[j] >> 4) & 0x0f]);
Serial.print(HEXC[serial::buffer[j] & 0x0f]);
}
Serial.println();
}
case 'r': case 'r':
tones::clear_all(); tones::clear_all();
Serial.println("OK"); Serial.println("OK");

33
eeprom.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <avr/pgmspace.h>
#include "midi_data.hpp"
#include "eeprom.hpp"
static int address = 0;
void eeprom::jump(int loc) {
address = loc;
}
inline size_t proc_len(size_t len) {
if(address + len >= sizeof(MIDI)) {
return sizeof(MIDI) - address;
} else {
return len;
}
}
void eeprom::read(void* data, size_t len) {
char* bytes = (char*)data;
len = proc_len(len);
memcpy_P(data, &MIDI[address], len);
address += len;
}
void eeprom::write(const void* data, size_t len) {
const char* bytes = (const char*)data;
len = proc_len(len);
address += len;
}

View File

@ -1,26 +1,10 @@
#pragma once #pragma once
#include <EEPROM.h>
namespace eeprom { namespace eeprom {
inline int address = 0; void jump(int loc = 0);
void read(void* data, size_t len);
inline void jump(int loc = 0) { void write(const void* data, size_t len);
address = 0;
}
template <typename T>
inline void read(T& type) {
EEPROM.get(address, type);
address += sizeof(type);
}
template <typename T>
inline void write(const T& type) {
EEPROM.put(address, type);
address += sizeof(type);
}
}; };

View File

@ -1,8 +1,22 @@
#pragma once #pragma once
template <int P, bool v>
struct Checker {
static constexpr bool value = v;
};
namespace entry { namespace entry {
enum Type : uint8_t {
stop,
config,
set,
set_ts,
clear,
clear_ts,
};
struct Set { struct Set {
uint8_t index; uint8_t index;
uint16_t frequency; uint16_t frequency;
@ -14,22 +28,51 @@ namespace entry {
struct SetTS { struct SetTS {
uint8_t index; uint8_t index;
uint32_t ticks;
uint16_t frequency; uint16_t frequency;
uint32_t timestamp;
}; };
struct ClearTS { struct ClearTS {
uint8_t index; uint8_t index;
uint32_t timestamp; uint32_t ticks;
}; };
enum Type { struct Stop {
STOP,
VOLUME,
SET,
SET_TS,
CLEAR,
CLEAR_TS,
}; };
struct Config {
uint32_t us_per_tick;
float amplitude;
};
struct Generic {
Type type;
union {
Set set;
SetTS set_ts;
Clear clear;
ClearTS clear_ts;
Stop stop;
Config config;
};
};
inline size_t get_size(Type type) {
switch(type) {
case Type::set:
return 3;
case Type::clear:
return 1;
case Type::set_ts:
return 5;
case Type::clear_ts:
return 4;
case Type::config:
return 8;
case Type::stop:
default:
return 1;
}
}
}; };

249
output.csv Normal file
View File

@ -0,0 +1,249 @@
mode ticks timestamp index frequency amplitude
RUN config : 0.33, 488
RUN set_ts 4882 2382964 0 1318 0.33
RUN set 2382964 1 330 0.33
RUN set_ts 4457 2175564 2 659 0.33
RUN clear_ts 19106 9324276 1 0 0.00
RUN clear_ts 55408 27039652 0 0 0.00
RUN set_ts 55896 27277796 0 988 0.33
RUN set 27277796 1 330 0.33
RUN clear_ts 5008 2444452 2 0 0.00
RUN clear_ts 25197 12296684 0 0 0.00
RUN set_ts 40334 19683540 0 1046 0.33
RUN set 19683540 2 659 0.33
RUN clear_ts 54982 26831764 1 0 0.00
RUN clear_ts 12077 5894124 0 0 0.00
RUN set_ts 27213 13280492 0 1174 0.33
RUN set 13280492 1 330 0.33
RUN clear_ts 41862 20429204 2 0 0.00
RUN set_ts 16369 7988620 2 659 0.33
RUN clear_ts 31265 15257868 1 0 0.00
RUN clear_ts 5622 2744084 0 0 0.00
RUN set_ts 6123 2988572 0 1046 0.33
RUN set 2988572 1 330 0.33
RUN clear_ts 21147 10320284 2 0 0.00
RUN clear_ts 47888 23369892 0 0 0.00
RUN set_ts 63546 31010996 0 988 0.33
RUN set 31010996 2 659 0.33
RUN clear_ts 13164 6424580 1 0 0.00
RUN clear_ts 42827 20900124 0 0 0.00
RUN set_ts 60660 29602628 0 880 0.33
RUN set 29602628 1 220 0.33
RUN clear_ts 8371 4085596 2 0 0.00
RUN set_ts 2978 1453812 2 440 0.33
RUN clear_ts 17627 8602524 1 0 0.00
RUN clear_ts 53929 26317900 0 0 0.00
RUN set_ts 54417 26556044 0 880 0.33
RUN set 26556044 1 220 0.33
RUN clear_ts 3529 1722700 2 0 0.00
RUN clear_ts 23718 11574932 0 0 0.00
RUN set_ts 38855 18961788 0 1046 0.33
RUN set 18961788 2 440 0.33
RUN clear_ts 53503 26110012 1 0 0.00
RUN clear_ts 10598 5172372 0 0 0.00
RUN set_ts 25734 12558740 0 1318 0.33
RUN set 12558740 1 220 0.33
RUN clear_ts 40383 19707452 2 0 0.00
RUN set_ts 12614 6156180 2 440 0.33
RUN clear_ts 27262 13304404 1 0 0.00
RUN clear_ts 62100 30305348 0 0 0.00
RUN set_ts 62588 30543492 0 1174 0.33
RUN set 30543492 1 220 0.33
RUN clear_ts 11700 5710148 2 0 0.00
RUN clear_ts 31889 15562380 0 0 0.00
RUN set_ts 47026 22949236 0 1046 0.33
RUN set 22949236 2 440 0.33
RUN clear_ts 61674 30097460 1 0 0.00
RUN clear_ts 17304 8444900 0 0 0.00
RUN set_ts 34394 16784820 0 988 0.33
RUN set 16784820 1 208 0.33
RUN clear_ts 47089 22979980 2 0 0.00
RUN set_ts 36898 18006772 2 415 0.33
RUN clear_ts 51547 25155484 1 0 0.00
RUN clear_ts 22313 10889292 0 0 0.00
RUN set_ts 22801 11127436 0 988 0.33
RUN set 11127436 1 208 0.33
RUN clear_ts 37449 18275660 2 0 0.00
RUN clear_ts 57638 28127892 0 0 0.00
RUN set_ts 7239 3533180 0 1046 0.33
RUN set 3533180 2 415 0.33
RUN clear_ts 21887 10681404 1 0 0.00
RUN clear_ts 44518 21725332 0 0 0.00
RUN set_ts 59654 29111700 0 1174 0.33
RUN set 29111700 1 208 0.33
RUN clear_ts 8767 4278844 2 0 0.00
RUN set_ts 46534 22709140 2 415 0.33
RUN clear_ts 61182 29857364 1 0 0.00
RUN clear_ts 30484 14876740 0 0 0.00
RUN set_ts 30972 15114884 0 1318 0.33
RUN set 15114884 1 208 0.33
RUN clear_ts 45620 22263108 2 0 0.00
RUN set_ts 15410 7520628 2 415 0.33
RUN clear_ts 30058 14668852 1 0 0.00
RUN clear_ts 336 164516 0 0 0.00
RUN set_ts 2778 1356212 0 1046 0.33
RUN set 1356212 1 220 0.33
RUN clear_ts 15473 7551372 2 0 0.00
RUN set_ts 5282 2578164 2 440 0.33
RUN clear_ts 19931 9726876 1 0 0.00
RUN clear_ts 56233 27442252 0 0 0.00
RUN set_ts 56721 27680396 0 880 0.33
RUN set 27680396 1 220 0.33
RUN clear_ts 5833 2847052 2 0 0.00
RUN set_ts 41159 20086140 2 440 0.33
RUN clear_ts 55807 27234364 1 0 0.00
RUN clear_ts 27550 13444948 0 0 0.00
RUN set_ts 28038 13683092 0 880 0.33
RUN set 13683092 1 220 0.33
RUN clear_ts 42687 20831804 2 0 0.00
RUN set_ts 14918 7280532 2 440 0.33
RUN clear_ts 29566 14428756 1 0 0.00
RUN clear_ts 64404 31429700 0 0 0.00
RUN set_ts 64892 31667844 0 247 0.33
RUN set 31667844 1 494 0.33
RUN clear_ts 14004 6834500 2 0 0.00
RUN clear_ts 18568 9061732 1 0 0.00
RUN set_ts 49330 24073588 1 262 0.33
RUN set 24073588 2 523 0.33
RUN clear_ts 63978 31221812 0 0 0.00
RUN clear_ts 3983 1944252 2 0 0.00
RUN set_ts 36698 17909172 0 294 0.33
RUN clear_ts 49393 24104332 1 0 0.00
RUN set_ts 39202 19131124 1 587 0.33
RUN clear_ts 53851 26279836 0 0 0.00
RUN set_ts 25105 12251788 0 1174 0.33
RUN set 12251788 2 294 0.33
RUN clear_ts 39753 19400012 1 0 0.00
RUN set_ts 9543 4657532 1 587 0.33
RUN clear_ts 24191 11805756 2 0 0.00
RUN clear_ts 61470 29997908 0 0 0.00
RUN set_ts 61958 30236052 0 1397 0.33
RUN set 30236052 2 294 0.33
RUN clear_ts 11071 5403196 1 0 0.00
RUN clear_ts 33701 16446636 0 0 0.00
RUN set_ts 48838 23833492 0 1760 0.33
RUN set 23833492 1 587 0.33
RUN clear_ts 63486 30981716 2 0 0.00
RUN set_ts 33276 16239236 2 294 0.33
RUN clear_ts 47924 23387460 1 0 0.00
RUN clear_ts 2577 1258124 0 0 0.00
RUN set_ts 17714 8644980 0 587 0.33
RUN clear_ts 32362 15793204 2 0 0.00
RUN set_ts 5082 2480564 1 1568 0.33
RUN set 2480564 2 294 0.33
RUN clear_ts 17777 8675724 0 0 0.00
RUN clear_ts 57985 28297228 1 0 0.00
RUN set_ts 7586 3702516 0 1397 0.33
RUN set 3702516 1 587 0.33
RUN clear_ts 22235 10851228 2 0 0.00
RUN clear_ts 43888 21417892 0 0 0.00
RUN set_ts 59025 28804748 0 1318 0.33
RUN set 28804748 2 262 0.33
RUN clear_ts 8137 3971404 1 0 0.00
RUN set_ts 43463 21210492 1 523 0.33
RUN clear_ts 58111 28358716 2 0 0.00
RUN set_ts 30342 14807444 2 262 0.33
RUN clear_ts 44991 21956156 1 0 0.00
RUN set_ts 17222 8404884 1 523 0.33
RUN clear_ts 31870 15553108 2 0 0.00
RUN clear_ts 1172 572484 0 0 0.00
RUN set_ts 1660 810628 0 1046 0.33
RUN set 810628 2 262 0.33
RUN clear_ts 16308 7958852 1 0 0.00
RUN clear_ts 36497 17811084 0 0 0.00
RUN set_ts 51634 25197940 0 1318 0.33
RUN set 25197940 1 523 0.33
RUN clear_ts 746 364596 2 0 0.00
RUN set_ts 39002 19033524 2 262 0.33
RUN clear_ts 51697 25228684 1 0 0.00
RUN set_ts 41506 20255476 1 523 0.33
RUN clear_ts 56155 27404188 2 0 0.00
RUN clear_ts 26921 13137996 0 0 0.00
RUN set_ts 27409 13376140 0 1174 0.33
RUN set 13376140 2 262 0.33
RUN clear_ts 42057 20524364 1 0 0.00
RUN clear_ts 617 301644 0 0 0.00
RUN set_ts 11847 5781884 0 1046 0.33
RUN set 5781884 1 523 0.33
RUN clear_ts 26495 12930108 2 0 0.00
RUN clear_ts 49126 23974036 0 0 0.00
RUN set_ts 64262 31360404 0 988 0.33
RUN set 31360404 2 208 0.33
RUN clear_ts 13375 6527548 1 0 0.00
RUN set_ts 51142 24957844 1 415 0.33
RUN clear_ts 254 124500 2 0 0.00
RUN set_ts 35580 17363588 2 208 0.33
RUN clear_ts 50228 24511812 1 0 0.00
RUN clear_ts 17577 8578124 0 0 0.00
RUN set_ts 20018 9769332 0 1046 0.33
RUN set 9769332 1 415 0.33
RUN clear_ts 34666 16917556 2 0 0.00
RUN clear_ts 55832 27246564 0 0 0.00
RUN set_ts 7386 3604916 0 1174 0.33
RUN set 3604916 2 208 0.33
RUN clear_ts 20081 9800076 1 0 0.00
RUN set_ts 9890 4826868 1 415 0.33
RUN clear_ts 24539 11975580 2 0 0.00
RUN clear_ts 46192 22542244 0 0 0.00
RUN set_ts 61329 29929100 0 1318 0.33
RUN set 29929100 2 208 0.33
RUN clear_ts 10441 5095756 1 0 0.00
RUN set_ts 45767 22334844 1 415 0.33
RUN clear_ts 60415 29483068 2 0 0.00
RUN clear_ts 32158 15693652 0 0 0.00
RUN set_ts 32646 15931796 0 1046 0.33
RUN set 15931796 2 220 0.33
RUN clear_ts 47295 23080508 1 0 0.00
RUN set_ts 19526 9529236 1 440 0.33
RUN clear_ts 34174 16677460 2 0 0.00
RUN clear_ts 3476 1696836 0 0 0.00
RUN set_ts 3964 1934980 0 880 0.33
RUN set 1934980 2 220 0.33
RUN clear_ts 18612 9083204 1 0 0.00
RUN set_ts 53938 26322292 1 440 0.33
RUN clear_ts 3050 1488948 2 0 0.00
RUN clear_ts 38864 18966180 0 0 0.00
RUN set_ts 41306 20157876 0 880 0.33
RUN set 20157876 2 220 0.33
RUN clear_ts 54001 26353036 1 0 0.00
RUN set_ts 43810 21379828 1 440 0.33
RUN clear_ts 58459 28528540 2 0 0.00
RUN clear_ts 29225 14262348 0 0 0.00
RUN set_ts 29713 14500492 0 220 0.33
RUN clear_ts 44361 21648716 1 0 0.00
RUN set_ts 14151 6906236 1 440 0.33
RUN clear_ts 28799 14054460 0 0 0.00
RUN clear_ts 15679 7651900 1 0 0.00
RUN set_ts 9690 4729268 0 1318 0.33
RUN set 4729268 1 220 0.33
RUN set_ts 12194 5951220 2 440 0.33
RUN clear_ts 26843 13099932 1 0 0.00
RUN set_ts 63633 31053452 1 220 0.33
RUN clear_ts 12745 6220108 2 0 0.00
RUN set_ts 48071 23459196 2 440 0.33
RUN clear_ts 62719 30607420 1 0 0.00
RUN clear_ts 34462 16818004 0 0 0.00
RUN set_ts 34950 17056148 0 1046 0.33
RUN set 17056148 1 220 0.33
RUN clear_ts 49599 24204860 2 0 0.00
RUN set_ts 21830 10653588 2 440 0.33
RUN clear_ts 36478 17801812 1 0 0.00
RUN set_ts 6268 3059332 1 220 0.33
RUN clear_ts 20916 10207556 2 0 0.00
RUN set_ts 56242 27446644 2 440 0.33
RUN clear_ts 5354 2613300 1 0 0.00
RUN clear_ts 41168 20090532 0 0 0.00
RUN set_ts 43610 21282228 0 1174 0.33
RUN set 21282228 1 208 0.33
RUN clear_ts 56305 27477388 2 0 0.00
RUN set_ts 46114 22504180 2 415 0.33
RUN clear_ts 60763 29652892 1 0 0.00
RUN set_ts 32017 15624844 1 208 0.33
RUN clear_ts 46665 22773068 2 0 0.00
RUN set_ts 16455 8030588 2 415 0.33
RUN clear_ts 31103 15178812 1 0 0.00
RUN clear_ts 2846 1389396 0 0 0.00
RUN set_ts 3334 1627540 0 988 0.33
RUN set 1627540 1 0 0.33
RUN stop
1 mode ticks timestamp index frequency amplitude
2 RUN config : 0.33, 488
3 RUN set_ts 4882 2382964 0 1318 0.33
4 RUN set 2382964 1 330 0.33
5 RUN set_ts 4457 2175564 2 659 0.33
6 RUN clear_ts 19106 9324276 1 0 0.00
7 RUN clear_ts 55408 27039652 0 0 0.00
8 RUN set_ts 55896 27277796 0 988 0.33
9 RUN set 27277796 1 330 0.33
10 RUN clear_ts 5008 2444452 2 0 0.00
11 RUN clear_ts 25197 12296684 0 0 0.00
12 RUN set_ts 40334 19683540 0 1046 0.33
13 RUN set 19683540 2 659 0.33
14 RUN clear_ts 54982 26831764 1 0 0.00
15 RUN clear_ts 12077 5894124 0 0 0.00
16 RUN set_ts 27213 13280492 0 1174 0.33
17 RUN set 13280492 1 330 0.33
18 RUN clear_ts 41862 20429204 2 0 0.00
19 RUN set_ts 16369 7988620 2 659 0.33
20 RUN clear_ts 31265 15257868 1 0 0.00
21 RUN clear_ts 5622 2744084 0 0 0.00
22 RUN set_ts 6123 2988572 0 1046 0.33
23 RUN set 2988572 1 330 0.33
24 RUN clear_ts 21147 10320284 2 0 0.00
25 RUN clear_ts 47888 23369892 0 0 0.00
26 RUN set_ts 63546 31010996 0 988 0.33
27 RUN set 31010996 2 659 0.33
28 RUN clear_ts 13164 6424580 1 0 0.00
29 RUN clear_ts 42827 20900124 0 0 0.00
30 RUN set_ts 60660 29602628 0 880 0.33
31 RUN set 29602628 1 220 0.33
32 RUN clear_ts 8371 4085596 2 0 0.00
33 RUN set_ts 2978 1453812 2 440 0.33
34 RUN clear_ts 17627 8602524 1 0 0.00
35 RUN clear_ts 53929 26317900 0 0 0.00
36 RUN set_ts 54417 26556044 0 880 0.33
37 RUN set 26556044 1 220 0.33
38 RUN clear_ts 3529 1722700 2 0 0.00
39 RUN clear_ts 23718 11574932 0 0 0.00
40 RUN set_ts 38855 18961788 0 1046 0.33
41 RUN set 18961788 2 440 0.33
42 RUN clear_ts 53503 26110012 1 0 0.00
43 RUN clear_ts 10598 5172372 0 0 0.00
44 RUN set_ts 25734 12558740 0 1318 0.33
45 RUN set 12558740 1 220 0.33
46 RUN clear_ts 40383 19707452 2 0 0.00
47 RUN set_ts 12614 6156180 2 440 0.33
48 RUN clear_ts 27262 13304404 1 0 0.00
49 RUN clear_ts 62100 30305348 0 0 0.00
50 RUN set_ts 62588 30543492 0 1174 0.33
51 RUN set 30543492 1 220 0.33
52 RUN clear_ts 11700 5710148 2 0 0.00
53 RUN clear_ts 31889 15562380 0 0 0.00
54 RUN set_ts 47026 22949236 0 1046 0.33
55 RUN set 22949236 2 440 0.33
56 RUN clear_ts 61674 30097460 1 0 0.00
57 RUN clear_ts 17304 8444900 0 0 0.00
58 RUN set_ts 34394 16784820 0 988 0.33
59 RUN set 16784820 1 208 0.33
60 RUN clear_ts 47089 22979980 2 0 0.00
61 RUN set_ts 36898 18006772 2 415 0.33
62 RUN clear_ts 51547 25155484 1 0 0.00
63 RUN clear_ts 22313 10889292 0 0 0.00
64 RUN set_ts 22801 11127436 0 988 0.33
65 RUN set 11127436 1 208 0.33
66 RUN clear_ts 37449 18275660 2 0 0.00
67 RUN clear_ts 57638 28127892 0 0 0.00
68 RUN set_ts 7239 3533180 0 1046 0.33
69 RUN set 3533180 2 415 0.33
70 RUN clear_ts 21887 10681404 1 0 0.00
71 RUN clear_ts 44518 21725332 0 0 0.00
72 RUN set_ts 59654 29111700 0 1174 0.33
73 RUN set 29111700 1 208 0.33
74 RUN clear_ts 8767 4278844 2 0 0.00
75 RUN set_ts 46534 22709140 2 415 0.33
76 RUN clear_ts 61182 29857364 1 0 0.00
77 RUN clear_ts 30484 14876740 0 0 0.00
78 RUN set_ts 30972 15114884 0 1318 0.33
79 RUN set 15114884 1 208 0.33
80 RUN clear_ts 45620 22263108 2 0 0.00
81 RUN set_ts 15410 7520628 2 415 0.33
82 RUN clear_ts 30058 14668852 1 0 0.00
83 RUN clear_ts 336 164516 0 0 0.00
84 RUN set_ts 2778 1356212 0 1046 0.33
85 RUN set 1356212 1 220 0.33
86 RUN clear_ts 15473 7551372 2 0 0.00
87 RUN set_ts 5282 2578164 2 440 0.33
88 RUN clear_ts 19931 9726876 1 0 0.00
89 RUN clear_ts 56233 27442252 0 0 0.00
90 RUN set_ts 56721 27680396 0 880 0.33
91 RUN set 27680396 1 220 0.33
92 RUN clear_ts 5833 2847052 2 0 0.00
93 RUN set_ts 41159 20086140 2 440 0.33
94 RUN clear_ts 55807 27234364 1 0 0.00
95 RUN clear_ts 27550 13444948 0 0 0.00
96 RUN set_ts 28038 13683092 0 880 0.33
97 RUN set 13683092 1 220 0.33
98 RUN clear_ts 42687 20831804 2 0 0.00
99 RUN set_ts 14918 7280532 2 440 0.33
100 RUN clear_ts 29566 14428756 1 0 0.00
101 RUN clear_ts 64404 31429700 0 0 0.00
102 RUN set_ts 64892 31667844 0 247 0.33
103 RUN set 31667844 1 494 0.33
104 RUN clear_ts 14004 6834500 2 0 0.00
105 RUN clear_ts 18568 9061732 1 0 0.00
106 RUN set_ts 49330 24073588 1 262 0.33
107 RUN set 24073588 2 523 0.33
108 RUN clear_ts 63978 31221812 0 0 0.00
109 RUN clear_ts 3983 1944252 2 0 0.00
110 RUN set_ts 36698 17909172 0 294 0.33
111 RUN clear_ts 49393 24104332 1 0 0.00
112 RUN set_ts 39202 19131124 1 587 0.33
113 RUN clear_ts 53851 26279836 0 0 0.00
114 RUN set_ts 25105 12251788 0 1174 0.33
115 RUN set 12251788 2 294 0.33
116 RUN clear_ts 39753 19400012 1 0 0.00
117 RUN set_ts 9543 4657532 1 587 0.33
118 RUN clear_ts 24191 11805756 2 0 0.00
119 RUN clear_ts 61470 29997908 0 0 0.00
120 RUN set_ts 61958 30236052 0 1397 0.33
121 RUN set 30236052 2 294 0.33
122 RUN clear_ts 11071 5403196 1 0 0.00
123 RUN clear_ts 33701 16446636 0 0 0.00
124 RUN set_ts 48838 23833492 0 1760 0.33
125 RUN set 23833492 1 587 0.33
126 RUN clear_ts 63486 30981716 2 0 0.00
127 RUN set_ts 33276 16239236 2 294 0.33
128 RUN clear_ts 47924 23387460 1 0 0.00
129 RUN clear_ts 2577 1258124 0 0 0.00
130 RUN set_ts 17714 8644980 0 587 0.33
131 RUN clear_ts 32362 15793204 2 0 0.00
132 RUN set_ts 5082 2480564 1 1568 0.33
133 RUN set 2480564 2 294 0.33
134 RUN clear_ts 17777 8675724 0 0 0.00
135 RUN clear_ts 57985 28297228 1 0 0.00
136 RUN set_ts 7586 3702516 0 1397 0.33
137 RUN set 3702516 1 587 0.33
138 RUN clear_ts 22235 10851228 2 0 0.00
139 RUN clear_ts 43888 21417892 0 0 0.00
140 RUN set_ts 59025 28804748 0 1318 0.33
141 RUN set 28804748 2 262 0.33
142 RUN clear_ts 8137 3971404 1 0 0.00
143 RUN set_ts 43463 21210492 1 523 0.33
144 RUN clear_ts 58111 28358716 2 0 0.00
145 RUN set_ts 30342 14807444 2 262 0.33
146 RUN clear_ts 44991 21956156 1 0 0.00
147 RUN set_ts 17222 8404884 1 523 0.33
148 RUN clear_ts 31870 15553108 2 0 0.00
149 RUN clear_ts 1172 572484 0 0 0.00
150 RUN set_ts 1660 810628 0 1046 0.33
151 RUN set 810628 2 262 0.33
152 RUN clear_ts 16308 7958852 1 0 0.00
153 RUN clear_ts 36497 17811084 0 0 0.00
154 RUN set_ts 51634 25197940 0 1318 0.33
155 RUN set 25197940 1 523 0.33
156 RUN clear_ts 746 364596 2 0 0.00
157 RUN set_ts 39002 19033524 2 262 0.33
158 RUN clear_ts 51697 25228684 1 0 0.00
159 RUN set_ts 41506 20255476 1 523 0.33
160 RUN clear_ts 56155 27404188 2 0 0.00
161 RUN clear_ts 26921 13137996 0 0 0.00
162 RUN set_ts 27409 13376140 0 1174 0.33
163 RUN set 13376140 2 262 0.33
164 RUN clear_ts 42057 20524364 1 0 0.00
165 RUN clear_ts 617 301644 0 0 0.00
166 RUN set_ts 11847 5781884 0 1046 0.33
167 RUN set 5781884 1 523 0.33
168 RUN clear_ts 26495 12930108 2 0 0.00
169 RUN clear_ts 49126 23974036 0 0 0.00
170 RUN set_ts 64262 31360404 0 988 0.33
171 RUN set 31360404 2 208 0.33
172 RUN clear_ts 13375 6527548 1 0 0.00
173 RUN set_ts 51142 24957844 1 415 0.33
174 RUN clear_ts 254 124500 2 0 0.00
175 RUN set_ts 35580 17363588 2 208 0.33
176 RUN clear_ts 50228 24511812 1 0 0.00
177 RUN clear_ts 17577 8578124 0 0 0.00
178 RUN set_ts 20018 9769332 0 1046 0.33
179 RUN set 9769332 1 415 0.33
180 RUN clear_ts 34666 16917556 2 0 0.00
181 RUN clear_ts 55832 27246564 0 0 0.00
182 RUN set_ts 7386 3604916 0 1174 0.33
183 RUN set 3604916 2 208 0.33
184 RUN clear_ts 20081 9800076 1 0 0.00
185 RUN set_ts 9890 4826868 1 415 0.33
186 RUN clear_ts 24539 11975580 2 0 0.00
187 RUN clear_ts 46192 22542244 0 0 0.00
188 RUN set_ts 61329 29929100 0 1318 0.33
189 RUN set 29929100 2 208 0.33
190 RUN clear_ts 10441 5095756 1 0 0.00
191 RUN set_ts 45767 22334844 1 415 0.33
192 RUN clear_ts 60415 29483068 2 0 0.00
193 RUN clear_ts 32158 15693652 0 0 0.00
194 RUN set_ts 32646 15931796 0 1046 0.33
195 RUN set 15931796 2 220 0.33
196 RUN clear_ts 47295 23080508 1 0 0.00
197 RUN set_ts 19526 9529236 1 440 0.33
198 RUN clear_ts 34174 16677460 2 0 0.00
199 RUN clear_ts 3476 1696836 0 0 0.00
200 RUN set_ts 3964 1934980 0 880 0.33
201 RUN set 1934980 2 220 0.33
202 RUN clear_ts 18612 9083204 1 0 0.00
203 RUN set_ts 53938 26322292 1 440 0.33
204 RUN clear_ts 3050 1488948 2 0 0.00
205 RUN clear_ts 38864 18966180 0 0 0.00
206 RUN set_ts 41306 20157876 0 880 0.33
207 RUN set 20157876 2 220 0.33
208 RUN clear_ts 54001 26353036 1 0 0.00
209 RUN set_ts 43810 21379828 1 440 0.33
210 RUN clear_ts 58459 28528540 2 0 0.00
211 RUN clear_ts 29225 14262348 0 0 0.00
212 RUN set_ts 29713 14500492 0 220 0.33
213 RUN clear_ts 44361 21648716 1 0 0.00
214 RUN set_ts 14151 6906236 1 440 0.33
215 RUN clear_ts 28799 14054460 0 0 0.00
216 RUN clear_ts 15679 7651900 1 0 0.00
217 RUN set_ts 9690 4729268 0 1318 0.33
218 RUN set 4729268 1 220 0.33
219 RUN set_ts 12194 5951220 2 440 0.33
220 RUN clear_ts 26843 13099932 1 0 0.00
221 RUN set_ts 63633 31053452 1 220 0.33
222 RUN clear_ts 12745 6220108 2 0 0.00
223 RUN set_ts 48071 23459196 2 440 0.33
224 RUN clear_ts 62719 30607420 1 0 0.00
225 RUN clear_ts 34462 16818004 0 0 0.00
226 RUN set_ts 34950 17056148 0 1046 0.33
227 RUN set 17056148 1 220 0.33
228 RUN clear_ts 49599 24204860 2 0 0.00
229 RUN set_ts 21830 10653588 2 440 0.33
230 RUN clear_ts 36478 17801812 1 0 0.00
231 RUN set_ts 6268 3059332 1 220 0.33
232 RUN clear_ts 20916 10207556 2 0 0.00
233 RUN set_ts 56242 27446644 2 440 0.33
234 RUN clear_ts 5354 2613300 1 0 0.00
235 RUN clear_ts 41168 20090532 0 0 0.00
236 RUN set_ts 43610 21282228 0 1174 0.33
237 RUN set 21282228 1 208 0.33
238 RUN clear_ts 56305 27477388 2 0 0.00
239 RUN set_ts 46114 22504180 2 415 0.33
240 RUN clear_ts 60763 29652892 1 0 0.00
241 RUN set_ts 32017 15624844 1 208 0.33
242 RUN clear_ts 46665 22773068 2 0 0.00
243 RUN set_ts 16455 8030588 2 415 0.33
244 RUN clear_ts 31103 15178812 1 0 0.00
245 RUN clear_ts 2846 1389396 0 0 0.00
246 RUN set_ts 3334 1627540 0 988 0.33
247 RUN set 1627540 1 0 0.33
248 RUN stop

View File

@ -7,62 +7,115 @@
#include "tones.hpp" #include "tones.hpp"
#include "util.hpp" #include "util.hpp"
static float amplitude = 0.25; static uint32_t ts_init = 0;
static float ts_last = 0; static uint32_t ts_last = 0;
struct {
float amplitude = 1;
uint32_t us_per_tick = 1;
} config;
struct { struct {
uint8_t index; uint8_t index;
uint16_t frequency; uint16_t frequency;
float amplitude; float amplitude;
} next; } next;
constexpr uint64_t bm(int n) {
return ((uint64_t)1 << n) - 1;
}
void scheduler::do_next() { void scheduler::do_next() {
int i = 1;
if(running) { if(running) {
tones::set(next.index, next.frequency, next.amplitude); tones::set(next.index, next.frequency, next.amplitude);
} else { } else {
eeprom::jump(0); eeprom::jump(0);
tones::clear_all();
ts_init = micros();
running = true; running = true;
} }
entry::Type type; uint64_t v = 0;
eeprom::read(type); 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(") ");
switch(type) { switch(type) {
case entry::Type::VOLUME: { case entry::Type::config: {
eeprom::read(amplitude); 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);
break; break;
} }
case entry::Type::SET: { case entry::Type::set: {
entry::Set e; Serial.println("set");
eeprom::read(e); v >>= 4;
next = {e.index, e.frequency, amplitude}; next.frequency = v & bm(12);
v >>= 12;
next.index = v & bm(5);
next.amplitude = config.amplitude;
break; break;
} }
case entry::Type::CLEAR: { case entry::Type::set_ts: {
entry::Clear e; Serial.println("set_ts");
eeprom::read(e); next.frequency = v & bm(12);
next = {e.index, 0, 0}; 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;
break; break;
} }
case entry::Type::SET_TS: { case entry::Type::clear: {
entry::SetTS e; Serial.println("clear");
eeprom::read(e); next.index = v & bm(5);
timestamp = e.timestamp; next.frequency = 0;
next = {e.index, e.frequency, amplitude}; next.amplitude = 0;
break; break;
} }
case entry::Type::CLEAR_TS: { case entry::Type::clear_ts: {
entry::ClearTS e; Serial.println("clear_ts");
eeprom::read(e); v >>= 4;
timestamp = e.timestamp; uint32_t ticks = v & bm(20);
next = {e.index, 0, 0}; v >>= 20;
next.index = v & bm(5);
next.frequency = 0;
next.amplitude = 0;
timestamp = ticks * config.us_per_tick + ts_init;
break; break;
} }
default: { default: {
running = false; running = false;
tones::clear_all();
Serial.println("stop");
break; break;
} }
} }
Serial.println("DONE");
} }
void scheduler::clear() { void scheduler::clear() {
@ -71,41 +124,40 @@ void scheduler::clear() {
} }
void scheduler::add_set(uint32_t ts, uint8_t index, uint16_t freq) { void scheduler::add_set(uint32_t ts, uint8_t index, uint16_t freq) {
if(index >= size(tones::all)) { // if(index >= size(tones::all)) {
return; // return;
} // }
if(ts <= ts_last) { // if(ts <= ts_last) {
entry::Set e {index, freq}; // entry::Set e {.type=entry::Type::SET, .index=index, .frequency=freq};
eeprom::write(entry::Type::SET); // eeprom::write(&e, sizeof(e));
eeprom::write(e); // } else {
} else { // entry::SetTS e {.type=entry::Type::SET_TS, .index=index, .ticks=ts, .frequency=freq};
entry::SetTS e {index, freq, ts}; // eeprom::write(&e, sizeof(e));
eeprom::write(entry::Type::SET_TS); // ts_last = ts;
eeprom::write(e); // }
}
} }
void scheduler::add_clear(uint32_t ts, uint8_t index) { void scheduler::add_clear(uint32_t ts, uint8_t index) {
if(index >= size(tones::all)) { // if(index >= size(tones::all)) {
return; // return;
} // }
if(ts <= ts_last) { // if(ts <= ts_last) {
entry::Clear e {index}; // entry::Clear e {.type=entry::Type::CLEAR, .index=index};
eeprom::write(entry::Type::CLEAR); // eeprom::write(&e, sizeof(e));
eeprom::write(e); // } else {
} else { // entry::ClearTS e {.type=entry::Type::CLEAR_TS, .index=index, .ticks=ts};
entry::ClearTS e {index, ts}; // eeprom::write(&e, sizeof(e));
eeprom::write(entry::Type::CLEAR_TS); // ts_last = ts;
eeprom::write(e); // }
}
} }
void scheduler::add_volume(float volume) { void scheduler::add_config(float amplitude, uint32_t us_per_tick) {
eeprom::write(entry::Type::VOLUME); // entry::Config e {.type=entry::Type::CONFIG, .p=0, .us_per_tick=us_per_tick, .amplitude=amplitude};
eeprom::write(volume); // eeprom::write(&e, sizeof(e));
} }
void scheduler::finalize() { void scheduler::finalize() {
eeprom::write(entry::Type::STOP); // entry::Header e {.type=entry::Type::STOP};
// eeprom::write(&e, sizeof(e));
} }

View File

@ -10,7 +10,7 @@ namespace scheduler {
void clear(); void clear();
void add_set(uint32_t ts, uint8_t index, uint16_t freq); void add_set(uint32_t ts, uint8_t index, uint16_t freq);
void add_clear(uint32_t ts, uint8_t index); void add_clear(uint32_t ts, uint8_t index);
void add_volume(float volume); void add_config(float volume, uint32_t us_per_tick);
void finalize(); void finalize();
}; };

View File

@ -17,8 +17,14 @@ float serial::read_float(char ch) {
return atof(buffer); return atof(buffer);
} }
long serial::read_long(char ch) {
read_until(ch);
return atol(buffer);
}
int serial::read_int(char ch) { int serial::read_int(char ch) {
read_until(ch); read_until(ch);
return atoi(buffer); return atoi(buffer);
} }

View File

@ -9,5 +9,6 @@ namespace serial {
void read_until(char ch = ','); void read_until(char ch = ',');
float read_float(char ch = ','); float read_float(char ch = ',');
int read_int(char ch = ','); int read_int(char ch = ',');
long read_long(char ch = ',');
}; };

View File

@ -24,12 +24,13 @@ void setup() {
} }
void loop() { void loop() {
if(Serial.available() && Serial.read() == ':') { if(scheduler::running) {
commands::process(); if(timing::at >= scheduler::timestamp) {
scheduler::do_next();
}
} }
else if(Serial.available() && Serial.read() == ':') {
if(scheduler::running && timing::at >= scheduler::timestamp) { commands::process();
scheduler::do_next();
} }
timing::update(); timing::update();

View File

@ -4,7 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
inline const float SIN[] = { inline const PROGMEM float SIN[] = {
0.000000, 0.024541, 0.049068, 0.073565, 0.098017, 0.122411, 0.146730, 0.170962, 0.195090, 0.219101, 0.242980, 0.266713, 0.290285, 0.313682, 0.336890, 0.359895, 0.000000, 0.024541, 0.049068, 0.073565, 0.098017, 0.122411, 0.146730, 0.170962, 0.195090, 0.219101, 0.242980, 0.266713, 0.290285, 0.313682, 0.336890, 0.359895,
0.382683, 0.405241, 0.427555, 0.449611, 0.471397, 0.492898, 0.514103, 0.534998, 0.555570, 0.575808, 0.595699, 0.615232, 0.634393, 0.653173, 0.671559, 0.689541, 0.382683, 0.405241, 0.427555, 0.449611, 0.471397, 0.492898, 0.514103, 0.534998, 0.555570, 0.575808, 0.595699, 0.615232, 0.634393, 0.653173, 0.671559, 0.689541,
0.707107, 0.724247, 0.740951, 0.757209, 0.773010, 0.788346, 0.803208, 0.817585, 0.831470, 0.844854, 0.857729, 0.870087, 0.881921, 0.893224, 0.903989, 0.914210, 0.707107, 0.724247, 0.740951, 0.757209, 0.773010, 0.788346, 0.803208, 0.817585, 0.831470, 0.844854, 0.857729, 0.870087, 0.881921, 0.893224, 0.903989, 0.914210,
@ -38,7 +38,7 @@ struct Tone {
inline float get() const { inline float get() const {
uint8_t id = phase >> 12; uint8_t id = phase >> 12;
return SIN[id] * amplitude; return pgm_read_float(&SIN[id]) * amplitude;
} }
}; };

View File

@ -5,7 +5,7 @@
namespace tones { namespace tones {
inline Tone all[16]; inline Tone all[32];
inline int active; inline int active;
void prune(uint8_t index); void prune(uint8_t index);