diff --git a/ch32v-insert-coin/src/app.rs b/ch32v-insert-coin/src/app.rs new file mode 100644 index 0000000..81caeee --- /dev/null +++ b/ch32v-insert-coin/src/app.rs @@ -0,0 +1,66 @@ +#[derive(Default)] +pub enum State { + // system is asleep, waiting for wake from coin insertion + DeepSleep, + // system is in low-power mode, dimmed lights, waiting for interaction + Idle, + // system is active. on entry: play coin sound. on button press: play different sound + #[default] + Active, +} + +mod settings { + pub enum Level { + Off, + Low, + Medium, + High, + Maximum, + } + + impl Level { + pub fn next(&mut self) { + *self = match self { + Self::Off => Self::Low, + Self::Low => Self::Medium, + Self::Medium => Self::High, + Self::High => Self::Maximum, + Self::Maximum => Self::Off, + }; + } + } + + pub struct Settings { + pub brightness: Level, + pub volume: Level, + pub button_sound_index: usize, + } + + impl Default for Settings { + fn default() -> Self { + Self { + brightness: Level::Medium, + volume: Level::Medium, + button_sound_index: 0, + } + } + } +} + +pub use settings::Settings; + +pub struct App { + state: State, + pub settings: Settings, + // TODO: make this the "sound module" or whatever. + // synthesizers: AppSynthesizers, +} + +impl Default for App { + fn default() -> Self { + Self { + state: State::default(), + settings: Settings::default(), + } + } +} diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index ed5e986..f044b57 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -13,6 +13,9 @@ mod system; mod synthesizer; use synthesizer::AppSynthesizers; +mod app; +use app::App; + use ch32_hal::{adc::AdcChannel, interrupt::typelevel::Handler, timer::low_level::OutputPolarity}; use insert_coin::{CoreConfig, InsertCoin, SimplePwmCore}; @@ -28,42 +31,6 @@ use hal::println; use qingke::riscv; -enum Level { - Off, - Low, - Medium, - High, - Maximum, -} - -impl Level { - pub fn next(&mut self) { - *self = match self { - Self::Off => Self::Low, - Self::Low => Self::Medium, - Self::Medium => Self::High, - Self::High => Self::Maximum, - Self::Maximum => Self::Off, - }; - } -} - -struct Settings { - brightness: Level, - volume: Level, - button_sound_index: usize, -} - -impl Default for Settings { - fn default() -> Self { - Self { - brightness: Level::Medium, - volume: Level::Medium, - button_sound_index: 0, - } - } -} - struct DebouncedGPIO<'a> { input: Input<'a>, // value of the GPIO @@ -508,7 +475,8 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { interfaces.set_active(true); // insert_coin.init(); - let mut settings = Settings::default(); + // let mut settings = Se::default(); + let mut app = App::default(); let mut led0_index = 0; let led0_dcs = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8]; @@ -672,10 +640,10 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { // .dac // .load_data(button_sounds[settings.button_sound_index]); - settings.button_sound_index += 1; - if settings.button_sound_index > 2 { + app.settings.button_sound_index += 1; + if app.settings.button_sound_index > 2 { // if settings.button_sound_index > button_sounds.len() - 1 { - settings.button_sound_index = 0; + app.settings.button_sound_index = 0; } #[cfg(feature = "enable_print")] println!("reset hold timers + enable"); @@ -691,13 +659,13 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { if volume_btn_input.ready() { #[cfg(feature = "enable_print")] println!("volume btn value: {}", volume_btn_input.value()); - settings.volume.next(); + app.settings.volume.next(); volume_btn_input.reset(); } if light_ctrl_btn_input.ready() { #[cfg(feature = "enable_print")] println!("light_ctrl_btn value: {}", light_ctrl_btn_input.value()); - settings.brightness.next(); + app.settings.brightness.next(); light_ctrl_btn_input.reset(); }