Skip to content

Commit

Permalink
Merge _dispatch_response into reader
Browse files Browse the repository at this point in the history
  • Loading branch information
lukipuki committed Nov 10, 2024
1 parent a8388e3 commit 3967319
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
40 changes: 22 additions & 18 deletions nrf52840/src/at_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ static CHANNEL: Channel<ThreadModeRawMutex, Result<String<AT_COMMAND_SIZE>, Erro
Channel::new();

#[embassy_executor::task]
async fn reader(mut rx: UarteRxWithIdle<'static, UARTE1, TIMER0>) {
async fn reader(
mut rx: UarteRxWithIdle<'static, UARTE1, TIMER0>,
callback_dispatcher: fn(&str, &str) -> bool,
) {
const AT_BUF_SIZE: usize = 300;
let mut buf = [0; AT_BUF_SIZE];
loop {
Expand All @@ -35,9 +38,23 @@ async fn reader(mut rx: UarteRxWithIdle<'static, UARTE1, TIMER0>) {
Ok(len) => {
let lines = split_lines(&buf[..len]).unwrap();
for line in lines {
CHANNEL
.send(String::from_str(line).map_err(|_| Error::StringEncodingError))
.await;
let mut is_callback = false;
if line.starts_with('+') {
let prefix_len = line.find(": ");
if let Some(prefix_len) = prefix_len {
let prefix = &line[1..prefix_len];
let rest = &line[prefix_len..];
if (callback_dispatcher)(prefix, rest) {
is_callback = true;
}
}
}

if !is_callback {
CHANNEL
.send(String::from_str(line).map_err(|_| Error::StringEncodingError))
.await;
}
}
CHANNEL.send(Ok(String::new())).await; // Stop transmission
}
Expand All @@ -52,7 +69,7 @@ impl AtUart {
callback_dispatcher: fn(&str, &str) -> bool,
spawner: &Spawner,
) -> Self {
unwrap!(spawner.spawn(reader(rx)));
unwrap!(spawner.spawn(reader(rx, callback_dispatcher)));
Self {
tx,
callback_dispatcher,
Expand Down Expand Up @@ -87,19 +104,6 @@ impl AtUart {

self.read(timeout).await
}

async fn _dispatch_response(self, line: &str) {
if line.starts_with('+') {
let prefix_len = line.find(": ");
if let Some(prefix_len) = prefix_len {
let prefix = &line[1..prefix_len];
let rest = &line[prefix_len..];
if !(self.callback_dispatcher)(prefix, rest) {
// TODO: forward
}
}
}
}
}

fn readline(s: &str) -> (Option<&str>, &str) {
Expand Down
2 changes: 1 addition & 1 deletion nrf52840/src/bg77.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use embassy_time::{Duration, Timer};
use heapless::format;

static MINIMUM_TIMEOUT: Duration = Duration::from_millis(300);
const CLIENT_ID: u32 = 1;
const CLIENT_ID: u32 = 0;

pub struct BG77 {
uart1: AtUart,
Expand Down

0 comments on commit 3967319

Please sign in to comment.