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

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

View file

@ -42,6 +42,15 @@ impl<'d, T: GeneralInstance16bit> SimplePwmCore<'d, T> {
// }
}
// pub struct SimplePwmHandle<T: GeneralInstance16Bit> {
// core: &'static SimplePwmCore<'_, T: GeneralInstance16Bit>,
// channel: Channel,
// }
// impl<T: GeneralInstance16Bit> SimplePwmHandle<T> {
// pub fn set_amplitude(&self, amplitude: u8) {}
// }
pub struct CoreConfig {
pub tick_rate_hz: usize,
}

View file

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

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