Skip to content

Commit

Permalink
- OpenSCAD updates
Browse files Browse the repository at this point in the history
- Code updates to 2024-03
- Extracted set_divider function
  • Loading branch information
zarath committed Mar 30, 2024
1 parent 0f55ecb commit 7701b1c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 55 deletions.
23 changes: 12 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
edition = "2021"
name = "subtone"
version = "0.3.0"
version = "0.4.0"
license = "MIT"
resolver = "2"

Expand All @@ -15,19 +15,20 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }

embassy-embedded-hal = { version ="0.1.0", features = ["defmt"] }

embassy-executor = { version = "0.4.0", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-executor = { version = "0.5.0", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
embassy-futures = { version = "0.1.1", features = ["defmt"] }
embassy-sync = { version = "0.5.0", features = ["defmt"] }
embassy-time = { version = "0.2.0", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-time = { version = "0.3.0", features = ["defmt", "defmt-timestamp-uptime"] }
embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl"] }

ssd1306 = "0.8.0"
fixed = "1.23.1"
pio-proc = "0.2"
pio = "0.2.1"
static_cell = "1.3.0"
static_cell = "2.0.0"
portable-atomic = { version = "1.6.0", features = ["critical-section"] }
embedded-graphics = "0.8.1"
display-interface = "0.4.1"
display-interface = "0.5.0"

rp2040-boot2 = "0.3.0"

Expand All @@ -37,12 +38,12 @@ tinybmp = "0.5.0"
pdm = {version = "0.1.0", git = "https://github.com/zarath/embedded-libs" }

[patch.crates-io]
embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" }
embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "ca2e3759ad31f511c239d30132a28714b0e402db" }
embassy-embedded-hal = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" }
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" }
embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" }
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "f3a0bcb15eb7f7d1b2f9d948f8d487b76a4308fe" }

# cargo build/run
[profile.dev]
Expand Down
13 changes: 12 additions & 1 deletion OpenSCAD/case.scad
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,19 @@ module knob()
cylinder(h = 12.0, d1 = 2.4, d2 = 2.0);
}
}
for (w = [45.0:90.0:315.0])
{
rotate([0.0, 0.0, w])
translate([4.0, 0.0, 3.5])
cube([2.0, 0.6, 7], center=true);
}
for (w = [45.0:90.0:135.0])
{
rotate([0.0, 0.0, w])
translate([3.8, 0.0, 3.5])
cube([2.6, 0.6, 7], center=true);
}
}
// baseplate();
// case ();
knob();
// pipe(h=20, do=20, di=16);
Binary file removed OpenSCAD/case.stl
Binary file not shown.
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Before upgrading check that everything is available on all tier1 targets here:
# https://rust-lang.github.io/rustup-components-history
[toolchain]
channel = "nightly-2023-12-19"
channel = "nightly-2024-03-26"
components = [ "rust-src", "rustfmt" ]
targets = [
"thumbv6m-none-eabi",
Expand Down
19 changes: 6 additions & 13 deletions src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use defmt::info;
use embassy_futures::join::join;
use embassy_futures::select::{select, Either};
use embassy_rp::gpio::{Flex, Pin, Pull};
use embassy_rp::Peripheral;
use embassy_rp::gpio::Input;
use embassy_time::{Duration, Timer};

pub enum EncoderDirection {
Expand All @@ -12,20 +11,14 @@ pub enum EncoderDirection {

pub static DELAY_DEFAULT: Duration = Duration::from_millis(5);

pub struct Encoder<'d, T: Pin, V: Pin> {
pin_a: Flex<'d, T>,
pin_b: Flex<'d, V>,
pub struct Encoder<'d> {
pin_a: Input<'d>,
pin_b: Input<'d>,
}

impl<'d, T: Pin, V: Pin> Encoder<'d, T, V> {
impl<'d> Encoder<'d> {
#[inline]
pub fn new(pin_a: impl Peripheral<P = T> + 'd, pin_b: impl Peripheral<P = V> + 'd) -> Self {
let mut pin_a = Flex::new(pin_a);
let mut pin_b = Flex::new(pin_b);
pin_a.set_as_input();
pin_a.set_pull(Pull::Up);
pin_b.set_as_input();
pin_b.set_pull(Pull::Up);
pub fn new(pin_a: Input<'static>, pin_b: Input<'static>) -> Self {
Self { pin_a, pin_b }
}

Expand Down
78 changes: 49 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

#[allow(static_mut_refs)]
use core::mem::size_of;
use core::ptr::addr_of_mut;

use defmt::info;
use embassy_executor::Executor;
Expand All @@ -13,8 +14,8 @@ use embassy_rp::{
gpio::{Input, Pull},
i2c::{Config as I2cConfig, I2c},
multicore::{spawn_core1, Stack},
peripherals::{DMA_CH0, DMA_CH1, FLASH, I2C1, PIN_2, PIN_26, PIN_3, PIN_4, PIO0},
pio::{Config as PioConfig, FifoJoin, InterruptHandler, Pio, ShiftDirection},
peripherals::{DMA_CH0, DMA_CH1, FLASH, I2C1, PIN_26, PIO0},
pio::{Config as PioConfig, FifoJoin, InterruptHandler, Pio, ShiftDirection, StateMachine},
rom_data::{float_funcs::fdiv, reset_to_usb_boot},
Peripheral,
};
Expand Down Expand Up @@ -88,8 +89,8 @@ static CHANNEL: Channel<CriticalSectionRawMutex, Message, 1> = Channel::new();

mod encoder;

type Encoder = encoder::Encoder<'static, PIN_2, PIN_4>;
type Button = Input<'static, PIN_3>;
type Encoder = encoder::Encoder<'static>;
type Button = Input<'static>;
type I2cChan1 = embassy_rp::i2c::I2c<'static, I2C1, embassy_rp::i2c::Blocking>;

type Display = Ssd1306<
Expand Down Expand Up @@ -118,11 +119,11 @@ fn write_config(flash: &mut Flash<'_, FLASH, Async, FLASH_SIZE>, cfg: Config) {
if read_config(flash) == cfg {
return;
}
// info!("Flashing!");
defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32));
let buf: &[u8] = unsafe {
core::slice::from_raw_parts(&cfg as *const Config as *const u8, size_of::<Config>())
};
info!("Flashing!");
defmt::unwrap!(flash.blocking_erase(ADDR_OFFSET, ADDR_OFFSET + ERASE_SIZE as u32));
flash.blocking_write(ADDR_OFFSET, buf).unwrap();
}

Expand Down Expand Up @@ -173,7 +174,7 @@ fn main() -> ! {
let p = embassy_rp::init(Default::default());

// info!("Init Input");
let enc = encoder::Encoder::new(p.PIN_2, p.PIN_4);
let enc = encoder::Encoder::new(Input::new(p.PIN_2, Pull::Up), Input::new(p.PIN_4, Pull::Up));
let button = Input::new(p.PIN_3, Pull::Up);

// info!("Set up I2c");
Expand All @@ -183,14 +184,18 @@ fn main() -> ! {
i2c_config.frequency = 400_000;
let i2c = I2c::new_blocking(p.I2C1, scl, sda, i2c_config);

spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| {
spawner
.spawn(core1_task(p.PIN_26, p.PIO0, p.DMA_CH0))
.unwrap()
});
});
spawn_core1(
p.CORE1,
unsafe { &mut *addr_of_mut!(CORE1_STACK) },
move || {
let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| {
spawner
.spawn(core1_task(p.PIN_26, p.PIO0, p.DMA_CH0))
.unwrap()
});
},
);

let executor0 = EXECUTOR0.init(Executor::new());
executor0.run(|spawner| {
Expand Down Expand Up @@ -309,20 +314,35 @@ async fn core1_task(pdm_pin: PIN_26, pio_0: PIO0, dma_0: DMA_CH0) {
let mut dma_out_ref = dma_0.into_ref();

loop {
match select(
CHANNEL.receive(),
sm.tx().dma_push(dma_out_ref.reborrow(), &PDM_TABLE.0),
)
.await
{
Either::First(Message { freq, enabled }) => {
info!("Got Message: {} {}", freq, enabled);
sm.set_enable(false);
cfg.clock_divider = freq_2_divider(freq);
sm.set_config(&cfg);
sm.set_enable(enabled);
if enabled {
match select(
CHANNEL.receive(),
sm.tx().dma_push(dma_out_ref.reborrow(), &PDM_TABLE.0),
)
.await
{
Either::First(Message { freq, enabled }) => {
set_divider(&mut sm, &mut cfg, freq, enabled);
}
Either::Second(_) => (),
}
Either::Second(_) => (),
} else {
let Message { freq, enabled } = CHANNEL.receive().await;
set_divider(&mut sm, &mut cfg, freq, enabled);
}
}
}

#[inline]
fn set_divider<'d>(
sm: &mut StateMachine<'d, PIO0, 0>,
cfg: &mut PioConfig<'d, PIO0>,
freq: f32,
enabled: bool,
) {
info!("Set freq: {}", freq);
sm.set_enable(false);
cfg.clock_divider = freq_2_divider(freq);
sm.set_config(&cfg);
sm.set_enable(enabled);
}

0 comments on commit 7701b1c

Please sign in to comment.