diff --git a/src/core/units.rs b/src/core/units.rs index 7de34d2..0f62def 100644 --- a/src/core/units.rs +++ b/src/core/units.rs @@ -1,13 +1,74 @@ +use serde::{Deserialize, Serialize}; + +///Standard conversion constant to convert to micro from base unit +pub const TO_MICRO: f32 = 1_000_000.0; +///Standard conversion constant to convert from micro to base unit; +pub const FROM_MICRO: f32 = 1.0 / 1_000_000.0; +///Standard conversion constant to convert to milli from base unit; +pub const TO_MILLI: f32 = 1_000.0; +///Standard conversion constant to convert from milli to base unit; +pub const FROM_MILLI: f32 = 1.0 / 1_000.0; + +///Standard way for the RNavP system to transfer around Current measurements. +/// This system by default uses Amps as f32. +/// Offers MilliAmps and MicroAmps for utilizing higher precision raw data for when needed. +/// Implements all needed conversions for handling f32 into and from this data type. +/// Uses Amps by default, please use MicroAmps and MilliVolts when needed for higher precision. +#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum Current { - MilliAmps(u32), - MicroAmps(u32), + Amps(f32), + MilliAmps(i32), + MicroAmps(i32), } +impl From for Current { + fn from(value: f32) -> Self { + Self::Amps(value) + } +} + +impl Into for Current { + fn into(self) -> f32 { + match self { + Self::MicroAmps(micro) => {micro as f32 * FROM_MICRO} + Self::MilliAmps(milli) => {milli as f32 * FROM_MILLI} + Self::Amps(amps) => {amps} + } + } +} + +///Standard way for the RNavP system to transfer around voltage measurements. +/// This system by default uses Volts as f32. +/// Offers MilliVolts and MicroVolts for utilizing higher precison raw data for when needed. +/// Implements all needed conversions for handling f32 into and from this dataType. +/// Uses Volts by default, please use MicroVolts and MilliVolts when needed for higher precision. +#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum Voltage { - MilliVolts(u32), - MicroVolts(u32), + Volts(f32), + MilliVolts(i32), + MicroVolts(i32), } +impl From for Voltage { + fn from(value: f32) -> Self { + Self::Volts(value) + } +} + +impl Into for Voltage { + fn into(self) -> f32 { + match self { + Self::MicroVolts(micro) => {micro as f32 * FROM_MICRO} + Self::MilliVolts(milli) => {milli as f32 * FROM_MILLI} + Self::Volts(volts) => {volts} + } + } +} + +///Standard way for the RnavP system for handling angles. +/// The system does use a f32, so no conversions needed. +/// Has methods for returning the wanted unit type regardless of what is stored. +#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum Angle { Degrees(f32), Radians(f32),