kernel/hil/buzzer.rs
1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Interface for buzzer use.
6
7use crate::ErrorCode;
8
9pub trait BuzzerClient {
10 /// Called when the current sound played by the buzzer has finished
11 /// or it was stopped.
12 fn buzzer_done(&self, status: Result<(), ErrorCode>);
13}
14
15/// The Buzzer HIL is used to play a sound on a buzzer at a fixed frequency and
16/// for a certain duration.
17pub trait Buzzer<'a> {
18 /// Play a sound at a chosen frequency and for a chosen duration.
19 /// Once the buzzer finishes buzzing, the `buzzer_done()` callback
20 /// is called.
21 /// If it is called while the buzzer is playing, the buzzer command will be
22 /// overridden with the new frequency and duration values.
23 ///
24 /// Return values:
25 ///
26 /// - `Ok(())`: The attempt at starting the buzzer was successful.
27 /// - `FAIL`: Cannot start the buzzer.
28 fn buzz(&self, frequency_hz: usize, duration_ms: usize) -> Result<(), ErrorCode>;
29
30 /// Stop the sound currently playing.
31 /// After the buzzer is successfully stopped, the `buzzer_done()`
32 /// callback is called.
33 ///
34 /// Return values:
35 ///
36 /// - `Ok(())`: The attempt at stopping the buzzer was successful.
37 /// - `FAIL`: Cannot stop the buzzer.
38 /// - `OFF`: The buzzer wasn't playing a sound when the stop command was called.
39 fn stop(&self) -> Result<(), ErrorCode>;
40
41 /// Set the client to be used for callbacks of the Buzzer
42 /// implementation.
43 fn set_client(&self, client: &'a dyn BuzzerClient);
44}