31
31
#![ deny( missing_docs) ]
32
32
#![ no_std]
33
33
34
- use core:: { fmt:: Debug , ops :: DerefMut } ;
34
+ use core:: fmt:: Debug ;
35
35
36
36
use esp_hal:: {
37
37
clock:: Clocks ,
38
38
delay:: Delay ,
39
- gpio:: { AnyPin , Level , Output , OutputPin , Pin } ,
39
+ gpio:: { AnyPin , Level , Output , OutputConfig , OutputPin } ,
40
40
ledc:: {
41
41
channel:: { self , Channel , ChannelIFace } ,
42
42
timer:: { self , Timer , TimerIFace } ,
43
43
Ledc , LowSpeed ,
44
44
} ,
45
- peripheral:: { Peripheral , PeripheralRef } ,
45
+ peripheral:: Peripheral ,
46
+ time:: Rate ,
46
47
} ;
47
- use fugit:: RateExtU32 ;
48
48
49
49
pub mod notes;
50
50
@@ -124,27 +124,27 @@ struct Volume {
124
124
}
125
125
126
126
/// A buzzer instance driven by Ledc
127
- pub struct Buzzer < ' a , O : OutputPin > {
127
+ pub struct Buzzer < ' a > {
128
128
timer : Timer < ' a , LowSpeed > ,
129
129
channel_number : channel:: Number ,
130
- output_pin : PeripheralRef < ' a , O > ,
130
+ output_pin : AnyPin ,
131
131
delay : Delay ,
132
132
volume : Option < Volume > ,
133
133
}
134
134
135
- impl < ' a , O : OutputPin + Peripheral < P = O > > Buzzer < ' a , O > {
135
+ impl < ' a > Buzzer < ' a > {
136
136
/// Create a new buzzer for the given pin
137
- pub fn new (
137
+ pub fn new < O > (
138
138
ledc : & ' a Ledc ,
139
139
timer_number : timer:: Number ,
140
140
channel_number : channel:: Number ,
141
- output_pin : impl Peripheral < P = O > + ' a ,
141
+ output_pin : impl Peripheral < P = O > + OutputPin + ' a ,
142
142
) -> Self {
143
143
let timer = ledc. timer ( timer_number) ;
144
144
Self {
145
145
timer,
146
146
channel_number,
147
- output_pin : output_pin. into_ref ( ) ,
147
+ output_pin : output_pin. degrade ( ) ,
148
148
delay : Delay :: new ( ) ,
149
149
volume : None :: < Volume > ,
150
150
}
@@ -153,7 +153,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
153
153
/// Add a volume control for the buzzer.
154
154
pub fn with_volume < V > (
155
155
mut self ,
156
- volume_pin : impl Peripheral < P = V > + Pin + ' a ,
156
+ volume_pin : impl Peripheral < P = V > + OutputPin + ' a ,
157
157
volume_type : VolumeType ,
158
158
) -> Self {
159
159
self . volume = Some ( Volume {
@@ -178,6 +178,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
178
178
Output :: new (
179
179
unsafe { volume. volume_pin . clone_unchecked ( ) } ,
180
180
if level != 0 { Level :: High } else { Level :: Low } ,
181
+ OutputConfig :: default ( ) ,
181
182
) ;
182
183
Ok ( ( ) )
183
184
}
@@ -191,7 +192,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
191
192
self . timer . configure ( timer:: config:: Config {
192
193
duty : timer:: config:: Duty :: Duty11Bit ,
193
194
clock_source : timer:: LSClockSource :: APBClk ,
194
- frequency : 20_000 . Hz ( ) ,
195
+ frequency : Rate :: from_hz ( 20_000 ) ,
195
196
} ) ?;
196
197
}
197
198
@@ -211,6 +212,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
211
212
Output :: new (
212
213
unsafe { volume. volume_pin . clone_unchecked ( ) } ,
213
214
Level :: High ,
215
+ OutputConfig :: default ( ) ,
214
216
) ;
215
217
Ok ( ( ) )
216
218
}
@@ -227,7 +229,9 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
227
229
///
228
230
/// The muting is done by simply setting the duty to 0
229
231
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
+ } ) ;
231
235
channel
232
236
. configure ( channel:: config:: Config {
233
237
timer : & self . timer ,
@@ -247,7 +251,7 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
247
251
// Max duty resolution for a frequency:
248
252
// Integer(log2(LEDC_APB_CKL / frequency))
249
253
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 ) ;
251
255
252
256
// Limit duty resolution to 14 bits
253
257
while value > 1 && result < 14 {
@@ -259,10 +263,12 @@ impl<'a, O: OutputPin + Peripheral<P = O>> Buzzer<'a, O> {
259
263
// Safety: This should never fail because resolution is limited to 14 bits
260
264
duty : timer:: config:: Duty :: try_from ( result) . unwrap ( ) ,
261
265
clock_source : timer:: LSClockSource :: APBClk ,
262
- frequency : frequency . Hz ( ) ,
266
+ frequency : Rate :: from_hz ( frequency ) ,
263
267
} ) ?;
264
268
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
+ } ) ;
266
272
channel. configure ( channel:: config:: Config {
267
273
timer : & self . timer ,
268
274
// Use volume as duty if set since we use the same channel.
0 commit comments