Skip to content

Commit

Permalink
added all crsf packet types and addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
tact1m4n3 committed Apr 24, 2024
1 parent 593e9eb commit ed6f51d
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
//!
//! let mut reader = PacketReader::new();
//!
//! let addr = PacketAddress::Controller;
//! let addr = PacketAddress::FlightController;
//! let typ = PacketType::RcChannelsPacked;
//! ```
//! ### Packet Construction
//! ```rust
//! use crsf::{Packet, RcChannels, PacketAddress};
//!
//! let addr = PacketAddress::Controller;
//! let addr = PacketAddress::FlightController;
//! let channels: [u16; 16] = [0xffff; 16];
//! let packet = Packet::RcChannels(RcChannels(channels));
//!
Expand Down Expand Up @@ -115,6 +115,12 @@ impl PacketReader {
}
}

impl Default for PacketReader {
fn default() -> Self {
Self::new()
}
}

/// Represents a raw packet (not parsed)
#[derive(Clone, Copy, Debug)]
pub struct RawPacket<'a> {
Expand Down Expand Up @@ -245,10 +251,20 @@ pub struct BufferLenError {
#[derive(Clone, Copy, Debug, PartialEq, Eq, TryFromPrimitive)]
#[repr(u8)]
pub enum PacketAddress {
Transmitter = 0xEE,
Broadcast = 0x00,
Usb = 0x10,
Bluetooth = 0x12,
TbsCorePnpPro = 0x80,
Reserved1 = 0x8A,
CurrentSensor = 0xC0,
Gps = 0xC2,
TbsBlackbox = 0xC4,
FlightController = 0xC8,
Reserved2 = 0xCA,
RaceTag = 0xCC,
Handset = 0xEA,
Controller = 0xC8,
Receiver = 0xEC,
Transmitter = 0xEE,
}

/// Represents all CRSF packet types
Expand All @@ -260,18 +276,22 @@ pub enum PacketType {
Vario = 0x07,
BatterySensor = 0x08,
BaroAltitude = 0x09,
Heartbeat = 0x0B,
LinkStatistics = 0x14,
OpenTxSync = 0x10,
RadioId = 0x3A,
RcChannelsPacked = 0x16,
Altitude = 0x1E,
SubsetRcChannelsPacked = 0x17,
LinkRxId = 0x1C,
LinkTxId = 0x1D,
Attitude = 0x1E,
FlightMode = 0x21,
DevicePing = 0x28,
DeviceInfo = 0x29,
ParameterSettingsEntry = 0x2B,
ParameterRead = 0x2C,
ParameterWrite = 0x2D,
ElrsStatus = 0x2E,
Command = 0x32,
RadioId = 0x3A,
KissRequest = 0x78,
KissResponse = 0x79,
MspRequest = 0x7A,
Expand Down Expand Up @@ -363,7 +383,7 @@ mod tests {
fn test_parse_next_packet() {
let mut reader = PacketReader::new();

let addr = PacketAddress::Controller;
let addr = PacketAddress::FlightController;
let typ = PacketType::RcChannelsPacked;

// Sync
Expand All @@ -385,7 +405,7 @@ mod tests {
fn test_parse_full_packet() {
let mut reader = PacketReader::new();

let addr = PacketAddress::Controller;
let addr = PacketAddress::FlightController;
let typ = PacketType::RcChannelsPacked;

let data = [
Expand All @@ -409,7 +429,7 @@ mod tests {
fn test_parse_next_packet_with_validation_error() {
let mut reader = PacketReader::new();

let addr = PacketAddress::Controller;
let addr = PacketAddress::FlightController;

// Sync
reader.push_bytes(&[addr as u8]);
Expand Down Expand Up @@ -450,7 +470,7 @@ mod tests {

let mut buf = [0u8; 10];
assert_eq!(
packet.dump(&mut buf, PacketAddress::Controller),
packet.dump(&mut buf, PacketAddress::FlightController),
Err(BufferLenError {
expected: 14,
actual: 10
Expand Down Expand Up @@ -489,7 +509,7 @@ mod tests {
});

let mut buf = [0u8; Packet::MAX_LENGTH];
let len = packet.dump(&mut buf, PacketAddress::Controller).unwrap();
let len = packet.dump(&mut buf, PacketAddress::FlightController).unwrap();
let expected_data = [0xc8, 12, 0x14, 16, 19, 99, 151, 1, 2, 3, 8, 88, 148, 252];
assert_eq!(&buf[..len], &expected_data)
}
Expand Down

0 comments on commit ed6f51d

Please sign in to comment.