Compare commits
73 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3265325d28 | |||
| d8477e3d2d | |||
| 472e43056d | |||
| 4c80907c0f | |||
| 1d2e4a5482 | |||
| d053f8d080 | |||
| f26920a099 | |||
| 60d6aaecd6 | |||
| 10a38f9bbc | |||
| 5934336984 | |||
| f3ac43614c | |||
| b6fa0e3b2d | |||
| cd91b3f540 | |||
| 45db5e8af8 | |||
| 6ba94f1cbb | |||
| c71ace5063 | |||
| 7e187680f5 | |||
| 64aa1808d2 | |||
| 2f92805c1a | |||
| c43cc5599e | |||
| 8ea4b4401e | |||
| 1c2823eb1b | |||
| 17d6f156db | |||
| b0b77a1538 | |||
| 43790abbc5 | |||
| 5288cba869 | |||
| f4759a0c71 | |||
| 3d8ddd1317 | |||
| 713f3882a2 | |||
| a7cd209989 | |||
| 935129baed | |||
| 4e9428cb5f | |||
| b52d911c12 | |||
| a50895ec96 | |||
| 88d07fa69d | |||
| a1767420f2 | |||
| 1bdb68c0d4 | |||
| 48b836904b | |||
| 9328087e23 | |||
| 9e67026345 | |||
| 7d93cd8977 | |||
| 5aa56a244d | |||
| 2d8e2ce6ee | |||
| d815507bcc | |||
| c8c42c0194 | |||
| 930d10218f | |||
| 95b55f88a8 | |||
| d3ccc70782 | |||
| b786bf174a | |||
| f17811cdce | |||
| 9e34e77e95 | |||
| e1943accd2 | |||
| 791d5db4c8 | |||
| 008bf334a4 | |||
| 0836fc58df | |||
| 58579ae6c2 | |||
| 3ea7aac1f4 | |||
| 17c82e5a6c | |||
| 9f12502a3d | |||
| 144d24cf59 | |||
| 485f617515 | |||
| b1d7574a80 | |||
| 73e4b482a6 | |||
| 3eb410c796 | |||
| 08d7289c93 | |||
| 8c88456fcb | |||
| 885c7746b3 | |||
| 1a420018d1 | |||
| fba0fbec61 | |||
| 8d612d1559 | |||
| 94848d0d43 | |||
| bdfef566bf | |||
| 3f4b8112fd |
32 changed files with 1981 additions and 458 deletions
13
.gitmodules
vendored
13
.gitmodules
vendored
|
|
@ -1,9 +1,12 @@
|
|||
[submodule "adpcm-pwm-dac"]
|
||||
path = ch32v-insert-coin/ext/adpcm-pwm-dac
|
||||
url = ssh://git@git.glyphs.tech:222/sigil-03/adpcm-pwm-dac.git
|
||||
[submodule "ch32v-insert-coin/ext/ch32-hal"]
|
||||
path = ch32v-insert-coin/ext/ch32-hal
|
||||
url = git@github.com:sigil-03/ch32-hal.git
|
||||
url = https://github.com/sigil-03/ch32-hal.git
|
||||
[submodule "ch32v-insert-coin/ext/qingke"]
|
||||
path = ch32v-insert-coin/ext/qingke
|
||||
url = git@github.com:ch32-rs/qingke.git
|
||||
url = https://github.com/ch32-rs/qingke.git
|
||||
[submodule "ch32v-insert-coin/ext/adpcm-pwm-dac"]
|
||||
path = ch32v-insert-coin/ext/adpcm-pwm-dac
|
||||
url = https://git.glyphs.tech/sigil-03/adpcm-pwm-dac.git
|
||||
[submodule "ch32v-insert-coin/ext/wavetable-synth"]
|
||||
path = ch32v-insert-coin/ext/wavetable-synth
|
||||
url = https://git.glyphs.tech/sigil-03/wavetable-synth.git
|
||||
|
|
|
|||
5
ch32v-insert-coin/Cargo.lock
generated
5
ch32v-insert-coin/Cargo.lock
generated
|
|
@ -78,6 +78,7 @@ dependencies = [
|
|||
"panic-halt",
|
||||
"qingke 0.5.0",
|
||||
"qingke-rt",
|
||||
"wavetable-synth",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -626,3 +627,7 @@ name = "void"
|
|||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
|
||||
|
||||
[[package]]
|
||||
name = "wavetable-synth"
|
||||
version = "0.1.0"
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@ name = "ch32v-insert-coin"
|
|||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[features]
|
||||
enable_print = []
|
||||
|
||||
[dependencies]
|
||||
ch32-hal = { path = "ext/ch32-hal/", features = [
|
||||
"ch32v003f4u6",
|
||||
|
|
@ -28,6 +31,8 @@ qingke = {path = "ext/qingke"}
|
|||
|
||||
adpcm-pwm-dac = { path = "ext/adpcm-pwm-dac/" }
|
||||
|
||||
wavetable-synth = { path = "ext/wavetable-synth" }
|
||||
|
||||
critical-section = { version = "1.2.0" }
|
||||
|
||||
[profile.release]
|
||||
|
|
|
|||
BIN
ch32v-insert-coin/audio/button_1.raw
Normal file
BIN
ch32v-insert-coin/audio/button_1.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/button_2.raw
Normal file
BIN
ch32v-insert-coin/audio/button_2.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/button_3.raw
Normal file
BIN
ch32v-insert-coin/audio/button_3.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coin.raw
Normal file
BIN
ch32v-insert-coin/audio/coin.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coin2.raw
Normal file
BIN
ch32v-insert-coin/audio/coin2.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coin3.raw
Normal file
BIN
ch32v-insert-coin/audio/coin3.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coin4.raw
Normal file
BIN
ch32v-insert-coin/audio/coin4.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coin5.raw
Normal file
BIN
ch32v-insert-coin/audio/coin5.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coin8ksps.raw
Normal file
BIN
ch32v-insert-coin/audio/coin8ksps.raw
Normal file
Binary file not shown.
BIN
ch32v-insert-coin/audio/coinMixTest1_dpcm_u4.raw
Normal file
BIN
ch32v-insert-coin/audio/coinMixTest1_dpcm_u4.raw
Normal file
Binary file not shown.
18
ch32v-insert-coin/audio/sequences
Normal file
18
ch32v-insert-coin/audio/sequences
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
"wahwahwahwah" sound - let play a few times
|
||||
note timing: 6hz
|
||||
data: let freqs = [100, 200, 300, 400, 500, 600, 700, 800, 900];
|
||||
|
||||
"falling" sound
|
||||
note timing: 6hz
|
||||
[
|
||||
1000,
|
||||
990, 980, 970, 960, 950, 940, 930, 920, 910, 900,
|
||||
890, 880, 870, 860, 850, 840, 830, 820, 810, 800,
|
||||
790, 780, 770, 760, 750, 740, 730, 720, 710, 700,
|
||||
690, 680, 670, 660, 650, 640, 630, 620, 610, 600,
|
||||
590, 580, 570, 560, 550, 540, 530, 520, 510, 500,
|
||||
490, 480, 470, 460, 450, 440, 430, 420, 410, 400,
|
||||
390, 380, 370, 360, 350, 340, 330, 320, 310, 300,
|
||||
290, 280, 270, 260, 250, 240, 230, 220, 210, 200,
|
||||
190, 180, 170, 160, 150, 140, 130, 120, 110, 100,
|
||||
]
|
||||
|
|
@ -1 +1 @@
|
|||
Subproject commit e4bb93e0399f27024434adf2558a893574fbfef3
|
||||
Subproject commit 99ce71e8d03e382b51732db7d0771349c51c7f48
|
||||
|
|
@ -1 +1 @@
|
|||
Subproject commit 412b9f5ee3a3708de8602d6103ec83c6dd436b63
|
||||
Subproject commit 4f11d68e62dcb0e7098eecf357168724a8322d80
|
||||
1
ch32v-insert-coin/ext/wavetable-synth
Submodule
1
ch32v-insert-coin/ext/wavetable-synth
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 30033e1438c25aed4ea506cdbd69cc4ceffa0b4e
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
DIR=${PWD}
|
||||
mkdir ${PWD}/tmp
|
||||
cd tmp
|
||||
git clone ssh://git@git.glyphs.tech:222/sigil-03/docker-devtools.git
|
||||
git clone ssh://git@git.glyphs.tech:222/taproot-tech/docker-devtools.git
|
||||
cd docker-devtools/ch32
|
||||
ls
|
||||
./build.sh
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
#!/bin/bash
|
||||
docker run --privileged -it --rm -v "$PWD":/usr/src/app -w /usr/src/app ch32-rust:latest /bin/bash
|
||||
podman run --privileged -it --rm -v "$PWD":/usr/src/app -w /usr/src/app ch32-rust:latest /bin/bash
|
||||
|
|
|
|||
|
|
@ -14,5 +14,5 @@
|
|||
"max-atomic-width": 32,
|
||||
"panic-strategy": "abort",
|
||||
"relocation-model": "static",
|
||||
"target-pointer-width": "32"
|
||||
"target-pointer-width": 32
|
||||
}
|
||||
705
ch32v-insert-coin/src/app.rs
Normal file
705
ch32v-insert-coin/src/app.rs
Normal file
|
|
@ -0,0 +1,705 @@
|
|||
#[derive(Default, Clone, Copy)]
|
||||
pub enum State {
|
||||
// system is asleep, waiting for wake from coin insertion
|
||||
DeepSleep,
|
||||
// system is in low-power mode, dimmed lights, waiting for interaction
|
||||
Idle,
|
||||
// system is active. on entry: play coin sound. on button press: play different sound
|
||||
#[default]
|
||||
Active,
|
||||
}
|
||||
|
||||
pub mod settings {
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub enum Level {
|
||||
Off,
|
||||
Low,
|
||||
#[default]
|
||||
Medium,
|
||||
High,
|
||||
Maximum,
|
||||
}
|
||||
|
||||
impl Level {
|
||||
pub fn next(&mut self) {
|
||||
*self = match self {
|
||||
Self::Off => Self::Low,
|
||||
Self::Low => Self::Medium,
|
||||
Self::Medium => Self::High,
|
||||
Self::High => Self::Maximum,
|
||||
Self::Maximum => Self::Off,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// volume control
|
||||
impl Level {
|
||||
pub fn as_volume_divisor(&self) -> u8 {
|
||||
match self {
|
||||
Self::Off => u8::MAX,
|
||||
Self::Low => 4,
|
||||
Self::Medium => 3,
|
||||
Self::High => 2,
|
||||
Self::Maximum => 1,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_brightness_divisor(&self) -> u8 {
|
||||
match self {
|
||||
Self::Off => u8::MAX,
|
||||
Self::Low => 8,
|
||||
Self::Medium => 6,
|
||||
Self::High => 4,
|
||||
Self::Maximum => 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Settings {
|
||||
pub brightness: Level,
|
||||
pub volume: Level,
|
||||
pub button_sound_index: usize,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
brightness: Level::Medium,
|
||||
volume: Level::Medium,
|
||||
button_sound_index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SequenceEntry {
|
||||
pub frequency_hz: u16,
|
||||
pub duration_ms: u16,
|
||||
}
|
||||
|
||||
pub struct DynamicSequence<'a> {
|
||||
sequence: &'a [SequenceEntry],
|
||||
index: usize,
|
||||
|
||||
// timer stuff
|
||||
system_tick_rate_hz: usize,
|
||||
ticks_remaining: usize,
|
||||
|
||||
// playback stuff
|
||||
num_loops: usize,
|
||||
loop_count: usize,
|
||||
|
||||
// misc
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> DynamicSequence<'a> {
|
||||
pub fn new(sequence: &'a [SequenceEntry], system_tick_rate_hz: usize) -> Self {
|
||||
Self {
|
||||
sequence,
|
||||
index: 0,
|
||||
system_tick_rate_hz,
|
||||
ticks_remaining: 0,
|
||||
num_loops: 1,
|
||||
loop_count: 0,
|
||||
enabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn play_sequence(&mut self, sequence: &'a [SequenceEntry], num_loops: usize) {
|
||||
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) {
|
||||
self.enabled = true;
|
||||
}
|
||||
|
||||
pub fn disable(&mut self) {
|
||||
self.enabled = false;
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
if self.enabled {
|
||||
self.ticks_remaining = self.ticks_remaining.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn need_service(&self) -> bool {
|
||||
self.enabled && self.ticks_remaining == 0
|
||||
}
|
||||
|
||||
pub fn service(&mut self) -> Option<u16> {
|
||||
let entry = &self.sequence[self.index];
|
||||
self.ticks_remaining = self.system_tick_rate_hz * (entry.duration_ms as usize) / 1000;
|
||||
self.index = self.index.saturating_add(1);
|
||||
|
||||
let out = if self.loop_count > self.num_loops {
|
||||
None
|
||||
} else {
|
||||
Some(entry.frequency_hz)
|
||||
};
|
||||
|
||||
if self.index > self.sequence.len() - 1 {
|
||||
self.index = 0;
|
||||
self.loop_count = self.loop_count.saturating_add(1);
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use crate::insert_coin::{
|
||||
DacService, LedService, Service, SimplePwmCore, TickService, TickServiceData, TickTimerService,
|
||||
};
|
||||
use crate::synthesizer::SynthesizerService;
|
||||
|
||||
pub use settings::Settings;
|
||||
|
||||
// #[cfg(feature = "enable_print")]
|
||||
use ch32_hal::println;
|
||||
|
||||
pub struct TimerConfig {
|
||||
pub sp_timer_ms: usize,
|
||||
pub lp_timer_ms: usize,
|
||||
pub batt_adc_timer_ms: usize,
|
||||
pub usb_adc_timer_ms: usize,
|
||||
pub led0_timer_ms: usize,
|
||||
pub led1_timer_ms: usize,
|
||||
pub shutdown_timer_s: usize,
|
||||
// pub led2_timer_ms: usize,
|
||||
}
|
||||
|
||||
pub struct Timers {
|
||||
sp_timer: TickTimerService,
|
||||
lp_timer: TickTimerService,
|
||||
batt_adc_timer: TickTimerService,
|
||||
usb_adc_timer: TickTimerService,
|
||||
led0_timer: TickTimerService,
|
||||
led1_timer: TickTimerService,
|
||||
shutdown_timer: TickTimerService,
|
||||
pps_timer: TickTimerService,
|
||||
// led2_timer: TickTimerService,
|
||||
}
|
||||
|
||||
impl Timers {
|
||||
pub fn new(config: TimerConfig, system_tick_rate_hz: usize) -> Self {
|
||||
Self {
|
||||
sp_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.sp_timer_ms * system_tick_rate_hz / 1000),
|
||||
false,
|
||||
),
|
||||
lp_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.lp_timer_ms * system_tick_rate_hz / 1000),
|
||||
false,
|
||||
),
|
||||
batt_adc_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.batt_adc_timer_ms * system_tick_rate_hz / 1000),
|
||||
false,
|
||||
),
|
||||
usb_adc_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.usb_adc_timer_ms * system_tick_rate_hz / 1000),
|
||||
false,
|
||||
),
|
||||
led0_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.led0_timer_ms * system_tick_rate_hz / 1000),
|
||||
true,
|
||||
),
|
||||
led1_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.led1_timer_ms * system_tick_rate_hz / 1000),
|
||||
true,
|
||||
),
|
||||
shutdown_timer: TickTimerService::new(
|
||||
TickServiceData::new(config.shutdown_timer_s),
|
||||
false,
|
||||
),
|
||||
pps_timer: TickTimerService::new(TickServiceData::new(system_tick_rate_hz), true),
|
||||
// led2_timer: TickTimerService::new(
|
||||
// TickServiceData::new(config.led2_timer_ms * system_tick_rate_hz / 1000),
|
||||
// true,
|
||||
// ),
|
||||
}
|
||||
}
|
||||
pub fn tick(&mut self) {
|
||||
self.sp_timer.tick();
|
||||
self.lp_timer.tick();
|
||||
self.batt_adc_timer.tick();
|
||||
self.usb_adc_timer.tick();
|
||||
self.led0_timer.tick();
|
||||
self.led1_timer.tick();
|
||||
self.pps_timer.tick();
|
||||
// self.led2_timer.tick();
|
||||
}
|
||||
pub fn need_service(&self) -> bool {
|
||||
self.sp_timer.need_service()
|
||||
| self.lp_timer.need_service()
|
||||
| self.batt_adc_timer.need_service()
|
||||
| self.usb_adc_timer.need_service()
|
||||
| self.led0_timer.need_service()
|
||||
| self.led1_timer.need_service()
|
||||
| self.shutdown_timer.need_service()
|
||||
| self.pps_timer.need_service()
|
||||
// | self.led2_timer.need_service()
|
||||
}
|
||||
pub fn init(&mut self) {
|
||||
self.led0_timer.reset();
|
||||
self.led0_timer.enable(true);
|
||||
}
|
||||
}
|
||||
|
||||
// pub struct ServiceConfigs {
|
||||
|
||||
// }
|
||||
// things that sort of don't touch hardware but also do?
|
||||
// TODO: make this merged with the interfaces maybe
|
||||
// but also maybe not, since these are things that require servicing
|
||||
pub struct Services {
|
||||
pub led0: LedService,
|
||||
pub led1: LedService,
|
||||
// pub led2: LedService,
|
||||
pub synth0: SynthesizerService,
|
||||
pub sample_player: DacService<'static>,
|
||||
pub sequencer: sequencer::DynamicSequence<'static>,
|
||||
}
|
||||
|
||||
impl Services {
|
||||
pub fn tick(&mut self) {
|
||||
self.synth0.tick();
|
||||
self.sample_player.tick();
|
||||
self.sequencer.tick();
|
||||
}
|
||||
}
|
||||
|
||||
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 audio: &'static [(&'static [sequencer::SequenceEntry], usize)],
|
||||
}
|
||||
|
||||
// things that touch hardware
|
||||
pub struct Interfaces {
|
||||
pub pwm_core: SimplePwmCore<'static, ch32_hal::peripherals::TIM1>,
|
||||
pub adc_core: crate::AdcCore,
|
||||
pub amp: crate::Amplifier,
|
||||
pub usb: crate::Usb,
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
state: State,
|
||||
pub settings: Settings,
|
||||
timers: Timers,
|
||||
services: Services,
|
||||
sequences: Sequences,
|
||||
interfaces: Interfaces,
|
||||
}
|
||||
|
||||
use settings::Level;
|
||||
impl App {
|
||||
pub fn new(
|
||||
config: Config,
|
||||
services: Services,
|
||||
sequences: Sequences,
|
||||
interfaces: Interfaces,
|
||||
settings: Settings,
|
||||
) -> Self {
|
||||
Self {
|
||||
state: State::default(),
|
||||
settings,
|
||||
timers: Timers::new(config.timers, config.system_tick_rate_hz),
|
||||
services,
|
||||
sequences,
|
||||
interfaces,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
// self.timers.init();
|
||||
self.interfaces.amp.enable();
|
||||
|
||||
self.timers.batt_adc_timer.reset();
|
||||
self.timers.batt_adc_timer.enable(true);
|
||||
|
||||
self.timers.usb_adc_timer.reset();
|
||||
self.timers.usb_adc_timer.enable(true);
|
||||
|
||||
self.timers.led0_timer.reset();
|
||||
self.timers.led0_timer.enable(true);
|
||||
|
||||
self.timers.led1_timer.reset();
|
||||
self.timers.led1_timer.enable(true);
|
||||
|
||||
self.timers.shutdown_timer.reset();
|
||||
self.timers.shutdown_timer.enable(true);
|
||||
|
||||
self.timers.pps_timer.reset();
|
||||
self.timers.pps_timer.enable(true);
|
||||
|
||||
// self.timers.led2_timer.reset();
|
||||
// self.timers.led2_timer.enable(true);
|
||||
|
||||
// self.services.synth0.set_freq(1);
|
||||
self.services.synth0.disable();
|
||||
self.services.sequencer.disable();
|
||||
|
||||
crate::riscv::asm::delay(2_500_000);
|
||||
}
|
||||
|
||||
pub fn set_state(&mut self, state: State) {
|
||||
self.state = state
|
||||
}
|
||||
|
||||
pub fn state(&self) -> State {
|
||||
self.state
|
||||
}
|
||||
|
||||
pub fn settings(&self) -> Settings {
|
||||
self.settings
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
self.timers.tick();
|
||||
self.services.tick();
|
||||
}
|
||||
|
||||
pub fn service(&mut self) {
|
||||
// timers
|
||||
if self.timers.sp_timer.need_service() {
|
||||
self.timers.sp_timer.service();
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("sp service");
|
||||
self.timers.sp_timer.reset();
|
||||
self.main_button_short_press();
|
||||
}
|
||||
if self.timers.lp_timer.need_service() {
|
||||
self.timers.lp_timer.service();
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("lp service");
|
||||
self.timers.lp_timer.reset();
|
||||
self.main_button_long_press();
|
||||
}
|
||||
if self.timers.batt_adc_timer.need_service() {
|
||||
self.timers.batt_adc_timer.service();
|
||||
if !self.interfaces.usb.powered() {
|
||||
let bv = self.interfaces.adc_core.get_battery_voltage();
|
||||
let avg = self.interfaces.adc_core.get_average();
|
||||
// #[cfg(feature = "enable_print")]
|
||||
// println!("batt adc service: {bv}, {avg}");
|
||||
// println!("none USB");
|
||||
if avg < 421 {
|
||||
self.set_state(State::DeepSleep);
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.timers.usb_adc_timer.need_service() {
|
||||
self.timers.usb_adc_timer.service();
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("usb adc service");
|
||||
}
|
||||
if self.timers.led0_timer.need_service() {
|
||||
let out = match self.settings.brightness {
|
||||
Level::Off => 0,
|
||||
Level::Low => 5,
|
||||
Level::Medium => 25,
|
||||
Level::High => 75,
|
||||
Level::Maximum => {
|
||||
self.sequences.led0.next();
|
||||
self.sequences.led0.get_value() / 6
|
||||
}
|
||||
};
|
||||
|
||||
self.timers.led0_timer.service();
|
||||
self.services.led0.set_amplitude(out);
|
||||
// #[cfg(feature = "enable_print")]
|
||||
// println!("led0 sevice {}", self.sequences.led0.get_value());
|
||||
}
|
||||
if self.timers.led1_timer.need_service() {
|
||||
let out = match self.settings.brightness {
|
||||
Level::Off => 0,
|
||||
Level::Low => 5,
|
||||
Level::Medium => 25,
|
||||
Level::High => 75,
|
||||
Level::Maximum => {
|
||||
self.sequences.led1.next();
|
||||
self.sequences.led1.get_value() / 6
|
||||
}
|
||||
};
|
||||
self.timers.led1_timer.service();
|
||||
self.services.led1.set_amplitude(out);
|
||||
|
||||
// #[cfg(feature = "enable_print")]
|
||||
// println!("led1 service");
|
||||
}
|
||||
if self.timers.pps_timer.need_service() {
|
||||
self.timers.pps_timer.service();
|
||||
self.timers.shutdown_timer.tick();
|
||||
}
|
||||
if self.timers.shutdown_timer.need_service() {
|
||||
self.timers.shutdown_timer.service();
|
||||
self.timers.shutdown_timer.reset();
|
||||
// println!("eepy");
|
||||
self.set_state(State::DeepSleep);
|
||||
}
|
||||
// if self.timers.led2_timer.need_service() {
|
||||
// let out = match self.settings.brightness {
|
||||
// Level::Off => 0,
|
||||
// Level::Low => 5,
|
||||
// Level::Medium => 25,
|
||||
// Level::High => 75,
|
||||
// Level::Maximum => {
|
||||
// self.sequences.led2.next();
|
||||
// self.sequences.led2.get_value() / 6
|
||||
// }
|
||||
// };
|
||||
// self.timers.led2_timer.service();
|
||||
// self.services.led2.set_amplitude(out);
|
||||
|
||||
// // #[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();
|
||||
// }
|
||||
|
||||
// TODO: disable when you get to the end automatically
|
||||
// in the sequencer, not here
|
||||
if self.services.sequencer.need_service() {
|
||||
if let Some(out) = self.services.sequencer.service() {
|
||||
if out == 0 {
|
||||
self.services.synth0.disable();
|
||||
} else {
|
||||
self.services.synth0.enable();
|
||||
self.services.synth0.set_freq(out.into());
|
||||
}
|
||||
} else {
|
||||
self.services.sequencer.disable();
|
||||
self.services.synth0.disable();
|
||||
}
|
||||
}
|
||||
|
||||
if self.services.synth0.need_service() {
|
||||
let out = match self.services.synth0.service() {
|
||||
Some(value) => value / 6 / self.settings.volume.as_volume_divisor(),
|
||||
None => 0,
|
||||
};
|
||||
self.interfaces
|
||||
.pwm_core
|
||||
.write_amplitude(ch32_hal::timer::Channel::Ch4, out);
|
||||
}
|
||||
|
||||
if self.services.sample_player.need_service() {
|
||||
self.services.sample_player.service();
|
||||
let out = self.services.sample_player.get_amplitude() / 2;
|
||||
self.interfaces
|
||||
.pwm_core
|
||||
.write_amplitude(ch32_hal::timer::Channel::Ch4, out as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// interfaces to the app (for buttons, etc.)
|
||||
impl App {
|
||||
pub fn shut_down(&mut self) {
|
||||
self.interfaces
|
||||
.pwm_core
|
||||
.write_amplitude(self.services.led0.channel, 0);
|
||||
self.interfaces
|
||||
.pwm_core
|
||||
.write_amplitude(self.services.led1.channel, 0);
|
||||
crate::riscv::asm::delay(10_000_000);
|
||||
|
||||
self.interfaces.pwm_core.pwm.borrow().shutdown();
|
||||
self.interfaces.adc_core.shutdown();
|
||||
|
||||
self.interfaces.amp.disable();
|
||||
}
|
||||
pub fn volume_button(&mut self) {
|
||||
self.settings.volume.next();
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("new volume: {:?}", self.settings.volume);
|
||||
self.services
|
||||
.sequencer
|
||||
.play_sequence(&crate::sequences::COIN_CHIRP, 0);
|
||||
self.timers.shutdown_timer.reset();
|
||||
self.timers.shutdown_timer.enable(true);
|
||||
}
|
||||
pub fn brightness_button(&mut self) {
|
||||
self.settings.brightness.next();
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("new brightness: {:?}", self.settings.brightness);
|
||||
self.timers.shutdown_timer.reset();
|
||||
self.timers.shutdown_timer.enable(true);
|
||||
}
|
||||
pub fn main_button_press(&mut self) {
|
||||
// TODO
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("main button press");
|
||||
self.timers.sp_timer.reset();
|
||||
self.timers.lp_timer.reset();
|
||||
self.timers.shutdown_timer.reset();
|
||||
self.timers.sp_timer.enable(true);
|
||||
self.timers.lp_timer.enable(true);
|
||||
self.timers.shutdown_timer.enable(true);
|
||||
self.main_button_click();
|
||||
}
|
||||
pub fn main_button_release(&mut self) {
|
||||
// TODO
|
||||
// #[cfg(feature = "enable_print")]
|
||||
// println!("main button release");
|
||||
// let timers = (
|
||||
// self.timers.sp_timer.is_enabled(),
|
||||
// self.timers.lp_timer.is_enabled(),
|
||||
// );
|
||||
|
||||
self.timers.sp_timer.reset();
|
||||
self.timers.lp_timer.reset();
|
||||
// match timers {
|
||||
// // click
|
||||
// (true, true) => self.main_button_click(),
|
||||
// // short press
|
||||
// (false, true) => self.main_button_short_press(),
|
||||
// // long press
|
||||
// (false, false) => self.main_button_long_press(),
|
||||
// // anything else is not possible
|
||||
// _ => {}
|
||||
// }
|
||||
}
|
||||
pub fn coin_detect(&mut self) {
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("coin detect");
|
||||
// self.services.sample_player.play_sample();
|
||||
self.services
|
||||
.sequencer
|
||||
.play_sequence(&crate::sequences::COIN_CHIRP, 0);
|
||||
self.timers.shutdown_timer.reset();
|
||||
self.timers.shutdown_timer.enable(true);
|
||||
}
|
||||
}
|
||||
|
||||
// Events
|
||||
impl App {
|
||||
pub fn main_button_click(&mut self) {
|
||||
// TODO
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("click");
|
||||
let data = self.sequences.audio[self.settings.button_sound_index];
|
||||
self.services.sequencer.play_sequence(data.0, data.1);
|
||||
|
||||
self.settings.button_sound_index += 1;
|
||||
if self.settings.button_sound_index > self.sequences.audio.len() - 1 {
|
||||
self.settings.button_sound_index = 0;
|
||||
}
|
||||
|
||||
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
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("short press");
|
||||
}
|
||||
fn main_button_long_press(&mut self) {
|
||||
// TODO
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("long press");
|
||||
self.set_state(State::DeepSleep);
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
impl App {
|
||||
pub fn get_state(&self) -> State {
|
||||
self.state
|
||||
}
|
||||
pub fn get_settings(&self) -> Settings {
|
||||
self.settings
|
||||
}
|
||||
pub fn should_wake(&self) -> bool {
|
||||
if self.interfaces.usb.powered() {
|
||||
return true;
|
||||
} else {
|
||||
if self.interfaces.adc_core.get_average() >= 421 {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO LIST
|
||||
// BROKEN:
|
||||
// 1. audio scaling causes crash
|
||||
// 2. popping on sample playback
|
||||
// 3. actual app sequence (start at idle?)
|
||||
//
|
||||
// AUDIO:
|
||||
// 3. amp_en control
|
||||
//
|
||||
// LED:
|
||||
//
|
||||
// INTERFACE:
|
||||
// 1. short press handling
|
||||
// 2. long press handling
|
||||
//
|
||||
// SYSTEM:
|
||||
// 1. deep sleep
|
||||
// 2. battery voltage monitoring
|
||||
// 3. battery voltage cutoff
|
||||
//
|
||||
// STRETCH TODO LIST
|
||||
// 1. clean up edge detector
|
||||
// 2. better handling for pwm scaling (brightness / volume)
|
||||
// 3. better interrupt handling structs
|
||||
// 4. led DynamicSequence
|
||||
66
ch32v-insert-coin/src/debounced_gpio.rs
Normal file
66
ch32v-insert-coin/src/debounced_gpio.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use crate::insert_coin::{TickService, TickServiceData, TickTimerService};
|
||||
use ch32_hal::gpio::{AnyPin, Input, Pull};
|
||||
|
||||
pub struct DebouncedGPIO<'a> {
|
||||
input: Input<'a>,
|
||||
// value of the GPIO
|
||||
value: bool,
|
||||
// GPIO is ready (debounced)
|
||||
ready: bool,
|
||||
// debouncer is active
|
||||
active: bool,
|
||||
// debounce timer
|
||||
timer: TickTimerService,
|
||||
}
|
||||
|
||||
impl<'a> DebouncedGPIO<'a> {
|
||||
pub fn new(pin: AnyPin, system_tick_rate_hz: usize, debounce_time_ms: usize) -> Self {
|
||||
// coin debounce timer (100ms)
|
||||
Self {
|
||||
input: Input::new(pin, Pull::None),
|
||||
value: false,
|
||||
ready: false,
|
||||
active: false,
|
||||
timer: TickTimerService::new(
|
||||
TickServiceData::new(system_tick_rate_hz * debounce_time_ms / 1000),
|
||||
false,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ready(&self) -> bool {
|
||||
self.ready
|
||||
}
|
||||
|
||||
pub fn active(&self) -> bool {
|
||||
self.active
|
||||
}
|
||||
|
||||
pub fn value(&self) -> bool {
|
||||
self.value
|
||||
}
|
||||
|
||||
pub fn begin(&mut self) {
|
||||
self.reset();
|
||||
self.timer.enable(true);
|
||||
self.active = true;
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.timer.reset();
|
||||
self.ready = false;
|
||||
self.active = false;
|
||||
}
|
||||
|
||||
pub fn service(&mut self) {
|
||||
self.timer.tick();
|
||||
if self.timer.need_service() {
|
||||
self.timer.reset();
|
||||
self.value = self.input.is_high();
|
||||
self.ready = true;
|
||||
}
|
||||
}
|
||||
pub fn is_high_immediate(&self) -> bool {
|
||||
self.input.is_high()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,8 @@
|
|||
use ch32_hal::timer::GeneralInstance16bit;
|
||||
use ch32_hal::timer::simple_pwm::SimplePwm;
|
||||
use ch32_hal::timer::Channel;
|
||||
use ch32_hal::timer::GeneralInstance16bit;
|
||||
|
||||
use crate::insert_coin::services::{DacService, LedService, TickService, TickServiceData, Service};
|
||||
|
||||
use crate::insert_coin::services::{DacService, LedService, Service, TickService, TickServiceData};
|
||||
|
||||
// static mut led0_index: usize = 0;
|
||||
// static LED0: [u8; 8] = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
|
||||
|
|
@ -12,7 +11,7 @@ use crate::insert_coin::services::{DacService, LedService, TickService, TickServ
|
|||
// static LED1_DCS: [u8; 5] = [0u8, 25u8, 50u8, 75u8, 100u8];
|
||||
|
||||
pub struct SimplePwmCore<'d, T: GeneralInstance16bit> {
|
||||
pwm: core::cell::RefCell<SimplePwm<'d, T>>,
|
||||
pub pwm: core::cell::RefCell<SimplePwm<'d, T>>,
|
||||
}
|
||||
|
||||
impl<'d, T: GeneralInstance16bit> SimplePwmCore<'d, T> {
|
||||
|
|
@ -38,22 +37,26 @@ impl<'d, T: GeneralInstance16bit> SimplePwmCore<'d, T> {
|
|||
self.pwm.borrow_mut().set_duty(ch, dc);
|
||||
}
|
||||
|
||||
// pub fn disable(&self, ch: Channel) {
|
||||
// self.pwm.borrow_mut().disable(ch);
|
||||
// }
|
||||
pub fn disable(&self, ch: Channel) {
|
||||
self.pwm.borrow_mut().disable(ch);
|
||||
}
|
||||
}
|
||||
|
||||
// pub struct SimplePwmHandle<T: GeneralInstance16Bit> {
|
||||
// core: &'static SimplePwmCore<'_, T: GeneralInstance16Bit>,
|
||||
// channel: Channel,
|
||||
// }
|
||||
|
||||
|
||||
// impl<T: GeneralInstance16Bit> SimplePwmHandle<T> {
|
||||
// pub fn set_amplitude(&self, amplitude: u8) {}
|
||||
// }
|
||||
|
||||
pub struct CoreConfig {
|
||||
pub tick_rate_hz: usize,
|
||||
}
|
||||
impl CoreConfig {
|
||||
pub fn new(tick_rate_hz: usize) -> Self {
|
||||
Self {
|
||||
tick_rate_hz,
|
||||
}
|
||||
Self { tick_rate_hz }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -70,25 +73,20 @@ pub struct InsertCoin<'a, T: GeneralInstance16bit> {
|
|||
pub led0: LedService,
|
||||
pub led1: LedService,
|
||||
// led2: LedService,
|
||||
|
||||
pub dac: DacService<'a>,
|
||||
}
|
||||
|
||||
impl<'a, T: GeneralInstance16bit> InsertCoin<'a, T> {
|
||||
|
||||
pub fn new(config: CoreConfig, pwm_core: SimplePwmCore<'a, T>) -> Self {
|
||||
|
||||
|
||||
// LED0 servicer setup
|
||||
let led0 = LedService::new(ch32_hal::timer::Channel::Ch3);
|
||||
|
||||
// LED1 servicer setup
|
||||
let led1 = LedService::new(ch32_hal::timer::Channel::Ch1);
|
||||
|
||||
|
||||
// DAC servicer setup
|
||||
let dac_sample_rate_hz = 16000;
|
||||
let dac_tick_per_service = config.tick_rate_hz/(dac_sample_rate_hz);
|
||||
let dac_sample_rate_hz = 4000;
|
||||
let dac_tick_per_service = config.tick_rate_hz / (dac_sample_rate_hz);
|
||||
let dac_service_data = TickServiceData::new(dac_tick_per_service);
|
||||
let dac = DacService::new(ch32_hal::timer::Channel::Ch4, dac_service_data);
|
||||
|
||||
|
|
@ -105,28 +103,28 @@ impl<'a, T: GeneralInstance16bit> InsertCoin<'a, T> {
|
|||
|
||||
/// takes self reference and runs
|
||||
pub fn service(&mut self) {
|
||||
|
||||
if self.is_active() {
|
||||
self.dac.tick();
|
||||
|
||||
|
||||
if self.led0.need_service() {
|
||||
self.pwm_core.write_amplitude(self.led0.channel, self.led0.amplitude);
|
||||
self.pwm_core
|
||||
.write_amplitude(self.led0.channel, self.led0.amplitude);
|
||||
self.led0.service();
|
||||
}
|
||||
|
||||
if self.led1.need_service() {
|
||||
self.pwm_core.write_amplitude(self.led1.channel, self.led1.amplitude);
|
||||
self.pwm_core
|
||||
.write_amplitude(self.led1.channel, self.led1.amplitude);
|
||||
self.led1.service();
|
||||
}
|
||||
|
||||
if self.dac.need_service() {
|
||||
self.dac.service();
|
||||
// TODO: adpcm-pwm-dac:e4c811653781e69e40b63fd27a8c1e20
|
||||
self.pwm_core.write_amplitude(self.dac.channel, self.dac.get_amplitude() as u8);
|
||||
self.pwm_core
|
||||
.write_amplitude(self.dac.channel, self.dac.get_amplitude() as u8);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// /// consumes self and runs
|
||||
|
|
@ -134,7 +132,6 @@ impl<'a, T: GeneralInstance16bit> InsertCoin<'a, T> {
|
|||
// let mut delay = Delay;
|
||||
// let tick_interval_us = 1000000/self.config.tick_rate_hz;
|
||||
|
||||
|
||||
// let mut led0_index = 0;
|
||||
// let led0_dcs = [0u8, 25u8, 50u8, 75u8, 100u8, 75u8, 50u8, 25u8];
|
||||
|
||||
|
|
@ -144,7 +141,6 @@ impl<'a, T: GeneralInstance16bit> InsertCoin<'a, T> {
|
|||
// loop {
|
||||
// self.dac.tick();
|
||||
|
||||
|
||||
// if(self.led0.need_service()) {
|
||||
// self.led0.set_amplitude(led0_dcs[led0_index]);
|
||||
// self.pwm_core.write_amplitude(self.led0.channel, self.led0.amplitude);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
mod insert_coin;
|
||||
mod services;
|
||||
|
||||
pub use services::TickTimerService;
|
||||
pub use services::{DacService, LedService, Service, TickTimerService};
|
||||
|
||||
pub use insert_coin::{CoreConfig, InsertCoin, SimplePwmCore};
|
||||
pub use services::{TickService, TickServiceData};
|
||||
pub use insert_coin::{InsertCoin, CoreConfig, SimplePwmCore};
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
use crate::insert_coin::services::{TickServiceData, TickService};
|
||||
|
||||
use crate::insert_coin::services::{TickService, TickServiceData};
|
||||
|
||||
use adpcm_pwm_dac::dac::DpcmDecoder;
|
||||
use ch32_hal::timer::Channel;
|
||||
|
|
@ -9,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> {
|
||||
|
|
@ -18,9 +18,19 @@ 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(&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]) {
|
||||
self.dpcm_decoder.borrow_mut().load_data(data);
|
||||
self.dpcm_decoder.borrow_mut().seek_to_sample(0);
|
||||
|
|
@ -36,17 +46,23 @@ impl<'a> DacService<'a> {
|
|||
|
||||
impl<'a> TickService for DacService<'a> {
|
||||
fn tick(&self) {
|
||||
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) {
|
||||
let mut tc = self.service_data.borrow_mut();
|
||||
tc.ticks_remaining = tc.ticks_per_service;
|
||||
if (self.dpcm_decoder.borrow().is_done()) {
|
||||
self.set_amplitude(0);
|
||||
} else {
|
||||
self.set_amplitude(self.dpcm_decoder.borrow_mut().output_next());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use ch32_hal::timer::Channel;
|
||||
|
||||
use crate::insert_coin::services::{Service};
|
||||
use crate::insert_coin::services::Service;
|
||||
|
||||
pub struct LedService {
|
||||
// need_service: core::cell::RefCell<bool>,
|
||||
|
|
@ -25,9 +25,7 @@ impl LedService {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl Service for LedService {
|
||||
|
||||
fn need_service(&self) -> bool {
|
||||
self.need_service
|
||||
}
|
||||
|
|
@ -36,5 +34,3 @@ impl Service for LedService {
|
|||
self.need_service = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::insert_coin::services::{TickServiceData, TickService};
|
||||
use crate::insert_coin::services::{TickService, TickServiceData};
|
||||
|
||||
pub struct TickTimerService {
|
||||
service_data: core::cell::RefCell<TickServiceData>,
|
||||
|
|
@ -15,9 +15,9 @@ impl TickTimerService {
|
|||
}
|
||||
}
|
||||
|
||||
// pub fn is_enabled(&self) -> bool {
|
||||
// self.enabled
|
||||
// }
|
||||
pub fn is_enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
let mut sd = self.service_data.borrow_mut();
|
||||
|
|
@ -30,7 +30,6 @@ impl TickTimerService {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
impl TickService for TickTimerService {
|
||||
fn tick(&self) {
|
||||
if self.enabled {
|
||||
|
|
@ -48,5 +47,3 @@ impl TickService for TickTimerService {
|
|||
tc.ticks_remaining = tc.ticks_per_service;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
346
ch32v-insert-coin/src/sequences.rs
Normal file
346
ch32v-insert-coin/src/sequences.rs
Normal file
|
|
@ -0,0 +1,346 @@
|
|||
use crate::app::sequencer::SequenceEntry;
|
||||
|
||||
pub static TEST_SEQ: [SequenceEntry; 2] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 440,
|
||||
duration_ms: 1000,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 220,
|
||||
duration_ms: 1000,
|
||||
},
|
||||
];
|
||||
|
||||
pub static TEST_SEQ1: [SequenceEntry; 2] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 440,
|
||||
duration_ms: 100,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 220,
|
||||
duration_ms: 100,
|
||||
},
|
||||
];
|
||||
|
||||
// play twice
|
||||
pub static WAHWAH: [SequenceEntry; 9] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 100,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 200,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 300,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 400,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 500,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 600,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 700,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 800,
|
||||
duration_ms: 25,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 900,
|
||||
duration_ms: 25,
|
||||
},
|
||||
];
|
||||
|
||||
pub static DECENDING_TONES: [SequenceEntry; 21] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 1100,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1050,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1000,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 950,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 900,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 850,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 800,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 750,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 700,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 650,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 600,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 550,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 500,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 450,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 400,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 350,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 300,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 250,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 200,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 150,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 100,
|
||||
duration_ms: 50,
|
||||
},
|
||||
];
|
||||
|
||||
// PLAY ONCE
|
||||
pub static COIN_CHIRP: [SequenceEntry; 2] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 1000,
|
||||
duration_ms: 100,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1500,
|
||||
duration_ms: 500,
|
||||
},
|
||||
];
|
||||
|
||||
// PLAY ONCE
|
||||
pub static SOUND_8_BIT_GAME_4: [SequenceEntry; 4] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 220,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 440,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 660,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 880,
|
||||
duration_ms: 50,
|
||||
},
|
||||
];
|
||||
|
||||
// PLAY TWICE
|
||||
// TODO: this isn't working quite right with the 0hz "non note" for a rest...
|
||||
pub static SOUND_8_BIT_GAME_1: [SequenceEntry; 3] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 440,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1500,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 0,
|
||||
duration_ms: 50,
|
||||
},
|
||||
];
|
||||
|
||||
// PLAY 3 TIMES
|
||||
pub static SOUND_8_BIT_GAME_3: [SequenceEntry; 3] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 440,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 660,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1000,
|
||||
duration_ms: 50,
|
||||
},
|
||||
];
|
||||
|
||||
// PLAY 1 TIMES
|
||||
pub static ASCENDING_TONES: [SequenceEntry; 29] = [
|
||||
SequenceEntry {
|
||||
frequency_hz: 100,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 150,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 200,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 250,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 300,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 350,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 400,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 450,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 500,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 550,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 600,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 650,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 700,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 750,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 800,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 850,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 900,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 950,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1000,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1050,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1100,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1150,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1200,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1250,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1300,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1350,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1400,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1450,
|
||||
duration_ms: 50,
|
||||
},
|
||||
SequenceEntry {
|
||||
frequency_hz: 1500,
|
||||
duration_ms: 1000,
|
||||
},
|
||||
];
|
||||
|
||||
pub static SEQUENCE_LIST: [(&'static [SequenceEntry], usize); 7] = [
|
||||
(&SOUND_8_BIT_GAME_3, 2),
|
||||
(&SOUND_8_BIT_GAME_4, 0),
|
||||
(&ASCENDING_TONES, 0),
|
||||
(&COIN_CHIRP, 0),
|
||||
(&WAHWAH, 5),
|
||||
(&DECENDING_TONES, 0),
|
||||
(&SOUND_8_BIT_GAME_1, 1),
|
||||
// &TEST_SEQ,
|
||||
];
|
||||
66
ch32v-insert-coin/src/synthesizer.rs
Normal file
66
ch32v-insert-coin/src/synthesizer.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
use crate::insert_coin::SimplePwmCore;
|
||||
use ch32_hal::println;
|
||||
use ch32_hal::timer::GeneralInstance16bit;
|
||||
use wavetable_synth::{synthesizer::SimpleWavetableSynthesizer, wavetable::SimpleWavetable};
|
||||
|
||||
const SQUARE_WAVETABLE: [u8; 2] = [0, 100];
|
||||
// const SQUARE_WAVETABLE: [u8; 32] = [
|
||||
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100, 100, 100, 100, 100,
|
||||
// 100, 100, 100, 100, 100, 100, 100,
|
||||
// ];
|
||||
|
||||
pub struct SynthesizerService {
|
||||
pub synth: SimpleWavetableSynthesizer<SimpleWavetable<'static, u8>>,
|
||||
pub enabled: bool,
|
||||
pub need_service: bool,
|
||||
}
|
||||
|
||||
impl SynthesizerService {
|
||||
pub fn new(clock_freq_hz: usize) -> Self {
|
||||
let square_wt = SimpleWavetable::new(&SQUARE_WAVETABLE);
|
||||
let synth = SimpleWavetableSynthesizer::new(square_wt, clock_freq_hz);
|
||||
|
||||
Self {
|
||||
synth,
|
||||
enabled: true,
|
||||
need_service: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
if self.enabled {
|
||||
self.synth.tick();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn need_service(&self) -> bool {
|
||||
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 {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SynthesizerService {
|
||||
pub fn set_freq(&mut self, freq_hz: usize) {
|
||||
self.synth.set_freq(freq_hz);
|
||||
}
|
||||
|
||||
pub fn enable(&mut self) {
|
||||
self.enabled = true;
|
||||
self.need_service = true;
|
||||
// TODO: write the enable function
|
||||
}
|
||||
|
||||
pub fn disable(&mut self) {
|
||||
self.enabled = false;
|
||||
self.need_service = true;
|
||||
// TODO: write the disable function
|
||||
}
|
||||
}
|
||||
134
ch32v-insert-coin/src/system.rs
Normal file
134
ch32v-insert-coin/src/system.rs
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
use ch32_hal as hal;
|
||||
use hal::println;
|
||||
|
||||
pub unsafe fn init_gpio_irq(pin: u8, port: u8, rising: bool, falling: bool) {
|
||||
critical_section::with(|_| {
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("init_gpio_irq {pin}:{port}");
|
||||
let exti = &hal::pac::EXTI;
|
||||
let afio = &hal::pac::AFIO;
|
||||
|
||||
let port = port as u8;
|
||||
let pin = pin as usize;
|
||||
|
||||
// let b = afio.exticr().read();
|
||||
afio.exticr().modify(|w| w.set_exti(pin, port));
|
||||
|
||||
exti.intenr().modify(|w| w.set_mr(pin, true)); // enable interrupt
|
||||
exti.rtenr().modify(|w| w.set_tr(pin, rising));
|
||||
exti.ftenr().modify(|w| w.set_tr(pin, falling));
|
||||
});
|
||||
}
|
||||
|
||||
pub fn clear_interrupt(coin_pin: u8, button_pin: u8) -> [bool; 2] {
|
||||
let mut output = [false, false];
|
||||
|
||||
let exti = &hal::pac::EXTI;
|
||||
|
||||
let coin_pin = coin_pin as usize;
|
||||
let button_pin = button_pin as usize;
|
||||
|
||||
exti.intenr().modify(|w| w.set_mr(coin_pin, false)); // disable interrupt
|
||||
exti.intenr().modify(|w| w.set_mr(button_pin, false)); // disable interrupt
|
||||
|
||||
let bits = exti.intfr().read();
|
||||
|
||||
// We don't handle or change any EXTI lines above 24.
|
||||
let bits = bits.0 & 0x00FFFFFF;
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("bits: {bits:08x}");
|
||||
|
||||
// coin_flag
|
||||
if (bits & (0x1 << coin_pin)) != 0x0 {
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("coin irq!");
|
||||
output[0] = true;
|
||||
}
|
||||
|
||||
// button_flag
|
||||
if (bits & (0x1 << button_pin)) != 0x0 {
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("main_btn irq!");
|
||||
output[1] = true;
|
||||
}
|
||||
|
||||
// Clear pending - Clears the EXTI's line pending bits.
|
||||
exti.intfr().write(|w| w.0 = bits);
|
||||
|
||||
use hal::pac::Interrupt;
|
||||
unsafe {
|
||||
qingke::pfic::unpend_interrupt(Interrupt::EXTI7_0 as u8);
|
||||
};
|
||||
|
||||
// exti.intenr().modify(|w| w.0 = w.0 & !bits);
|
||||
exti.intenr().modify(|w| w.set_mr(coin_pin, true)); // enable interrupt
|
||||
exti.intenr().modify(|w| w.set_mr(button_pin, true)); // enable interrupt
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
/// enter standby (SLEEPDEEP) mode, with WFE enabled.
|
||||
/// from CH32V003 reference manual 2.3.1:
|
||||
/// entry:
|
||||
/// 1. configure core register control bit: SLEEPDEEP=1 | PDDS = 1
|
||||
/// 2. configure external interrupt register to mask all but EXTI7_0
|
||||
/// 3. execute WFI or WFE (optionally SEVONPEND) and SLEEPONEXIT
|
||||
/// * (probably WFI?)
|
||||
/// exit:
|
||||
/// 1. any interrupt/event (set in external interrupt register)
|
||||
pub unsafe fn enter_standby() {
|
||||
critical_section::with(|_| {
|
||||
use hal::pac::Interrupt;
|
||||
use qingke_rt::CoreInterrupt;
|
||||
|
||||
// configure core register control bit (SLEEPDEEP=1 | PDDS=1)
|
||||
let pfic = &hal::pac::PFIC;
|
||||
pfic.sctlr().modify(|w| {
|
||||
// we only want to wake on enabled interrupts
|
||||
w.set_sevonpend(false);
|
||||
// we want to enable deep sleep
|
||||
w.set_sleepdeep(true);
|
||||
w.set_wfitowfe(false);
|
||||
w.set_sleeponexit(false);
|
||||
});
|
||||
|
||||
// set PDDS=1:
|
||||
// get current value of PWR_CTLR
|
||||
let mut reg: u32 = 0x4000_7000;
|
||||
let mut val: u32 = unsafe { (reg as *mut u32).read_volatile() };
|
||||
// modify PDDS
|
||||
val |= 1 << 1; // PWR_CTLR[1] -> PDDS
|
||||
unsafe {
|
||||
(reg as *mut u32).write_volatile(val);
|
||||
}
|
||||
|
||||
// // disable all exti interrupts
|
||||
let exti = &hal::pac::EXTI;
|
||||
// exti.intenr().write(| w| {
|
||||
// w.0 = 0x00000000;
|
||||
// // w.set_mr(pin, true);
|
||||
// // w.set_mr(pin, true);
|
||||
// });
|
||||
|
||||
// clear all pending exti interrupts
|
||||
let bits = 0xFFFFFFFF;
|
||||
exti.intfr().write(|w| w.0 = bits);
|
||||
|
||||
// enable all exti interrupts
|
||||
let exti = &hal::pac::EXTI;
|
||||
exti.intenr().write(|w| {
|
||||
w.0 = 0x00FFFFFF;
|
||||
// w.set_mr(pin, true);
|
||||
// w.set_mr(pin, true);
|
||||
});
|
||||
|
||||
unsafe {
|
||||
qingke::pfic::disable_interrupt(CoreInterrupt::SysTick as u8);
|
||||
qingke::pfic::enable_interrupt(Interrupt::EXTI7_0 as u8);
|
||||
}
|
||||
|
||||
// execute WFI
|
||||
#[cfg(feature = "enable_print")]
|
||||
println!("WFI CONFIGURED HOPEFULLY");
|
||||
});
|
||||
}
|
||||
47
notes.md
47
notes.md
|
|
@ -48,3 +48,50 @@ it turns out you can not put the chip into deep sleep and have it wake up correc
|
|||
# EVAL BOARD NOTES
|
||||
## PINOUT
|
||||
WCHLinkE SWDIO -> PD1
|
||||
|
||||
|
||||
|
||||
# AUDIO NOTES
|
||||
## LIMITS
|
||||
we have ~9-10kB of space remaining on the chip.
|
||||
i can likely squeeze out _maybe_ another kB by dropping every print, but
|
||||
that appears to cause some lockup, i think because the SDI peripheral is
|
||||
trying to attach, but unable to.
|
||||
|
||||
for how things sit currently, we have the following (conservative) limits
|
||||
to fit within 9kB:
|
||||
|
||||
16ksps, 4b depth -> 64kbps -> 1.125s
|
||||
8ksps, 4b depth -> 32kbps -> 2.25s
|
||||
6ksps, 4b depth -> 24kbps -> 3.0s
|
||||
4ksps, 4b depth -> 16kbps -> 4.5s
|
||||
|
||||
in reality, the numbers i have seen are a little smaller than this, likely due
|
||||
to compiler optimizations when the dac is unloaded / there's no audio.
|
||||
|
||||
a 16ksps file has an _experimental_ max of 0.75s. applying that offset
|
||||
everywhere, we get empirical limits of:
|
||||
|
||||
16ksps, 4b depth: 0.75s
|
||||
8ksps, 4b depth: 1.875s
|
||||
6ksps, 4b depth: 2.625s
|
||||
4ksps, 4b depth: 4.225s
|
||||
|
||||
## FFMPEG
|
||||
use the following command to convert an input .wav to a raw mono pcm_u8 with 8ksps:
|
||||
```shell
|
||||
ffmpeg -i input.wav -f u8 -c:a pcm_u8 -ar 8000 -ac 1 output.raw
|
||||
```
|
||||
|
||||
|
||||
# PINOUTS
|
||||
LED0: PC3
|
||||
LED1: PD2
|
||||
DAC: PC4
|
||||
COIN SWITCH: PC2
|
||||
BUTTON SWITCH: PD6
|
||||
ADC: PD4
|
||||
GPIO: any remaining pin
|
||||
|
||||
## FORBIDDEN:
|
||||
PD1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue