2024-08-19 22:43:24 +10:00
|
|
|
|
|
|
|
#include "util.hpp"
|
|
|
|
#include "tones.hpp"
|
|
|
|
#include "tone.hpp"
|
|
|
|
|
2024-08-25 16:53:23 +10:00
|
|
|
using tones::all;
|
|
|
|
using tones::active;
|
|
|
|
|
|
|
|
void tones::init() {
|
|
|
|
for(uint8_t i = 0; i < size(all); i++) {
|
|
|
|
lookup[i] = i;
|
|
|
|
rlookup[i] = i;
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-25 16:53:23 +10:00
|
|
|
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++;
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
2024-08-25 16:53:23 +10:00
|
|
|
active = j;
|
|
|
|
}
|
2024-08-19 22:43:24 +10:00
|
|
|
|
2024-08-25 16:53:23 +10:00
|
|
|
void tones::clear_all() {
|
|
|
|
for(int i = 0; i < size(all); i++) {
|
|
|
|
all[i] = {0, 0, 0};
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
2024-08-25 16:53:23 +10:00
|
|
|
active = 0;
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
|
2024-10-03 02:27:58 +10:00
|
|
|
void tones::set(uint8_t index, uint16_t frequency, float amplitude, Tone::Type mode) {
|
2024-08-19 22:43:24 +10:00
|
|
|
if(index >= size(all)) {
|
|
|
|
return;
|
|
|
|
}
|
2024-08-25 16:53:23 +10:00
|
|
|
Tone& t = all[lookup[index]];
|
|
|
|
if(amplitude == 0) {
|
2024-10-03 02:27:58 +10:00
|
|
|
t = {0, 0, 0, Tone::tt_sine};
|
2024-08-25 16:53:23 +10:00
|
|
|
} else {
|
|
|
|
t.frequency = frequency;
|
|
|
|
t.amplitude = amplitude;
|
2024-10-03 02:27:58 +10:00
|
|
|
t.mode = mode;
|
2024-08-19 22:43:24 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|