embedded_uart_config/
embedded_uart_config.rs1#![allow(dead_code)]
5
6extern crate at_parser_rs;
7
8use at_parser_rs::{Args, AtError, AtResult};
9use at_parser_rs::context::AtContext;
10
11struct DummyUart {
13 baudrate: u32,
14 mode: u8,
15}
16
17impl DummyUart {
18 const fn new() -> Self {
19 Self { baudrate: 9600, mode: 0 }
20 }
21
22 fn write(&self, data: &str) {
23 let _ = data;
25 }
26}
27
28impl AtContext for DummyUart {
29 fn exec(&self) -> AtResult<'static> {
30 Err(AtError::NotSupported)
32 }
33 fn set(&mut self, args: Args) -> AtResult<'static> {
34 if let Some(data) = args.get(0) {
35 self.write(data);
36 Ok("SENT")
37 } else {
38 Err(AtError::InvalidArgs)
39 }
40 }
41}
42
43struct ConfigContext;
45
46impl AtContext for ConfigContext {
47 fn exec(&self) -> AtResult<'static> {
48 Err(AtError::NotSupported)
50 }
51
52 fn query(&mut self) -> AtResult<'static> {
53 Ok("115200,1")
56 }
57
58 fn test(&mut self) -> AtResult<'static> {
59 Ok("+SETCFG: (baudrate),(mode)")
61 }
62
63 fn set(&mut self, args: Args) -> AtResult<'static> {
64 let baud = args.get(0).and_then(|s| s.parse::<u32>().ok());
65 let mode = args.get(1).and_then(|s| s.parse::<u8>().ok());
66 match (baud, mode) {
67 (Some(b), Some(m)) => unsafe {
68 UART.baudrate = b;
70 UART.mode = m;
71 Ok("CONFIGURED")
72 },
73 _ => Err(AtError::InvalidArgs),
74 }
75 }
76}
77
78static mut UART: DummyUart = DummyUart { baudrate: 9600, mode: 0 };
79static mut CONFIG_CTX: ConfigContext = ConfigContext;
80
81fn example_with_parser() -> Result<&'static str, AtError> {
83 Ok("OK")
86}
87
88fn example_with_commands_macro() -> Result<&'static str, AtError> {
90 Ok("OK")
93}
94
95fn main() {
97 let _result = example_with_commands_macro();
99}