Skip to content

Commit

Permalink
add mode set command
Browse files Browse the repository at this point in the history
  • Loading branch information
liamkinne committed Sep 5, 2024
1 parent c6af893 commit 58ffeb2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/bin/telesto/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ptr::addr_of_mut;
use clap::{Parser, Subcommand};
use heapless::spsc::Queue;
use tokio_serial::SerialPortBuilderExt;
use wurth_telesto::{Event, Radio};
use wurth_telesto::{Event, Mode, Radio};

#[derive(Parser)]
pub struct Cli {
Expand Down Expand Up @@ -42,6 +42,8 @@ enum Commands {
DestNet { id: u8 },
/// Destination address.
DestAddr { address: u8 },
/// Operating mode.
Mode { mode: Mode },
}

#[tokio::main]
Expand Down Expand Up @@ -106,6 +108,7 @@ async fn main() {
Commands::Channel { channel } => radio.channel(channel).await.unwrap(),
Commands::DestNet { id } => radio.destination_net(id).await.unwrap(),
Commands::DestAddr { address } => radio.destination_address(address).await.unwrap(),
Commands::Mode { mode } => radio.mode(mode).await.unwrap(),
_ => todo!(),
}

Expand Down
10 changes: 10 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ pub enum Event {
PacketTransmit,
}

/// Operating mode.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
pub enum Mode {
/// Transparent mode.
Transparent,
/// Command mode.
Command,
}

impl Event {
pub fn try_from_raw(raw: u8) -> Option<Self> {
// bit of a hack, but it ensures a single source of truth for the integer -> enum mapping.
Expand Down
22 changes: 20 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(not(any(test, feature = "cli")), no_std)]

mod command;
mod setting;

use core::future::poll_fn;
use core::task::Poll;

pub use command::{Event, Response};
pub use command::{Event, Mode, Response};

use command::{command, Request, SendDataError, MAX_PAYLOAD_LEN, START};
use embedded_io_async::{Read, Write};
Expand Down Expand Up @@ -225,6 +225,24 @@ where
}
}

/// Set operating mode.
///
/// The mode change is performed after the achnoledge response is transmitted.
pub async fn mode(&mut self, mode: Mode) -> Result<(), Error<(), W::Error>> {
let mut buf = [0; 224];
let size = command(&mut buf, command::Request::SetMode, &[mode as u8]);
self.serial.write(&buf[..size]).await.map_err(Error::Io)?;

let response = self.poll_response().await;
let status = response.data[0];

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

/// Poll until a response frame is received through the response channel.
async fn poll_response(&mut self) -> Frame<Response> {
poll_fn(|cx| {
Expand Down

0 comments on commit 58ffeb2

Please sign in to comment.