summaryrefslogtreecommitdiff
path: root/src/devices/clock.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/devices/clock.rs')
-rw-r--r--src/devices/clock.rs57
1 files changed, 29 insertions, 28 deletions
diff --git a/src/devices/clock.rs b/src/devices/clock.rs
index 3faa604..60b1cad 100644
--- a/src/devices/clock.rs
+++ b/src/devices/clock.rs
@@ -1,22 +1,16 @@
use std::time::{Duration, Instant};
+macro_rules! to_ticks { ($dur:expr) => {($dur.as_millis() / 4) as u16}; }
+macro_rules! from_ticks { ($ticks:expr) => {Duration::from_millis(($ticks * 4).into())}; }
macro_rules! now { () => {
- time::OffsetDateTime::now_local()
- .unwrap_or_else(|_| time::OffsetDateTime::now_utc())
-};}
-
-macro_rules! to_ticks {
- ($duration:expr) => {($duration.as_millis() / 4) as u16}; }
-macro_rules! from_ticks {
- ($ticks:expr) => {Duration::from_millis(($ticks * 4).into())}; }
-
+ time::OffsetDateTime::now_local().unwrap_or_else(|_| time::OffsetDateTime::now_utc())};}
/// Create a method to set the instant of a timer.
macro_rules! generate_set_timer_method {
($i:tt) => { mini_paste::item!{
pub fn [< set_timer_ $i >] (&mut self) {
let ticks = &mut self. [< timer_ $i >];
- let instant = &mut self. [< timer_ $i _instant >];
+ let instant = &mut self. [< timer_ $i _end >];
*instant = Instant::now() + from_ticks!(*ticks);
}
@@ -28,13 +22,13 @@ macro_rules! generate_update_timer_method {
($i:tt) => { mini_paste::item!{
pub fn [< update_timer_ $i >] (&mut self) -> u16 {
let ticks = &mut self. [< timer_ $i >];
- let instant = &mut self. [< timer_ $i _instant >];
+ let instant = &mut self. [< timer_ $i _end >];
if *ticks > 0 {
*ticks = to_ticks!(instant.duration_since(Instant::now()));
if *ticks == 0 { self.wake_flag = true; }
}
- *ticks
+ return *ticks;
}
}};
}
@@ -42,13 +36,13 @@ macro_rules! generate_update_timer_method {
pub struct ClockDevice {
pub wake_flag: bool,
- pub boot_time: Instant,
- pub cumulative_timer: u16,
+ pub program_start: Instant,
+ pub uptime: u16,
- pub timer_1_instant: Instant,
- pub timer_2_instant: Instant,
- pub timer_3_instant: Instant,
- pub timer_4_instant: Instant,
+ pub timer_1_end: Instant,
+ pub timer_2_end: Instant,
+ pub timer_3_end: Instant,
+ pub timer_4_end: Instant,
pub timer_1: u16,
pub timer_2: u16,
pub timer_3: u16,
@@ -60,13 +54,13 @@ impl ClockDevice {
Self {
wake_flag: false,
- boot_time: Instant::now(),
- cumulative_timer: 0,
+ program_start: Instant::now(),
+ uptime: 0,
- timer_1_instant: Instant::now(),
- timer_2_instant: Instant::now(),
- timer_3_instant: Instant::now(),
- timer_4_instant: Instant::now(),
+ timer_1_end: Instant::now(),
+ timer_2_end: Instant::now(),
+ timer_3_end: Instant::now(),
+ timer_4_end: Instant::now(),
timer_1: 0,
timer_2: 0,
timer_3: 0,
@@ -84,9 +78,16 @@ 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 update_timers(&mut self) {
+ self.update_timer_1();
+ self.update_timer_2();
+ self.update_timer_3();
+ self.update_timer_4();
+ }
+
+ pub fn update_uptime(&mut self) -> u16 {
+ self.uptime = to_ticks!(self.program_start.elapsed());
+ return self.uptime;
}
pub fn year(&self) -> u8 {
@@ -113,7 +114,7 @@ impl ClockDevice {
now!().second()
}
- pub fn shortest_active_timer(&self) -> Option<Duration> {
+ pub fn time_to_next_wake(&self) -> Option<Duration> {
[self.timer_1, self.timer_2, self.timer_3, self.timer_4]
.iter()
.filter(|t| **t > 0)