Skip to content

Commit

Permalink
Use patched esp-hal until new release
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyGrondin committed Sep 17, 2024
1 parent d981e92 commit 8bc4edd
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
4 changes: 2 additions & 2 deletions esp-hal-buzzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ esp32s2 = ["esp-backtrace/esp32s2", "esp-hal/esp32s2", "esp-println/esp32s2"]
## Target the ESP32-S3.
esp32s3 = ["esp-backtrace/esp32s3", "esp-hal/esp32s3", "esp-println/esp32s3"]

# Patch until https://github.com/esp-rs/esp-hal/pull/1984 is merged
# Patch until next esp-hal release
[patch.crates-io]
esp-hal = { git = "https://github.com/AnthonyGrondin/esp-hal", rev = "00248bc" }
esp-hal = { git = "https://github.com/esp-rs/esp-hal", rev = "a787a13" }
5 changes: 2 additions & 3 deletions esp-hal-buzzer/examples/buzzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@ use esp_println::println;

#[entry]
fn main() -> ! {
let (peripherals, clocks) = esp_hal::init(esp_hal::Config::default());
let peripherals = esp_hal::init(esp_hal::Config::default());

let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);

let mut ledc = Ledc::new(peripherals.LEDC, &clocks);
let mut ledc = Ledc::new(peripherals.LEDC);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);

let mut buzzer = Buzzer::new(
&ledc,
timer::Number::Timer0,
channel::Number::Channel1,
io.pins.gpio6,
&clocks,
);

buzzer.play_song(DOOM).unwrap();
Expand Down
26 changes: 11 additions & 15 deletions esp-hal-buzzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ use core::{fmt::Debug, ops::DerefMut};
use esp_hal::{
clock::Clocks,
delay::Delay,
gpio::{AnyPin, CreateErasedPin, InputPin, Level, Output, OutputPin},
gpio::{AnyPin, Level, Output, OutputPin, Pin},
ledc::{
channel::{self, Channel, ChannelIFace},
timer::{self, Timer, TimerHW, TimerIFace, TimerSpeed},
Ledc,
LowSpeed,
Ledc, LowSpeed,
},
peripheral::{Peripheral, PeripheralRef},
};
Expand Down Expand Up @@ -110,9 +109,9 @@ pub enum VolumeType {
}

/// Volume configuration for the buzzer
struct Volume<'a> {
struct Volume {
/// Output pin for the volume
volume_pin: AnyPin<'a>,
volume_pin: AnyPin,

/// Type of the volume
volume_type: VolumeType,
Expand All @@ -130,8 +129,7 @@ pub struct Buzzer<'a, S: TimerSpeed, O: OutputPin> {
channel_number: channel::Number,
output_pin: PeripheralRef<'a, O>,
delay: Delay,
volume: Option<Volume<'a>>,
clocks: &'a Clocks<'a>,
volume: Option<Volume>,
}

impl<'a, S: TimerSpeed, O: OutputPin + Peripheral<P = O>> Buzzer<'a, S, O>
Expand All @@ -146,27 +144,25 @@ where
timer_number: timer::Number,
channel_number: channel::Number,
output_pin: impl Peripheral<P = O> + 'a,
clocks: &'a Clocks,
) -> Self {
let timer = ledc.get_timer(timer_number);
Self {
timer,
channel_number,
output_pin: output_pin.into_ref(),
delay: Delay::new(clocks),
volume: None::<Volume<'a>>,
clocks,
delay: Delay::new(),
volume: None::<Volume>,
}
}

/// Add a volume control for the buzzer.
pub fn with_volume<V: OutputPin + InputPin + CreateErasedPin>(
pub fn with_volume<V>(
mut self,
volume_pin: impl Peripheral<P = V> + 'a,
volume_pin: impl Peripheral<P = V> + Pin + 'a,
volume_type: VolumeType,
) -> Self {
self.volume = Some(Volume {
volume_pin: AnyPin::new(volume_pin),
volume_pin: volume_pin.degrade(),
volume_type,
level: 50,
});
Expand Down Expand Up @@ -256,7 +252,7 @@ where
// Max duty resolution for a frequency:
// Integer(log2(LEDC_APB_CKL / frequency))
let mut result = 0;
let mut value = (self.clocks.apb_clock / frequency).raw();
let mut value = (Clocks::get().apb_clock / frequency).raw();

// Limit duty resolution to 14 bits
while value > 1 && result < 14 {
Expand Down

0 comments on commit 8bc4edd

Please sign in to comment.