move led services inside app

This commit is contained in:
sigil-03 2025-11-02 13:41:46 -07:00
parent 791d5db4c8
commit e1943accd2
4 changed files with 117 additions and 51 deletions

View file

@ -51,7 +51,30 @@ mod settings {
} }
} }
use crate::insert_coin::{TickService, TickServiceData, TickTimerService}; pub mod sequencer {
pub struct BasicSequence<'a> {
sequence: &'a [u8],
index: usize,
}
impl<'a> BasicSequence<'a> {
pub fn new(sequence: &'a [u8]) -> Self {
Self { sequence, index: 0 }
}
pub fn next(&mut self) {
self.index += 1;
if self.index > self.sequence.len() - 1 {
self.index = 0;
}
}
pub fn get_value(&self) -> u8 {
self.sequence[self.index]
}
}
}
use crate::insert_coin::{
LedService, Service, SimplePwmCore, TickService, TickServiceData, TickTimerService,
};
pub use settings::Settings; pub use settings::Settings;
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
@ -134,25 +157,52 @@ impl Timers {
} }
} }
pub struct Services {
pub led0: LedService,
pub led1: LedService,
pub led2: LedService,
}
pub struct Config { pub struct Config {
pub system_tick_rate_hz: usize, pub system_tick_rate_hz: usize,
pub timers: TimerConfig, pub timers: TimerConfig,
} }
pub struct Sequences {
pub led0: sequencer::BasicSequence<'static>,
pub led1: sequencer::BasicSequence<'static>,
pub led2: sequencer::BasicSequence<'static>,
}
pub struct Interfaces {
pub pwm_core: SimplePwmCore<'static, ch32_hal::peripherals::TIM1>,
}
pub struct App { pub struct App {
state: State, state: State,
pub settings: Settings, pub settings: Settings,
timers: Timers, timers: Timers,
services: Services,
sequences: Sequences,
interfaces: Interfaces,
// TODO: make this the "sound module" or whatever. // TODO: make this the "sound module" or whatever.
// synthesizers: AppSynthesizers, // synthesizers: AppSynthesizers,
} }
impl App { impl App {
pub fn new(config: Config) -> Self { pub fn new(
config: Config,
services: Services,
sequences: Sequences,
interfaces: Interfaces,
) -> Self {
Self { Self {
state: State::default(), state: State::default(),
settings: Settings::default(), settings: Settings::default(),
timers: Timers::new(config.timers, config.system_tick_rate_hz), timers: Timers::new(config.timers, config.system_tick_rate_hz),
services,
sequences,
interfaces,
} }
} }
@ -191,6 +241,7 @@ impl App {
} }
pub fn service(&mut self) { pub fn service(&mut self) {
// timers
if self.timers.sp_timer.need_service() { if self.timers.sp_timer.need_service() {
self.timers.batt_adc_timer.service(); self.timers.batt_adc_timer.service();
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
@ -213,18 +264,50 @@ impl App {
} }
if self.timers.led0_timer.need_service() { if self.timers.led0_timer.need_service() {
self.timers.led0_timer.service(); self.timers.led0_timer.service();
self.sequences.led0.next();
self.services
.led0
.set_amplitude(self.sequences.led0.get_value());
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
println!("led0 service"); println!("led0 sevice {}", self.sequences.led0.get_value());
} }
if self.timers.led1_timer.need_service() { if self.timers.led1_timer.need_service() {
self.timers.led1_timer.service(); self.timers.led1_timer.service();
self.sequences.led1.next();
self.services
.led1
.set_amplitude(self.sequences.led1.get_value());
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
println!("led1 service"); println!("led1 service");
} }
if self.timers.led2_timer.need_service() { if self.timers.led2_timer.need_service() {
self.timers.led2_timer.service(); self.timers.led2_timer.service();
self.sequences.led2.next();
self.services
.led2
.set_amplitude(self.sequences.led2.get_value());
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]
println!("led2 service"); println!("led2 service");
} }
// services
if self.services.led0.need_service() {
self.interfaces
.pwm_core
.write_amplitude(self.services.led0.channel, self.services.led0.amplitude);
self.services.led0.service();
}
if self.services.led1.need_service() {
self.interfaces
.pwm_core
.write_amplitude(self.services.led1.channel, self.services.led1.amplitude);
self.services.led1.service();
}
if self.services.led2.need_service() {
self.interfaces
.pwm_core
.write_amplitude(self.services.led2.channel, self.services.led2.amplitude);
self.services.led2.service();
}
} }
} }

View file

@ -1,7 +1,7 @@
mod insert_coin; mod insert_coin;
mod services; mod services;
pub use services::TickTimerService; pub use services::{LedService, Service, TickTimerService};
pub use insert_coin::{CoreConfig, InsertCoin, SimplePwmCore};
pub use services::{TickService, TickServiceData}; pub use services::{TickService, TickServiceData};
pub use insert_coin::{InsertCoin, CoreConfig, SimplePwmCore};

View file

@ -1,6 +1,6 @@
use ch32_hal::timer::Channel; use ch32_hal::timer::Channel;
use crate::insert_coin::services::{Service}; use crate::insert_coin::services::Service;
pub struct LedService { pub struct LedService {
// need_service: core::cell::RefCell<bool>, // need_service: core::cell::RefCell<bool>,
@ -25,9 +25,7 @@ impl LedService {
} }
} }
impl Service for LedService { impl Service for LedService {
fn need_service(&self) -> bool { fn need_service(&self) -> bool {
self.need_service self.need_service
} }
@ -36,5 +34,3 @@ impl Service for LedService {
self.need_service = false; self.need_service = false;
} }
} }

View file

@ -14,10 +14,12 @@ mod synthesizer;
use synthesizer::AppSynthesizers; use synthesizer::AppSynthesizers;
mod app; mod app;
use app::{App, Config, State, TimerConfig}; use app::{
sequencer::BasicSequence, App, Config, Interfaces, Sequences, Services, State, TimerConfig,
};
use ch32_hal::{adc::AdcChannel, interrupt::typelevel::Handler, timer::low_level::OutputPolarity}; use ch32_hal::{adc::AdcChannel, interrupt::typelevel::Handler, timer::low_level::OutputPolarity};
use insert_coin::{CoreConfig, InsertCoin, SimplePwmCore}; use insert_coin::{CoreConfig, InsertCoin, LedService, SimplePwmCore};
use ch32_hal as hal; use ch32_hal as hal;
use hal::bind_interrupts; use hal::bind_interrupts;
@ -31,6 +33,8 @@ use hal::println;
use qingke::riscv; use qingke::riscv;
static LED0_SEQ: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
struct DebouncedGPIO<'a> { struct DebouncedGPIO<'a> {
input: Input<'a>, input: Input<'a>,
// value of the GPIO // value of the GPIO
@ -498,8 +502,8 @@ fn app_main(mut p: hal::Peripherals) -> ! {
let mut light_ctrl_btn_input = let mut light_ctrl_btn_input =
DebouncedGPIO::new(light_ctrl_btn_pin.degrade(), core_config.tick_rate_hz, 100); DebouncedGPIO::new(light_ctrl_btn_pin.degrade(), core_config.tick_rate_hz, 100);
let mut interfaces = InsertCoin::new(core_config, pwm_core); // let mut interfaces = InsertCoin::new(core_config, pwm_core);
interfaces.set_active(true); // interfaces.set_active(true);
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];
@ -514,14 +518,12 @@ fn app_main(mut p: hal::Peripherals) -> ! {
// adc1_timer.reset(); // adc1_timer.reset();
// adc1_timer.enable(true); // adc1_timer.enable(true);
let tick_interval_us = 1000000 / interfaces.config.tick_rate_hz - 10;
let timer_config = TimerConfig { let timer_config = TimerConfig {
sp_timer_ms: 2000, sp_timer_ms: 2000,
lp_timer_ms: 5000, lp_timer_ms: 5000,
batt_adc_timer_ms: 10000, batt_adc_timer_ms: 10000,
usb_adc_timer_ms: 10000, usb_adc_timer_ms: 10000,
led0_timer_ms: 1000, led0_timer_ms: 100,
led1_timer_ms: 300, led1_timer_ms: 300,
led2_timer_ms: 1100, led2_timer_ms: 1100,
}; };
@ -531,7 +533,21 @@ fn app_main(mut p: hal::Peripherals) -> ! {
timers: timer_config, timers: timer_config,
}; };
let mut app = App::new(app_config); let app_services = Services {
led0: LedService::new(led0_ch),
led1: LedService::new(led1_ch),
led2: LedService::new(led2_ch),
};
let app_sequences = Sequences {
led0: BasicSequence::new(&LED0_SEQ),
led1: BasicSequence::new(&LED0_SEQ),
led2: BasicSequence::new(&LED0_SEQ),
};
let app_interfaces = Interfaces { pwm_core };
let mut app = App::new(app_config, app_services, app_sequences, app_interfaces);
// dac data // dac data
// let coin_sound = include_bytes!("../audio/coin2.raw"); // let coin_sound = include_bytes!("../audio/coin2.raw");
@ -542,9 +558,9 @@ fn app_main(mut p: hal::Peripherals) -> ! {
// let button_sound_2 = include_bytes!("../audio/button_2.raw"); // let button_sound_2 = include_bytes!("../audio/button_2.raw");
// let button_sound_3 = include_bytes!("../audio/button_3.raw"); // let button_sound_3 = include_bytes!("../audio/button_3.raw");
interfaces.led0.set_amplitude(0); // interfaces.led0.set_amplitude(0);
interfaces.led1.set_amplitude(0); // interfaces.led1.set_amplitude(0);
interfaces.service(); // interfaces.service();
// init systick // init systick
systick_init(tick_rate_hz); systick_init(tick_rate_hz);
@ -590,17 +606,12 @@ fn app_main(mut p: hal::Peripherals) -> ! {
#[allow(static_mut_refs)] #[allow(static_mut_refs)]
INPUT_FLAGS.systick_flag.clear(); INPUT_FLAGS.systick_flag.clear();
} }
// #[cfg(feature = "enable_print")]
// println!("tick!");
// app tick // app tick
app.tick(); app.tick();
// interfaces
} }
app.service(); app.service();
interfaces.service(); // interfaces.service();
} }
// // edge detector // // edge detector
@ -774,31 +785,7 @@ fn app_main(mut p: hal::Peripherals) -> ! {
// } // }
// State::Idle => {} // State::Idle => {}
// State::Active => { // State::Active => {
// // tt0.tick();
// // tt1.tick();
// // if tt0.need_service() {
// // interfaces.led0.set_amplitude(led0_dcs[led0_index]);
// // led0_index += 1;
// // if led0_index > led0_dcs.len() - 1 {
// // led0_index = 0;
// // }
// // tt0.service();
// // }
// // if tt1.need_service() {
// // interfaces.led1.set_amplitude(led1_dcs[led1_index]);
// // led1_index += 1;
// // if led1_index > led1_dcs.len() - 1 {
// // led1_index = 0;
// // }
// // tt1.service()
// // }
// }
// }
// }
} }
#[qingke_rt::entry] #[qingke_rt::entry]
fn main() -> ! { fn main() -> ! {
#[cfg(feature = "enable_print")] #[cfg(feature = "enable_print")]