113 lines
2.3 KiB
Rust
113 lines
2.3 KiB
Rust
use crate::{
|
|
apps,
|
|
apps::App,
|
|
display::Display,
|
|
keypad::*,
|
|
state::{self, ModeState},
|
|
strf, Context,
|
|
};
|
|
|
|
use arrayvec::ArrayString;
|
|
use embedded_graphics::{
|
|
mono_font::{ascii::FONT_10X20, MonoTextStyleBuilder},
|
|
pixelcolor::BinaryColor,
|
|
prelude::*,
|
|
text::{Alignment, Text},
|
|
};
|
|
|
|
pub struct Clock;
|
|
|
|
pub struct ClockState {
|
|
year: i32,
|
|
month: u8,
|
|
day: u8,
|
|
week_day: u8,
|
|
}
|
|
|
|
impl Default for ClockState {
|
|
fn default() -> Self {
|
|
Self {
|
|
year: 0,
|
|
month: 0,
|
|
day: 0,
|
|
week_day: 0,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl App for Clock {
|
|
type AppModeState = ClockState;
|
|
|
|
fn update(ctx: &mut Context, mode_state: &mut ClockState) -> Option<ModeState> {
|
|
// TODO move at init
|
|
let clock_text_style = MonoTextStyleBuilder::new()
|
|
.font(&FONT_10X20)
|
|
.text_color(BinaryColor::On)
|
|
.background_color(BinaryColor::Off)
|
|
.build();
|
|
|
|
for key_event in &ctx.key_events {
|
|
if key_event.event_type != KeyEventType::Pressed {
|
|
continue;
|
|
}
|
|
match key_event.key {
|
|
_ => {
|
|
if let Some(ch) = key_event.get_char(KeyInputMode::Digit) {
|
|
let mut buf = [0; 4];
|
|
return Some(ModeState::Dial(apps::dial::DialState {
|
|
line: ArrayString::from(&*ch.encode_utf8(&mut buf)).unwrap(),
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ctx.hm_change {
|
|
Text::with_alignment(
|
|
unsafe {
|
|
core::str::from_utf8_unchecked(&strf::fmt_time_hm(
|
|
ctx.state.hour,
|
|
ctx.state.minute,
|
|
))
|
|
},
|
|
ctx.display.inner().bounding_box().center() + Point::new(0, 10),
|
|
clock_text_style,
|
|
Alignment::Center,
|
|
)
|
|
.draw(ctx.display.inner_mut())
|
|
.unwrap();
|
|
|
|
let year_ = ctx.now.year();
|
|
let month_ = ctx.now.month();
|
|
let day_ = ctx.now.month_day();
|
|
let week_day_ = ctx.now.week_day();
|
|
if (year_, month_, day_, week_day_)
|
|
!= (
|
|
mode_state.year,
|
|
mode_state.month,
|
|
mode_state.day,
|
|
mode_state.week_day,
|
|
) {
|
|
mode_state.year = year_;
|
|
mode_state.month = month_;
|
|
mode_state.day = day_;
|
|
mode_state.week_day = week_day_;
|
|
Text::with_alignment(
|
|
unsafe {
|
|
core::str::from_utf8_unchecked(&strf::fmt_time_ymdw(
|
|
year_, month_, day_, week_day_,
|
|
))
|
|
},
|
|
ctx.display.inner().bounding_box().center() + Point::new(0, -20),
|
|
clock_text_style,
|
|
Alignment::Center,
|
|
)
|
|
.draw(ctx.display.inner_mut())
|
|
.unwrap();
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
}
|