From 6f67bc11de77956473ce4d5c99ef6d9b1d56af65 Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Fri, 21 Nov 2025 12:00:42 -0700 Subject: [PATCH 1/2] add ability to set max volume pct --- ch32v-insert-coin/src/app.rs | 14 ++++++++++++-- ch32v-insert-coin/src/main.rs | 3 ++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ch32v-insert-coin/src/app.rs b/ch32v-insert-coin/src/app.rs index a8fdbb8..fcc37cd 100644 --- a/ch32v-insert-coin/src/app.rs +++ b/ch32v-insert-coin/src/app.rs @@ -186,6 +186,7 @@ pub use settings::Settings; // #[cfg(feature = "enable_print")] use ch32_hal::println; +#[derive(Clone)] pub struct TimerConfig { pub sp_timer_ms: usize, pub lp_timer_ms: usize, @@ -297,9 +298,11 @@ impl Services { } } +#[derive(Clone)] pub struct Config { pub system_tick_rate_hz: usize, pub timers: TimerConfig, + pub max_volume_pct: u8, } pub struct Sequences { @@ -324,6 +327,7 @@ pub struct App { services: Services, sequences: Sequences, interfaces: Interfaces, + config: Config, } use settings::Level; @@ -335,13 +339,15 @@ impl App { interfaces: Interfaces, settings: Settings, ) -> Self { + let tmr_cfg = config.clone(); Self { state: State::default(), settings, - timers: Timers::new(config.timers, config.system_tick_rate_hz), + timers: Timers::new(tmr_cfg.timers, tmr_cfg.system_tick_rate_hz), services, sequences, interfaces, + config, } } @@ -527,10 +533,14 @@ impl App { } if self.services.synth0.need_service() { - let out = match self.services.synth0.service() { + let mut out = match self.services.synth0.service() { Some(value) => value / 6 / self.settings.volume.as_volume_divisor(), None => 0, }; + + if out > self.config.max_volume_pct { + out = self.config.max_volume_pct; + } self.interfaces .pwm_core .write_amplitude(ch32_hal::timer::Channel::Ch4, out); diff --git a/ch32v-insert-coin/src/main.rs b/ch32v-insert-coin/src/main.rs index 694ce75..386a809 100644 --- a/ch32v-insert-coin/src/main.rs +++ b/ch32v-insert-coin/src/main.rs @@ -42,7 +42,7 @@ use qingke::riscv; use crate::app::sequencer::{DynamicSequence, SequenceEntry}; -static LED0_SEQ: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8]; +static LED0_SEQ: [u8; 8] = [5u8, 20u8, 45u8, 60u8, 75u8, 60u8, 45u8, 20u8]; pub struct Usb { usb_pin: Input<'static>, @@ -371,6 +371,7 @@ fn app_main(mut p: hal::Peripherals, app_settings: Settings) -> Settings { let app_config = Config { system_tick_rate_hz: tick_rate_hz, timers: timer_config, + max_volume_pct: 10, }; // DAC servicer setup From b35b1cd883e24b1b4336e470a9b196936763e41e Mon Sep 17 00:00:00 2001 From: sigil-03 Date: Fri, 21 Nov 2025 14:01:04 -0700 Subject: [PATCH 2/2] update LED "off" state to be 2% PWM --- ch32v-insert-coin/src/app.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ch32v-insert-coin/src/app.rs b/ch32v-insert-coin/src/app.rs index fcc37cd..b4e483f 100644 --- a/ch32v-insert-coin/src/app.rs +++ b/ch32v-insert-coin/src/app.rs @@ -436,13 +436,13 @@ impl App { } if self.timers.led0_timer.need_service() { let out = match self.settings.brightness { - Level::Off => 0, + Level::Off => 2, Level::Low => 5, Level::Medium => 25, Level::High => 75, Level::Maximum => { self.sequences.led0.next(); - self.sequences.led0.get_value() / 6 + core::cmp::max(self.sequences.led0.get_value() / 6, 2) } }; @@ -453,13 +453,13 @@ impl App { } if self.timers.led1_timer.need_service() { let out = match self.settings.brightness { - Level::Off => 0, + Level::Off => 2, Level::Low => 5, Level::Medium => 25, Level::High => 75, Level::Maximum => { self.sequences.led1.next(); - self.sequences.led1.get_value() / 6 + core::cmp::max(self.sequences.led1.get_value() / 6, 2) } }; self.timers.led1_timer.service();