-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from peterkrull/refactor
PR containing many things
- Loading branch information
Showing
11 changed files
with
933 additions
and
596 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use num_enum::TryFromPrimitive; | ||
|
||
/// Represents all CRSF packet addresses | ||
#[non_exhaustive] | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, TryFromPrimitive)] | ||
#[repr(u8)] | ||
pub enum PacketAddress { | ||
Broadcast = 0x00, | ||
Usb = 0x10, | ||
Bluetooth = 0x12, | ||
TbsCorePnpPro = 0x80, | ||
Reserved1 = 0x8A, | ||
CurrentSensor = 0xC0, | ||
Gps = 0xC2, | ||
TbsBlackbox = 0xC4, | ||
FlightController = 0xC8, | ||
Reserved2 = 0xCA, | ||
RaceTag = 0xCC, | ||
Handset = 0xEA, | ||
Receiver = 0xEC, | ||
Transmitter = 0xEE, | ||
} | ||
|
||
bitflags::bitflags! { | ||
pub(crate) struct PacketAddressFlags: u16 { | ||
const BROADCAST = 1; | ||
const USB = 1 << 1; | ||
const BLUETOOTH = 1 << 2; | ||
const TBS_CORE_PNP_PRO = 1 << 3; | ||
const RESERVED1 = 1 << 4; | ||
const CURRENT_SENSOR = 1 << 5; | ||
const GPS = 1 << 6; | ||
const TBS_BLACKBOX = 1 << 7; | ||
const FLIGHT_CONTROLLER = 1 << 8; | ||
const RESERVED2 = 1 << 9; | ||
const RACE_TAG = 1 << 10; | ||
const HANDSET = 1 << 11; | ||
const RECEIVER = 1 << 12; | ||
const TRANSMITTER = 1 << 13; | ||
} | ||
} | ||
|
||
impl PacketAddressFlags { | ||
pub(crate) fn from_address(address: PacketAddress) -> Self { | ||
use PacketAddress::*; | ||
|
||
match address { | ||
Broadcast => PacketAddressFlags::BROADCAST, | ||
Usb => PacketAddressFlags::USB, | ||
Bluetooth => PacketAddressFlags::BLUETOOTH, | ||
TbsCorePnpPro => PacketAddressFlags::TBS_CORE_PNP_PRO, | ||
Reserved1 => PacketAddressFlags::RESERVED1, | ||
CurrentSensor => PacketAddressFlags::CURRENT_SENSOR, | ||
Gps => PacketAddressFlags::GPS, | ||
TbsBlackbox => PacketAddressFlags::TBS_BLACKBOX, | ||
FlightController => PacketAddressFlags::FLIGHT_CONTROLLER, | ||
Reserved2 => PacketAddressFlags::RESERVED2, | ||
RaceTag => PacketAddressFlags::RACE_TAG, | ||
Handset => PacketAddressFlags::HANDSET, | ||
Receiver => PacketAddressFlags::RECEIVER, | ||
Transmitter => PacketAddressFlags::TRANSMITTER, | ||
} | ||
} | ||
|
||
pub(crate) fn contains_u8(&self, value: u8) -> bool { | ||
PacketAddress::try_from(value) | ||
.map_or(false, |address| self.contains(Self::from_address(address))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/// Create a new look-up table for the CRC8 algorithm. | ||
pub const fn new_crc8_lut() -> [u8; 256] { | ||
let mut crc_table = [0u8; 256]; | ||
|
||
let mut i = 0; | ||
while i < 256 { | ||
let mut crc = i as u8; | ||
let mut j = 0; | ||
while j < 8 { | ||
if crc & 0x80 != 0 { | ||
crc = (crc << 1) ^ 0xd5; | ||
} else { | ||
crc <<= 1; | ||
} | ||
j += 1; | ||
} | ||
crc_table[i] = crc; | ||
i += 1; | ||
} | ||
|
||
crc_table | ||
} | ||
|
||
const CRC8_LUT: [u8; 256] = new_crc8_lut(); | ||
|
||
/// Software based CRC8 implementation. | ||
#[derive(Debug)] | ||
pub struct Crc8 { | ||
crc_val: u8, | ||
crc_table: &'static [u8; 256], | ||
} | ||
|
||
impl Crc8 { | ||
pub const fn new() -> Self { | ||
Crc8 { | ||
crc_table: &CRC8_LUT, | ||
crc_val: 0, | ||
} | ||
} | ||
|
||
pub fn compute(&mut self, data: &[u8]) { | ||
for e in data { | ||
self.crc_val = self.crc_table[(self.crc_val ^ e) as usize]; | ||
} | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.crc_val = 0; | ||
} | ||
|
||
pub fn get_checksum(&self) -> u8 { | ||
self.crc_val | ||
} | ||
} |
Oops, something went wrong.