move to modular servicer design
This commit is contained in:
parent
77188a3eaa
commit
51af5c3343
9 changed files with 371 additions and 154 deletions
42
ch32v-insert-coin/src/insert_coin/services/led.rs
Normal file
42
ch32v-insert-coin/src/insert_coin/services/led.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use ch32_hal::timer::Channel;
|
||||
|
||||
use crate::insert_coin::services::{ServiceData, Service};
|
||||
|
||||
pub struct LedService {
|
||||
service_data: core::cell::RefCell<ServiceData>,
|
||||
pub channel: Channel,
|
||||
pub amplitude: u8,
|
||||
}
|
||||
|
||||
impl LedService {
|
||||
pub fn new(channel: Channel, service_data: ServiceData) -> Self {
|
||||
Self {
|
||||
service_data: core::cell::RefCell::new(service_data),
|
||||
channel,
|
||||
amplitude: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_amplitude(&mut self, amplitude: u8) {
|
||||
self.amplitude = amplitude;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Service for LedService {
|
||||
fn tick(&self) {
|
||||
let mut tc = self.service_data.borrow_mut();
|
||||
tc.ticks_remaining = tc.ticks_remaining.saturating_sub(1);
|
||||
}
|
||||
|
||||
fn need_service(&self) -> bool {
|
||||
self.service_data.borrow().ticks_remaining == 0
|
||||
}
|
||||
|
||||
fn service(&self) {
|
||||
let mut tc = self.service_data.borrow_mut();
|
||||
tc.ticks_remaining = tc.ticks_per_service;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue