diff --git a/src/api_dio_utils.rs b/src/api_dio_utils.rs new file mode 100644 index 0000000..be70510 --- /dev/null +++ b/src/api_dio_utils.rs @@ -0,0 +1,16 @@ +// Print debug support +use crate::{api_dio::PicohaDioRequest, print_debug_message}; +use core::fmt::{self, Write}; + +// Message deserialization support +use femtopb::Message; + +impl fmt::Debug for PicohaDioRequest<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "PicohaDioRequest {{ r#type: {:?}, pin_num: {:?}, value: {:?} }}", + self.r#type, self.pin_num, self.value + ) + } +} diff --git a/src/app_dio.rs b/src/app_dio.rs index e2efedc..119a3d5 100644 --- a/src/app_dio.rs +++ b/src/app_dio.rs @@ -1,6 +1,7 @@ // Print debug support +use crate::api_dio_utils; use crate::{api_dio::PicohaDioRequest, print_debug_message}; -use core::fmt::Write; +use core::fmt::{self, Write}; // Message deserialization support use femtopb::Message; @@ -22,6 +23,8 @@ pub struct AppDio { } impl AppDio { + /// Create a new instance of the AppDio + /// pub fn new() -> Self { AppDio { in_buf: [0u8; 64], @@ -31,6 +34,7 @@ impl AppDio { } /// Accumulate new data + /// fn accumulate_new_data(&mut self, data: &[u8]) { // Compute indexes let data_len = data.len(); @@ -46,7 +50,7 @@ impl AppDio { /// Try to decode an API request /// - fn try_to_decode_api_request(&mut self, frame: &[u8]) -> Option { + fn try_to_decode_api_request(frame: &[u8]) -> Option { match PicohaDioRequest::decode(frame) { Ok(ppp) => { let mut new_request = PicohaDioRequest::default(); @@ -64,22 +68,37 @@ impl AppDio { fn try_to_decode_buffer(&mut self) -> Option { let mut slip_decoder = serial_line_ip::Decoder::new(); - slip_decoder - .decode(&self.in_buf[..self.in_buf_size], &mut self.decode_buffer) - .map(|(input_bytes_processed, output_slice, is_end_of_packet)| { + match slip_decoder.decode(&self.in_buf[..self.in_buf_size], &mut self.decode_buffer) { + Ok((input_bytes_processed, output_slice, is_end_of_packet)) => { + if is_end_of_packet { + Self::try_to_decode_api_request(output_slice) + } else { + None + } + } + Err(_) => None, + } + } - // match { - // Ok(ppp) => { - // print_debug_message!("deco {:?}", ppp.pin_num); - // Some(ppp) - // } - // Err(e) => { - // print_debug_message!("error deco {:?}", e); - // None - // } - // } - }) - .ok() + /// + /// + fn process_request( + &self, + serial: &mut SerialPort, + request: PicohaDioRequest, + ) { + print_debug_message!("+ processing request: {:?}", request); + + match request.r#type { + femtopb::EnumValue::Known(k) => match k { + crate::api_dio::RequestType::Ping => todo!(), + crate::api_dio::RequestType::SetPinDirection => todo!(), + crate::api_dio::RequestType::SetPinValue => todo!(), + crate::api_dio::RequestType::GetPinDirection => todo!(), + crate::api_dio::RequestType::GetPinValue => todo!(), + }, + femtopb::EnumValue::Unknown(_) => todo!(), + } } /// Process incoming data @@ -91,5 +110,11 @@ impl AppDio { ) { print_debug_message!("+ recieved data: {:?}", data); self.accumulate_new_data(data); + while self.try_to_decode_buffer().is_some() { + if let Some(request) = self.try_to_decode_buffer() { + print_debug_message!("+ decoded request: {:?}", request); + self.process_request(serial, request); + } + } } } diff --git a/src/main.rs b/src/main.rs index f1d541f..75dc728 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use uart_debug::uart_debug_init; use uart_debug::uart_debug_print; // application logic +mod api_dio_utils; mod app_dio; use app_dio::AppDio; @@ -181,29 +182,9 @@ unsafe fn main() -> ! { .build(); // -------------------------------------------------------------- - let mut cmd_buf = [0; 20]; - let mut cmd_buf_size = 0; - - let mut said_hello = false; let mut app = AppDio::new(); loop { - // A welcome message at the beginning - if !said_hello && timer.get_counter().ticks() >= 2_000_000 { - said_hello = true; - let _ = serial.write(b"Hello, World!\r\n"); - - let time = timer.get_counter().ticks(); - let mut text: String<64> = String::new(); - writeln!(&mut text, "Current timer ticks: {}", time).unwrap(); - - // This only works reliably because the number of bytes written to - // the serial port is smaller than the buffers available to the USB - // peripheral. In general, the return value should be handled, so that - // bytes not transferred yet don't get lost. - let _ = serial.write(text.as_bytes()); - } - // Check for new data if usb_dev.poll(&mut [&mut serial]) { let mut buf = [0u8; 512]; @@ -216,38 +197,6 @@ unsafe fn main() -> ! { } Ok(count) => { app.process_incoming_data(&mut serial, &buf[..count]); - - // - let mut slip_decoder = serial_line_ip::Decoder::new(); - let mut decoded_buffer = [0u8; 30]; - match slip_decoder.decode(&cmd_buf[..cmd_buf_size], &mut decoded_buffer) { - Ok((input_bytes_processed, output_slice, is_end_of_packet)) => { - // writeln!( - // &mut message, - // "!!! {:?}, {:?}, {:?}", - // input_bytes_processed, output_slice, is_end_of_packet - // ) - // .unwrap(); - // DEBUG_UART - // .as_ref() - // .unwrap() - // .write_full_blocking(message.as_bytes()); - - match api_dio::PicohaDioRequest::decode(output_slice) { - Ok(ppp) => { - print_debug_message!("deco {:?}", ppp.pin_num); - } - Err(e) => { - // writeln!(&mut message, "error deco {:?}", e).unwrap(); - // DEBUG_UART - // .as_ref() - // .unwrap() - // .write_full_blocking(message.as_bytes()); - } - }; - } - Err(_) => todo!(), - }; } } }