mavwire 0.1.0

no_std, no_alloc parser and encoder for MAVLink v1/v2 telemetry messages
Documentation
//! `RC_CHANNELS` (message id 65): raw RC receiver channel values.

use super::payload::{u8_at, u16_at, u32_at};

/// This message's id.
pub const ID: u32 = 65;
/// This message's `crc_extra` byte (see [`crate::crc`]).
pub const CRC_EXTRA: u8 = 118;
const LEN: usize = 42;
const CHANNEL_COUNT: usize = 18;

/// A decoded `RC_CHANNELS` message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RcChannels {
    /// Timestamp, milliseconds since system boot.
    pub time_boot_ms: u32,
    /// Raw channel values, microseconds (`0` for channel 1..`chancount`
    /// means "not received"; `u16::MAX` marks a channel beyond
    /// `chancount` as unused, per `MAVLink`'s own convention - represented
    /// here as a fixed 18-element array rather than 18 separate named
    /// fields, unlike the wire definition's `chan1_raw`..`chan18_raw`).
    pub channels: [u16; CHANNEL_COUNT],
    /// Total number of RC channels received.
    pub chancount: u8,
    /// Receive signal strength: `0` = weakest, `254` = maximum, `255` if
    /// unknown.
    pub rssi: u8,
}

impl RcChannels {
    pub(crate) fn decode(payload: &[u8]) -> Self {
        let mut channels = [0u16; CHANNEL_COUNT];
        for (i, ch) in channels.iter_mut().enumerate() {
            *ch = u16_at(payload, 4 + i * 2);
        }
        Self {
            time_boot_ms: u32_at(payload, 0),
            channels,
            chancount: u8_at(payload, 4 + CHANNEL_COUNT * 2),
            rssi: u8_at(payload, 5 + CHANNEL_COUNT * 2),
        }
    }

    pub(crate) fn encode(&self) -> [u8; LEN] {
        let mut buf = [0u8; LEN];
        buf[0..4].copy_from_slice(&self.time_boot_ms.to_le_bytes());
        for (i, &ch) in self.channels.iter().enumerate() {
            buf[4 + i * 2..6 + i * 2].copy_from_slice(&ch.to_le_bytes());
        }
        buf[4 + CHANNEL_COUNT * 2] = self.chancount;
        buf[5 + CHANNEL_COUNT * 2] = self.rssi;
        buf
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn decodes_a_real_captured_frame() {
        // Real wire payload from pymavlink: time_boot_ms=5000 chancount=8
        // chan1..8 = 1500,1500,1000,1500,1000,1000,1000,1000, chan9..18=0,
        // rssi=200.
        let hex =
            "88130000dc05dc05e803dc05e803e803e803e803000000000000000000000000000000000000000008c8";
        let mut payload = [0u8; LEN];
        for (i, chunk) in hex.as_bytes().chunks(2).enumerate() {
            payload[i] = u8::from_str_radix(core::str::from_utf8(chunk).unwrap(), 16).unwrap();
        }
        let rc = RcChannels::decode(&payload);
        assert_eq!(rc.time_boot_ms, 5000);
        assert_eq!(
            rc.channels[..8],
            [1500, 1500, 1000, 1500, 1000, 1000, 1000, 1000]
        );
        assert_eq!(rc.channels[8..], [0; 10]);
        assert_eq!(rc.chancount, 8);
        assert_eq!(rc.rssi, 200);
    }

    #[test]
    fn round_trips_through_decode() {
        let mut channels = [0u16; CHANNEL_COUNT];
        for (i, ch) in channels.iter_mut().enumerate() {
            *ch = 1000 + u16::try_from(i).unwrap();
        }
        let rc = RcChannels {
            time_boot_ms: 123,
            channels,
            chancount: 18,
            rssi: 255,
        };
        assert_eq!(RcChannels::decode(&rc.encode()), rc);
    }
}