From cf7ab17d2094f77e63f11e3b2bbf826a1785b455 Mon Sep 17 00:00:00 2001 From: BradenEverson Date: Sat, 12 Oct 2024 12:01:22 -0500 Subject: [PATCH 1/2] Add tokio feature --- rplidar-rppal/Cargo.toml | 5 +++++ rplidar-rppal/src/rplidar_a1.rs | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/rplidar-rppal/Cargo.toml b/rplidar-rppal/Cargo.toml index e7b9a12..fbe38d9 100644 --- a/rplidar-rppal/Cargo.toml +++ b/rplidar-rppal/Cargo.toml @@ -12,3 +12,8 @@ categories = ["embedded", "hardware-support"] [dependencies] rppal = "0.19.0" +tokio = { version = "1.40.0", features = ["full"], optional = true} + +[features] +default = ["tokio"] +tokio = ["dep:tokio"] diff --git a/rplidar-rppal/src/rplidar_a1.rs b/rplidar-rppal/src/rplidar_a1.rs index 61de439..3c395fa 100644 --- a/rplidar-rppal/src/rplidar_a1.rs +++ b/rplidar-rppal/src/rplidar_a1.rs @@ -4,6 +4,8 @@ use std::time::Duration; use rppal::{gpio::OutputPin, uart::Uart}; + + /// An RpLidar A1 Device pub struct RpLidarA1 { /// The UART channel connected to the RpLidar @@ -71,4 +73,9 @@ impl RpLidarA1 { self.motor.set_low(); self.send_command(Command::Stop); } + + #[cfg(feature = "tokio")] + pub fn run_with_channel(&mut self) -> tokio::sync::mpsc::Receiver<([u8; 255], u8)> { + todo!() + } } From 516e5d4c4c7bff4d2df804329d186b4d19913fba Mon Sep 17 00:00:00 2001 From: BradenEverson Date: Sun, 13 Oct 2024 16:34:21 -0500 Subject: [PATCH 2/2] Implement rplidar handle return --- rplidar-rppal/examples/hello_lidar.rs | 4 ++-- rplidar-rppal/src/rplidar_a1.rs | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/rplidar-rppal/examples/hello_lidar.rs b/rplidar-rppal/examples/hello_lidar.rs index 441ee21..ccf2da7 100644 --- a/rplidar-rppal/examples/hello_lidar.rs +++ b/rplidar-rppal/examples/hello_lidar.rs @@ -20,12 +20,12 @@ fn main() { uart.set_read_mode(255, Duration::from_millis(500)) .expect("Failed to set timeout"); - let lidar = RpLidarA1::new(uart, motor_pin); + let mut lidar = RpLidarA1::new(uart, motor_pin); lidar.run_with_callback(parse_scan_data); } /// Scans the lidar data and prints the readings to the console -pub fn parse_scan_data(data: &[u8]) { +pub fn parse_scan_data(data: [u8; 255], _read: u8) { if data.len() < 5 { return; } diff --git a/rplidar-rppal/src/rplidar_a1.rs b/rplidar-rppal/src/rplidar_a1.rs index 3c395fa..b0985d7 100644 --- a/rplidar-rppal/src/rplidar_a1.rs +++ b/rplidar-rppal/src/rplidar_a1.rs @@ -4,8 +4,6 @@ use std::time::Duration; use rppal::{gpio::OutputPin, uart::Uart}; - - /// An RpLidar A1 Device pub struct RpLidarA1 { /// The UART channel connected to the RpLidar @@ -54,7 +52,7 @@ impl RpLidarA1 { /// Continously reads lidar data until a failure happens. Pipes the data through to a /// `response_callback` function that does something to a `&[u8]` allowing fine-grained control /// over data response - pub fn run_with_callback(mut self, response_callback: FN) { + pub fn run_with_callback(&mut self, response_callback: FN) { self.motor.set_high(); self.send_command(Command::Reset); @@ -65,7 +63,7 @@ impl RpLidarA1 { let mut buffer: [u8; 255] = [0; 255]; loop { match self.uart.read(&mut buffer) { - Ok(bytes_read) if bytes_read > 0 => response_callback(&buffer), + Ok(bytes_read) if bytes_read > 0 => response_callback(buffer, bytes_read as u8), _ => break, } } @@ -75,7 +73,16 @@ impl RpLidarA1 { } #[cfg(feature = "tokio")] - pub fn run_with_channel(&mut self) -> tokio::sync::mpsc::Receiver<([u8; 255], u8)> { - todo!() + pub fn run_with_channel(&mut self) -> tokio::sync::mpsc::UnboundedReceiver<([u8; 255], u8)> { + let (writer, reader) = tokio::sync::mpsc::unbounded_channel(); + + let handler = move |buf: [u8; 255], read: u8| { + let writer = writer.clone(); + writer.send((buf, read)).expect("Failed to send"); + }; + + self.run_with_callback(handler); + + reader } }