add edge detecting inputs for volume and brightness settings

This commit is contained in:
sigil-03 2025-10-26 17:26:34 -06:00
parent 144d24cf59
commit 9f12502a3d
2 changed files with 62 additions and 32 deletions

View file

@ -47,7 +47,7 @@ impl Level {
struct Settings { struct Settings {
brightness: Level, brightness: Level,
volume: Level, volume: Level,
button_sound_index: u8, button_sound_index: usize,
} }
impl Default for Settings { impl Default for Settings {
@ -156,20 +156,20 @@ impl Default for InputFlags {
} }
} }
impl InputFlags { // impl InputFlags {
pub fn set_sense_coin_flag(&mut self, val: bool) { // pub fn set_sense_coin_flag(&mut self, val: bool) {
self.sense_coin_flag = val; // self.sense_coin_flag = val;
} // }
pub fn set_main_btn_flag(&mut self, val: bool) { // pub fn set_main_btn_flag(&mut self, val: bool) {
self.main_btn_flag = val; // self.main_btn_flag = val;
} // }
pub fn set_volume_btn_flag(&mut self, val: bool) { // pub fn set_volume_btn_flag(&mut self, val: bool) {
self.volume_btn_flag = val; // self.volume_btn_flag = val;
} // }
pub fn set_light_ctrl_btn_flag(&mut self, val: bool) { // pub fn set_light_ctrl_btn_flag(&mut self, val: bool) {
self.light_ctrl_btn_flag = val; // self.light_ctrl_btn_flag = val;
} // }
} // }
static mut INPUT_FLAGS: InputFlags = InputFlags { static mut INPUT_FLAGS: InputFlags = InputFlags {
sense_coin_flag: false, sense_coin_flag: false,
@ -211,7 +211,7 @@ fn debug_main(mut p: hal::Peripherals, mut delay: Delay) -> ! {
delay.delay_ms(1000); delay.delay_ms(1000);
unsafe { system::enter_standby(4) }; unsafe { system::enter_standby() };
delay.delay_ms(1000); delay.delay_ms(1000);
riscv::asm::wfi(); riscv::asm::wfi();
@ -332,6 +332,8 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! {
interfaces.set_active(true); interfaces.set_active(true);
// insert_coin.init(); // insert_coin.init();
let mut settings = Settings::default();
let mut led0_index = 0; let mut led0_index = 0;
let led0_dcs = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8]; 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")] #[cfg(feature = "enable_print")]
println!("begin"); 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 { 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 // system input servicing
unsafe { unsafe {
@ -426,16 +451,16 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! {
INPUT_FLAGS.main_btn_flag = false; INPUT_FLAGS.main_btn_flag = false;
main_btn_input.begin(); main_btn_input.begin();
} }
if !volume_btn_input.active() { if INPUT_FLAGS.volume_btn_flag {
if !volume_btn_input.is_high_immediate() && !volume_btn_input.active() { #[cfg(feature = "enable_print")]
#[cfg(feature = "enable_print")] println!("volume btn triggered");
println!("volume btn triggered: {}", volume_btn_input.active()); INPUT_FLAGS.volume_btn_flag = false;
volume_btn_input.begin(); 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")] #[cfg(feature = "enable_print")]
println!("light ctrl btn triggered!"); println!("light ctrl btn triggered!");
INPUT_FLAGS.light_ctrl_btn_flag = false;
light_ctrl_btn_input.begin(); 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); println!("debounced button_input value: {}", value);
if !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")] #[cfg(feature = "enable_print")]
println!("reset hold timers + enable"); println!("reset hold timers + enable");
sp_timer.reset(); sp_timer.reset();
@ -474,12 +505,14 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! {
if volume_btn_input.ready() { if volume_btn_input.ready() {
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
println!("volume btn value: {}", volume_btn_input.value()); println!("volume btn value: {}", volume_btn_input.value());
settings.volume.next();
volume_btn_input.reset(); volume_btn_input.reset();
} }
if light_ctrl_btn_input.ready() { if light_ctrl_btn_input.ready() {
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
println!("light_ctrl_btn value: {}", sense_coin_input.value()); println!("light_ctrl_btn value: {}", light_ctrl_btn_input.value());
sense_coin_input.reset(); settings.brightness.next();
light_ctrl_btn_input.reset();
} }
// timers // timers
@ -527,7 +560,7 @@ fn app_main(mut p: hal::Peripherals, mut delay: Delay) -> ! {
match system_state { match system_state {
SystemState::DeepSleep => { SystemState::DeepSleep => {
// TODO: make this REALLY deep sleep // TODO: make this REALLY deep sleep
unsafe { system::enter_standby(4) }; unsafe { system::enter_standby() };
loop { loop {
riscv::asm::wfi(); riscv::asm::wfi();
let mut config = hal::Config::default(); let mut config = hal::Config::default();

View file

@ -105,7 +105,7 @@ pub fn clear_interrupt(coin_pin: u8, button_pin: u8) -> crate::InputFlags {
/// * (probably WFI?) /// * (probably WFI?)
/// exit: /// exit:
/// 1. any interrupt/event (set in external interrupt register) /// 1. any interrupt/event (set in external interrupt register)
pub unsafe fn enter_standby(pin: usize) { pub unsafe fn enter_standby() {
critical_section::with(|_| { critical_section::with(|_| {
use hal::pac::Interrupt; use hal::pac::Interrupt;
use qingke_rt::CoreInterrupt; use qingke_rt::CoreInterrupt;
@ -124,10 +124,7 @@ pub unsafe fn enter_standby(pin: usize) {
// set PDDS=1: // set PDDS=1:
// get current value of PWR_CTLR // get current value of PWR_CTLR
let mut reg: u32 = 0x4000_7000; let mut reg: u32 = 0x4000_7000;
let mut val: u32 = 0; let mut val: u32 = unsafe { (reg as *mut u32).read_volatile() };
unsafe {
val = (reg as *mut u32).read_volatile();
}
// modify PDDS // modify PDDS
val |= 1 << 1; // PWR_CTLR[1] -> PDDS val |= 1 << 1; // PWR_CTLR[1] -> PDDS
unsafe { unsafe {