move to modular servicer design
This commit is contained in:
parent
77188a3eaa
commit
51af5c3343
9 changed files with 371 additions and 154 deletions
|
|
@ -3,6 +3,10 @@
|
|||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(impl_trait_in_assoc_type)]
|
||||
|
||||
mod insert_coin;
|
||||
use insert_coin::{InsertCoin, SimplePwmCore, CoreConfig};
|
||||
|
||||
|
||||
use adpcm_pwm_dac::dac::DpcmDac;
|
||||
use {ch32_hal as hal};
|
||||
use hal::peripherals::EXTI4;
|
||||
|
|
@ -14,6 +18,15 @@ use hal::timer::low_level::CountingMode;
|
|||
use hal::timer::simple_pwm::{PwmPin, SimplePwm};
|
||||
use hal::timer::{Channel, GeneralInstance16bit};
|
||||
|
||||
// #[qingke_rt::interrupt]
|
||||
// fn EXTI4(){
|
||||
// unsafe{IRQ1_FLAG = true;};
|
||||
// }
|
||||
// bind_interrupts!(struct Irqs {
|
||||
// EXTI4 => button_press();
|
||||
// });
|
||||
|
||||
|
||||
// const DAC_DATA: [u8; 4] = [0x0, 0x80, 0xFF, 0x80];
|
||||
const DAC_DATA: [u8; 8] = [0, 25, 50, 75, 100, 75, 50, 25];
|
||||
|
||||
|
|
@ -29,90 +42,47 @@ const DAC_DATA: [u8; 8] = [0, 25, 50, 75, 100, 75, 50, 25];
|
|||
// - 25 -> 0xA
|
||||
const DPCM_DAC_DATA: [u8; 4] = [0xBB, 0xBB, 0xAA, 0xAA];
|
||||
|
||||
// const DATA2: [u8; 1] = [0x00u8];
|
||||
|
||||
|
||||
static mut IRQ1_FLAG: bool = false;
|
||||
|
||||
struct SimplePwmDacPin<'d, T: GeneralInstance16bit>{
|
||||
pin: SimplePwm<'d, T>,
|
||||
ch: Channel,
|
||||
}
|
||||
|
||||
// struct SimplePwmHandle<'a, 'c, T: GeneralInstance16bit>{
|
||||
// core: &'a SimplePwmCore<'c, T>,
|
||||
// channel: Channel,
|
||||
// }
|
||||
|
||||
use adpcm_pwm_dac::{interface::DacInterface, dac::Dac};
|
||||
|
||||
impl<T> DacInterface for SimplePwmDacPin<'_, T>
|
||||
where T: GeneralInstance16bit {
|
||||
fn write_amplitude(&mut self, amplitude: u8) {
|
||||
if !self.pin.is_enabled(self.ch) {
|
||||
self.pin.enable(self.ch);
|
||||
}
|
||||
let max_duty = self.pin.get_max_duty();
|
||||
let dc = amplitude as u32 * max_duty / 100;
|
||||
self.pin.set_duty(self.ch, dc);
|
||||
}
|
||||
|
||||
fn disable(&mut self) {
|
||||
self.pin.disable(self.ch);
|
||||
}
|
||||
}
|
||||
|
||||
fn blink(pin: AnyPin, interval_ms: u64) {
|
||||
let mut led = Output::new(pin, Level::Low, Default::default());
|
||||
|
||||
let mut delay = Delay;
|
||||
|
||||
loop {
|
||||
let hb_count = 3;
|
||||
let hb_period_ms = 1000;
|
||||
for _ in 0..hb_count {
|
||||
led.set_low();
|
||||
delay.delay_ms((interval_ms/2) as u32);
|
||||
led.set_high();
|
||||
delay.delay_ms((interval_ms/2) as u32);
|
||||
}
|
||||
delay.delay_ms((hb_period_ms - (interval_ms * hb_count)) as u32);
|
||||
}
|
||||
}
|
||||
|
||||
fn pwm_blink<T: GeneralInstance16bit>(mut pwm_dac_pin: SimplePwmDacPin<'_, T>) {
|
||||
let mut delay = Delay;
|
||||
|
||||
|
||||
|
||||
let interval_ms = 1000u32;
|
||||
loop {
|
||||
pwm_dac_pin.write_amplitude(75);
|
||||
delay.delay_ms((interval_ms/2) as u32);
|
||||
pwm_dac_pin.write_amplitude(50);
|
||||
delay.delay_ms((interval_ms/2) as u32);
|
||||
pwm_dac_pin.write_amplitude(25);
|
||||
delay.delay_ms((interval_ms/2) as u32);
|
||||
}
|
||||
}
|
||||
|
||||
// // fn dac_run<T: DacInterface>(mut dac: Dac<'_, T>, sample_rate: usize) {
|
||||
// fn dac_run<T: DacInterface>(mut dac: DpcmDac<'_, T>, sample_rate: usize) {
|
||||
|
||||
|
||||
// dac.load_data(data);
|
||||
// // dac.load_data(&DAC_DATA);
|
||||
|
||||
// let interval_us = 1000000/sample_rate as u32;
|
||||
// loop {
|
||||
// impl<'a, 'c, T: GeneralInstance16bit> SimplePwmHandle<'a, 'c, T> {
|
||||
// pub fn write_amplitude(&'a self, amplitude: u8) {
|
||||
// self.core.write_amplitude(self.channel, amplitude);
|
||||
// }
|
||||
|
||||
// pub fn disable(&'a self) {
|
||||
// self.core.disable(self.channel);
|
||||
// }
|
||||
// }
|
||||
|
||||
// struct SimplePwmDacHandle<'d, T: GeneralInstance16bit>{
|
||||
// pin: core::cell::RefCell<SimplePwm<'d, T>>,
|
||||
// ch: Channel,
|
||||
// }
|
||||
|
||||
#[qingke_rt::interrupt]
|
||||
fn EXTI4(){
|
||||
unsafe{IRQ1_FLAG = true;};
|
||||
}
|
||||
// bind_interrupts!(struct Irqs {
|
||||
// EXTI4 => button_press();
|
||||
// });
|
||||
|
||||
// use adpcm_pwm_dac::{interface::DacInterface, dac::Dac};
|
||||
|
||||
// impl<T> DacInterface for SimplePwmDacHandle<'_, T>
|
||||
// where T: GeneralInstance16bit {
|
||||
// fn write_amplitude(&mut self, amplitude: u8) {
|
||||
// if !self.pin.borrow().is_enabled(self.ch) {
|
||||
// self.pin.get_mut().enable(self.ch);
|
||||
// }
|
||||
// let max_duty = self.pin.borrow().get_max_duty();
|
||||
// let dc = amplitude as u32 * max_duty / 100;
|
||||
// self.pin.get_mut().set_duty(self.ch, dc);
|
||||
// }
|
||||
|
||||
// fn disable(&mut self) {
|
||||
// self.pin.get_mut().disable(self.ch);
|
||||
// }
|
||||
// }
|
||||
|
||||
#[qingke_rt::entry]
|
||||
fn main() -> ! {
|
||||
|
|
@ -120,108 +90,59 @@ fn main() -> ! {
|
|||
config.rcc = hal::rcc::Config::SYSCLK_FREQ_48MHZ_HSE;
|
||||
let p = hal::init(config);
|
||||
|
||||
|
||||
// button 1 setup
|
||||
// let b1 = Input::new(p.PA2, Pull::Up);
|
||||
|
||||
// p.EXTI4
|
||||
// let ei = hal::exti::ExtiInput::new(p.PD4, p.EXTI4, Pull::Up);
|
||||
|
||||
// let led_pin = PwmPin::new_ch4::<0>(p.PD0);
|
||||
// let ch = hal::timer::Channel::Ch4;
|
||||
// let mut pwm = SimplePwm::new(
|
||||
// p.TIM1,
|
||||
// None,
|
||||
// None,
|
||||
// None,
|
||||
// Some(pin),
|
||||
// Hertz::khz(100),
|
||||
// CountingMode::default(),
|
||||
// );
|
||||
// pwm.enable(ch);
|
||||
|
||||
//
|
||||
|
||||
// LED output setup
|
||||
let mut led = Output::new(p.PD0, Level::Low, Default::default());
|
||||
// LED0 output setup
|
||||
let mut led0_pin = PwmPin::new_ch3::<0>(p.PC3);
|
||||
let led0_ch = hal::timer::Channel::Ch3;
|
||||
|
||||
// LED1 output setup
|
||||
let mut led1 = Output::new(p.PD6, Level::High, Default::default());
|
||||
let mut led1_pin = PwmPin::new_ch1::<0>(p.PD2);
|
||||
let led1_ch = hal::timer::Channel::Ch1;
|
||||
|
||||
|
||||
// LED2 output setup
|
||||
|
||||
// DAC output setup
|
||||
let dac_pin = PwmPin::new_ch4::<0>(p.PC4);
|
||||
let dac_ch = hal::timer::Channel::Ch4;
|
||||
|
||||
|
||||
|
||||
// PWM DAC output pin setup
|
||||
let pin = PwmPin::new_ch4::<0>(p.PC4);
|
||||
let ch = hal::timer::Channel::Ch4;
|
||||
// PWM timer setup
|
||||
let mut pwm = SimplePwm::new(
|
||||
p.TIM1,
|
||||
Some(led1_pin),
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
Some(pin),
|
||||
Some(led0_pin),
|
||||
Some(dac_pin),
|
||||
Hertz::khz(100),
|
||||
CountingMode::default(),
|
||||
);
|
||||
pwm.enable(ch);
|
||||
let mut pwm_dac_pin = SimplePwmDacPin{pin: pwm, ch};
|
||||
|
||||
|
||||
let mut delay = Delay;
|
||||
|
||||
|
||||
|
||||
// DAC setup
|
||||
let mut dac = DpcmDac::new(pwm_dac_pin);
|
||||
let data = include_bytes!("../../../dpcm-encoder-decoder/sweep_dpcm_u4.raw");
|
||||
dac.load_data(data);
|
||||
|
||||
|
||||
// DAC servicer computations
|
||||
let mut dac_active = true;
|
||||
let sample_rate_hz = 16000;
|
||||
let dac_tick_per_service = 5;
|
||||
let tick_rate_hz = sample_rate_hz * dac_tick_per_service;
|
||||
let tick_interval = 1000000/(tick_rate_hz);
|
||||
|
||||
// LED servicer computations
|
||||
let mut led_active = true;
|
||||
let led_blink_rate_hz = 3;
|
||||
let led_tick_per_service = (tick_rate_hz/(led_blink_rate_hz * 2));
|
||||
let config = CoreConfig::new(tick_rate_hz);
|
||||
|
||||
// LED1 servicer vars
|
||||
let mut led1_need_service = false;
|
||||
let pwm_core = SimplePwmCore::new(pwm);
|
||||
|
||||
let app = InsertCoin::new(config, pwm_core);
|
||||
|
||||
// pwm_blink(pwm_dac_pin);
|
||||
let mut tick: usize = 0;
|
||||
loop{
|
||||
// handle IRQ flags
|
||||
// unsafe {
|
||||
// if(IRQ1_FLAG) {
|
||||
// led1_need_service = true;
|
||||
// // led_active = true;
|
||||
// IRQ1_FLAG = false;
|
||||
// }
|
||||
// }
|
||||
// insert_coin.init();
|
||||
|
||||
app.run();
|
||||
|
||||
let dac_need_service = ((tick % dac_tick_per_service) == 0);
|
||||
if(dac_need_service && dac_active) {
|
||||
// dac_active = dac.output_next();
|
||||
dac.output_next();
|
||||
}
|
||||
|
||||
let led_need_service = ((tick % led_tick_per_service) == 0);
|
||||
if(led_need_service && led_active) {
|
||||
led.toggle();
|
||||
}
|
||||
|
||||
if led1_need_service {
|
||||
led1.set_low();
|
||||
led1_need_service = false;
|
||||
}
|
||||
|
||||
tick = tick.wrapping_add(1);
|
||||
delay.delay_us(tick_interval as u32);
|
||||
};
|
||||
// // DAC servicer setup
|
||||
// let mut pwm_dac_interface = SimplePwmDacHandle{pin: core::cell::RefCell::new(pwm), ch: dac_ch};
|
||||
// let mut dac = DpcmDac::new(pwm_dac_interface);
|
||||
// let mut dac_active = true;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue