-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactir time to use generic device instead of time driver and write …
…duration type in shared
- Loading branch information
1 parent
e7d5217
commit ab4ac37
Showing
8 changed files
with
221 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use core::{ | ||
ops::{Add, AddAssign, Sub, SubAssign}, | ||
time::Duration, | ||
}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] | ||
pub struct Duration_type { | ||
Seconds: u64, | ||
Nanoseconds: u32, | ||
} | ||
|
||
impl Duration_type { | ||
pub const fn New(Seconds: u64, Nanoseconds: u32) -> Self { | ||
Duration_type { | ||
Seconds, | ||
Nanoseconds, | ||
} | ||
} | ||
|
||
pub fn Get_duration_since(&self, Earlier: &Duration_type) -> Duration_type { | ||
self.Get_duration_since_checked(Earlier).unwrap_or_default() | ||
} | ||
|
||
pub fn Get_duration_since_checked(&self, Earlier: &Duration_type) -> Option<Duration_type> { | ||
let self_duration = Duration::new(self.Seconds, self.Nanoseconds); | ||
let earlier_duration = Duration::new(Earlier.Seconds, Earlier.Nanoseconds); | ||
self_duration | ||
.checked_sub(earlier_duration) | ||
.map(|d| Duration_type { | ||
Seconds: d.as_secs(), | ||
Nanoseconds: d.subsec_nanos(), | ||
}) | ||
} | ||
|
||
pub fn Get_duration_since_saturating(&self, earlier: &Duration_type) -> Duration_type { | ||
let self_duration = Duration::new(self.Seconds, self.Nanoseconds); | ||
let earlier_duration = Duration::new(earlier.Seconds, earlier.Nanoseconds); | ||
let result = self_duration.saturating_sub(earlier_duration); | ||
Duration_type { | ||
Seconds: result.as_secs(), | ||
Nanoseconds: result.subsec_nanos(), | ||
} | ||
} | ||
|
||
pub fn Add_checked(&self, Duration: &Duration_type) -> Option<Duration_type> { | ||
let self_duration = Duration::new(self.Seconds, self.Nanoseconds); | ||
let duration = Duration::new(Duration.Seconds, Duration.Nanoseconds); | ||
self_duration.checked_add(duration).map(|d| Duration_type { | ||
Seconds: d.as_secs(), | ||
Nanoseconds: d.subsec_nanos(), | ||
}) | ||
} | ||
|
||
pub fn Substract_checked(&self, Duration: &Duration_type) -> Option<Duration_type> { | ||
let self_duration = Duration::new(self.Seconds, self.Nanoseconds); | ||
let duration = Duration::new(Duration.Seconds, Duration.Nanoseconds); | ||
self_duration.checked_sub(duration).map(|d| Duration_type { | ||
Seconds: d.as_secs(), | ||
Nanoseconds: d.subsec_nanos(), | ||
}) | ||
} | ||
|
||
pub fn Add_saturating(&self, Duration: &Duration_type) -> Duration_type { | ||
let self_duration = Duration::new(self.Seconds, self.Nanoseconds); | ||
let duration = Duration::new(Duration.Seconds, Duration.Nanoseconds); | ||
let result = self_duration.saturating_add(duration); | ||
Duration_type { | ||
Seconds: result.as_secs(), | ||
Nanoseconds: result.subsec_nanos(), | ||
} | ||
} | ||
|
||
pub fn Substract_saturating(&self, duration: &Duration_type) -> Duration_type { | ||
let self_duration = Duration::new(self.Seconds, self.Nanoseconds); | ||
let duration = Duration::new(duration.Seconds, duration.Nanoseconds); | ||
let result = self_duration.saturating_sub(duration); | ||
Duration_type { | ||
Seconds: result.as_secs(), | ||
Nanoseconds: result.subsec_nanos(), | ||
} | ||
} | ||
|
||
pub fn As_seconds(&self) -> u64 { | ||
self.Seconds | ||
} | ||
|
||
pub fn As_milliseconds(&self) -> u64 { | ||
self.As_microseconds() as u64 / 1_000 | ||
} | ||
|
||
pub fn As_microseconds(&self) -> u128 { | ||
self.As_nanoseconds() / 1_000 | ||
} | ||
|
||
pub fn As_nanoseconds(&self) -> u128 { | ||
u128::from(self.Seconds) * 1_000_000_000 + u128::from(self.Nanoseconds) | ||
} | ||
} | ||
|
||
impl Add<&Duration_type> for Duration_type { | ||
type Output = Duration_type; | ||
|
||
fn add(self, Duration: &Duration_type) -> Duration_type { | ||
self.Add_checked(Duration) | ||
.expect("Overflow when adding duration") | ||
} | ||
} | ||
|
||
impl AddAssign<&Duration_type> for Duration_type { | ||
fn add_assign(&mut self, Duration: &Duration_type) { | ||
*self = self | ||
.Add_checked(Duration) | ||
.expect("Overflow when adding duration"); | ||
} | ||
} | ||
|
||
impl Sub<&Duration_type> for Duration_type { | ||
type Output = Duration_type; | ||
|
||
fn sub(self, Duration: &Duration_type) -> Duration_type { | ||
self.Substract_checked(Duration) | ||
.expect("Overflow when substracting duration") | ||
} | ||
} | ||
|
||
impl SubAssign<&Duration_type> for Duration_type { | ||
fn sub_assign(&mut self, Duration: &Duration_type) { | ||
*self = self | ||
.Substract_checked(Duration) | ||
.expect("Overflow when substracting duration"); | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for Duration_type { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
unsafe { | ||
core::slice::from_raw_parts_mut(self as *mut _ as *mut u8, core::mem::size_of::<Self>()) | ||
} | ||
} | ||
} | ||
|
||
impl AsRef<[u8]> for Duration_type { | ||
fn as_ref(&self) -> &[u8] { | ||
unsafe { | ||
core::slice::from_raw_parts(self as *const _ as *const u8, core::mem::size_of::<Self>()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![allow(non_camel_case_types)] | ||
#![allow(non_snake_case)] | ||
#![allow(non_upper_case_globals)] | ||
|
||
use File_system::Create_device; | ||
use Shared::Duration_type; | ||
|
||
#[test] | ||
fn Test_get_current_time() { | ||
let _ = Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New())); | ||
|
||
let Current_time = Time::Get_instance().Get_current_time().unwrap(); | ||
|
||
println!("Current time : {:?}", Current_time); | ||
|
||
assert_ne!(Current_time, Duration_type::default()); | ||
} | ||
|
||
#[test] | ||
fn Test_get_current_time_since_startup() { | ||
let _ = Time::Initialize(Create_device!(Drivers::Native::Time_driver_type::New())); | ||
|
||
let Current_time = Time::Get_instance() | ||
.Get_current_time_since_startup() | ||
.unwrap(); | ||
|
||
println!("Time since startup : {:?}", Current_time); | ||
|
||
assert_ne!(Current_time, Duration_type::default()); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,51 @@ | ||
use std::time::Duration; | ||
use std::sync::OnceLock; | ||
|
||
use crate::{Driver::Driver_trait, Error_type, Result_type}; | ||
use File_system::Device_type; | ||
use Shared::Duration_type; | ||
|
||
pub static mut Manager: Option<Manager_type> = None; | ||
use crate::{Error_type, Result_type}; | ||
|
||
pub fn Get_instance() -> &'static Manager_type { | ||
unsafe { Manager.as_ref().expect("Time manager is not initialized") } | ||
} | ||
pub static Manager: OnceLock<Manager_type> = OnceLock::new(); | ||
|
||
pub fn Is_initialized() -> bool { | ||
unsafe { Manager.is_some() } | ||
pub fn Get_instance() -> &'static Manager_type { | ||
Manager.get().expect("Time manager is not initialized") | ||
} | ||
|
||
pub fn Initialize(Driver: Box<dyn Driver_trait>) -> Result_type<&'static Manager_type> { | ||
if Is_initialized() { | ||
return Err(Error_type::Already_initialized); | ||
} | ||
unsafe { | ||
Manager.replace(Manager_type::New(Driver)?); | ||
} | ||
pub fn Initialize(Driver: Device_type) -> Result_type<&'static Manager_type> { | ||
Manager.get_or_init(|| Manager_type::New(Driver).expect("Failed to initialize time manager")); | ||
|
||
Ok(Get_instance()) | ||
} | ||
|
||
pub struct Manager_type { | ||
Driver: Box<dyn Driver_trait>, | ||
Device: Device_type, | ||
Start_time: Duration_type, | ||
} | ||
|
||
impl Manager_type { | ||
pub fn New(Driver: Box<dyn Driver_trait>) -> Result_type<Self> { | ||
Ok(Self { Driver }) | ||
pub fn New(Device: Device_type) -> Result_type<Self> { | ||
let mut Start_time = Duration_type::default(); | ||
|
||
Device | ||
.Read(Start_time.as_mut()) | ||
.map_err(Error_type::Device_error)?; | ||
|
||
Ok(Self { Device, Start_time }) | ||
} | ||
|
||
pub fn Get_current_time_since_startup(&self) -> Duration { | ||
self.Driver.Get_instant_since_startup() | ||
pub fn Get_current_time_since_startup(&self) -> Result_type<Duration_type> { | ||
let Current_time = self.Get_current_time()?; | ||
|
||
Ok(Current_time.Get_duration_since(&self.Start_time)) | ||
} | ||
|
||
pub fn Get_current_time(&self) -> Duration { | ||
self.Driver.Get_current_time() | ||
pub fn Get_current_time(&self) -> Result_type<Duration_type> { | ||
let mut Current_time = Duration_type::default(); | ||
|
||
self.Device | ||
.Read(Current_time.as_mut()) | ||
.map_err(Error_type::Device_error)?; | ||
|
||
Ok(Current_time) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters