add coin button handling and move coin dac inside app
This commit is contained in:
parent
930d10218f
commit
c8c42c0194
5 changed files with 81 additions and 57 deletions
|
|
@ -22,7 +22,7 @@ use app::{
|
|||
};
|
||||
|
||||
use ch32_hal::{adc::AdcChannel, interrupt::typelevel::Handler, timer::low_level::OutputPolarity};
|
||||
use insert_coin::{CoreConfig, InsertCoin, LedService, SimplePwmCore};
|
||||
use insert_coin::{CoreConfig, DacService, InsertCoin, LedService, SimplePwmCore};
|
||||
|
||||
use ch32_hal as hal;
|
||||
use hal::bind_interrupts;
|
||||
|
|
@ -56,8 +56,8 @@ impl Flag {
|
|||
|
||||
#[derive(Debug)]
|
||||
struct InputFlags {
|
||||
sense_coin_flag: bool,
|
||||
main_btn_flag: bool,
|
||||
sense_coin_flag: Flag,
|
||||
main_btn_flag: Flag,
|
||||
volume_btn_flag: bool,
|
||||
light_ctrl_btn_flag: bool,
|
||||
systick_flag: Flag,
|
||||
|
|
@ -66,8 +66,8 @@ struct InputFlags {
|
|||
impl Default for InputFlags {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
sense_coin_flag: false,
|
||||
main_btn_flag: false,
|
||||
sense_coin_flag: Flag { value: false },
|
||||
main_btn_flag: Flag { value: false },
|
||||
volume_btn_flag: false,
|
||||
light_ctrl_btn_flag: false,
|
||||
systick_flag: Flag { value: false },
|
||||
|
|
@ -76,8 +76,8 @@ impl Default for InputFlags {
|
|||
}
|
||||
|
||||
static mut INPUT_FLAGS: InputFlags = InputFlags {
|
||||
sense_coin_flag: false,
|
||||
main_btn_flag: false,
|
||||
sense_coin_flag: Flag { value: false },
|
||||
main_btn_flag: Flag { value: false },
|
||||
volume_btn_flag: false,
|
||||
light_ctrl_btn_flag: false,
|
||||
systick_flag: Flag { value: false },
|
||||
|
|
@ -90,8 +90,15 @@ impl Handler<hal::interrupt::typelevel::EXTI7_0> for Test {
|
|||
println!("on_interrupt()");
|
||||
critical_section::with(|_| unsafe {
|
||||
let flags = system::clear_interrupt(2, 6);
|
||||
INPUT_FLAGS.sense_coin_flag = flags.sense_coin_flag;
|
||||
INPUT_FLAGS.main_btn_flag = flags.main_btn_flag;
|
||||
if flags[0] {
|
||||
// safe because single-threaded
|
||||
#[allow(static_mut_refs)]
|
||||
INPUT_FLAGS.sense_coin_flag.set();
|
||||
}
|
||||
if flags[1] {
|
||||
#[allow(static_mut_refs)]
|
||||
INPUT_FLAGS.main_btn_flag.set();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -184,10 +191,11 @@ fn app_main(mut p: hal::Peripherals) -> ! {
|
|||
pwm.set_polarity(led0_ch, OutputPolarity::ActiveLow);
|
||||
pwm.set_polarity(led1_ch, OutputPolarity::ActiveLow);
|
||||
|
||||
let sample_rate_hz = 4000;
|
||||
// let sample_rate_hz = 4000;
|
||||
// let sample_rate_hz = 16000;
|
||||
let dac_tick_per_service = 5;
|
||||
let tick_rate_hz = sample_rate_hz * dac_tick_per_service;
|
||||
// let dac_tick_per_service = 5;
|
||||
// let tick_rate_hz = sample_rate_hz * dac_tick_per_service;
|
||||
let tick_rate_hz = 50000;
|
||||
|
||||
let core_config = CoreConfig::new(tick_rate_hz);
|
||||
|
||||
|
|
@ -265,11 +273,22 @@ fn app_main(mut p: hal::Peripherals) -> ! {
|
|||
// let square_wt = SimpleWavetable::new(&SQUARE_WAVETABLE);
|
||||
// let square = SimpleWavetableSynthesizer::new(square_wt, tick_rate_hz);
|
||||
|
||||
// DAC servicer setup
|
||||
let dac_sample_rate_hz = 16000;
|
||||
let dac_tick_per_service = tick_rate_hz / dac_sample_rate_hz;
|
||||
let dac_service_data = TickServiceData::new(dac_tick_per_service);
|
||||
|
||||
let coin_sound = include_bytes!("../audio/coin2.raw");
|
||||
|
||||
let sample_player = DacService::new(ch32_hal::timer::Channel::Ch4, dac_service_data);
|
||||
sample_player.load_data(coin_sound);
|
||||
|
||||
let app_services = Services {
|
||||
led0: LedService::new(led0_ch),
|
||||
led1: LedService::new(led1_ch),
|
||||
led2: LedService::new(led2_ch),
|
||||
synth0: SynthesizerService::new(tick_rate_hz),
|
||||
sample_player,
|
||||
};
|
||||
|
||||
let app_sequences = Sequences {
|
||||
|
|
@ -368,6 +387,27 @@ fn app_main(mut p: hal::Peripherals) -> ! {
|
|||
light_ctrl_btn_input.reset();
|
||||
}
|
||||
|
||||
// coin_detect interrupt
|
||||
|
||||
unsafe {
|
||||
#[allow(static_mut_refs)]
|
||||
if INPUT_FLAGS.sense_coin_flag.active() {
|
||||
#[allow(static_mut_refs)]
|
||||
INPUT_FLAGS.sense_coin_flag.clear();
|
||||
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("coin flag active");
|
||||
sense_coin_input.begin();
|
||||
}
|
||||
sense_coin_input.service();
|
||||
if sense_coin_input.ready() {
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("debounced coin_input value: {}", sense_coin_input.value());
|
||||
sense_coin_input.reset();
|
||||
app.coin_detect();
|
||||
}
|
||||
}
|
||||
|
||||
// systick tick
|
||||
if unsafe {
|
||||
#[allow(static_mut_refs)]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue