diff --git a/commands.cpp b/commands.cpp deleted file mode 100644 index a171ee6..0000000 --- a/commands.cpp +++ /dev/null @@ -1,139 +0,0 @@ - -#include -#include "util.hpp" -#include "commands.hpp" -#include "serial.hpp" -#include "tones.hpp" -#include "scheduler.hpp" -#include "eeprom.hpp" - -static void help() { - Serial.println("OK help"); - Serial.println(" s: set :s,index,freq,amplitude,"); - Serial.println(" c: clear :c,index,"); - Serial.println(" g: get :g,index,"); - Serial.println(" w: write :w,[v|s|c,...]...;,"); - 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,"); -} - -static bool get() { - int index = serial::read_int(); - if(index < 0 || index >= size(tones::all)) { - return true; - } - if(index >= tones::active) { - tones::active = index + 1; - } - Serial.print("OK "); - Serial.print(index); - Serial.print(','); - Serial.print(tones::all[index].frequency); - Serial.print(','); - Serial.print(tones::all[index].amplitude); - Serial.print(','); - Serial.println(); - return false; -} - -static bool write() { - - scheduler::clear(); - - for(;;) { - serial::read_until(); - switch(serial::buffer[0]) { - 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': { - 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': { - 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]) { - case 'w': - if(write()) break; - Serial.println("OK"); - return; - 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"); - return; - case 'g': - if(get()) break; - return; - case 'p': - scheduler::running = false; - scheduler::do_next(); - Serial.println("OK"); - 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"); - return; - case 'h': - help(); - return; - } - - Serial.println("ERROR"); -} - diff --git a/commands.hpp b/commands.hpp deleted file mode 100644 index 8c367c1..0000000 --- a/commands.hpp +++ /dev/null @@ -1,9 +0,0 @@ - -#pragma once - -namespace commands { - - void process(); - -}; - diff --git a/dac.hpp b/dac.hpp index 88042c5..bd28d1b 100644 --- a/dac.hpp +++ b/dac.hpp @@ -5,18 +5,12 @@ namespace dac { - constexpr int BITMASK_B = 0x07; - constexpr int BITMASK_D = 0xf8; - inline void init() { - DDRB |= BITMASK_B; - DDRD |= BITMASK_D; + DDRD = 0xff; } inline void set(float v_f) { - uint8_t v = clamp((int)(v_f * -128) + 127, 0, 255); - PORTB = (PORTB & ~BITMASK_B) | (v & BITMASK_B); - PORTD = (PORTD & ~BITMASK_D) | (v & BITMASK_D); + PORTD = clamp((int)(v_f * -128) + 127, 0, 255); } }; diff --git a/output.csv b/output.csv deleted file mode 100644 index f37db72..0000000 --- a/output.csv +++ /dev/null @@ -1,249 +0,0 @@ -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 261af6f..3e95eec 100644 --- a/scheduler.cpp +++ b/scheduler.cpp @@ -17,23 +17,31 @@ struct { uint8_t index; uint16_t frequency; float amplitude; + bool active = false; } next; constexpr uint64_t bm(int n) { return ((uint64_t)1 << n) - 1; } +void scheduler::init() { + eeprom::jump(0); + tones::clear_all(); + ts_init = micros(); + running = true; +} + void scheduler::do_next() { int i = 1; if(running) { - tones::set(next.index, next.frequency, next.amplitude); + if(next.active) { + tones::set(next.index, next.frequency, next.amplitude); + next.active = false; + } } else { - eeprom::jump(0); - tones::clear_all(); - ts_init = micros(); - running = true; + init(); } uint64_t v = 0; @@ -44,78 +52,69 @@ void scheduler::do_next() { 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::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: { - Serial.println("set"); v >>= 4; next.frequency = v & bm(12); v >>= 12; next.index = v & bm(5); next.amplitude = config.amplitude; + next.active = true; break; } 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; + next.active = true; timestamp = ticks * config.us_per_tick + ts_init; break; } case entry::Type::clear: { - Serial.println("clear"); next.index = v & bm(5); next.frequency = 0; next.amplitude = 0; + next.active = true; break; } 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; + next.active = true; timestamp = ticks * config.us_per_tick + ts_init; break; } default: { running = false; tones::clear_all(); - Serial.println("stop"); break; } } +} - Serial.println("DONE"); +void scheduler::do_all() { + while(running && timing::at >= timestamp) { + do_next(); + } + tones::recalc(); } void scheduler::clear() { @@ -123,41 +122,3 @@ void scheduler::clear() { ts_last = 0; } -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 {.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 {.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_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() { -// entry::Header e {.type=entry::Type::STOP}; -// eeprom::write(&e, sizeof(e)); -} - diff --git a/scheduler.hpp b/scheduler.hpp index 778641c..d44f009 100644 --- a/scheduler.hpp +++ b/scheduler.hpp @@ -6,11 +6,9 @@ namespace scheduler { inline uint32_t timestamp = 0; inline bool running = false; + void init(); void do_next(); + void do_all(); 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_config(float volume, uint32_t us_per_tick); - void finalize(); }; diff --git a/serial.cpp b/serial.cpp deleted file mode 100644 index f297a04..0000000 --- a/serial.cpp +++ /dev/null @@ -1,30 +0,0 @@ - -#include "util.hpp" -#include "serial.hpp" -#include - -void serial::init() { - Serial.begin(115200); - Serial.println("READY"); -} - -void serial::read_until(char ch) { - buffer[Serial.readBytesUntil(ch, buffer, sizeof(buffer) - 1)] = '\0'; -} - -float serial::read_float(char ch) { - read_until(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 deleted file mode 100644 index 2d29871..0000000 --- a/serial.hpp +++ /dev/null @@ -1,14 +0,0 @@ - -#pragma once - -namespace serial { - - inline char buffer[16]; - - void init(); - void read_until(char ch = ','); - float read_float(char ch = ','); - int read_int(char ch = ','); - long read_long(char ch = ','); -}; - diff --git a/size.hpp b/size.hpp deleted file mode 100644 index 7b87be3..0000000 --- a/size.hpp +++ /dev/null @@ -1,5 +0,0 @@ - -#pragma once - -#define size(V) (sizeof(V) / sizeof(V[0])) - diff --git a/tone-generator.ino b/tone-generator.ino index 8256cec..cd80a47 100644 --- a/tone-generator.ino +++ b/tone-generator.ino @@ -1,7 +1,5 @@ -#include "commands.hpp" #include "tone.hpp" -#include "serial.hpp" #include "tones.hpp" #include "dac.hpp" #include "timing.hpp" @@ -19,30 +17,22 @@ inline unsigned long micros_diff() { void setup() { dac::init(); - serial::init(); - scheduler::do_next(); + tones::init(); + scheduler::init(); } void loop() { - if(scheduler::running) { - if(timing::at >= scheduler::timestamp) { - scheduler::do_next(); - } - } - else if(Serial.available() && Serial.read() == ':') { - commands::process(); + if(scheduler::running && timing::at >= scheduler::timestamp) { + scheduler::do_all(); } timing::update(); float value = 0; - uint32_t passed = ((uint64_t)timing::diff << (20)) / 1000000L; + uint32_t passed = timing::diff; for(int i = 0; i < tones::active; i++) { Tone& t = tones::all[i]; - if(!t.active()) { - continue; - } t.update(passed); value += t.get(); } diff --git a/tones.cpp b/tones.cpp index 31169cd..e25b1f9 100644 --- a/tones.cpp +++ b/tones.cpp @@ -3,6 +3,39 @@ #include "tones.hpp" #include "tone.hpp" +using tones::all; +using tones::active; + +static uint8_t lookup[size(all)]; +static uint8_t rlookup[size(all)]; + +void tones::init() { + for(uint8_t i = 0; i < size(all); i++) { + lookup[i] = i; + rlookup[i] = i; + } +} + +void tones::recalc() { + int j = 0; + for(int i = 0; i < size(all); i++) { + if(all[j].active()) { + j++; + continue; + } + if(!all[i].active()) { + continue; + } + if(i != j) { + swap(all[i], all[j]); + swap(rlookup[i], rlookup[j]); + swap(lookup[rlookup[i]], lookup[rlookup[j]]); + } + j++; + } + active = j; +} + void tones::clear_all() { for(int i = 0; i < size(all); i++) { all[i] = {0, 0, 0}; @@ -10,36 +43,16 @@ void tones::clear_all() { active = 0; } -void tones::clear(uint8_t index) { - if(index >= size(all)) { - return; - } - all[index] = {0, 0, 0}; - - if(index == active - 1) { - prune(index); - } -} - void tones::set(uint8_t index, uint16_t frequency, float amplitude) { - if(amplitude == 0) { - return clear(index); - } if(index >= size(all)) { return; } - all[index].frequency = frequency; - all[index].amplitude = amplitude; - - if(index >= tones::active) { - tones::active = index + 1; + Tone& t = all[lookup[index]]; + if(amplitude == 0) { + t = {0, 0, 0}; + } else { + t.frequency = frequency; + t.amplitude = amplitude; } } -void tones::prune(uint8_t index) { - int i; - for(i = index; i >= 0 && all[i].amplitude == 0; i--) { - } - active = i + 1; -} - diff --git a/tones.hpp b/tones.hpp index 49b8fa6..0d2c0dd 100644 --- a/tones.hpp +++ b/tones.hpp @@ -8,9 +8,9 @@ namespace tones { inline Tone all[32]; inline int active; - void prune(uint8_t index); + void init(); void set(uint8_t index, uint16_t frequency, float amplitude); - void clear(uint8_t index); void clear_all(); + void recalc(); } diff --git a/util.hpp b/util.hpp index 1977be9..0dda16e 100644 --- a/util.hpp +++ b/util.hpp @@ -6,5 +6,12 @@ inline T clamp(T v, T a, T b) { return min(max(v, a), b); } +template +inline void swap(T& a, T& b) { + T t = b; + b = a; + a = t; +} + #define size(V) (sizeof(V) / sizeof(V[0]))