QuadrantTracker: add start/end

This commit is contained in:
Sebastien Bourdeauducq 2019-12-12 14:43:09 +08:00
parent 9b2eba0761
commit bc7ab2bef5
1 changed files with 19 additions and 0 deletions

View File

@ -126,6 +126,7 @@ enum Quadrant {
#[derive(Clone)]
struct QuadrantTracker {
prev_state: Quadrant,
state: Quadrant,
min: i64,
max: i64,
@ -138,6 +139,7 @@ struct QuadrantTracker {
impl QuadrantTracker {
pub fn new() -> QuadrantTracker {
QuadrantTracker {
prev_state: Quadrant::BelowMin,
state: Quadrant::BelowMin,
min: i64::max_value(),
max: i64::min_value(),
@ -171,6 +173,7 @@ impl QuadrantTracker {
}
}
self.prev_state = self.state;
if self.state != next_state {
match (self.state, next_state) {
(Quadrant::BelowMin, Quadrant::Up) => (),
@ -199,6 +202,22 @@ impl QuadrantTracker {
self.new_max = max;
self.middle = (min + max)/2;
}
pub fn up_start(&self) -> bool {
self.prev_state == Quadrant::BelowMin && self.state == Quadrant::Up
}
pub fn up_end(&self) -> bool {
self.prev_state == Quadrant::Up && self.state == Quadrant::AboveMax
}
pub fn down_start(&self) -> bool {
self.prev_state == Quadrant::AboveMax && self.state == Quadrant::Down
}
pub fn down_end(&self) -> bool {
self.prev_state == Quadrant::Down && self.state == Quadrant::BelowMin
}
}
fn do_wavemeter(config: &Config) {