move debounced gpio to module file

This commit is contained in:
sigil-03 2025-11-04 12:14:13 -07:00
parent f17811cdce
commit b786bf174a
2 changed files with 69 additions and 80 deletions

View file

@ -0,0 +1,66 @@
use crate::insert_coin::{TickService, TickServiceData, TickTimerService};
use ch32_hal::gpio::{AnyPin, Input, Pull};
pub struct DebouncedGPIO<'a> {
input: Input<'a>,
// value of the GPIO
value: bool,
// GPIO is ready (debounced)
ready: bool,
// debouncer is active
active: bool,
// debounce timer
timer: TickTimerService,
}
impl<'a> DebouncedGPIO<'a> {
pub fn new(pin: AnyPin, system_tick_rate_hz: usize, debounce_time_ms: usize) -> Self {
// coin debounce timer (100ms)
Self {
input: Input::new(pin, Pull::Up),
value: false,
ready: false,
active: false,
timer: TickTimerService::new(
TickServiceData::new(system_tick_rate_hz * debounce_time_ms / 1000),
false,
),
}
}
pub fn ready(&self) -> bool {
self.ready
}
pub fn active(&self) -> bool {
self.active
}
pub fn value(&self) -> bool {
self.value
}
pub fn begin(&mut self) {
self.reset();
self.timer.enable(true);
self.active = true;
}
pub fn reset(&mut self) {
self.timer.reset();
self.ready = false;
self.active = false;
}
pub fn service(&mut self) {
self.timer.tick();
if self.timer.need_service() {
self.timer.reset();
self.value = self.input.is_high();
self.ready = true;
}
}
pub fn is_high_immediate(&self) -> bool {
self.input.is_high()
}
}

View file

@ -9,6 +9,9 @@ mod insert_coin;
// system stuff
mod system;
mod debounced_gpio;
use debounced_gpio::DebouncedGPIO;
// synthesizer :3
mod synthesizer;
use synthesizer::AppSynthesizers;
@ -35,70 +38,6 @@ use qingke::riscv;
static LED0_SEQ: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
struct DebouncedGPIO<'a> {
input: Input<'a>,
// value of the GPIO
value: bool,
// GPIO is ready (debounced)
ready: bool,
// debouncer is active
active: bool,
// debounce timer
timer: TickTimerService,
}
impl<'a> DebouncedGPIO<'a> {
pub fn new(pin: AnyPin, system_tick_rate_hz: usize, debounce_time_ms: usize) -> Self {
// coin debounce timer (100ms)
Self {
input: Input::new(pin, Pull::Up),
value: false,
ready: false,
active: false,
timer: TickTimerService::new(
TickServiceData::new(system_tick_rate_hz * debounce_time_ms / 1000),
false,
),
}
}
pub fn ready(&self) -> bool {
self.ready
}
pub fn active(&self) -> bool {
self.active
}
pub fn value(&self) -> bool {
self.value
}
pub fn begin(&mut self) {
self.reset();
self.timer.enable(true);
self.active = true;
}
pub fn reset(&mut self) {
self.timer.reset();
self.ready = false;
self.active = false;
}
pub fn service(&mut self) {
self.timer.tick();
if self.timer.need_service() {
self.timer.reset();
self.value = self.input.is_high();
self.ready = true;
}
}
pub fn is_high_immediate(&self) -> bool {
self.input.is_high()
}
}
#[derive(Debug)]
struct Flag {
value: bool,
@ -308,22 +247,6 @@ fn app_main(mut p: hal::Peripherals) -> ! {
let mut light_ctrl_btn_input =
DebouncedGPIO::new(light_ctrl_btn_pin.degrade(), core_config.tick_rate_hz, 100);
// let mut interfaces = InsertCoin::new(core_config, pwm_core);
// interfaces.set_active(true);
let mut led0_index = 0;
let led0_dcs = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
let mut led1_index = 0;
let led1_dcs = [0u8, 25u8, 50u8, 75u8, 100u8];
// // battery read timer
// let adc1_ticks = 5 * interfaces.config.tick_rate_hz;
// let adc1_timer_data = TickServiceData::new(adc1_ticks);
// let mut adc1_timer = TickTimerService::new(adc1_timer_data, false);
// adc1_timer.reset();
// adc1_timer.enable(true);
let timer_config = TimerConfig {
sp_timer_ms: 2000,
lp_timer_ms: 5000,