From d3ccc70782d05c02ce3f42015155ec2bd9b9460a Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Wed, 5 Nov 2025 08:51:11 -0700 Subject: [PATCH] move synthesizer into app struct --- ch32v-insert-coin/src/app.rs | 48 +++++++++++++++++- .../src/insert_coin/insert_coin.rs | 9 ++++ ch32v-insert-coin/src/main.rs | 6 ++- ch32v-insert-coin/src/synthesizer.rs | 50 ++++++++++--------- 4 files changed, 87 insertions(+), 26 deletions(-) diff --git a/ch32v-insert-coin/src/app.rs b/ch32v-insert-coin/src/app.rs index e7c5eaf..da8b9ca 100644 --- a/ch32v-insert-coin/src/app.rs +++ b/ch32v-insert-coin/src/app.rs @@ -75,6 +75,8 @@ pub mod sequencer { use crate::insert_coin::{ LedService, Service, SimplePwmCore, TickService, TickServiceData, TickTimerService, }; +use crate::synthesizer::SynthesizerService; + pub use settings::Settings; #[cfg(feature = "enable_print")] @@ -157,10 +159,23 @@ impl Timers { } } +// pub struct ServiceConfigs { + +// } +// things that sort of don't touch hardware but also do? +// TODO: make this merged with the interfaces maybe +// but also maybe not, since these are things that require servicing pub struct Services { pub led0: LedService, pub led1: LedService, pub led2: LedService, + pub synth0: SynthesizerService, +} + +impl Services { + pub fn tick(&mut self) { + self.synth0.tick(); + } } pub struct Config { @@ -174,6 +189,7 @@ pub struct Sequences { pub led2: sequencer::BasicSequence<'static>, } +// things that touch hardware pub struct Interfaces { pub pwm_core: SimplePwmCore<'static, ch32_hal::peripherals::TIM1>, } @@ -185,8 +201,6 @@ pub struct App { services: Services, sequences: Sequences, interfaces: Interfaces, - // TODO: make this the "sound module" or whatever. - // synthesizers: AppSynthesizers, } impl App { @@ -222,6 +236,8 @@ impl App { self.timers.led2_timer.reset(); self.timers.led2_timer.enable(true); + + self.services.synth0.set_freq(440); } pub fn set_state(&mut self, state: State) { @@ -238,6 +254,7 @@ impl App { pub fn tick(&mut self) { self.timers.tick(); + self.services.tick(); } pub fn service(&mut self) { @@ -309,5 +326,32 @@ impl App { .write_amplitude(self.services.led2.channel, self.services.led2.amplitude); self.services.led2.service(); } + + if self.services.synth0.need_service() { + let out = self.services.synth0.service(); + self.interfaces + .pwm_core + .write_amplitude(ch32_hal::timer::Channel::Ch4, out); + } } } + +// TODO LIST +// +// AUDIO: +// 1. volume control +// 2. dac switching +// +// LED: +// 1. brightness control +// +// INTERFACE: +// 1. short press handling +// 2. long press handling +// 3. volume press handling +// 4. brightness press handling +// +// SYSTEM: +// 1. deep sleep +// 2. battery voltage monitoring +// 3. battery voltage cutoff diff --git a/ch32v-insert-coin/src/insert_coin/insert_coin.rs b/ch32v-insert-coin/src/insert_coin/insert_coin.rs index 79d6598..426c174 100644 --- a/ch32v-insert-coin/src/insert_coin/insert_coin.rs +++ b/ch32v-insert-coin/src/insert_coin/insert_coin.rs @@ -42,6 +42,15 @@ impl<'d, T: GeneralInstance16bit> SimplePwmCore<'d, T> { // } } +// pub struct SimplePwmHandle { +// core: &'static SimplePwmCore<'_, T: GeneralInstance16Bit>, +// channel: Channel, +// } + +// impl SimplePwmHandle { +// pub fn set_amplitude(&self, amplitude: u8) {} +// } + pub struct CoreConfig { pub tick_rate_hz: usize, } diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 7fdfb35..9ddeb15 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -14,7 +14,7 @@ use debounced_gpio::DebouncedGPIO; // synthesizer :3 mod synthesizer; -use synthesizer::AppSynthesizers; +use synthesizer::SynthesizerService; mod app; use app::{ @@ -262,10 +262,14 @@ fn app_main(mut p: hal::Peripherals) -> ! { timers: timer_config, }; + // let square_wt = SimpleWavetable::new(&SQUARE_WAVETABLE); + // let square = SimpleWavetableSynthesizer::new(square_wt, tick_rate_hz); + let app_services = Services { led0: LedService::new(led0_ch), led1: LedService::new(led1_ch), led2: LedService::new(led2_ch), + synth0: SynthesizerService::new(tick_rate_hz), }; let app_sequences = Sequences { diff --git a/ch32v-insert-coin/src/synthesizer.rs b/ch32v-insert-coin/src/synthesizer.rs index a85d978..b91da38 100644 --- a/ch32v-insert-coin/src/synthesizer.rs +++ b/ch32v-insert-coin/src/synthesizer.rs @@ -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>, - output: SimplePwmCore<'a, T>, +pub struct SynthesizerService { + pub synth: SimpleWavetableSynthesizer>, } -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 } }