@@ -61,6 +61,9 @@ pub enum Error {
6161
6262 /// When the volume level is out of range. Either too low or too high.
6363 VolumeOutOfRange ,
64+
65+ /// Sequence and timings slice aren't of the same length
66+ LengthMismatch ,
6467}
6568
6669/// Converts [channel::Error] into [self::Error]
@@ -325,6 +328,55 @@ impl<'a> Buzzer<'a> {
325328 Ok ( ( ) )
326329 }
327330
331+ /// Play a sound sequence through the buzzer
332+ ///
333+ /// Uses a pair of frequency and duration slices to play a sound sequence.
334+ /// Both slices must be of the same length, where each pair of `(frequency, duration)` defines one tone.
335+ ///
336+ /// # Arguments
337+ /// * `sequence` - A slice of frequencies to play through the buzzer
338+ /// * `timings` - A slice of durations in milliseconds for each frequency
339+ ///
340+ /// # Examples
341+ /// Play a single beep at 300Hz for 1 second
342+ /// ```
343+ /// buzzer.play_tones_from_slice(&[300], &[1000]);
344+ /// ```
345+ ///
346+ /// Play a sequence of 3 beeps with a break in between
347+ /// ```
348+ /// buzzer.play_tones_from_slice(&[200, 0, 200, 0, 200], &[200, 50, 200, 50, 200]);
349+ /// ```
350+ ///
351+ /// Play a sequence of 3 beeps with the same duration
352+ /// ```
353+ /// buzzer.play_tones_from_slice(&[100, 200, 300], &[100; 3]);
354+ /// ```
355+ ///
356+ /// # Errors
357+ /// This function returns an [Error] in the following cases:
358+ /// - If the `sequence` and `timings` slices have different lengths ([Error::LengthMismatch])
359+ /// - If playing a frequency results in an error
360+ pub fn play_tones_from_slice (
361+ & mut self ,
362+ sequence : & [ u32 ] ,
363+ timings : & [ u32 ] ,
364+ ) -> Result < ( ) , Error > {
365+ if sequence. len ( ) != timings. len ( ) {
366+ return Err ( Error :: LengthMismatch ) ;
367+ }
368+
369+ // Iterate for each frequency / timing pair
370+ for ( frequency, timing) in sequence. iter ( ) . zip ( timings. iter ( ) ) {
371+ self . play ( * frequency) ?;
372+ self . delay . delay_millis ( * timing) ;
373+ self . mute ( ) ;
374+ }
375+ // Mute at the end of the sequence
376+ self . mute ( ) ;
377+ Ok ( ( ) )
378+ }
379+
328380 /// Play a tone sequence through the buzzer
329381 ///
330382 /// Uses a pair of frequencies and timings to play a sound sequence.
@@ -349,19 +401,20 @@ impl<'a> Buzzer<'a> {
349401 /// duration: 100,
350402 /// },
351403 /// ];
352- /// buzzer.play_song(song);
404+ /// buzzer.play_song(& song);
353405 /// ```
354406 ///
355407 /// # Errors
356408 /// This function returns an [Error] in case of an error.
357409 /// An error can occur when an invalid value is used as a tone
358- pub fn play_song < const T : usize > ( & mut self , tones : [ ToneValue ; T ] ) -> Result < ( ) , Error > {
359- let mut sequence: [ u32 ; T ] = [ 0 ; T ] ;
360- let mut timings: [ u32 ; T ] = [ 0 ; T ] ;
361- for ( index, tone) in tones. iter ( ) . enumerate ( ) {
362- sequence[ index] = tone. frequency ;
363- timings[ index] = tone. duration ;
410+ pub fn play_song ( & mut self , tones : & [ ToneValue ] ) -> Result < ( ) , Error > {
411+ for tone in tones {
412+ self . play ( tone. frequency ) ?;
413+ self . delay . delay_millis ( tone. duration ) ;
414+ self . mute ( ) ;
364415 }
365- self . play_tones ( sequence, timings)
416+ // Mute at the end of the sequence
417+ self . mute ( ) ;
418+ Ok ( ( ) )
366419 }
367420}
0 commit comments