optimisations

This commit is contained in:
Jay Robson 2024-08-25 16:53:23 +10:00
parent aa752faa5f
commit 9263b619a1
13 changed files with 80 additions and 563 deletions

View File

@ -1,139 +0,0 @@
#include <Arduino.h>
#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");
}

View File

@ -1,9 +0,0 @@
#pragma once
namespace commands {
void process();
};

10
dac.hpp
View File

@ -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);
}
};

View File

@ -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
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

@ -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) {
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));
}

View File

@ -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();
};

View File

@ -1,30 +0,0 @@
#include "util.hpp"
#include "serial.hpp"
#include <Arduino.h>
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);
}

View File

@ -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 = ',');
};

View File

@ -1,5 +0,0 @@
#pragma once
#define size(V) (sizeof(V) / sizeof(V[0]))

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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();
}

View File

@ -6,5 +6,12 @@ inline T clamp(T v, T a, T b) {
return min(max(v, a), b);
}
template <typename T>
inline void swap(T& a, T& b) {
T t = b;
b = a;
a = t;
}
#define size(V) (sizeof(V) / sizeof(V[0]))