Skip to content

Commit

Permalink
Merge pull request #3 from nathansbradshaw/feat/allow-user-to-set-buf…
Browse files Browse the repository at this point in the history
…fer-size

Feat/allow user to set buffer size
  • Loading branch information
nathansbradshaw authored Jul 15, 2024
2 parents 1592872 + cac4ba2 commit cec6c1c
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 60 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build_examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Build LibDaisy Examples

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Install target
run: rustup target add thumbv7em-none-eabihf

- name: Cache cargo registry
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Build examples
run: |
excluded_examples="sdmmc usb_midi" # List of examples to exclude
for example in $(ls examples/*.rs); do
example_name=$(basename $example .rs)
if [[ ! " $excluded_examples " =~ " $example_name " ]]; then
cargo build --example $example_name
else
echo "Skipping $example_name"
fi
done
6 changes: 5 additions & 1 deletion examples/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

#[rtic::app(device = stm32h7xx_hal::stm32, peripherals = true)]
mod app {
use libdaisy::{gpio, system, hal::gpio::{Edge, ExtiPin}};
use libdaisy::{
gpio,
hal::gpio::{Edge, ExtiPin},
system,
};

#[shared]
struct SharedResources {}
Expand Down
12 changes: 8 additions & 4 deletions examples/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ mod app {
#[local]
struct Local {
audio: audio::Audio,
buffer: audio::AudioBuffer,
buffer: audio::AudioBuffer<{ audio::BLOCK_SIZE_MAX }>,
sdram: &'static mut [f32],
}

#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let system = system::System::init(ctx.core, ctx.device);
let buffer = [(0.0, 0.0); audio::BLOCK_SIZE_MAX];

let mut core = ctx.core;
let device = ctx.device;
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let system = libdaisy::system_init!(core, device, ccdr);
let buffer: audio::AudioBuffer<{ audio::BLOCK_SIZE_MAX }> = audio::AudioBuffer::new();

info!("Startup done!");

Expand Down Expand Up @@ -57,7 +61,7 @@ mod app {
let index: &mut usize = ctx.local.index;

if audio.get_stereo(buffer) {
for (left, right) in buffer {
for (left, right) in buffer.iter() {
audio
.push_stereo((sdram[*index], sdram[*index + 1]))
.unwrap();
Expand Down
19 changes: 14 additions & 5 deletions examples/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ mod app {
#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let mut system = system::System::init(ctx.core, ctx.device);
let mut core = ctx.core;
let device = ctx.device;
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let system = libdaisy::system_init!(core, device, ccdr);
info!("Startup done!");
let mut timer2 = stm32h7xx_hal::timer::TimerExt::timer(
device.TIM2,
MilliSeconds::from_ticks(100).into_rate(),
ccdr.peripheral.TIM2,
&ccdr.clocks,
);

system
.timer2
.set_freq(MilliSeconds::from_ticks(500).into_rate());
timer2.listen(stm32h7xx_hal::timer::Event::TimeOut);

timer2.set_freq(MilliSeconds::from_ticks(500).into_rate());

let mut flash = system.flash;

Expand Down Expand Up @@ -79,7 +88,7 @@ mod app {
Shared {},
Local {
seed_led: system.gpio.led,
timer2: system.timer2,
timer2,
},
init::Monotonics(),
)
Expand Down
22 changes: 17 additions & 5 deletions examples/hid_blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ mod app {
#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let mut system = system::System::init(ctx.core, ctx.device);
let mut core = ctx.core;
let device = ctx.device;
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let mut system = libdaisy::system_init!(core, device, ccdr);
info!("Startup done!");

system
.timer2
.set_freq(MilliSeconds::from_ticks(1).into_rate());
//TODO check that this timer is setup correctly
let mut timer2 = stm32h7xx_hal::timer::TimerExt::timer(
device.TIM2,
MilliSeconds::from_ticks(100).into_rate(),
ccdr.peripheral.TIM2,
&ccdr.clocks,
);

timer2.listen(stm32h7xx_hal::timer::Event::TimeOut);

timer2.set_freq(MilliSeconds::from_ticks(1).into_rate());

let mut led1 = hid::Led::new(system.gpio.led, false, 1000);
led1.set_brightness(0.5);
Expand All @@ -55,7 +67,7 @@ mod app {
led1,
adc1,
control1,
timer2: system.timer2,
timer2,
},
init::Monotonics(),
)
Expand Down
20 changes: 17 additions & 3 deletions examples/knob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod app {
use libdaisy::logger;
use libdaisy::{gpio::*, hid, prelude::*, system};
use log::info;
use stm32h7xx_hal::time::MilliSeconds;
use stm32h7xx_hal::{adc, stm32, timer::Timer};

#[shared]
Expand All @@ -27,12 +28,25 @@ mod app {
#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let mut system = system::System::init(ctx.core, ctx.device);
let mut core = ctx.core;
let device = ctx.device;
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let mut system = libdaisy::system_init!(core, device, ccdr);
info!("Startup done!");
//TODO check this
let mut timer2 = stm32h7xx_hal::timer::TimerExt::timer(
device.TIM2,
MilliSeconds::from_ticks(100).into_rate(),
ccdr.peripheral.TIM2,
&ccdr.clocks,
);

timer2.listen(stm32h7xx_hal::timer::Event::TimeOut);

let duty_cycle = 50;
let resolution = 20;

system.timer2.set_freq((duty_cycle * resolution).Hz());
timer2.set_freq((duty_cycle * resolution).Hz());

let daisy28 = system
.gpio
Expand Down Expand Up @@ -62,7 +76,7 @@ mod app {
led1,
adc1,
control1,
timer2: system.timer2,
timer2,
},
init::Monotonics(),
)
Expand Down
15 changes: 7 additions & 8 deletions examples/passthru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
peripherals = true,
)]
mod app {
use libdaisy::audio::AudioBuffer;
use libdaisy::logger;
use libdaisy::{gpio, system, audio};
use libdaisy::{audio, system};
use log::info;

#[shared]
Expand All @@ -16,7 +17,7 @@ mod app {
#[local]
struct Local {
audio: audio::Audio,
buffer: audio::AudioBuffer,
buffer: audio::AudioBuffer<{ audio::BLOCK_SIZE_MAX }>,
}

#[init]
Expand All @@ -31,8 +32,7 @@ mod app {
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let system = libdaisy::system_init!(core, device, ccdr);


let buffer = [(0.0, 0.0); audio::BLOCK_SIZE_MAX];
let buffer: AudioBuffer<{ audio::BLOCK_SIZE_MAX }> = AudioBuffer::new();

info!("Startup done!!");

Expand Down Expand Up @@ -62,12 +62,11 @@ mod app {
let buffer = ctx.local.buffer;

if audio.get_stereo(buffer) {

for (left, right) in buffer {
audio.push_stereo((*left, *right));
for (left, right) in buffer.iter() {
let _ = audio.push_stereo((*left, *right));
}
} else {
info!("Error reading data!");
}
}
}
}
19 changes: 14 additions & 5 deletions examples/sdram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,20 @@ mod app {
#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let mut system = system::System::init(ctx.core, ctx.device);
let mut core = ctx.core;
let device = ctx.device;
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let system = libdaisy::system_init!(core, device, ccdr);
info!("Startup done!");
let mut timer2 = stm32h7xx_hal::timer::TimerExt::timer(
device.TIM2,
MilliSeconds::from_ticks(100).into_rate(),
ccdr.peripheral.TIM2,
&ccdr.clocks,
);
timer2.listen(stm32h7xx_hal::timer::Event::TimeOut);

system
.timer2
.set_freq(MilliSeconds::from_ticks(500).into_rate());
timer2.set_freq(MilliSeconds::from_ticks(500).into_rate());

let sdram = system.sdram;

Expand Down Expand Up @@ -72,7 +81,7 @@ mod app {
Shared {},
Local {
seed_led: system.gpio.led,
timer2: system.timer2,
timer2,
},
init::Monotonics(),
)
Expand Down
25 changes: 13 additions & 12 deletions examples/sine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
peripherals = true
)]
mod app {
use libdaisy::{audio, logger, system};
use libdaisy::{audio, gpio::SeedLed, logger, system};
use libm;
use log::info;
use stm32h7xx_hal::time::MilliSeconds;

pub struct AudioRate {
pub audio: audio::Audio,
pub buffer: audio::AudioBuffer,
buffer: audio::AudioBuffer<{ audio::BLOCK_SIZE_MAX }>,
}

#[shared]
Expand All @@ -25,19 +24,18 @@ mod app {
ar: AudioRate,
phase: f32,
pitch: f32,
led: SeedLed,
}

#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
logger::init();
let mut system = system::System::init(ctx.core, ctx.device);
let mut core = ctx.core;
let device = ctx.device;
let ccdr = system::System::init_clocks(device.PWR, device.RCC, &device.SYSCFG);
let system = libdaisy::system_init!(core, device, ccdr);
info!("Startup done!");

system
.timer2
.set_freq(MilliSeconds::from_ticks(500).into_rate());

let buffer = [(0.0, 0.0); audio::BLOCK_SIZE_MAX]; // audio ring buffer
let buffer: audio::AudioBuffer<{ audio::BLOCK_SIZE_MAX }> = audio::AudioBuffer::new();

(
Shared {},
Expand All @@ -48,6 +46,7 @@ mod app {
},
phase: 0.0,
pitch: 440.0,
led: system.gpio.led,
},
init::Monotonics(),
)
Expand All @@ -61,15 +60,16 @@ mod app {
}

// Interrupt handler for audio
#[task(binds = DMA1_STR1, local = [ar, phase, pitch], shared = [], priority = 8)]
#[task(binds = DMA1_STR1, local = [ar, phase, pitch, led], shared = [], priority = 8)]
fn audio_handler(ctx: audio_handler::Context) {
let audio = &mut ctx.local.ar.audio;
let buffer = &mut ctx.local.ar.buffer;
let phase = ctx.local.phase;
let pitch = ctx.local.pitch;
let led = ctx.local.led;

audio.get_stereo(buffer);
for _ in 0..buffer.len() {
for _ in 0..buffer.iter().len() {
// phase is gonna get bigger and bigger
// at some point floating point errors will quantize the pitch
*phase += *pitch / libdaisy::AUDIO_SAMPLE_RATE as f32;
Expand All @@ -78,6 +78,7 @@ mod app {

if *pitch > 10_000.0 {
*pitch = 440.0;
led.toggle();
}

*pitch += 0.1;
Expand Down
Loading

0 comments on commit cec6c1c

Please sign in to comment.