Skip to content

Commit

Permalink
up protocole
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Jul 22, 2024
1 parent 9814e90 commit d5a2343
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 42 deletions.
25 changes: 13 additions & 12 deletions src/api_dio.proto
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
syntax = "proto3";

enum RequestType {
PING = 0;
SET_PIN_DIRECTION = 1;
SET_PIN_VALUE = 2;
GET_PIN_DIRECTION = 3;
GET_PIN_VALUE = 4;
PING = 1;
SET_PIN_DIRECTION = 2;
SET_PIN_VALUE = 3;
GET_PIN_DIRECTION = 4;
GET_PIN_VALUE = 5;
}

enum PinValue {
LOW = 0;
HIGH = 1;
INPUT = 2;
OUTPUT = 3;
LOW = 1;
HIGH = 2;
INPUT = 3;
OUTPUT = 4;
}

message PicohaDioRequest {
Expand All @@ -22,11 +22,12 @@ message PicohaDioRequest {
}

enum AnswerType {
SUCCESS = 0;
FAILURE = 1;
SUCCESS = 1;
FAILURE = 2;
}

message PicohaDioAnswer {
AnswerType type = 1;
optional PinValue value = 3;
optional PinValue value = 2;
optional String error_message = 3;
}
102 changes: 72 additions & 30 deletions src/dio_request_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
};
use core::fmt::Write;

use embedded_hal::digital::OutputPin;
use rp2040_hal::gpio::new_pin;
// Message deserialization support
use femtopb::Message;
Expand Down Expand Up @@ -75,22 +76,54 @@ impl DioRequestProcessor {
.ok();
}

/// Try to decode an API request
///
fn try_to_decode_api_request(frame: &[u8]) -> Option<PicohaDioRequest> {
match PicohaDioRequest::decode(frame) {
Ok(ppp) => {
let mut new_request = PicohaDioRequest::default();
new_request.r#type = ppp.r#type;
new_request.pin_num = ppp.pin_num;
new_request.value = ppp.value;
Some(new_request)
}
Err(e) => {
print_debug_message!(" * error decoding request: {:?}", e);
None
}
}
///
fn set_pin_as_input(&mut self, pin_num: u32) {
self.pins_id[pin_num as usize]
.map(|dyn_id| unsafe {
let pin = new_pin(dyn_id);
pin.try_into_function::<rp2040_hal::gpio::FunctionSioInput>()
.and_then(|pin_in| {
self.pins_i[pin_num as usize] = Some(pin_in);
Ok(())
})
// Ignore the error, just a warning
.map_err(|_| {
print_debug_message!(" * error converting pin {:?} to input", pin_num);
})
.ok();
})
// Ignore the error, just a warning
.ok_or_else(|| {
print_debug_message!(" * pin {:?} not available", pin_num);
})
.ok();
}

fn set_pin_low(&mut self, pin_num: u32) {
self.pins_o[pin_num as usize]
.as_mut()
.map(|pin| {
pin.set_low().unwrap();
})
.ok_or_else(|| {
print_debug_message!(" * pin {:?} not available", pin_num);
})
.ok();
}

///
///
fn set_pin_high(&mut self, pin_num: u32) {
self.pins_o[pin_num as usize]
.as_mut()
.map(|pin| {
pin.set_high().unwrap();
})
.ok_or_else(|| {
print_debug_message!(" * pin {:?} not available", pin_num);
})
.ok();
}

///
Expand All @@ -109,13 +142,13 @@ impl DioRequestProcessor {
self.process_request_set_pin_direction(serial, request)
}
crate::api_dio::RequestType::SetPinValue => {
Self::process_request_set_pin_value(serial, request)
self.process_request_set_pin_value(serial, request)
}
crate::api_dio::RequestType::GetPinDirection => {
Self::process_request_get_pin_direction(serial, request)
self.process_request_get_pin_direction(serial, request)
}
crate::api_dio::RequestType::GetPinValue => {
Self::process_request_get_pin_value(serial, request)
self.process_request_get_pin_value(serial, request)
}
},
femtopb::EnumValue::Unknown(_) => todo!(),
Expand All @@ -141,19 +174,13 @@ impl DioRequestProcessor {
print_debug_message!(b" * processing request: SET_PIN_DIRECTION");

match request.value {
femtopb::EnumValue::Known(v) => {
match v {
crate::api_dio::PinValue::Input => {
//
}
crate::api_dio::PinValue::Output => {
self.set_pin_as_output(request.pin_num);
}
_ => {
print_debug_message!(" * invalid value: {:?}", v);
}
femtopb::EnumValue::Known(v) => match v {
crate::api_dio::PinValue::Input => self.set_pin_as_input(request.pin_num),
crate::api_dio::PinValue::Output => self.set_pin_as_output(request.pin_num),
_ => {
print_debug_message!(" * invalid value: {:?}", v);
}
}
},
femtopb::EnumValue::Unknown(_) => todo!(),
}

Expand All @@ -163,16 +190,30 @@ impl DioRequestProcessor {
}

fn process_request_set_pin_value(
&mut self,
serial: &mut SerialPort<rp2040_hal::usb::UsbBus>,
request: PicohaDioRequest,
) {
print_debug_message!(b" * processing request: SET_PIN_VALUE");

match request.value {
femtopb::EnumValue::Known(v) => match v {
crate::api_dio::PinValue::Low => self.set_pin_low(request.pin_num),
crate::api_dio::PinValue::High => self.set_pin_high(request.pin_num),
_ => {
print_debug_message!(" * invalid value: {:?}", v);
}
},
femtopb::EnumValue::Unknown(_) => todo!(),
}

let mut answer = PicohaDioAnswer::default();
answer.r#type = femtopb::EnumValue::Known(crate::api_dio::AnswerType::Success);
Self::send_answer(serial, answer);
}

fn process_request_get_pin_direction(
&mut self,
serial: &mut SerialPort<rp2040_hal::usb::UsbBus>,
request: PicohaDioRequest,
) {
Expand All @@ -183,6 +224,7 @@ impl DioRequestProcessor {
}

fn process_request_get_pin_value(
&mut self,
serial: &mut SerialPort<rp2040_hal::usb::UsbBus>,
request: PicohaDioRequest,
) {
Expand Down

0 comments on commit d5a2343

Please sign in to comment.