cu_bdshot/
messages.rs

1use bincode::{Decode, Encode};
2use serde::{Deserialize, Serialize};
3
4/// Telemetry payload decoded from a DSHOT ESC.
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
6pub enum DShotTelemetry {
7    EncodingError,
8    Erpm(u16),
9    Temp(u8),
10    Voltage(u8),
11    Amps(u8),
12    Debug1(u8),
13    Debug2(u8),
14    Debug3(u8),
15    Event(u8),
16}
17
18impl defmt::Format for DShotTelemetry {
19    fn format(&self, fmt: defmt::Formatter) {
20        match self {
21            Self::EncodingError => defmt::write!(fmt, "Encoding Error"),
22            Self::Erpm(v) => defmt::write!(fmt, "eRPM: {}", v),
23            Self::Amps(v) => defmt::write!(fmt, "Amps: {}", v),
24            Self::Temp(v) => defmt::write!(fmt, "Temp: {}", v),
25            Self::Voltage(v) => defmt::write!(fmt, "Voltage: {}", v),
26            Self::Debug1(v) => defmt::write!(fmt, "Debug 1: {}", v),
27            Self::Debug2(v) => defmt::write!(fmt, "Debug 2: {}", v),
28            Self::Debug3(v) => defmt::write!(fmt, "Debug 3: {}", v),
29            Self::Event(v) => defmt::write!(fmt, "Event: {}", v),
30        }
31    }
32}
33
34/// Command sent from Copper into the ESC bridge.
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
36pub struct EscCommand {
37    /// Raw throttle value (0 - 2047). Values >= 48 arm the ESC per DSHOT spec.
38    pub throttle: u16,
39    /// Whether the bridge should request telemetry for this frame.
40    pub request_telemetry: bool,
41}
42
43impl Default for EscCommand {
44    fn default() -> Self {
45        Self {
46            throttle: 0,
47            request_telemetry: true,
48        }
49    }
50}
51
52impl EscCommand {
53    pub fn disarm() -> Self {
54        Self::default()
55    }
56}
57
58/// Telemetry sample received from the ESC bridge.
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
60pub struct EscTelemetry {
61    pub sample: Option<DShotTelemetry>,
62}