Skip to content

Commit

Permalink
Add types for managing control cycles and register measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde committed Oct 6, 2021
1 parent ab66c9c commit 568966f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
84 changes: 84 additions & 0 deletions crates/msr-core/src/control/cyclic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::{fmt, time::Instant};

use crate::register;

pub type CycleIdValue = u16;

/// Numeric identifier of a control cycle
///
/// Periodic control cycles are usually distinguished by their
/// frequency. This identifier allows to reference control cycles
/// independent of their actual properties.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct CycleId(CycleIdValue);

impl CycleId {
pub const fn from_value(value: CycleIdValue) -> Self {
Self(value)
}

pub const fn to_value(self) -> CycleIdValue {
self.0
}
}

impl From<CycleIdValue> for CycleId {
fn from(from: CycleIdValue) -> Self {
Self::from_value(from)
}
}

impl From<CycleId> for CycleIdValue {
fn from(from: CycleId) -> Self {
from.to_value()
}
}

impl fmt::Display for CycleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", self.to_value())
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CycleTimeStamp {
pub id: CycleId,

pub ts: Instant,
}

impl CycleTimeStamp {
pub fn now(id: CycleId) -> Self {
Self {
id,
ts: Instant::now(),
}
}
}

#[derive(Debug, Clone)]
pub struct CyclicRegisterMeasurements<RegisterValue> {
pub cycle_id: CycleId,

/// The cycle during which the measurements have been collected
pub cycle_ts: CycleTimeStamp,

/// Measurements for a set of registers
///
/// Each register is supposed to appear at most once in the
/// vector!
pub registers: Vec<register::IndexedMeasurement<RegisterValue>>,
}

impl<RegisterValue> CyclicRegisterMeasurements<RegisterValue> {
pub fn count_number_unique_of_registers(&self) -> usize {
let mut register_indices: Vec<_> = self.registers.iter().map(|m| m.index).collect();
register_indices.sort_unstable();
register_indices.dedup();
register_indices.len()
}

pub fn contains_duplicate_registers(&self) -> bool {
self.registers.len() > self.count_number_unique_of_registers()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::Measurement;

pub mod cyclic;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value<V> {
Input(Input<V>),
Expand Down
8 changes: 2 additions & 6 deletions crates/msr-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
mod control;
mod measure;
mod value;

pub use self::{
control::{Input, Output, Value as ControlValue},
measure::*,
value::*,
};
pub use self::{measure::*, value::*};

pub mod audit;
pub mod control;
pub mod io;
pub mod register;
pub mod storage;
Expand Down

0 comments on commit 568966f

Please sign in to comment.