From d053f14f381629fd8bf4483d9e9fd95956b6adc5 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 24 Oct 2017 10:24:01 -0400 Subject: add countdown panel --- src/i2c.rs | 1 - src/main.rs | 612 ++++++++++++++++++++++++++++++++++++++---------------------- src/tim.rs | 4 + 3 files changed, 389 insertions(+), 228 deletions(-) diff --git a/src/i2c.rs b/src/i2c.rs index 5ecfec0..35a618a 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -27,7 +27,6 @@ pub enum DutyType { } impl<'a> I2C<'a> { - #[inline(always)] fn get_pclk1(rcc: &RCC) -> u32 { use stm32f103xx::rcc::cfgr::{SWSR, PLLSRCR, PLLXTPRER}; let cfgr = rcc.cfgr.read(); diff --git a/src/main.rs b/src/main.rs index 7778d92..3c4272e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,9 +5,7 @@ #[macro_use] extern crate stm32f103xx; extern crate cortex_m; -use stm32f103xx::{GPIOA, GPIOB, RCC, SYST, I2C1, EXTI, NVIC, Interrupt, AFIO, Peripherals}; -use stm32f103xx::{gpioa}; -use cortex_m::peripheral::SystClkSource; +use stm32f103xx::{Interrupt, Peripherals, gpioa}; use core::cell::{Cell, RefCell}; mod mutex; @@ -16,9 +14,7 @@ mod ds3231; mod at24c; mod tim; -const SYNC_PERIOD: u8 = 10; -const BLINK_PERIOD: u32 = 500; - +#[inline] fn digits2bcds(digs: &[u8]) -> u32 { let mut res: u32 = 0; for d in digs.iter().rev() { @@ -27,12 +23,19 @@ fn digits2bcds(digs: &[u8]) -> u32 { res } +fn inc_rotate(n: &mut u8, reset: u8, limit: u8) { + *n += 1; + if *n == limit { + *n = reset; + } +} + struct ShiftRegister<'a> { gpioa: &'a gpioa::RegisterBlock, width: u8, } -#[derive(Copy, Clone)] +#[derive(Clone, Copy)] struct Time { sec: u8, min: u8, @@ -40,19 +43,18 @@ struct Time { } struct GlobalState { + perip: Option>, disp: Option>, - i2c: Option>, btn1: Option>, - //tim: Option>, + i2c: Option>, i2c_inited: bool, - sync_cnt: u8, + sync_cnt: Cell, buff: RefCell<[u8; 6]>, + disp_on: bool, blinky: RefCell<[bool; 6]>, blink_state: Cell, - perip: Option>, - disp_on: bool, pidx: usize, - panels: [&'static Panel; 3], + panels: [&'static Panel; 4], } struct Button<'a> { @@ -67,6 +69,133 @@ enum ButtonResult { LongPress } +trait Panel { + 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 update_output(&self); +} + +#[derive(PartialEq, Clone, Copy)] +enum TimePanelState { + Inactive, + View, + EditHr, + EditMin, + EditSec, +} + +struct TimePanel<'a> { + gs: &'a GlobalState, + state: Cell, + time: RefCell