diff options
Diffstat (limited to 'src/devices/clock.rs')
-rw-r--r-- | src/devices/clock.rs | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/devices/clock.rs b/src/devices/clock.rs index 063c67b..b974a4a 100644 --- a/src/devices/clock.rs +++ b/src/devices/clock.rs @@ -8,6 +8,10 @@ macro_rules! now { () => { macro_rules! time_from_ticks { ($ticks:expr) => { Instant::now() + Duration::from_millis(($ticks * 4).into()) }; } +macro_rules! to_ticks { + ($duration:expr) => {($duration.as_millis() / 4) as u16}; } +macro_rules! from_ticks { + ($ticks:expr) => {Duration::from_millis(($ticks * 4).into())}; } macro_rules! ticks_since_time { ($time:expr) => { ($time.duration_since(Instant::now()).as_millis() / 4) as u16 }; @@ -30,6 +34,7 @@ macro_rules! generate_update_timer_method { self. [< timer_ $i >] = ticks_since_time!(self.[< timer_ $i _instant >]); if self. [< timer_ $i >] == 0 { self.wake_flag = true; } } + self. [< timer_ $i >] } }}; } @@ -38,7 +43,7 @@ pub struct ClockDevice { pub wake_flag: bool, pub boot_time: Instant, - pub cumulative_seconds: u16, + pub cumulative_timer: u16, pub timer_1_instant: Instant, pub timer_2_instant: Instant, @@ -56,7 +61,7 @@ impl ClockDevice { wake_flag: false, boot_time: Instant::now(), - cumulative_seconds: 0, + cumulative_timer: 0, timer_1_instant: Instant::now(), timer_2_instant: Instant::now(), @@ -69,10 +74,6 @@ impl ClockDevice { } } - pub fn update_cumulative_seconds(&mut self) { - self.cumulative_seconds = self.boot_time.elapsed().as_secs() as u16; - } - generate_set_timer_method!{1} generate_set_timer_method!{2} generate_set_timer_method!{3} @@ -83,6 +84,11 @@ impl ClockDevice { generate_update_timer_method!{3} generate_update_timer_method!{4} + pub fn update_cumulative_timer(&mut self) -> u16 { + self.cumulative_timer = to_ticks!(self.boot_time.elapsed()); + return self.cumulative_timer; + } + pub fn year(&self) -> u8 { now!().year().saturating_sub(2000).try_into().unwrap_or(u8::MAX) } |