3131#![ deny( missing_docs) ]
3232#![ no_std]
3333
34- use core:: { fmt:: Debug , ops :: DerefMut } ;
34+ use core:: fmt:: Debug ;
3535
3636use 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
4949pub 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