move synthesizer into app struct

This commit is contained in:
sigil-03 2025-11-05 08:51:11 -07:00
parent b786bf174a
commit d3ccc70782
4 changed files with 87 additions and 26 deletions

View file

@ -9,37 +9,41 @@ const SQUARE_WAVETABLE: [u8; 2] = [0, 100];
// 100, 100, 100, 100, 100, 100, 100,
// ];
pub struct AppSynthesizers<'a, T: GeneralInstance16bit> {
pub square: SimpleWavetableSynthesizer<SimpleWavetable<'static, u8>>,
output: SimplePwmCore<'a, T>,
pub struct SynthesizerService {
pub synth: SimpleWavetableSynthesizer<SimpleWavetable<'static, u8>>,
}
impl<'a, T: GeneralInstance16bit> AppSynthesizers<'a, T> {
pub fn new(clock_freq_hz: usize, output: SimplePwmCore<'a, T>) -> Self {
impl SynthesizerService {
pub fn new(clock_freq_hz: usize) -> Self {
let square_wt = SimpleWavetable::new(&SQUARE_WAVETABLE);
let square = SimpleWavetableSynthesizer::new(square_wt, clock_freq_hz);
let synth = SimpleWavetableSynthesizer::new(square_wt, clock_freq_hz);
Self { square, output }
Self { synth }
}
pub fn tick(&mut self) {
self.square.tick();
if self.square.has_new_output() {
let out = self.square.get_output();
self.output.write_amplitude(
ch32_hal::timer::Channel::Ch4,
// TODO: set level here. or maybe use dac?
out / 2,
);
}
self.synth.tick();
}
pub fn service(&mut self) {
// println!("HERE");
if self.square.has_new_output() {
let out = self.square.get_output();
self.output
.write_amplitude(ch32_hal::timer::Channel::Ch4, out / 2);
}
pub fn need_service(&self) -> bool {
self.synth.has_new_output()
}
pub fn service(&mut self) -> u8 {
self.synth.get_output()
}
}
impl SynthesizerService {
pub fn set_freq(&mut self, freq_hz: usize) {
self.synth.set_freq(freq_hz);
}
pub fn enable(&mut self) {
// TODO: write the enable function
}
pub fn disable(&mut self) {
// TODO: write the disable function
}
}