Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Jul 21, 2024
1 parent 30b5192 commit 0cdb9ae
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 69 deletions.
16 changes: 16 additions & 0 deletions src/api_dio_utils.rs
Original file line number Diff line number Diff line change
@@ -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
)
}
}
59 changes: 42 additions & 17 deletions src/app_dio.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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],
Expand All @@ -31,6 +34,7 @@ impl AppDio {
}

/// Accumulate new data
///
fn accumulate_new_data(&mut self, data: &[u8]) {
// Compute indexes
let data_len = data.len();
Expand All @@ -46,7 +50,7 @@ impl AppDio {

/// Try to decode an API request
///
fn try_to_decode_api_request(&mut self, frame: &[u8]) -> Option<PicohaDioRequest> {
fn try_to_decode_api_request(frame: &[u8]) -> Option<PicohaDioRequest> {
match PicohaDioRequest::decode(frame) {
Ok(ppp) => {
let mut new_request = PicohaDioRequest::default();
Expand All @@ -64,22 +68,37 @@ impl AppDio {
fn try_to_decode_buffer(&mut self) -> Option<PicohaDioRequest> {
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<rp2040_hal::usb::UsbBus>,
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
Expand All @@ -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);
}
}
}
}
53 changes: 1 addition & 52 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
Expand All @@ -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!(),
};
}
}
}
Expand Down

0 comments on commit 0cdb9ae

Please sign in to comment.