minor fixes to sequence support

This commit is contained in:
sigil-03 2025-11-05 20:25:29 -07:00
parent 9e67026345
commit 9328087e23
4 changed files with 38 additions and 14 deletions

View file

@ -124,7 +124,7 @@ pub mod sequencer {
ticks_remaining: 0,
num_loops: 1,
loop_count: 0,
enabled: true,
enabled: false,
}
}
@ -132,6 +132,9 @@ pub mod sequencer {
self.sequence = sequence;
self.num_loops = num_loops;
self.loop_count = 0;
self.enabled = true;
self.index = 0;
self.ticks_remaining = 0;
}
pub fn enable(&mut self) {
@ -343,6 +346,7 @@ impl App {
self.timers.led2_timer.enable(true);
self.services.synth0.set_freq(440);
self.services.synth0.disable();
}
pub fn set_state(&mut self, state: State) {
@ -455,10 +459,10 @@ impl App {
if self.services.sample_player.need_service() {
self.services.sample_player.service();
// let out = self.services.sample_player.get_amplitude();
// self.interfaces
// .pwm_core
// .write_amplitude(ch32_hal::timer::Channel::Ch4, out as u8);
let out = self.services.sample_player.get_amplitude();
self.interfaces
.pwm_core
.write_amplitude(ch32_hal::timer::Channel::Ch4, out as u8);
}
}
}
@ -504,7 +508,7 @@ impl App {
self.timers.sp_timer.reset();
self.timers.lp_timer.reset();
}
pub fn coin_detect(&self) {
pub fn coin_detect(&mut self) {
#[cfg(feature = "enable_print")]
println!("coin detect");
self.services.sample_player.play_sample();
@ -513,10 +517,16 @@ impl App {
// Events
impl App {
fn main_button_click(&self) {
fn main_button_click(&mut self) {
// TODO
#[cfg(feature = "enable_print")]
println!("click");
self.services.sequencer.play_sequence(&crate::TEST_SEQ, 1);
self.services.synth0.enable();
// TODO:
// this is a hack to stop the coin thing from playing.
self.services.sample_player.disable();
}
fn main_button_short_press(&self) {
// TODO
@ -543,6 +553,8 @@ impl App {
// AUDIO:
// 2. dac switching
// 3. amp_en control
// 4. write sequences
// 5. sample player start disabled
//
// LED:
//
@ -559,3 +571,4 @@ impl App {
// 1. clean up edge detector
// 2. better handling for pwm scaling (brightness / volume)
// 3. better interrupt handling structs
// 4. led DynamicSequence

View file

@ -8,6 +8,7 @@ pub struct DacService<'a> {
dpcm_decoder: core::cell::RefCell<DpcmDecoder<'a>>,
amplitude: core::cell::RefCell<usize>,
pub channel: Channel,
enabled: bool,
}
impl<'a> DacService<'a> {
@ -17,11 +18,17 @@ impl<'a> DacService<'a> {
dpcm_decoder: core::cell::RefCell::new(DpcmDecoder::new()),
amplitude: core::cell::RefCell::new(0),
channel,
enabled: false,
}
}
pub fn play_sample(&self) {
pub fn play_sample(&mut self) {
self.dpcm_decoder.borrow_mut().seek_to_sample(0);
self.enabled = true;
}
pub fn disable(&mut self) {
self.enabled = false;
}
pub fn load_data(&self, data: &'a [u8]) {
@ -39,12 +46,14 @@ impl<'a> DacService<'a> {
impl<'a> TickService for DacService<'a> {
fn tick(&self) {
let mut tc = self.service_data.borrow_mut();
tc.ticks_remaining = tc.ticks_remaining.saturating_sub(1);
if self.enabled {
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
self.enabled && self.service_data.borrow().ticks_remaining == 0
}
fn service(&self) {

View file

@ -39,7 +39,7 @@ use qingke::riscv;
use crate::app::sequencer::{DynamicSequence, SequenceEntry};
static LED0_SEQ: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
static TEST_SEQ: [app::sequencer::SequenceEntry; 2] = [
pub static TEST_SEQ: [app::sequencer::SequenceEntry; 2] = [
SequenceEntry {
frequency_hz: 440,
duration_ms: 1000,
@ -267,7 +267,8 @@ fn app_main(mut p: hal::Peripherals) -> ! {
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 coin_sound = include_bytes!("../audio/coin.raw");
// 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);

View file

@ -34,10 +34,11 @@ impl SynthesizerService {
}
pub fn need_service(&self) -> bool {
self.need_service || self.synth.has_new_output()
self.need_service || (self.enabled && self.synth.has_new_output())
}
pub fn service(&mut self) -> Option<u8> {
self.need_service = false;
if self.enabled {
Some(self.synth.get_output())
} else {