From f02632b4965e095e1850bca005f9ba75d812744e Mon Sep 17 00:00:00 2001 From: Determinant Date: Sat, 28 Oct 2017 00:35:52 -0400 Subject: multiplex the event timer and fix bugs --- src/ds3231.rs | 24 ++--- src/i2c.rs | 3 +- src/main.rs | 280 ++++++++++++++++++++++++++++++++-------------------------- src/tim.rs | 2 +- 4 files changed, 167 insertions(+), 142 deletions(-) diff --git a/src/ds3231.rs b/src/ds3231.rs index b07acf7..de2201b 100644 --- a/src/ds3231.rs +++ b/src/ds3231.rs @@ -1,4 +1,4 @@ -use i2c::{I2C, TransDir, DutyType}; +use i2c::{I2C, TransDir}; const DS3231_ADDR: u8 = 0b1101000; const DS3231_REG_SEC: u8 = 0x00; @@ -103,23 +103,17 @@ impl<'a> DS3231<'a> { self.write_register(DS3231_REG_SEC, buf.len(), &buf); } - pub fn write_time(&self, date: &Date) { - let hour = if date.am_enabled { - (1 << 6) | ((if date.am {0} else {1}) << 5) | - ((date.hour / 10) << 4) | (date.hour % 10) - } else { - dec2bcd(date.hour) - }; - let buf: [u8; 3] = [dec2bcd(date.second), - dec2bcd(date.minute), - hour]; + pub fn write_time(&self, hr: u8, min: u8, sec: u8) { + let buf: [u8; 3] = [dec2bcd(sec), + dec2bcd(min), + dec2bcd(hr)]; self.write_register(DS3231_REG_SEC, buf.len(), &buf); } - pub fn write_date(&self, date: &Date) { - let buf: [u8; 3] = [dec2bcd(date.date), - dec2bcd(date.month), - dec2bcd(date.year)]; + pub fn write_date(&self, yy: u8, mm: u8, dd: u8) { + let buf: [u8; 3] = [dec2bcd(dd), + dec2bcd(mm), + dec2bcd(yy)]; self.write_register(DS3231_REG_DATE, buf.len(), &buf); } diff --git a/src/i2c.rs b/src/i2c.rs index 35a618a..b5b8b10 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -1,7 +1,6 @@ #![allow(dead_code)] -extern crate stm32f103xx; use core::cmp::max; -use stm32f103xx::{i2c1, rcc, RCC}; +use stm32f103xx::{i2c1, RCC}; pub const EVENT_MASTER_STARTED: u32 = 0x00030001; /* BUSY, MSL and SB flag */ pub const EVENT_MASTER_TRANSMITTER_MODE_SELECTED: u32 = 0x00070082; /* BUSY, MSL, ADDR, TXE and TRA flags */ diff --git a/src/main.rs b/src/main.rs index 56ba36e..41bcafc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,7 @@ extern crate cortex_m; use stm32f103xx::{Interrupt, Peripherals, gpioa}; -use core::cell::{Cell, RefCell}; +use core::cell::{Cell, RefCell, UnsafeCell}; mod mutex; mod i2c; @@ -42,33 +42,37 @@ struct Time { hr: u8, } -struct GlobalState { - perip: Option>, - disp: Option>, - btn1: Option>, - btn2: Option>, - i2c: Option>, +struct GlobalState<'a> { + perip: Option>, + disp: Option>, + btn1: Option>, + btn2: Option>, + events: Option>, + i2c: Option>, i2c_inited: bool, sync_cnt: Cell, buff: RefCell<[u8; 6]>, disp_on: bool, blinky: RefCell<[bool; 6]>, blink_state: Cell, + blinky_enabled: Cell>, pidx: usize, - panels: [&'static Panel; 5], + panels: [&'a Panel; 5], } #[derive(PartialEq, Clone, Copy)] enum ButtonState { Idle, PressedLock, - PressedUnlock + PressedUnlock, + ReleaseLock } struct Button<'a> { state: Cell, long: Cell, - timer: tim::Timer<'a> + events: &'a AlarmEventManager<'a>, + ev_id: Cell } enum ButtonResult { @@ -85,6 +89,10 @@ trait Panel { fn update_output(&self); } +trait Timeoutable<'a> { + fn timeout(&'a self); +} + #[derive(PartialEq, Clone, Copy)] enum TimePanelState { Inactive, @@ -95,7 +103,7 @@ enum TimePanelState { } struct TimePanel<'a> { - gs: &'a GlobalState, + gs: &'a GlobalState<'a>, state: Cell, time: RefCell