Skip to main content

rmk_types/
battery.rs

1//! Battery status types.
2
3use postcard::experimental::max_size::MaxSize;
4use serde::{Deserialize, Serialize};
5
6/// Charge state of the battery.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, MaxSize)]
8#[cfg_attr(feature = "defmt", derive(defmt::Format))]
9#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
10#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
11pub enum ChargeState {
12    Charging,
13    Discharging,
14    Unknown,
15}
16
17impl From<bool> for ChargeState {
18    /// `true` = Charging, `false` = Discharging.
19    fn from(charging: bool) -> Self {
20        if charging {
21            ChargeState::Charging
22        } else {
23            ChargeState::Discharging
24        }
25    }
26}
27
28/// Battery status used for both status queries and event notifications.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, MaxSize)]
30#[cfg_attr(feature = "defmt", derive(defmt::Format))]
31#[cfg_attr(feature = "wasm", derive(tsify::Tsify))]
32#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
33pub enum BatteryStatus {
34    Unavailable,
35    Available {
36        charge_state: ChargeState,
37        level: Option<u8>,
38    },
39}
40
41impl BatteryStatus {
42    pub fn is_available(&self) -> bool {
43        matches!(self, BatteryStatus::Available { .. })
44    }
45}