From e976718c8324d133bf55015a54c343aaa28bc6ef Mon Sep 17 00:00:00 2001 From: Noah Sprenger <69169402+NoahSprenger@users.noreply.github.com> Date: Wed, 2 Oct 2024 20:01:54 -0400 Subject: [PATCH] Update boards, timestamp, and sender to node. (#2) * Update boards, timestamp, and sender to node. * Remove proptest. * Update attributes. * :( --- Cargo.toml | 3 ++- messages-proc-macros-lib/src/lib.rs | 6 +++--- src/command.rs | 4 ++-- src/lib.rs | 21 ++++++++++---------- src/node.rs | 19 ++++++++++++++++++ src/sender.rs | 30 ----------------------------- 6 files changed, 36 insertions(+), 47 deletions(-) create mode 100644 src/node.rs delete mode 100644 src/sender.rs diff --git a/Cargo.toml b/Cargo.toml index 0aa4c36..a1ff90d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ bitflags = { version = "2.3.1", features = ["serde"] } proptest = { version = "1.2.0", optional = true } proptest-derive = { version = "0.3.0", optional = true } messages-proc-macros-lib = { path = "messages-proc-macros-lib" } +chrono = {version = "0.4.0", features = ["serde", "arbitrary"], default-features = false} [dev-dependencies] proptest = "1.2.0" @@ -26,4 +27,4 @@ postcard = { version = "1.0.4", features = ["alloc"] } [features] default = ["mavlink/embedded-hal-02", "mavlink/uorocketry"] -std = ["mavlink/std", "mavlink/tcp", "mavlink/udp", "mavlink/direct-serial", "mavlink/serde", "dep:proptest", "dep:proptest-derive"] +std = ["chrono/std", "mavlink/std", "mavlink/tcp", "mavlink/udp", "mavlink/direct-serial", "mavlink/serde", "dep:proptest", "dep:proptest-derive"] diff --git a/messages-proc-macros-lib/src/lib.rs b/messages-proc-macros-lib/src/lib.rs index 93861be..0189974 100644 --- a/messages-proc-macros-lib/src/lib.rs +++ b/messages-proc-macros-lib/src/lib.rs @@ -3,12 +3,12 @@ use quote::quote; /// Simple macro to easily add derives that are common for the messages crates. #[proc_macro_attribute] -pub fn common_derives(args: TokenStream, input: TokenStream) -> TokenStream { +pub fn common_derives(args: TokenStream, input: TokenStream) -> TokenStream { let mut output = TokenStream::from(quote! { #[derive(serde::Serialize, serde::Deserialize, Clone, Debug)] #[cfg_attr(feature = "ts", derive(ts_rs::TS))] #[cfg_attr(feature = "ts", ts(export))] - #[cfg_attr(test, derive(proptest_derive::Arbitrary))] + #[cfg_attr(std, derive(proptest_derive::Arbitrary))] }); // Allow to omit the defmt::Format derive. Useful if this should be manually implemented. @@ -20,4 +20,4 @@ pub fn common_derives(args: TokenStream, input: TokenStream) -> TokenStream { output.extend(input); output -} +} \ No newline at end of file diff --git a/src/command.rs b/src/command.rs index eb76a78..32b2703 100644 --- a/src/command.rs +++ b/src/command.rs @@ -1,4 +1,4 @@ -use crate::sender::Sender; +use crate::node::Node; use derive_more::From; use messages_proc_macros_lib::common_derives; @@ -39,7 +39,7 @@ pub struct DeployMain { #[common_derives] #[derive(From)] pub struct PowerDown { - pub board: Sender, // This isn't proper naming !! This is the board to be powered down. Changes name of sender.rs to board.rs. + pub board: Node, // This isn't proper naming !! This is the board to be powered down. Changes name of sender.rs to board.rs. } #[common_derives] diff --git a/src/lib.rs b/src/lib.rs index 910acc7..05f0744 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,23 +1,24 @@ #![cfg_attr(all(not(feature = "std"), not(test)), no_std)] #![no_main] -//! # HYDRA Messages +//! # Messages //! //! This crate contains all the message definitions that will be used for inter-board communication //! and ground-station communication. use crate::command::Command; -use crate::sender::Sender; +use crate::node::Node; use crate::sensor::Sensor; use crate::state::State; use derive_more::From; /// This is to help control versions. pub use mavlink; +use chrono::NaiveDateTime; use messages_proc_macros_lib::common_derives; pub mod command; mod logging; -pub mod sender; +pub mod node; pub mod sensor; pub mod sensor_status; pub mod state; @@ -28,14 +29,12 @@ pub use logging::{ErrorContext, Event, Log, LogLevel}; /// Topmost message. Encloses all the other possible messages, and is the only thing that should /// be sent over the wire. -#[common_derives] +#[common_derives(NoFormat)] pub struct Message { - /// Time in milliseconds since epoch. Note that the epoch here can be arbitrary and is not the - /// Unix epoch. - pub timestamp: u32, + pub timestamp: NaiveDateTime, /// The original sender of this message. - pub sender: Sender, + pub node: Node, /// The data contained in this message. pub data: Data, @@ -52,10 +51,10 @@ pub enum Data { } impl Message { - pub fn new(timestamp: u32, sender: Sender, data: impl Into) -> Self { + pub fn new(timestamp: NaiveDateTime, node: Node, data: impl Into) -> Self { Message { - timestamp: timestamp, - sender, + timestamp, + node, data: data.into(), } } diff --git a/src/node.rs b/src/node.rs new file mode 100644 index 0000000..a58236b --- /dev/null +++ b/src/node.rs @@ -0,0 +1,19 @@ +use messages_proc_macros_lib::common_derives; + +#[common_derives] +#[derive(Copy)] +pub enum Node { + PressureBoard, + TemperatureBoard, + StrainBoard, +} + +impl From for u16 { + fn from(node: Node) -> Self { + match node { + Node::PressureBoard => 0, + Node::TemperatureBoard => 1, + Node::StrainBoard => 2, + } + } +} diff --git a/src/sender.rs b/src/sender.rs deleted file mode 100644 index 8610893..0000000 --- a/src/sender.rs +++ /dev/null @@ -1,30 +0,0 @@ -use messages_proc_macros_lib::common_derives; - -// I don't agree with the naming, We can use these as Ids to sent commands to that specific board. -#[common_derives] -#[derive(Copy)] -pub enum Sender { - GroundStation, - SensorBoard, - RecoveryBoard, - CommunicationBoard, - PowerBoard, - CameraBoard, - BeaconBoard, - GpsBoard, -} - -impl From for u16 { - fn from(sender: Sender) -> Self { - match sender { - Sender::GroundStation => 0, - Sender::SensorBoard => 1, - Sender::RecoveryBoard => 2, - Sender::CommunicationBoard => 3, - Sender::PowerBoard => 4, - Sender::CameraBoard => 5, - Sender::BeaconBoard => 6, - Sender::GpsBoard => 7, - } - } -}