From 7d93cd8977a5ceb9a7aee82406d076e00b5e121c Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Wed, 5 Nov 2025 14:52:39 -0700 Subject: [PATCH] add initial long press deep sleep handling --- ch32v-insert-coin/src/app.rs | 10 +- ch32v-insert-coin/src/main.rs | 240 +++++----------------------------- 2 files changed, 44 insertions(+), 206 deletions(-) diff --git a/ch32v-insert-coin/src/app.rs b/ch32v-insert-coin/src/app.rs index a5d573b..9a4e69d 100644 --- a/ch32v-insert-coin/src/app.rs +++ b/ch32v-insert-coin/src/app.rs @@ -431,10 +431,18 @@ impl App { #[cfg(feature = "enable_print")] println!("short press"); } - fn main_button_long_press(&self) { + fn main_button_long_press(&mut self) { // TODO #[cfg(feature = "enable_print")] println!("long press"); + self.set_state(State::DeepSleep); + } +} + +// Getters +impl App { + pub fn get_state(&self) -> State { + self.state } } diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 34d5222..e993c35 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -160,10 +160,6 @@ fn app_main(mut p: hal::Peripherals) -> ! { // LED0 output setup let led0_pin = PwmPin::new_ch3::<0>(p.PC3); let led0_ch = hal::timer::Channel::Ch3; - // let mut led0_pin = Output::new(p.PC3, Level::High, Default::default()); - // unsafe { - // LED = Some(led0_pin); - // } // LED1 output setup let led1_pin = PwmPin::new_ch1::<0>(p.PD2); @@ -191,19 +187,12 @@ fn app_main(mut p: hal::Peripherals) -> ! { pwm.set_polarity(led0_ch, OutputPolarity::ActiveLow); pwm.set_polarity(led1_ch, OutputPolarity::ActiveLow); - // let sample_rate_hz = 4000; - // let sample_rate_hz = 16000; - // let dac_tick_per_service = 5; - // let tick_rate_hz = sample_rate_hz * dac_tick_per_service; let tick_rate_hz = 50000; let core_config = CoreConfig::new(tick_rate_hz); let pwm_core = SimplePwmCore::new(pwm); - // === synthesizer setup === - // let synthesizer = AppSynthesizers::new(core_config.tick_rate_hz, pwm_core); - // === input setup === // adc @@ -232,15 +221,6 @@ fn app_main(mut p: hal::Peripherals) -> ! { // set up interrupts unsafe { system::init_gpio_irq(sense_coin_pin.pin(), sense_coin_pin.port(), false, true) }; unsafe { system::init_gpio_irq(main_btn_pin.pin(), main_btn_pin.port(), true, true) }; - // unsafe { system::init_gpio_irq(volume_btn_pin.pin(), volume_btn_pin.port(), true, true) }; - // unsafe { - // system::init_gpio_irq( - // light_ctrl_btn_pin.pin(), - // light_ctrl_btn_pin.port(), - // true, - // true, - // ) - // }; // coin debouncer (100ms) let mut sense_coin_input = @@ -270,9 +250,6 @@ 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); - // DAC servicer setup let dac_sample_rate_hz = 16000; let dac_tick_per_service = tick_rate_hz / dac_sample_rate_hz; @@ -301,19 +278,6 @@ fn app_main(mut p: hal::Peripherals) -> ! { let mut app = App::new(app_config, app_services, app_sequences, app_interfaces); - // dac data - // let coin_sound = include_bytes!("../audio/coin2.raw"); - // let coin_sound = include_bytes!("../audio/sweep_dpcm_u4.raw"); - // let coin_sound = include_bytes!("../audio/button_1.raw"); - - // let button_sound_1 = include_bytes!("../audio/button_1.raw"); - // let button_sound_2 = include_bytes!("../audio/button_2.raw"); - // let button_sound_3 = include_bytes!("../audio/button_3.raw"); - - // interfaces.led0.set_amplitude(0); - // interfaces.led1.set_amplitude(0); - // interfaces.service(); - // init systick systick_init(tick_rate_hz); @@ -394,15 +358,10 @@ fn app_main(mut p: hal::Peripherals) -> ! { if INPUT_FLAGS.sense_coin_flag.active() { #[allow(static_mut_refs)] INPUT_FLAGS.sense_coin_flag.clear(); - - #[cfg(feature = "enable_print")] - println!("coin flag active"); sense_coin_input.begin(); } sense_coin_input.service(); if sense_coin_input.ready() { - #[cfg(feature = "enable_print")] - println!("debounced coin_input value: {}", sense_coin_input.value()); sense_coin_input.reset(); app.coin_detect(); } @@ -412,8 +371,6 @@ fn app_main(mut p: hal::Peripherals) -> ! { unsafe { #[allow(static_mut_refs)] if INPUT_FLAGS.main_btn_flag.active() { - #[cfg(feature = "enable_print")] - println!("button flag active"); #[allow(static_mut_refs)] INPUT_FLAGS.main_btn_flag.clear(); main_btn_input.begin(); @@ -446,170 +403,43 @@ fn app_main(mut p: hal::Peripherals) -> ! { } app.service(); - // interfaces.service(); + + match app.get_state() { + // enter standby + app::State::DeepSleep => { + unsafe { system::enter_standby() }; + loop { + riscv::asm::wfi(); + let mut config = hal::Config::default(); + config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI; + unsafe { + hal::rcc::init(config.rcc); + } + unsafe { + #[allow(static_mut_refs)] + if INPUT_FLAGS.sense_coin_flag.active() { + app.set_state(State::Active); + break; + } + }; + } + } + // for everything else, don't do anything + _ => {} + } } - - // 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 { - // if INPUT_FLAGS.sense_coin_flag { - // #[cfg(feature = "enable_print")] - // println!("coin flag active"); - // INPUT_FLAGS.sense_coin_flag = false; - // sense_coin_input.begin(); - // // enter the active state - // app.set_state(State::Active); - - // // todo: enter active - // // tt0.enable(true); - // // tt1.enable(true); - // // interfaces.dac.load_data(coin_sound); - // } - // if INPUT_FLAGS.main_btn_flag { - // #[cfg(feature = "enable_print")] - // println!("button flag active"); - // INPUT_FLAGS.main_btn_flag = false; - // main_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 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(); - // } - // } - - // // debouncer - // sense_coin_input.service(); - // main_btn_input.service(); - // volume_btn_input.service(); - // light_ctrl_btn_input.service(); - - // if sense_coin_input.ready() { - // #[cfg(feature = "enable_print")] - // println!("debounced coin_input value: {}", sense_coin_input.value()); - // sense_coin_input.reset(); - // } - // if main_btn_input.ready() { - // let value = main_btn_input.value(); - // main_btn_input.reset(); - // #[cfg(feature = "enable_print")] - // println!("debounced button_input value: {}", value); - - // if !value { - // // interfaces.dac.load_data(match settings.button_sound_index { - // // 0 => button_sound_1, - // // 1 => button_sound_2, - // // 2 => button_sound_3, - // // _ => button_sound_1, - // // }); - // // interfaces - // // .dac - // // .load_data(button_sounds[settings.button_sound_index]); - - // app.settings.button_sound_index += 1; - // if app.settings.button_sound_index > 2 { - // // if settings.button_sound_index > button_sounds.len() - 1 { - // app.settings.button_sound_index = 0; - // } - // #[cfg(feature = "enable_print")] - // println!("TODO reset hold timers + enable"); - // // sp_timer.reset(); - // // sp_timer.enable(true); - // // lp_timer.reset(); - // // lp_timer.enable(true); - // } else { - // // sp_timer.reset(); - // // lp_timer.reset(); - // } - // } - // if volume_btn_input.ready() { - // #[cfg(feature = "enable_print")] - // println!("volume btn value: {}", volume_btn_input.value()); - // 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()); - // app.settings.brightness.next(); - // light_ctrl_btn_input.reset(); - // } - - // // if sp_timer.need_service() { - // // #[cfg(feature = "enable_print")] - // // println!("sp detect!"); - // // sp_timer.reset(); - - // // // todo enter idle - // // app.set_state(State::Idle); - // // // TODO: fix polarity - // // interfaces.led0.set_amplitude(10); - // // interfaces.led1.set_amplitude(10); - // // interfaces.service(); - // // } - - // // if lp_timer.need_service() { - // // #[cfg(feature = "enable_print")] - // // println!("lp detect!"); - // // lp_timer.reset(); - - // // // todo enter deepsleep - // // app.set_state(State::DeepSleep); - // // // TODO: fix polarity - // // interfaces.led0.set_amplitude(0); - // // interfaces.led1.set_amplitude(0); - // // interfaces.service(); - // // } - - // // if adc1_timer.need_service() { - // // let val = adc.convert(&mut batt_monitor_pin, hal::adc::SampleTime::CYCLES241); - // // let val = adc.convert(&mut usb_detect_pin, hal::adc::SampleTime::CYCLES241); - // // #[cfg(feature = "enable_print")] - // // println!("ADC value: {}", val); - - // // adc1_timer.reset(); - // // adc1_timer.enable(true); - // // } - // } - - // match app.state() { - // State::DeepSleep => { - // // TODO: make this REALLY deep sleep - // unsafe { system::enter_standby() }; - // loop { - // riscv::asm::wfi(); - // let mut config = hal::Config::default(); - // config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI; - // unsafe { - // hal::rcc::init(config.rcc); - // } - // unsafe { - // if INPUT_FLAGS.sense_coin_flag { - // app.set_state(State::Active); - // break; - // } - // }; - // } - // } - // State::Idle => {} - // State::Active => { } +// // if adc1_timer.need_service() { +// // let val = adc.convert(&mut batt_monitor_pin, hal::adc::SampleTime::CYCLES241); +// // let val = adc.convert(&mut usb_detect_pin, hal::adc::SampleTime::CYCLES241); +// // #[cfg(feature = "enable_print")] +// // println!("ADC value: {}", val); + +// // adc1_timer.reset(); +// // adc1_timer.enable(true); +// // } +// } + #[qingke_rt::entry] fn main() -> ! { #[cfg(feature = "enable_print")]