From 9c23fc4d4c191452aef1572e2199d3a598e9fd94 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 1 Nov 2017 02:14:01 -0400 Subject: refactor the code --- src/main.rs | 660 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 320 insertions(+), 340 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 1d26662..f8e0a7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,8 @@ extern crate cortex_m; use stm32f103xx::{Interrupt, Peripherals, gpioa}; -use core::cell::{Cell, RefCell, UnsafeCell, RefMut}; +use core::cell::{Cell, UnsafeCell}; +use core::mem::uninitialized; mod mutex; mod i2c; @@ -15,6 +16,11 @@ mod ds3231; mod at24c; mod tim; +#[inline(always)] +fn get_gs() -> &'static mut GlobalState<'static> { + unsafe {GS.as_mut().unwrap()} +} + #[inline] fn digits2bcds(digs: &[u8]) -> u32 { let mut res: u32 = 0; @@ -36,14 +42,14 @@ fn countdown(n: u32) -> u32 { if n > 0 { n - 1 } else { n } } -fn render1(mut buff: RefMut<[u8; 6]>, mut n: u32) { +fn render1(buff: &mut [u8; 6], mut n: u32) { for i in 0..buff.len() { buff[i] = (n % 10) as u8; n /= 10; } } -fn render3(mut buff: RefMut<[u8; 6]>, n1: u8, n2: u8, n3: u8) { +fn render3(buff: &mut [u8; 6], n1: u8, n2: u8, n3: u8) { buff[1] = n3 / 10; buff[0] = n3 - buff[1] * 10; buff[3] = n2 / 10; buff[2] = n2 - buff[3] * 10; buff[5] = n1 / 10; buff[4] = n1 - buff[5] * 10; @@ -69,24 +75,29 @@ enum DispState { } struct GlobalState<'a> { - perip: Option>, - disp: Option>, - btn1: Option>, - btn2: Option>, - events: Option>, - i2c: Option>, - i2c_inited: bool, + perip: Peripherals<'a>, + disp: ShiftRegister<'a>, + btn1: Button<'a>, + btn2: Button<'a>, + events: EventTimer<'a>, + i2c: i2c::I2C<'a>, sync_cnt: Cell, - buff: RefCell<[u8; 6]>, + buff: [u8; 6], disp_state: Cell, tempon_cycle: Cell, tempon_peroid: Cell, tempon_cnt: Cell, - blinky: RefCell<[bool; 6]>, + blinky: [bool; 6], blink_state: Cell, blinky_enabled: Cell>, pidx: usize, - panels: [&'a Panel<'a>; 6], + time_panel: TimePanel, + date_panel: DatePanel, + temp_panel: TempPanel, + cd_panel: CountdownPanel, + cu_panel: CountupPanel, + set_panel: SettingPanel, + panels: [&'a mut Panel<'a>; 6], } #[derive(Clone, Copy)] @@ -108,7 +119,7 @@ enum ButtonMode { struct Button<'a> { state: Cell, long: Cell, - events: &'a AlarmEventManager<'a>, + events: &'a EventTimer<'a>, ev_id: Cell, mode: Cell } @@ -120,17 +131,17 @@ enum ButtonResult { } trait Panel<'a> { - fn btn1_press(&'a self) -> bool { - self.get_gs().btn1.as_ref().unwrap().press(ButtonMode::Release); + fn btn1_press(&'a mut self) -> bool { + get_gs().btn1.press(ButtonMode::Release); true } - fn btn1_release_extra(&self) {} + fn btn1_release_extra(&mut self) {} - fn btn1_release(&'a self) -> bool { + fn btn1_release(&'a mut self) -> bool { self.btn1_release_extra(); - let gs = self.get_gs(); - let btn1 = gs.btn1.as_ref().unwrap(); + let gs = get_gs(); + let btn1 = &gs.btn1; match btn1.release() { ButtonResult::FalseAlarm => true, ButtonResult::ShortPress => self.btn1_short(), @@ -142,11 +153,10 @@ trait Panel<'a> { } } - fn btn1_short(&self) -> bool {false} - fn btn1_long(&self) -> bool {false} - fn btn2_short(&self) -> bool {false} - fn btn2_long(&self) -> bool {false} - fn get_gs(&'a self) -> &GlobalState<'a>; + fn btn1_short(&mut self) -> bool {false} + fn btn1_long(&mut self) -> bool {false} + fn btn2_short(&mut self) -> bool {false} + fn btn2_long(&mut self) -> bool {false} fn update_output(&self); } @@ -163,11 +173,10 @@ enum TimePanelState { EditSec, } -struct TimePanel<'a> { - gs: &'a GlobalState<'a>, +struct TimePanel { state: Cell, - time: RefCell