From f26920a0994c811d18f4fb47d663e9af19b2d735 Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Sat, 15 Nov 2025 16:03:32 -0700 Subject: [PATCH] add persistent settings --- ch32v-insert-coin/src/app.rs | 8 ++++++-- ch32v-insert-coin/src/main.rs | 19 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ch32v-insert-coin/src/app.rs b/ch32v-insert-coin/src/app.rs index 1d5d0f3..775a908 100644 --- a/ch32v-insert-coin/src/app.rs +++ b/ch32v-insert-coin/src/app.rs @@ -9,7 +9,7 @@ pub enum State { Active, } -mod settings { +pub mod settings { #[derive(Debug, Default, Clone, Copy)] pub enum Level { @@ -333,10 +333,11 @@ impl App { services: Services, sequences: Sequences, interfaces: Interfaces, + settings: Settings, ) -> Self { Self { state: State::default(), - settings: Settings::default(), + settings, timers: Timers::new(config.timers, config.system_tick_rate_hz), services, sequences, @@ -659,6 +660,9 @@ impl App { pub fn get_state(&self) -> State { self.state } + pub fn get_settings(&self) -> Settings { + self.settings + } pub fn should_wake(&self) -> bool { if self.interfaces.usb.powered() { return true; diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index ca5a9dd..c92f562 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -245,10 +245,11 @@ bind_interrupts!(struct Irqs { }); // TODO: remove +use app::settings::Settings; use insert_coin::TickTimerService; use insert_coin::{TickService, TickServiceData}; -fn app_main(mut p: hal::Peripherals) { +fn app_main(mut p: hal::Peripherals, app_settings: Settings) -> Settings { // initialize ADC core first, and exit if battery is too low let mut adc = hal::adc::Adc::new(p.ADC1, Default::default()); let mut batt_monitor_pin = p.PD4; @@ -256,7 +257,7 @@ fn app_main(mut p: hal::Peripherals) { let bv = adc_core.get_battery_voltage(); if bv < 421 { adc_core.shutdown(); - return; + return app_settings; } // === output setup === @@ -397,7 +398,13 @@ fn app_main(mut p: hal::Peripherals) { usb, }; - let mut app = App::new(app_config, app_services, app_sequences, app_interfaces); + let mut app = App::new( + app_config, + app_services, + app_sequences, + app_interfaces, + app_settings, + ); let need_sound = unsafe { #[allow(static_mut_refs)] @@ -542,7 +549,7 @@ fn app_main(mut p: hal::Peripherals) { // enter standby app::State::DeepSleep => { app.shut_down(); - return; + return app.get_settings(); } _ => {} } @@ -642,12 +649,14 @@ fn main() -> ! { // println!("post"); // debug_main(p); + let mut app_settings = Settings::default(); + loop { unsafe { hal::rcc::init(hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI); } let mut p = unsafe { hal::Peripherals::steal() }; - app_main(p); + app_settings = app_main(p, app_settings); unsafe { hal::rcc::init(hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSI);