From aa752faa5f530af75fdff0843c00382f8dd8d841 Mon Sep 17 00:00:00 2001 From: Jay Robson Date: Sun, 25 Aug 2024 13:55:43 +1000 Subject: [PATCH] working play from progmem --- .gitignore | 1 + commands.cpp | 57 +++++++++-- eeprom.cpp | 33 ++++++ eeprom.hpp | 22 +--- entry.hpp | 61 +++++++++-- output.csv | 249 +++++++++++++++++++++++++++++++++++++++++++++ scheduler.cpp | 156 ++++++++++++++++++---------- scheduler.hpp | 2 +- serial.cpp | 6 ++ serial.hpp | 1 + tone-generator.ino | 11 +- tone.hpp | 4 +- tones.hpp | 2 +- 13 files changed, 506 insertions(+), 99 deletions(-) create mode 100644 eeprom.cpp create mode 100644 output.csv diff --git a/.gitignore b/.gitignore index 906ce73..b8da58f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ sketch.yaml +midi_data.hpp diff --git a/commands.cpp b/commands.cpp index 833b274..a171ee6 100644 --- a/commands.cpp +++ b/commands.cpp @@ -5,6 +5,7 @@ #include "serial.hpp" #include "tones.hpp" #include "scheduler.hpp" +#include "eeprom.hpp" static void help() { Serial.println("OK help"); @@ -12,11 +13,13 @@ static void help() { Serial.println(" c: clear :c,index,"); Serial.println(" g: get :g,index,"); 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(" c: clear v,timestamp,index,"); Serial.println(" p: play :p,"); + Serial.println(" q: stop :q,"); Serial.println(" r: reset :r,"); + Serial.println(" e: eeprom :e,"); Serial.println(" h: help :h,"); } @@ -46,25 +49,38 @@ static bool write() { for(;;) { serial::read_until(); switch(serial::buffer[0]) { - case 'v': - scheduler::add_volume(serial::read_float()); + case 'o': { + float amplitude = serial::read_float(); + uint32_t us_per_tick = serial::read_long(); + scheduler::add_config(amplitude, us_per_tick); 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; - 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; + } case ';': return false; default: return true; } + Serial.println(); } scheduler::finalize(); } +static const char HEXC[] = "0123456789abcdef"; + void commands::process() { serial::read_until(); switch(serial::buffer[0]) { @@ -72,10 +88,14 @@ void commands::process() { if(write()) break; Serial.println("OK"); return; - case 's': - tones::set(serial::read_int(), serial::read_int(), serial::read_int()); + case 's': { + 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"); return; + } case 'c': tones::clear(serial::read_int()); Serial.println("OK"); @@ -87,7 +107,24 @@ void commands::process() { scheduler::running = false; scheduler::do_next(); 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': tones::clear_all(); Serial.println("OK"); diff --git a/eeprom.cpp b/eeprom.cpp new file mode 100644 index 0000000..efb3111 --- /dev/null +++ b/eeprom.cpp @@ -0,0 +1,33 @@ + +#include +#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; +} + + diff --git a/eeprom.hpp b/eeprom.hpp index fc5232e..ca6a623 100644 --- a/eeprom.hpp +++ b/eeprom.hpp @@ -1,26 +1,10 @@ #pragma once -#include - namespace eeprom { - inline int address = 0; - - inline void jump(int loc = 0) { - address = 0; - } - - template - inline void read(T& type) { - EEPROM.get(address, type); - address += sizeof(type); - } - - template - inline void write(const T& type) { - EEPROM.put(address, type); - address += sizeof(type); - } + void jump(int loc = 0); + void read(void* data, size_t len); + void write(const void* data, size_t len); }; diff --git a/entry.hpp b/entry.hpp index b192579..1ed7c1a 100644 --- a/entry.hpp +++ b/entry.hpp @@ -1,8 +1,22 @@ #pragma once +template +struct Checker { + static constexpr bool value = v; +}; + namespace entry { + enum Type : uint8_t { + stop, + config, + set, + set_ts, + clear, + clear_ts, + }; + struct Set { uint8_t index; uint16_t frequency; @@ -14,22 +28,51 @@ namespace entry { struct SetTS { uint8_t index; + uint32_t ticks; uint16_t frequency; - uint32_t timestamp; }; struct ClearTS { uint8_t index; - uint32_t timestamp; + uint32_t ticks; }; - enum Type { - STOP, - VOLUME, - SET, - SET_TS, - CLEAR, - CLEAR_TS, + struct Stop { }; + + 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; + } + } }; diff --git a/output.csv b/output.csv new file mode 100644 index 0000000..f37db72 --- /dev/null +++ b/output.csv @@ -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 + diff --git a/scheduler.cpp b/scheduler.cpp index d0d1fbe..261af6f 100644 --- a/scheduler.cpp +++ b/scheduler.cpp @@ -7,62 +7,115 @@ #include "tones.hpp" #include "util.hpp" -static float amplitude = 0.25; -static float ts_last = 0; +static uint32_t ts_init = 0; +static uint32_t ts_last = 0; +struct { + float amplitude = 1; + uint32_t us_per_tick = 1; +} config; struct { uint8_t index; uint16_t frequency; float amplitude; } next; +constexpr uint64_t bm(int n) { + return ((uint64_t)1 << n) - 1; +} + void scheduler::do_next() { + + int i = 1; if(running) { tones::set(next.index, next.frequency, next.amplitude); } else { eeprom::jump(0); + tones::clear_all(); + ts_init = micros(); running = true; } - entry::Type type; - eeprom::read(type); + 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(") "); switch(type) { - case entry::Type::VOLUME: { - eeprom::read(amplitude); + 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); break; } - case entry::Type::SET: { - entry::Set e; - eeprom::read(e); - next = {e.index, e.frequency, amplitude}; + 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; break; } - case entry::Type::CLEAR: { - entry::Clear e; - eeprom::read(e); - next = {e.index, 0, 0}; + 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; break; } - case entry::Type::SET_TS: { - entry::SetTS e; - eeprom::read(e); - timestamp = e.timestamp; - next = {e.index, e.frequency, amplitude}; + case entry::Type::clear: { + Serial.println("clear"); + next.index = v & bm(5); + next.frequency = 0; + next.amplitude = 0; break; } - case entry::Type::CLEAR_TS: { - entry::ClearTS e; - eeprom::read(e); - timestamp = e.timestamp; - next = {e.index, 0, 0}; + 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; break; } default: { running = false; + tones::clear_all(); + Serial.println("stop"); break; } } + + Serial.println("DONE"); } void scheduler::clear() { @@ -71,41 +124,40 @@ void scheduler::clear() { } void scheduler::add_set(uint32_t ts, uint8_t index, uint16_t freq) { - if(index >= size(tones::all)) { - return; - } - if(ts <= ts_last) { - entry::Set e {index, freq}; - eeprom::write(entry::Type::SET); - eeprom::write(e); - } else { - entry::SetTS e {index, freq, ts}; - eeprom::write(entry::Type::SET_TS); - eeprom::write(e); - } +// 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; +// } } void scheduler::add_clear(uint32_t ts, uint8_t index) { - if(index >= size(tones::all)) { - return; - } - if(ts <= ts_last) { - entry::Clear e {index}; - eeprom::write(entry::Type::CLEAR); - eeprom::write(e); - } else { - entry::ClearTS e {index, ts}; - eeprom::write(entry::Type::CLEAR_TS); - eeprom::write(e); - } +// 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; +// } } -void scheduler::add_volume(float volume) { - eeprom::write(entry::Type::VOLUME); - eeprom::write(volume); +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)); } void scheduler::finalize() { - eeprom::write(entry::Type::STOP); +// entry::Header e {.type=entry::Type::STOP}; +// eeprom::write(&e, sizeof(e)); } diff --git a/scheduler.hpp b/scheduler.hpp index 6bb9573..778641c 100644 --- a/scheduler.hpp +++ b/scheduler.hpp @@ -10,7 +10,7 @@ namespace scheduler { void clear(); void add_set(uint32_t ts, uint8_t index, uint16_t freq); 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(); }; diff --git a/serial.cpp b/serial.cpp index 6ded87c..f297a04 100644 --- a/serial.cpp +++ b/serial.cpp @@ -17,8 +17,14 @@ float serial::read_float(char ch) { return atof(buffer); } +long serial::read_long(char ch) { + read_until(ch); + return atol(buffer); +} + int serial::read_int(char ch) { read_until(ch); return atoi(buffer); } + diff --git a/serial.hpp b/serial.hpp index 6fd458e..2d29871 100644 --- a/serial.hpp +++ b/serial.hpp @@ -9,5 +9,6 @@ namespace serial { void read_until(char ch = ','); float read_float(char ch = ','); int read_int(char ch = ','); + long read_long(char ch = ','); }; diff --git a/tone-generator.ino b/tone-generator.ino index ca5f136..8256cec 100644 --- a/tone-generator.ino +++ b/tone-generator.ino @@ -24,12 +24,13 @@ void setup() { } void loop() { - if(Serial.available() && Serial.read() == ':') { - commands::process(); + if(scheduler::running) { + if(timing::at >= scheduler::timestamp) { + scheduler::do_next(); + } } - - if(scheduler::running && timing::at >= scheduler::timestamp) { - scheduler::do_next(); + else if(Serial.available() && Serial.read() == ':') { + commands::process(); } timing::update(); diff --git a/tone.hpp b/tone.hpp index 1233658..d347071 100644 --- a/tone.hpp +++ b/tone.hpp @@ -4,7 +4,7 @@ #include #include -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.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, @@ -38,7 +38,7 @@ struct Tone { inline float get() const { uint8_t id = phase >> 12; - return SIN[id] * amplitude; + return pgm_read_float(&SIN[id]) * amplitude; } }; diff --git a/tones.hpp b/tones.hpp index 6e3adc9..49b8fa6 100644 --- a/tones.hpp +++ b/tones.hpp @@ -5,7 +5,7 @@ namespace tones { - inline Tone all[16]; + inline Tone all[32]; inline int active; void prune(uint8_t index);