From 58ffeb271006183e8810bb8948a70c7a093cd3ea Mon Sep 17 00:00:00 2001 From: Liam Kinne Date: Thu, 5 Sep 2024 17:49:01 +1000 Subject: [PATCH] add mode set command --- src/bin/telesto/main.rs | 5 ++++- src/command.rs | 10 ++++++++++ src/lib.rs | 22 ++++++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/bin/telesto/main.rs b/src/bin/telesto/main.rs index 49d7f5e..0bd1d86 100644 --- a/src/bin/telesto/main.rs +++ b/src/bin/telesto/main.rs @@ -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 { @@ -42,6 +42,8 @@ enum Commands { DestNet { id: u8 }, /// Destination address. DestAddr { address: u8 }, + /// Operating mode. + Mode { mode: Mode }, } #[tokio::main] @@ -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!(), } diff --git a/src/command.rs b/src/command.rs index 8dcf836..8596e29 100644 --- a/src/command.rs +++ b/src/command.rs @@ -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 { // bit of a hack, but it ensures a single source of truth for the integer -> enum mapping. diff --git a/src/lib.rs b/src/lib.rs index 36fc043..4705312 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(not(test), no_std)] +#![cfg_attr(not(any(test, feature = "cli")), no_std)] mod command; mod setting; @@ -6,7 +6,7 @@ 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}; @@ -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 { poll_fn(|cx| {