Skip to content

Commit b91f156

Browse files
feat(esp-hal-buzzer): Update to esp-hal 1.0.0-beta.0 (#19)
* feat(esp-hal-buzzer): Update to esp-hal 1.0.0-beta.0 * Add CHANGELOG.md entry
1 parent 3bac74b commit b91f156

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

esp-hal-buzzer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313

1414
### Fixed
15+
- Upgrade esp-hal to 1.0.0-beta.0 (#19)
1516

1617
### Removed
18+
- **Breaking Change:** Generic for Buzzer has been removed in favour of AnyPin (#19)
1719

1820
## 0.1.0 - 2024-08-21 - Initial release

esp-hal-buzzer/Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ features = ["esp32c3"]
1212
targets = ["riscv32imc-unknown-none-elf"]
1313

1414
[dependencies]
15-
defmt = { version = "0.3.8", optional = true }
16-
document-features = "0.2.10"
17-
esp-hal = "0.23.1"
18-
fugit = "0.3.7"
15+
defmt = { version = "0.3.10", optional = true }
16+
document-features = "0.2.11"
17+
esp-hal = { version = "1.0.0-beta.0", features = ["unstable"] }
1918

2019
[dev-dependencies]
21-
esp-backtrace = { version = "0.15.0", features = [
20+
esp-backtrace = { version = "0.15.1", features = [
2221
"exception-handler",
2322
"panic-handler",
2423
"println",
2524
] }
26-
esp-println = "0.13.0"
25+
esp-println = "0.13.1"
2726

2827
[features]
2928
## Implement `defmt::Format` on certain types.

esp-hal-buzzer/src/lib.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,20 @@
3131
#![deny(missing_docs)]
3232
#![no_std]
3333

34-
use core::{fmt::Debug, ops::DerefMut};
34+
use core::fmt::Debug;
3535

3636
use esp_hal::{
3737
clock::Clocks,
3838
delay::Delay,
39-
gpio::{AnyPin, Level, Output, OutputPin, Pin},
39+
gpio::{AnyPin, Level, Output, OutputConfig, OutputPin},
4040
ledc::{
4141
channel::{self, Channel, ChannelIFace},
4242
timer::{self, Timer, TimerIFace},
4343
Ledc, LowSpeed,
4444
},
45-
peripheral::{Peripheral, PeripheralRef},
45+
peripheral::Peripheral,
46+
time::Rate,
4647
};
47-
use fugit::RateExtU32;
4848

4949
pub mod notes;
5050

@@ -124,27 +124,27 @@ struct Volume {
124124
}
125125

126126
/// A buzzer instance driven by Ledc
127-
pub struct Buzzer<'a, O: OutputPin> {
127+
pub struct Buzzer<'a> {
128128
timer: Timer<'a, LowSpeed>,
129129
channel_number: channel::Number,
130-
output_pin: PeripheralRef<'a, O>,
130+
output_pin: AnyPin,
131131
delay: Delay,
132132
volume: Option<Volume>,
133133
}
134134

135-
impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
135+
impl<'a> Buzzer<'a> {
136136
/// Create a new buzzer for the given pin
137-
pub fn new(
137+
pub fn new<O>(
138138
ledc: &'a Ledc,
139139
timer_number: timer::Number,
140140
channel_number: channel::Number,
141-
output_pin: impl Peripheral<P = O> + 'a,
141+
output_pin: impl Peripheral<P = O> + OutputPin + 'a,
142142
) -> Self {
143143
let timer = ledc.timer(timer_number);
144144
Self {
145145
timer,
146146
channel_number,
147-
output_pin: output_pin.into_ref(),
147+
output_pin: output_pin.degrade(),
148148
delay: Delay::new(),
149149
volume: None::<Volume>,
150150
}
@@ -153,7 +153,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
153153
/// Add a volume control for the buzzer.
154154
pub fn with_volume<V>(
155155
mut self,
156-
volume_pin: impl Peripheral<P = V> + Pin + 'a,
156+
volume_pin: impl Peripheral<P = V> + OutputPin + 'a,
157157
volume_type: VolumeType,
158158
) -> Self {
159159
self.volume = Some(Volume {
@@ -178,6 +178,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
178178
Output::new(
179179
unsafe { volume.volume_pin.clone_unchecked() },
180180
if level != 0 { Level::High } else { Level::Low },
181+
OutputConfig::default(),
181182
);
182183
Ok(())
183184
}
@@ -191,7 +192,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
191192
self.timer.configure(timer::config::Config {
192193
duty: timer::config::Duty::Duty11Bit,
193194
clock_source: timer::LSClockSource::APBClk,
194-
frequency: 20_000.Hz(),
195+
frequency: Rate::from_hz(20_000),
195196
})?;
196197
}
197198

@@ -211,6 +212,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
211212
Output::new(
212213
unsafe { volume.volume_pin.clone_unchecked() },
213214
Level::High,
215+
OutputConfig::default(),
214216
);
215217
Ok(())
216218
}
@@ -227,7 +229,9 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
227229
///
228230
/// The muting is done by simply setting the duty to 0
229231
pub fn mute(&mut self) -> Result<(), Error> {
230-
let mut channel = Channel::new(self.channel_number, self.output_pin.deref_mut());
232+
let mut channel = Channel::new(self.channel_number, unsafe {
233+
self.output_pin.clone_unchecked()
234+
});
231235
channel
232236
.configure(channel::config::Config {
233237
timer: &self.timer,
@@ -247,7 +251,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
247251
// Max duty resolution for a frequency:
248252
// Integer(log2(LEDC_APB_CKL / frequency))
249253
let mut result = 0;
250-
let mut value = (Clocks::get().apb_clock / frequency).raw();
254+
let mut value = Clocks::get().apb_clock / Rate::from_hz(frequency);
251255

252256
// Limit duty resolution to 14 bits
253257
while value > 1 && result < 14 {
@@ -259,10 +263,12 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
259263
// Safety: This should never fail because resolution is limited to 14 bits
260264
duty: timer::config::Duty::try_from(result).unwrap(),
261265
clock_source: timer::LSClockSource::APBClk,
262-
frequency: frequency.Hz(),
266+
frequency: Rate::from_hz(frequency),
263267
})?;
264268

265-
let mut channel = Channel::new(self.channel_number, self.output_pin.deref_mut());
269+
let mut channel = Channel::new(self.channel_number, unsafe {
270+
self.output_pin.clone_unchecked()
271+
});
266272
channel.configure(channel::config::Config {
267273
timer: &self.timer,
268274
// Use volume as duty if set since we use the same channel.

0 commit comments

Comments
 (0)