initial commit of dac files
This commit is contained in:
parent
d06329a725
commit
8502ed75bf
6 changed files with 54 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "adpcm-pwm-dac"
|
||||||
|
version = "0.1.0"
|
||||||
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "adpcm-pwm-dac"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
33
src/dac.rs
Normal file
33
src/dac.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
use crate::interface::DacInterface;
|
||||||
|
pub struct Dac<'a, T: DacInterface> {
|
||||||
|
output: T,
|
||||||
|
data: Option<&'a [u8]>,
|
||||||
|
index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: DacInterface> Dac<'a, T> {
|
||||||
|
pub fn new(output: T) -> Self {
|
||||||
|
Self {
|
||||||
|
output,
|
||||||
|
data: None,
|
||||||
|
index: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn load_data(&mut self, data: &'a [u8]) {
|
||||||
|
self.data = Some(data);
|
||||||
|
}
|
||||||
|
pub fn seek_to_sample(&mut self, index: usize) {
|
||||||
|
self.index = index;
|
||||||
|
}
|
||||||
|
pub fn output_next(&mut self) {
|
||||||
|
if let Some(data) = self.data {
|
||||||
|
self.index = self.index + 1;
|
||||||
|
// reset the index to 0 if we roll over
|
||||||
|
if (self.index >= data.len()) {
|
||||||
|
self.index = 0;
|
||||||
|
}
|
||||||
|
self.output.write_amplitude(data[self.index]);
|
||||||
|
// self.output.write_amplitude(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/interface.rs
Normal file
4
src/interface.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
pub trait DacInterface {
|
||||||
|
// write the amplitude (0->100%)
|
||||||
|
fn write_amplitude(&mut self, amplitude: u8);
|
||||||
|
}
|
||||||
3
src/lib.rs
Normal file
3
src/lib.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
#![no_std]
|
||||||
|
pub mod interface;
|
||||||
|
pub mod dac;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue