Skip to content
Snippets Groups Projects
Commit a3885e25 authored by koenigl's avatar koenigl
Browse files

port timer patch

parent afe5bf97
No related branches found
No related tags found
No related merge requests found
diff --git a/src/timer.rs b/src/timer.rs
index ae60b07..2e7d544 100644
index 946c6e5..90d44c9 100644
--- a/src/timer.rs
+++ b/src/timer.rs
@@ -178,7 +178,7 @@ impl<'a> Timer<'a> {
@@ -136,7 +136,7 @@ impl<'a> Timer<'a> {
}
}
-#[derive(Copy, Clone, Debug)]
+#[derive(Copy, Clone, Debug, PartialEq)]
pub struct ClockFrequency {
hz: usize,
}
@@ -196,21 +196,53 @@ pub struct ClockValue {
@@ -154,21 +154,53 @@ pub struct ClockValue {
}
impl ClockValue {
+ pub const fn new(num_ticks: isize, clock_hz: usize) -> ClockValue {
+ ClockValue {
......@@ -22,10 +22,10 @@ index ae60b07..2e7d544 100644
+ }
+ }
+
pub fn num_ticks(&self) -> isize {
pub fn num_ticks(self) -> isize {
self.num_ticks
}
+ // Computes (value * factor) / divisor, even when value * factor >= isize::MAX.
+ fn scale_int(value: isize, factor: isize, divisor: isize) -> isize {
+ // As long as isize is not i64, this should be fine. If not, this is an alternative:
......@@ -33,7 +33,7 @@ index ae60b07..2e7d544 100644
+ ((value as i64 * factor as i64) / divisor as i64) as isize
+ }
+
pub fn ms(&self) -> isize {
pub fn ms(self) -> isize {
- if self.num_ticks.abs() < isize::MAX / 1000 {
- (1000 * self.num_ticks) / self.clock_frequency.hz() as isize
- } else {
......@@ -41,8 +41,8 @@ index ae60b07..2e7d544 100644
- }
+ ClockValue::scale_int(self.num_ticks, 1000, self.clock_frequency.hz() as isize)
}
pub fn ms_f64(&self) -> f64 {
pub fn ms_f64(self) -> f64 {
1000.0 * (self.num_ticks as f64) / (self.clock_frequency.hz() as f64)
}
+
......@@ -68,5 +68,5 @@ index ae60b07..2e7d544 100644
+ }
+ }
}
pub struct Alarm {
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment