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;
#[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 system_tick_rate_hz: usize,
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 {
state: State,
pub settings: Settings,
timers: Timers,
services: Services,
sequences: Sequences,
interfaces: Interfaces,
// TODO: make this the "sound module" or whatever.
// synthesizers: AppSynthesizers,
}
impl App {
pub fn new(config: Config) -> Self {
pub fn new(
config: Config,
services: Services,
sequences: Sequences,
interfaces: Interfaces,
) -> Self {
Self {
state: State::default(),
settings: Settings::default(),
timers: Timers::new(config.timers, config.system_tick_rate_hz),
services,
sequences,
interfaces,
}
}
@ -191,6 +241,7 @@ impl App {
}
pub fn service(&mut self) {
// timers
if self.timers.sp_timer.need_service() {
self.timers.batt_adc_timer.service();
#[cfg(feature = "enable_print")]
@ -213,18 +264,50 @@ impl App {
}
if self.timers.led0_timer.need_service() {
self.timers.led0_timer.service();
self.sequences.led0.next();
self.services
.led0
.set_amplitude(self.sequences.led0.get_value());
#[cfg(feature = "enable_print")]
println!("led0 service");
println!("led0 sevice {}", self.sequences.led0.get_value());
}
if self.timers.led1_timer.need_service() {
self.timers.led1_timer.service();
self.sequences.led1.next();
self.services
.led1
.set_amplitude(self.sequences.led1.get_value());
#[cfg(feature = "enable_print")]
println!("led1 service");
}
if self.timers.led2_timer.need_service() {
self.timers.led2_timer.service();
self.sequences.led2.next();
self.services
.led2
.set_amplitude(self.sequences.led2.get_value());
#[cfg(feature = "enable_print")]
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();
}
}
}