added doc test / example as well
This commit is contained in:
parent
1d9329ad63
commit
6341cf35d3
@ -121,6 +121,29 @@ pub struct FractionalPart(FractionalResolution, u32);
|
|||||||
/// Having a preamble field of one byte limits the width of the counter
|
/// Having a preamble field of one byte limits the width of the counter
|
||||||
/// type (generally seconds) to 4 bytes and the width of the fractions type to 3 bytes. This limits
|
/// type (generally seconds) to 4 bytes and the width of the fractions type to 3 bytes. This limits
|
||||||
/// the maximum time stamp size to [MAX_CUC_LEN_SMALL_PREAMBLE] (8 bytes).
|
/// the maximum time stamp size to [MAX_CUC_LEN_SMALL_PREAMBLE] (8 bytes).
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use spacepackets::time::cuc::{FractionalResolution, TimeProviderCcsdsEpoch};
|
||||||
|
/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, TimeReader, CcsdsTimeProvider};
|
||||||
|
///
|
||||||
|
/// // Highest fractional resolution
|
||||||
|
/// let timestamp_now = TimeProviderCcsdsEpoch::from_now(FractionalResolution::SixtyNs).expect("creating cuc stamp failed");
|
||||||
|
/// let mut raw_stamp = [0; 16];
|
||||||
|
/// {
|
||||||
|
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).expect("writing timestamp failed");
|
||||||
|
/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::CucCcsdsEpoch as u8);
|
||||||
|
/// // 1 byte preamble + 4 byte counter + 3 byte fractional part
|
||||||
|
/// assert_eq!(written, 8);
|
||||||
|
/// }
|
||||||
|
/// {
|
||||||
|
/// let read_result = TimeProviderCcsdsEpoch::from_bytes(&raw_stamp);
|
||||||
|
/// assert!(read_result.is_ok());
|
||||||
|
/// let stamp_deserialized = read_result.unwrap();
|
||||||
|
/// assert_eq!(stamp_deserialized, timestamp_now);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
pub struct TimeProviderCcsdsEpoch {
|
pub struct TimeProviderCcsdsEpoch {
|
||||||
@ -288,11 +311,8 @@ impl TimeProviderCcsdsEpoch {
|
|||||||
let ccsds_epoch = unix_epoch_to_ccsds_epoch(now.as_secs());
|
let ccsds_epoch = unix_epoch_to_ccsds_epoch(now.as_secs());
|
||||||
let fractions =
|
let fractions =
|
||||||
fractional_part_from_subsec_ns(fraction_resolution, now.subsec_nanos() as u64);
|
fractional_part_from_subsec_ns(fraction_resolution, now.subsec_nanos() as u64);
|
||||||
Ok(Self {
|
Self::new_with_fractions(ccsds_epoch as u32, fractions)
|
||||||
pfield: 0,
|
.map_err(|e| TimestampError::CucError(e).into())
|
||||||
counter: WidthCounterPair(4, ccsds_epoch as u32),
|
|
||||||
fractions,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
@ -330,10 +350,14 @@ impl TimeProviderCcsdsEpoch {
|
|||||||
if res == FractionalResolution::Seconds {
|
if res == FractionalResolution::Seconds {
|
||||||
self.fractions = None;
|
self.fractions = None;
|
||||||
}
|
}
|
||||||
|
let mut update_fractions = true;
|
||||||
if let Some(existing_fractions) = self.fractions {
|
if let Some(existing_fractions) = self.fractions {
|
||||||
if existing_fractions.0 != res {
|
if existing_fractions.0 == res {
|
||||||
self.fractions = Some(FractionalPart(res, 0));
|
update_fractions = false;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
if update_fractions {
|
||||||
|
self.fractions = Some(FractionalPart(res, 0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -863,4 +887,27 @@ mod tests {
|
|||||||
/ (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FifteenUs) as u64);
|
/ (10_u64.pow(9) / fractional_res_to_div(FractionalResolution::FifteenUs) as u64);
|
||||||
assert_eq!(fractional_part, 101);
|
assert_eq!(fractional_part, 101);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn update_fractions() {
|
||||||
|
let mut stamp = TimeProviderCcsdsEpoch::new(2000);
|
||||||
|
let res = stamp.set_fractions(FractionalPart(FractionalResolution::SixtyNs, 5000));
|
||||||
|
assert!(res.is_ok());
|
||||||
|
assert!(stamp.fractions.is_some());
|
||||||
|
let fractions = stamp.fractions.unwrap();
|
||||||
|
assert_eq!(fractions.0, FractionalResolution::SixtyNs);
|
||||||
|
assert_eq!(fractions.1, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_fract_resolution() {
|
||||||
|
let mut stamp = TimeProviderCcsdsEpoch::new(2000);
|
||||||
|
stamp.set_fractional_resolution(FractionalResolution::SixtyNs);
|
||||||
|
assert!(stamp.fractions.is_some());
|
||||||
|
let fractions = stamp.fractions.unwrap();
|
||||||
|
assert_eq!(fractions.0, FractionalResolution::SixtyNs);
|
||||||
|
assert_eq!(fractions.1, 0);
|
||||||
|
let res = stamp.update_from_now();
|
||||||
|
assert!(res.is_ok());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,6 @@ use core::fmt::{Display, Formatter};
|
|||||||
#[cfg(not(feature = "std"))]
|
#[cfg(not(feature = "std"))]
|
||||||
use num_traits::float::FloatCore;
|
use num_traits::float::FloatCore;
|
||||||
|
|
||||||
use crate::time::cuc::CucError;
|
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
@ -73,7 +72,7 @@ impl From<cds::CdsError> for TimestampError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<cuc::CucError> for TimestampError {
|
impl From<cuc::CucError> for TimestampError {
|
||||||
fn from(e: CucError) -> Self {
|
fn from(e: cuc::CucError) -> Self {
|
||||||
TimestampError::CucError(e)
|
TimestampError::CucError(e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user