Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Optionally parse sync (address) byte #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/tact1m4n3/crsf-rs"
documentation = "https://docs.rs/crsf"

[dependencies]
bitflags = "2.5.0"
crc = "3.2"
defmt = { version = "0.3.6", optional = true }
num_enum = { version = "0.7.2", default-features = false }
Expand Down
10 changes: 5 additions & 5 deletions examples/local_serial.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{env, io, time::Duration};

use crsf::{Packet, PacketReader};
use crsf::{Packet, PacketPayload, PacketReader};

fn main() {
let path = env::args().nth(1).expect("no serial port supplied");
Expand All @@ -10,19 +10,19 @@ fn main() {
.expect("failed to open serial port");

let mut buf = [0; 1024];
let mut reader = PacketReader::new();
let mut reader = PacketReader::default();
loop {
match port.read(buf.as_mut_slice()) {
Ok(n) => {
if n > 0 {
let mut remaining = &buf[..n];
while let (Some(raw_packet), consumed) = reader.push_bytes(remaining) {
match Packet::parse(raw_packet) {
Ok(packet) => match packet {
Packet::LinkStatistics(link_statistics) => {
Ok(packet) => match packet.payload {
PacketPayload::LinkStatistics(link_statistics) => {
println!("{:?}", link_statistics);
}
Packet::RcChannels(channels) => {
PacketPayload::RcChannels(channels) => {
println!("{:?}", channels);
}
_ => {}
Expand Down
40 changes: 40 additions & 0 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pub(crate) struct Buf<const C: usize> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this not almost just a heapless::Vec, except with a predefined type?

buf: [u8; C],
len: usize,
}

impl<const C: usize> Buf<C> {
pub(crate) const fn new() -> Self {
Self {
buf: [0; C],
len: 0,
}
}

pub(crate) fn len(&self) -> usize {
self.len
}

pub(crate) fn push(&mut self, c: u8) -> bool {
if let Some(v) = self.buf.get_mut(self.len) {
*v = c;
self.len += 1;
true
} else {
false
}
}

pub(crate) fn push_bytes(&mut self, data: &[u8]) {
self.buf[self.len..self.len + data.len()].copy_from_slice(data);
self.len += data.len();
}

pub(crate) fn data(&self) -> &[u8; C] {
&self.buf
}

pub(crate) fn clear(&mut self) {
self.len = 0;
}
}
Loading