Ctx, SD
This commit is contained in:
parent
8dda09689d
commit
d8865488e3
12 changed files with 328 additions and 166 deletions
192
src/main.rs
192
src/main.rs
|
|
@ -5,25 +5,19 @@ mod apps;
|
|||
mod config;
|
||||
mod display;
|
||||
mod energy;
|
||||
mod fs;
|
||||
mod gui;
|
||||
mod keypad;
|
||||
mod state;
|
||||
mod strf;
|
||||
mod time;
|
||||
|
||||
use apps::App;
|
||||
use display::Display;
|
||||
use energy::EnergyStatus;
|
||||
use keypad::KeyEvents;
|
||||
use state::*;
|
||||
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(not(feature = "simulator"))] {
|
||||
use maduino_zero_4g as bsp;
|
||||
use bsp::hal;
|
||||
|
||||
use hal::pac::{CorePeripherals, Peripherals};
|
||||
use hal::prelude::*;
|
||||
}
|
||||
}
|
||||
|
||||
use arrayvec::ArrayString;
|
||||
use core::fmt::Write;
|
||||
use embedded_graphics::{
|
||||
|
|
@ -33,36 +27,55 @@ use embedded_graphics::{
|
|||
primitives::{Line, PrimitiveStyle, PrimitiveStyleBuilder, Rectangle, StrokeAlignment},
|
||||
text::{Alignment, Text},
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "simulator"))]
|
||||
use panic_halt as _;
|
||||
|
||||
static mut STATE: State = State {
|
||||
energy: EnergyStatus {
|
||||
battery: 255,
|
||||
charging: false,
|
||||
},
|
||||
hour: 0,
|
||||
minute: 0,
|
||||
mode: Mode::Clock {
|
||||
year: 0,
|
||||
month: 0,
|
||||
day: 0,
|
||||
week_day: 0,
|
||||
use maduino_zero_4g::{
|
||||
self as bsp,
|
||||
hal::{
|
||||
clock::GenericClockController,
|
||||
delay::Delay,
|
||||
pac::{CorePeripherals, Peripherals},
|
||||
prelude::*,
|
||||
},
|
||||
};
|
||||
|
||||
fn state() -> &'static State {
|
||||
unsafe { &STATE }
|
||||
}
|
||||
|
||||
fn state_mut() -> &'static mut State {
|
||||
unsafe { &mut STATE }
|
||||
}
|
||||
#[cfg(not(feature = "simulator"))]
|
||||
use panic_halt as _;
|
||||
use tz::DateTime;
|
||||
|
||||
#[cfg_attr(not(feature = "simulator"), bsp::entry)]
|
||||
fn main() -> ! {
|
||||
let mut display = display::Display::new();
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(not(feature = "simulator"))] {
|
||||
let mut peripherals = Peripherals::take().unwrap();
|
||||
let mut pins = maduino_zero_4g::Pins::new(peripherals.PORT);
|
||||
let core = CorePeripherals::take().unwrap();
|
||||
let mut clocks = GenericClockController::with_external_32kosc(
|
||||
peripherals.GCLK,
|
||||
&mut peripherals.PM,
|
||||
&mut peripherals.SYSCTRL,
|
||||
&mut peripherals.NVMCTRL,
|
||||
);
|
||||
let mut delay = Delay::new(core.SYST, &mut clocks);
|
||||
|
||||
let mut fs = fs::Fs::new(atsamd_hal::sercom::spi::Config::new(
|
||||
&peripherals.PM,
|
||||
peripherals.SERCOM4,
|
||||
atsamd_hal::sercom::spi::Pads::default().data_in(pins.sd_miso).data_out(pins.sd_mosi).sclk(pins.sd_sck),
|
||||
10_u32.mhz(),
|
||||
).enable(), pins.d4.into_push_pull_output());
|
||||
}
|
||||
}
|
||||
|
||||
let mut state = State {
|
||||
energy: EnergyStatus {
|
||||
battery: 255,
|
||||
charging: false,
|
||||
},
|
||||
hour: 0,
|
||||
minute: 0,
|
||||
};
|
||||
let mut mode_state = ModeState::Clock(Default::default());
|
||||
|
||||
let mut display = Display::new();
|
||||
let mut keypad = keypad::Keypad::default();
|
||||
|
||||
let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1);
|
||||
|
|
@ -73,11 +86,6 @@ fn main() -> ! {
|
|||
.text_color(BinaryColor::On)
|
||||
.background_color(BinaryColor::Off)
|
||||
.build();
|
||||
let clock_text_style = MonoTextStyleBuilder::new()
|
||||
.font(&FONT_10X20)
|
||||
.text_color(BinaryColor::On)
|
||||
.background_color(BinaryColor::Off)
|
||||
.build();
|
||||
|
||||
Line::new(Point::new(0, 13), Point::new(199, 13))
|
||||
.into_styled(thin_stroke)
|
||||
|
|
@ -87,13 +95,19 @@ fn main() -> ! {
|
|||
#[cfg(feature = "simulator")]
|
||||
display.update();
|
||||
|
||||
let mut update = true;
|
||||
let mut hm_change = true;
|
||||
let mut ctx = Context {
|
||||
display: &mut display,
|
||||
hm_change: true,
|
||||
key_events: Default::default(),
|
||||
now: DateTime::from_timespec(0, 0, tz::TimeZoneRef::utc()).unwrap(),
|
||||
update: true,
|
||||
state: &mut state,
|
||||
};
|
||||
loop {
|
||||
let energy_status = energy::get_energy_status();
|
||||
|
||||
if energy_status != state().energy {
|
||||
update = true;
|
||||
if energy_status != ctx.state.energy {
|
||||
ctx.update = true;
|
||||
|
||||
Text::with_alignment(
|
||||
unsafe { core::str::from_utf8_unchecked(&strf::fmt_energy(&energy_status)) },
|
||||
|
|
@ -101,21 +115,21 @@ fn main() -> ! {
|
|||
statusbar_text_style,
|
||||
Alignment::Left,
|
||||
)
|
||||
.draw(display.inner_mut())
|
||||
.draw(ctx.display.inner_mut())
|
||||
.unwrap();
|
||||
|
||||
state_mut().energy = energy_status;
|
||||
ctx.state.energy = energy_status;
|
||||
}
|
||||
|
||||
let now = time::now();
|
||||
ctx.now = time::now();
|
||||
|
||||
let hour = now.hour();
|
||||
let minute = now.minute();
|
||||
if (hour, minute) != (state().hour, state().minute) {
|
||||
update = true;
|
||||
hm_change = true;
|
||||
state_mut().hour = hour;
|
||||
state_mut().minute = minute;
|
||||
let hour = ctx.now.hour();
|
||||
let minute = ctx.now.minute();
|
||||
if (hour, minute) != (ctx.state.hour, ctx.state.minute) {
|
||||
ctx.update = true;
|
||||
ctx.hm_change = true;
|
||||
ctx.state.hour = hour;
|
||||
ctx.state.minute = minute;
|
||||
|
||||
Text::with_alignment(
|
||||
unsafe { core::str::from_utf8_unchecked(&strf::fmt_time_hm(hour, minute)) },
|
||||
|
|
@ -123,67 +137,41 @@ fn main() -> ! {
|
|||
statusbar_text_style,
|
||||
Alignment::Right,
|
||||
)
|
||||
.draw(display.inner_mut())
|
||||
.draw(ctx.display.inner_mut())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let key_events = keypad.update();
|
||||
ctx.key_events = keypad.update();
|
||||
|
||||
match &mut state_mut().mode {
|
||||
Mode::Clock {
|
||||
year,
|
||||
month,
|
||||
day,
|
||||
week_day,
|
||||
} => {
|
||||
if hm_change {
|
||||
Text::with_alignment(
|
||||
unsafe { core::str::from_utf8_unchecked(&strf::fmt_time_hm(hour, minute)) },
|
||||
display.inner().bounding_box().center() + Point::new(0, 10),
|
||||
clock_text_style,
|
||||
Alignment::Center,
|
||||
)
|
||||
.draw(display.inner_mut())
|
||||
.unwrap();
|
||||
|
||||
let year_ = now.year();
|
||||
let month_ = now.month();
|
||||
let day_ = now.month_day();
|
||||
let week_day_ = now.week_day();
|
||||
if (year_, month_, day_, week_day_) != (*year, *month, *day, *week_day) {
|
||||
*year = year_;
|
||||
*month = month_;
|
||||
*day = day_;
|
||||
*week_day = week_day_;
|
||||
Text::with_alignment(
|
||||
unsafe {
|
||||
core::str::from_utf8_unchecked(&strf::fmt_time_ymdw(
|
||||
year_, month_, day_, week_day_,
|
||||
))
|
||||
},
|
||||
display.inner().bounding_box().center() + Point::new(0, -20),
|
||||
clock_text_style,
|
||||
Alignment::Center,
|
||||
)
|
||||
.draw(display.inner_mut())
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(new_mode_state) = match &mut mode_state {
|
||||
ModeState::Clock(clock_state) => apps::clock::Clock::update(&mut ctx, clock_state),
|
||||
ModeState::Dial(dial_state) => apps::dial::Dial::update(&mut ctx, dial_state),
|
||||
} {
|
||||
mode_state = new_mode_state;
|
||||
}
|
||||
|
||||
#[cfg(feature = "simulator")]
|
||||
{
|
||||
display.update();
|
||||
ctx.display.update();
|
||||
std::thread::sleep(core::time::Duration::from_millis(50));
|
||||
}
|
||||
#[cfg(not(feature = "simulator"))]
|
||||
if update {
|
||||
display.update();
|
||||
if ctx.update {
|
||||
ctx.display.update();
|
||||
}
|
||||
// TODO sleep on samd
|
||||
#[cfg(not(feature = "simulator"))]
|
||||
delay.delay_ms(50_u8);
|
||||
|
||||
update = false;
|
||||
hm_change = false;
|
||||
ctx.update = false;
|
||||
ctx.hm_change = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Context<'a> {
|
||||
pub state: &'a mut State,
|
||||
pub display: &'a mut Display,
|
||||
pub hm_change: bool,
|
||||
pub now: DateTime,
|
||||
pub update: bool,
|
||||
pub key_events: KeyEvents,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue