From 599a337ebc57507cd7d99ca513e80e458dc6d4ec Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 23 Oct 2017 00:17:35 -0400 Subject: add temperature panel --- src/ds3231.rs | 1 + src/main.rs | 189 ++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 137 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/ds3231.rs b/src/ds3231.rs index fd96fed..b07acf7 100644 --- a/src/ds3231.rs +++ b/src/ds3231.rs @@ -20,6 +20,7 @@ pub struct Date { pub am_enabled: bool } +#[derive(Copy, Clone)] pub struct Temp { pub cels: i8, pub quarter: u8 diff --git a/src/main.rs b/src/main.rs index 673dd01..7778d92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,7 @@ struct GlobalState { perip: Option>, disp_on: bool, pidx: usize, - panels: [&'static Panel; 2], + panels: [&'static Panel; 3], } struct Button<'a> { @@ -62,9 +62,9 @@ struct Button<'a> { } enum ButtonResult { - FALSE_ALARM, - SHORT_PRESS, - LONG_PRSSS + FalseAlarm, + ShortPress, + LongPress } impl<'a> Button<'a> { @@ -89,9 +89,9 @@ impl<'a> Button<'a> { if self.state.get() { self.timer.stop(); self.state.set(false); - if self.long.get() { ButtonResult::LONG_PRSSS } - else { ButtonResult::SHORT_PRESS } - } else { ButtonResult::FALSE_ALARM } + if self.long.get() { ButtonResult::LongPress } + else { ButtonResult::ShortPress } + } else { ButtonResult::FalseAlarm } } fn timeout(&self) { @@ -110,10 +110,11 @@ trait Panel { #[derive(PartialEq, Clone, Copy)] enum TimePanelState { + INACTIVE, VIEW, - EDIT_HR, - EDIT_MIN, - EDIT_SEC + EditHr, + EditMin, + EditSec, } struct TimePanel<'a> { @@ -129,19 +130,25 @@ impl<'a> Panel for TimePanel<'a> { { let mut tmp = self.tmp.borrow_mut(); match self.state.get() { - VIEW => return false, /* yield to the next panel */ - EDIT_HR => { + VIEW => { + self.state.set(INACTIVE); + return false; + }, /* yield to the next panel */ + EditHr => { tmp.hr += 1; if tmp.hr == 24 { tmp.hr = 0 }; }, - EDIT_MIN => { + EditMin => { tmp.min += 1; if tmp.min == 60 { tmp.min = 0 }; }, - EDIT_SEC => { + EditSec => { tmp.sec += 1; if tmp.sec == 60 { tmp.sec = 0 }; }, + INACTIVE => { + self.state.set(VIEW); + } }; } self.update_output(); @@ -157,11 +164,11 @@ impl<'a> Panel for TimePanel<'a> { tmp.hr = time.hr; tmp.min = time.min; tmp.sec = time.sec; - EDIT_HR + EditHr }, - EDIT_HR => EDIT_MIN, - EDIT_MIN => EDIT_SEC, - EDIT_SEC => { + EditHr => EditMin, + EditMin => EditSec, + EditSec => { let tmp = self.tmp.borrow(); ds3231::DS3231(self.gs.i2c.as_ref().unwrap()) .write_time(&ds3231::Date{second: tmp.sec, @@ -175,7 +182,8 @@ impl<'a> Panel for TimePanel<'a> { am_enabled: false}); *self.time.borrow_mut() = *tmp; VIEW - } + }, + s => s }; self.state.set(s); self.update_output(); @@ -186,10 +194,10 @@ impl<'a> Panel for TimePanel<'a> { use TimePanelState::*; let s = self.state.get(); self.gs.update_blinky(match s { - VIEW => [false; 6], - EDIT_HR => [false, false, false, false, true, true], - EDIT_MIN => [false, false, true, true, false, false], - EDIT_SEC => [true, true, false, false, false, false] + EditHr => [false, false, false, false, true, true], + EditMin => [false, false, true, true, false, false], + EditSec => [true, true, false, false, false, false], + _ => [false; 6], }); let time = self.time.borrow(); let tmp = self.tmp.borrow(); @@ -204,13 +212,14 @@ impl<'a> Panel for TimePanel<'a> { impl<'a> TimePanel<'a> { fn update_clock(&self, clk: Option