From 9f12502a3df5dddd58de1d42972ce8e867dbd92d Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Sun, 26 Oct 2025 17:26:34 -0600 Subject: [PATCH] add edge detecting inputs for volume and brightness settings --- ch32v-insert-coin/src/main.rs | 87 +++++++++++++++++++++++---------- ch32v-insert-coin/src/system.rs | 7 +-- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 9ad174c..71b3cc4 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -47,7 +47,7 @@ impl Level { struct Settings { brightness: Level, volume: Level, - button_sound_index: u8, + button_sound_index: usize, } impl Default for Settings { @@ -156,20 +156,20 @@ impl Default for InputFlags { } } -impl InputFlags { - pub fn set_sense_coin_flag(&mut self, val: bool) { - self.sense_coin_flag = val; - } - pub fn set_main_btn_flag(&mut self, val: bool) { - self.main_btn_flag = val; - } - pub fn set_volume_btn_flag(&mut self, val: bool) { - self.volume_btn_flag = val; - } - pub fn set_light_ctrl_btn_flag(&mut self, val: bool) { - self.light_ctrl_btn_flag = val; - } -} +// impl InputFlags { +// pub fn set_sense_coin_flag(&mut self, val: bool) { +// self.sense_coin_flag = val; +// } +// pub fn set_main_btn_flag(&mut self, val: bool) { +// self.main_btn_flag = val; +// } +// pub fn set_volume_btn_flag(&mut self, val: bool) { +// self.volume_btn_flag = val; +// } +// pub fn set_light_ctrl_btn_flag(&mut self, val: bool) { +// self.light_ctrl_btn_flag = val; +// } +// } static mut INPUT_FLAGS: InputFlags = InputFlags { sense_coin_flag: false, @@ -211,7 +211,7 @@ fn debug_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { delay.delay_ms(1000); - unsafe { system::enter_standby(4) }; + unsafe { system::enter_standby() }; delay.delay_ms(1000); riscv::asm::wfi(); @@ -332,6 +332,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 led0_index = 0; let led0_dcs = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8]; @@ -403,7 +405,30 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { #[cfg(feature = "enable_print")] println!("begin"); + + let mut volume_btn_prev = volume_btn_input.is_high_immediate(); + let mut light_ctrl_btn_prev = light_ctrl_btn_input.is_high_immediate(); loop { + // edge detector + if !volume_btn_input.active() { + let volume_btn_curr = volume_btn_input.is_high_immediate(); + if volume_btn_prev != volume_btn_curr { + volume_btn_prev = volume_btn_curr; + unsafe { + INPUT_FLAGS.volume_btn_flag = true; + } + } + } + + if !light_ctrl_btn_input.active() { + let light_ctrl_btn_curr = light_ctrl_btn_input.is_high_immediate(); + if light_ctrl_btn_prev != light_ctrl_btn_curr { + light_ctrl_btn_prev = light_ctrl_btn_curr; + unsafe { + INPUT_FLAGS.light_ctrl_btn_flag = true; + } + } + } { // system input servicing unsafe { @@ -426,16 +451,16 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { INPUT_FLAGS.main_btn_flag = false; main_btn_input.begin(); } - if !volume_btn_input.active() { - if !volume_btn_input.is_high_immediate() && !volume_btn_input.active() { - #[cfg(feature = "enable_print")] - println!("volume btn triggered: {}", volume_btn_input.active()); - volume_btn_input.begin(); - } + if INPUT_FLAGS.volume_btn_flag { + #[cfg(feature = "enable_print")] + println!("volume btn triggered"); + INPUT_FLAGS.volume_btn_flag = false; + volume_btn_input.begin(); } - if !light_ctrl_btn_input.is_high_immediate() && !light_ctrl_btn_input.active() { + if INPUT_FLAGS.light_ctrl_btn_flag { #[cfg(feature = "enable_print")] println!("light ctrl btn triggered!"); + INPUT_FLAGS.light_ctrl_btn_flag = false; light_ctrl_btn_input.begin(); } } @@ -458,8 +483,14 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { println!("debounced button_input value: {}", value); if !value { - interfaces.dac.load_data(button_sounds[0]); + interfaces + .dac + .load_data(button_sounds[settings.button_sound_index]); + settings.button_sound_index += 1; + if settings.button_sound_index > button_sounds.len() - 1 { + settings.button_sound_index = 0; + } #[cfg(feature = "enable_print")] println!("reset hold timers + enable"); sp_timer.reset(); @@ -474,12 +505,14 @@ 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(); volume_btn_input.reset(); } if light_ctrl_btn_input.ready() { #[cfg(feature = "enable_print")] - println!("light_ctrl_btn value: {}", sense_coin_input.value()); - sense_coin_input.reset(); + println!("light_ctrl_btn value: {}", light_ctrl_btn_input.value()); + settings.brightness.next(); + light_ctrl_btn_input.reset(); } // timers @@ -527,7 +560,7 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! { match system_state { SystemState::DeepSleep => { // TODO: make this REALLY deep sleep - unsafe { system::enter_standby(4) }; + unsafe { system::enter_standby() }; loop { riscv::asm::wfi(); let mut config = hal::Config::default(); diff --git a/ch32v-insert-coin/src/system.rs b/ch32v-insert-coin/src/system.rs index 5266894..97b43e4 100644 --- a/ch32v-insert-coin/src/system.rs +++ b/ch32v-insert-coin/src/system.rs @@ -105,7 +105,7 @@ pub fn clear_interrupt(coin_pin: u8, button_pin: u8) -> crate::InputFlags { /// * (probably WFI?) /// exit: /// 1. any interrupt/event (set in external interrupt register) -pub unsafe fn enter_standby(pin: usize) { +pub unsafe fn enter_standby() { critical_section::with(|_| { use hal::pac::Interrupt; use qingke_rt::CoreInterrupt; @@ -124,10 +124,7 @@ pub unsafe fn enter_standby(pin: usize) { // set PDDS=1: // get current value of PWR_CTLR let mut reg: u32 = 0x4000_7000; - let mut val: u32 = 0; - unsafe { - val = (reg as *mut u32).read_volatile(); - } + let mut val: u32 = unsafe { (reg as *mut u32).read_volatile() }; // modify PDDS val |= 1 << 1; // PWR_CTLR[1] -> PDDS unsafe {