Skip to content

Commit

Permalink
move on with protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Jul 6, 2024
1 parent e7aa764 commit 7bc12ae
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ fn main() {
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");

femtopb_build::compile_protos(&["src/api.proto"], &["src"]).unwrap();
femtopb_build::compile_protos_into(&["src/api.proto"], &["src"], "src").unwrap();
}
33 changes: 29 additions & 4 deletions src/api.proto
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
syntax = "proto3";

message SearchRequest {
uint32 query = 1;
uint32 page_number = 2;
uint32 results_per_page = 3;
enum RequestType {
PING = 0;
SET_PIN_DIRECTION = 1;
SET_PIN_VALUE = 2;
GET_PIN_DIRECTION = 3;
GET_PIN_VALUE = 4;
}

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

message PicohaDioRequest {
RequestType type = 1;
uint32 pin_num = 2;
PinValue value = 3;
}

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

message PicohaDioAnswer {
AnswerType type = 1;
optional PinValue value = 3;
}
151 changes: 151 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::femtopb::Message)]
pub struct PicohaDioRequest<'a> {
#[femtopb(enumeration, tag = 1)]
pub r#type: ::femtopb::enumeration::EnumValue<RequestType>,
#[femtopb(uint32, tag = 2)]
pub pin_num: u32,
#[femtopb(enumeration, tag = 3)]
pub value: ::femtopb::enumeration::EnumValue<PinValue>,
#[femtopb(unknown_fields)]
pub unknown_fields: femtopb::UnknownFields<'a>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::femtopb::Message)]
pub struct PicohaDioAnswer<'a> {
#[femtopb(enumeration, tag = 1)]
pub r#type: ::femtopb::enumeration::EnumValue<AnswerType>,
#[femtopb(enumeration, optional, tag = 3)]
pub value: ::core::option::Option<::femtopb::enumeration::EnumValue<PinValue>>,
#[femtopb(unknown_fields)]
pub unknown_fields: femtopb::UnknownFields<'a>,
}
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
::femtopb::Enumeration
)]
#[repr(i32)]
#[derive(Default)]
pub enum RequestType {
#[default]
Ping = 0,
SetPinDirection = 1,
SetPinValue = 2,
GetPinDirection = 3,
GetPinValue = 4,
}
impl RequestType {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
RequestType::Ping => "PING",
RequestType::SetPinDirection => "SET_PIN_DIRECTION",
RequestType::SetPinValue => "SET_PIN_VALUE",
RequestType::GetPinDirection => "GET_PIN_DIRECTION",
RequestType::GetPinValue => "GET_PIN_VALUE",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"PING" => Some(Self::Ping),
"SET_PIN_DIRECTION" => Some(Self::SetPinDirection),
"SET_PIN_VALUE" => Some(Self::SetPinValue),
"GET_PIN_DIRECTION" => Some(Self::GetPinDirection),
"GET_PIN_VALUE" => Some(Self::GetPinValue),
_ => None,
}
}
}
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
::femtopb::Enumeration
)]
#[repr(i32)]
#[derive(Default)]
pub enum PinValue {
#[default]
Low = 0,
High = 1,
Input = 2,
Output = 3,
}
impl PinValue {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
PinValue::Low => "LOW",
PinValue::High => "HIGH",
PinValue::Input => "INPUT",
PinValue::Output => "OUTPUT",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"LOW" => Some(Self::Low),
"HIGH" => Some(Self::High),
"INPUT" => Some(Self::Input),
"OUTPUT" => Some(Self::Output),
_ => None,
}
}
}
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
Hash,
PartialOrd,
Ord,
::femtopb::Enumeration
)]
#[repr(i32)]
#[derive(Default)]
pub enum AnswerType {
#[default]
Success = 0,
Failure = 1,
}
impl AnswerType {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
AnswerType::Success => "SUCCESS",
AnswerType::Failure => "FAILURE",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"SUCCESS" => Some(Self::Success),
"FAILURE" => Some(Self::Failure),
_ => None,
}
}
}
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bsp::entry;
use defmt::*;
use defmt_rtt as _;
use embedded_hal::digital::OutputPin;
use femtopb::Message;
use panic_probe as _;

use rp2040_hal::pio::PIOExt;
Expand Down Expand Up @@ -39,8 +40,7 @@ use bsp::hal::{

use rp_pico::hal::gpio::{FunctionPio0, Pin};

// use no_proto::error::NP_Error;
// use no_proto::NP_Factory;
mod api;

use serial_line_ip;

Expand All @@ -52,6 +52,16 @@ fn main() -> ! {
let mut watchdog = Watchdog::new(pac.WATCHDOG);
let sio = Sio::new(pac.SIO);

let mes = api::PicohaDioRequest {
r#type: femtopb::EnumValue::Known(api::RequestType::Ping),
pin_num: 0,
value: femtopb::EnumValue::Known(api::PinValue::Low),
unknown_fields: Default::default(),
};

// let mut cursor = femtopb::Cursor::new();
// let rr = mes.encoded_len()

// let user_factory = NP_Factory::new(
// r#"
// struct({ fields: {
Expand Down

0 comments on commit 7bc12ae

Please sign in to comment.