add basic volume control

This commit is contained in:
sigil-03 2025-11-05 10:06:41 -07:00
parent d3ccc70782
commit 95b55f88a8
2 changed files with 64 additions and 20 deletions

View file

@ -11,7 +11,7 @@ pub enum State {
mod settings {
#[derive(Default, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy)]
pub enum Level {
Off,
Low,
@ -33,6 +33,19 @@ mod settings {
}
}
// volume control
impl Level {
pub fn as_volume_divisor(&self) -> u8 {
match self {
Self::Off => u8::MAX,
Self::Low => 4,
Self::Medium => 3,
Self::High => 2,
Self::Maximum => 1,
}
}
}
#[derive(Clone, Copy)]
pub struct Settings {
pub brightness: Level,
@ -285,8 +298,8 @@ impl App {
self.services
.led0
.set_amplitude(self.sequences.led0.get_value());
#[cfg(feature = "enable_print")]
println!("led0 sevice {}", self.sequences.led0.get_value());
// #[cfg(feature = "enable_print")]
// println!("led0 sevice {}", self.sequences.led0.get_value());
}
if self.timers.led1_timer.need_service() {
self.timers.led1_timer.service();
@ -294,8 +307,8 @@ impl App {
self.services
.led1
.set_amplitude(self.sequences.led1.get_value());
#[cfg(feature = "enable_print")]
println!("led1 service");
// #[cfg(feature = "enable_print")]
// println!("led1 service");
}
if self.timers.led2_timer.need_service() {
self.timers.led2_timer.service();
@ -303,8 +316,8 @@ impl App {
self.services
.led2
.set_amplitude(self.sequences.led2.get_value());
#[cfg(feature = "enable_print")]
println!("led2 service");
// #[cfg(feature = "enable_print")]
// println!("led2 service");
}
// services
@ -328,7 +341,7 @@ impl App {
}
if self.services.synth0.need_service() {
let out = self.services.synth0.service();
let out = self.services.synth0.service() / self.settings.volume.as_volume_divisor();
self.interfaces
.pwm_core
.write_amplitude(ch32_hal::timer::Channel::Ch4, out);
@ -336,11 +349,31 @@ impl App {
}
}
// interfaces to the app (for buttons, etc.)
impl App {
pub fn volume_button(&mut self) {
self.settings.volume.next();
#[cfg(feature = "enable_print")]
println!("new volume: {:?}", self.settings.volume);
}
pub fn brightness_button(&mut self) {
#[cfg(feature = "enable_print")]
println!("brightness button app handler");
// TODO
}
pub fn main_button(&mut self) {
// TODO
}
pub fn coin_detect(&mut self) {
// TODO
}
}
// TODO LIST
//
// AUDIO:
// 1. volume control
// 2. dac switching
// 3. amp_en control
//
// LED:
// 1. brightness control
@ -355,3 +388,6 @@ impl App {
// 1. deep sleep
// 2. battery voltage monitoring
// 3. battery voltage cutoff
//
// STRETCH TODO LIST
// 1. clean up edge detector

View file

@ -329,6 +329,25 @@ fn app_main(mut p: hal::Peripherals) -> ! {
app.init();
loop {
// system servicing
//
// 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_input.begin();
volume_btn_prev = volume_btn_curr;
}
}
volume_btn_input.service();
if volume_btn_input.ready() {
#[cfg(feature = "enable_print")]
println!("volume btn value: {}", volume_btn_input.value());
if !volume_btn_input.value() {
app.volume_button();
}
volume_btn_input.reset();
}
// systick tick
if unsafe {
@ -347,17 +366,6 @@ fn app_main(mut p: hal::Peripherals) -> ! {
// interfaces.service();
}
// // 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 {