Skip to content

Commit

Permalink
add send method
Browse files Browse the repository at this point in the history
  • Loading branch information
liamkinne committed May 6, 2024
1 parent 3c97206 commit 67466d7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
31 changes: 25 additions & 6 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,33 @@ impl Into<u8> for Request {
}

/// Send data error kind.
#[allow(unused)]
#[derive(Debug, Clone, Copy)]
pub enum SendDataError {
AckTimeout = 0x01,
InvalidChannel = 0x02,
ChannelBusy = 0x03,
ModuleBusy = 0x04,
PayloadInvalid = 0xFF,
/// No ACK received within a time-out after using all MAC retrys.
AckTimeout,
/// Invalid channel selected.
InvalidChannel,
/// Channel is busy.
ChannelBusy,
/// Module is currently busy.
ModuleBusy,
/// Payload too long.
PayloadInvalid,
/// Unrecognised error response.
Other(u8),
}

impl From<u8> for SendDataError {
fn from(value: u8) -> Self {
match value {
0x01 => SendDataError::AckTimeout,
0x02 => SendDataError::InvalidChannel,
0x03 => SendDataError::ChannelBusy,
0x04 => SendDataError::ModuleBusy,
0xFF => SendDataError::PayloadInvalid,
_ => SendDataError::Other(value),
}
}
}

/// Command response.
Expand Down
30 changes: 22 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::task::Poll;

pub use command::{Event, Response};

use command::{command, MAX_PAYLOAD_LEN, START};
use command::{command, Request, SendDataError, MAX_PAYLOAD_LEN, START};
use embedded_io_async::{Read, Write};
use heapless::spsc::{Consumer, Producer, Queue};
use heapless::Vec;
Expand Down Expand Up @@ -75,6 +75,22 @@ where
.await
}

pub async fn send(&mut self, data: &[u8]) -> Result<(), Error<SendDataError, W::Error>> {
let mut buf = [0; 224];
let size = command(&mut buf, Request::SendData, data);
self.serial.write(&buf[..size]).await.map_err(Error::Io)?;

let response = self.poll_response().await;

let status: u8 = response.data[0].into();

if status == 0 {
Ok(())
} else {
Err(Error::Status(status.into()))
}
}

/// Performs a soft-reset of the radio module.
///
/// Returns [`Ok`] once the reset has been confirmed by the device.
Expand All @@ -85,14 +101,12 @@ where

let response = self.poll_response().await;

if let Some(status) = response.data.get(0) {
if *status == 0 {
return Ok(());
} else {
return Err(Error::Status(()));
}
let status = response.data[0];

if status == 0 {
Ok(())
} else {
return Err(Error::Status(()));
Err(Error::Status(()))
}
}

Expand Down

0 comments on commit 67466d7

Please sign in to comment.