Initial commit
This commit is contained in:
commit
726632a35a
19 changed files with 2467 additions and 0 deletions
9
src/apps.rs
Normal file
9
src/apps.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use crate::display::Display;
|
||||
|
||||
pub mod dial;
|
||||
|
||||
pub trait App {
|
||||
type State;
|
||||
|
||||
fn update(display: &mut Display);
|
||||
}
|
||||
1
src/apps/dial.rs
Normal file
1
src/apps/dial.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
1
src/config.rs
Normal file
1
src/config.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub struct Config {}
|
||||
9
src/display.rs
Normal file
9
src/display.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "simulator")] {
|
||||
mod simulator;
|
||||
pub use simulator::*;
|
||||
} else {
|
||||
mod epd;
|
||||
pub use epd::*;
|
||||
}
|
||||
}
|
||||
28
src/display/epd.rs
Normal file
28
src/display/epd.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
|
||||
use epd_waveshare::epd1in54::{Display1in54, Epd1in54};
|
||||
|
||||
pub struct Display {
|
||||
//epd: Epd1in54,
|
||||
display: Display1in54,
|
||||
}
|
||||
|
||||
impl Display {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
//epd: Epd1in54::new(&mut spi, cs, busy, dc, rst, &mut delay),
|
||||
display: Display1in54::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&self) {
|
||||
//self.epd.update_and_display_frame( & mut spi, & self.display.buffer());
|
||||
}
|
||||
|
||||
pub fn inner(&self) -> &Display1in54 {
|
||||
&self.display
|
||||
}
|
||||
|
||||
pub fn inner_mut(&mut self) -> &mut Display1in54 {
|
||||
&mut self.display
|
||||
}
|
||||
}
|
||||
47
src/display/simulator.rs
Normal file
47
src/display/simulator.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
use embedded_graphics::{pixelcolor::BinaryColor, prelude::*};
|
||||
use embedded_graphics_simulator::{
|
||||
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
|
||||
};
|
||||
|
||||
static mut WINDOW: core::mem::MaybeUninit<Window> = core::mem::MaybeUninit::uninit();
|
||||
|
||||
pub fn window() -> &'static Window {
|
||||
unsafe { WINDOW.assume_init_ref() }
|
||||
}
|
||||
|
||||
pub fn window_mut() -> &'static mut Window {
|
||||
unsafe { WINDOW.assume_init_mut() }
|
||||
}
|
||||
|
||||
pub struct Display {
|
||||
display: SimulatorDisplay<BinaryColor>,
|
||||
}
|
||||
|
||||
impl Display {
|
||||
pub fn new() -> Self {
|
||||
let output_settings = OutputSettingsBuilder::new()
|
||||
.theme(BinaryColorTheme::Default)
|
||||
.build();
|
||||
|
||||
unsafe { WINDOW.write(Window::new("Rustphone", &output_settings)) };
|
||||
|
||||
Self {
|
||||
display: SimulatorDisplay::new(Size::new(200, 200)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
window_mut().update(&self.display);
|
||||
if window_mut().events().any(|e| e == SimulatorEvent::Quit) {
|
||||
std::process::exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn inner(&self) -> &SimulatorDisplay<BinaryColor> {
|
||||
&self.display
|
||||
}
|
||||
|
||||
pub fn inner_mut(&mut self) -> &mut SimulatorDisplay<BinaryColor> {
|
||||
&mut self.display
|
||||
}
|
||||
}
|
||||
13
src/energy.rs
Normal file
13
src/energy.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#[derive(Eq, PartialEq)]
|
||||
pub struct EnergyStatus {
|
||||
/// Battery percentage
|
||||
pub battery: u8,
|
||||
pub charging: bool,
|
||||
}
|
||||
|
||||
pub fn get_energy_status() -> EnergyStatus {
|
||||
EnergyStatus {
|
||||
battery: 100,
|
||||
charging: true,
|
||||
}
|
||||
}
|
||||
1
src/gui.rs
Normal file
1
src/gui.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
144
src/keypad.rs
Normal file
144
src/keypad.rs
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
use arrayvec::ArrayVec;
|
||||
|
||||
const NB_KEYS: usize = 21;
|
||||
/// Key repeat max delay (ms)
|
||||
const REPEAT_DELAY: u64 = 500;
|
||||
|
||||
pub struct Keypad {
|
||||
last_key: Option<(Key, u64, u8)>,
|
||||
pressed: [bool; NB_KEYS],
|
||||
}
|
||||
|
||||
impl Default for Keypad {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
last_key: None,
|
||||
pressed: [false; NB_KEYS],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Keypad {
|
||||
pub fn update(&mut self) -> ArrayVec<KeyEvent, 4> {
|
||||
self.get_keys()
|
||||
.into_iter()
|
||||
.zip(self.pressed.iter_mut())
|
||||
.zip(0u8..)
|
||||
.filter_map(|((new_pressed, old_pressed), key)| {
|
||||
let mut repeats = 0;
|
||||
let event_type = match (*old_pressed, new_pressed) {
|
||||
(true, true) => KeyEventType::Down,
|
||||
(true, false) => {
|
||||
*old_pressed = new_pressed;
|
||||
KeyEventType::Released
|
||||
}
|
||||
(false, true) => {
|
||||
*old_pressed = new_pressed;
|
||||
'count_repeats: {
|
||||
if let Some((last_key, last_when, last_repeats)) = &mut self.last_key {
|
||||
if *last_key as u8 == key {
|
||||
let now = crate::time::millis();
|
||||
if now.wrapping_sub(*last_when) < REPEAT_DELAY {
|
||||
*last_repeats = last_repeats.saturating_add(1);
|
||||
} else {
|
||||
*last_repeats = 1;
|
||||
}
|
||||
repeats = *last_repeats;
|
||||
*last_when = now;
|
||||
break 'count_repeats;
|
||||
}
|
||||
}
|
||||
self.last_key =
|
||||
Some((Key::from_u8_unchecked(key), crate::time::millis(), 1));
|
||||
}
|
||||
KeyEventType::Pressed
|
||||
}
|
||||
(false, false) => return None,
|
||||
};
|
||||
|
||||
Some(KeyEvent {
|
||||
key: Key::from_u8_unchecked(key),
|
||||
event_type,
|
||||
repeats,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(feature = "simulator")]
|
||||
fn get_keys(&self) -> [bool; NB_KEYS] {
|
||||
use embedded_graphics_simulator::sdl2::Keycode;
|
||||
use embedded_graphics_simulator::SimulatorEvent;
|
||||
|
||||
let mut keys = [false; NB_KEYS];
|
||||
crate::display::window_mut()
|
||||
.events()
|
||||
.filter_map(|event| match event {
|
||||
SimulatorEvent::KeyDown { keycode, .. } => match keycode {
|
||||
Keycode::Num0 | Keycode::Kp0 => Some(Key::D0),
|
||||
Keycode::Num1 | Keycode::Kp1 => Some(Key::D1),
|
||||
Keycode::Num2 | Keycode::Kp2 => Some(Key::D2),
|
||||
Keycode::Num3 | Keycode::Kp3 => Some(Key::D3),
|
||||
Keycode::Num4 | Keycode::Kp4 => Some(Key::D4),
|
||||
Keycode::Num5 | Keycode::Kp5 => Some(Key::D5),
|
||||
Keycode::Num6 | Keycode::Kp6 => Some(Key::D6),
|
||||
Keycode::Num7 | Keycode::Kp7 => Some(Key::D7),
|
||||
Keycode::Num8 | Keycode::Kp8 => Some(Key::D8),
|
||||
Keycode::Num9 | Keycode::Kp9 => Some(Key::D9),
|
||||
Keycode::KpEnter | Keycode::Return => Some(Key::Enter),
|
||||
Keycode::Asterisk => Some(Key::Asterisk),
|
||||
Keycode::Hash | Keycode::KpHash => Some(Key::Hash),
|
||||
// TODO more
|
||||
_ => None,
|
||||
},
|
||||
SimulatorEvent::Quit => std::process::exit(0),
|
||||
_ => None,
|
||||
})
|
||||
.for_each(|key| keys[key as usize] = true);
|
||||
keys
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
#[repr(u8)]
|
||||
pub enum Key {
|
||||
D0 = 0,
|
||||
D1 = 1,
|
||||
D2 = 2,
|
||||
D3 = 3,
|
||||
D4 = 4,
|
||||
D5 = 5,
|
||||
D6 = 6,
|
||||
D7 = 7,
|
||||
D8 = 8,
|
||||
D9 = 9,
|
||||
Enter = 10,
|
||||
OptionRight = 11,
|
||||
OptionLeft = 12,
|
||||
Left = 13,
|
||||
Right = 14,
|
||||
Top = 15,
|
||||
Down = 16,
|
||||
Asterisk = 17,
|
||||
Hash = 18,
|
||||
PickUp = 19,
|
||||
HangUp = 20,
|
||||
}
|
||||
|
||||
impl Key {
|
||||
fn from_u8_unchecked(key: u8) -> Self {
|
||||
unsafe { core::mem::transmute(key) }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KeyEvent {
|
||||
event_type: KeyEventType,
|
||||
key: Key,
|
||||
repeats: u8,
|
||||
}
|
||||
|
||||
pub enum KeyEventType {
|
||||
Pressed,
|
||||
Down,
|
||||
Released,
|
||||
}
|
||||
179
src/main.rs
Normal file
179
src/main.rs
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
#![cfg_attr(not(feature = "simulator"), no_std)]
|
||||
#![cfg_attr(not(feature = "simulator"), no_main)]
|
||||
|
||||
mod apps;
|
||||
mod config;
|
||||
mod display;
|
||||
mod energy;
|
||||
mod gui;
|
||||
mod keypad;
|
||||
mod state;
|
||||
mod strf;
|
||||
mod time;
|
||||
|
||||
use energy::EnergyStatus;
|
||||
use state::*;
|
||||
|
||||
use arrayvec::ArrayString;
|
||||
use core::fmt::Write;
|
||||
use embedded_graphics::{
|
||||
mono_font::{ascii::FONT_10X20, ascii::FONT_6X10, ascii::FONT_9X15, MonoTextStyleBuilder},
|
||||
pixelcolor::BinaryColor,
|
||||
prelude::*,
|
||||
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,
|
||||
},
|
||||
};
|
||||
|
||||
fn state() -> &'static State {
|
||||
unsafe { &STATE }
|
||||
}
|
||||
|
||||
fn state_mut() -> &'static mut State {
|
||||
unsafe { &mut STATE }
|
||||
}
|
||||
|
||||
#[cfg_attr(not(feature = "simulator"), cortex_m_rt::entry)]
|
||||
fn main() -> ! {
|
||||
let mut display = display::Display::new();
|
||||
let mut keypad = keypad::Keypad::default();
|
||||
|
||||
let thin_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 1);
|
||||
let thick_stroke = PrimitiveStyle::with_stroke(BinaryColor::On, 3);
|
||||
let fill = PrimitiveStyle::with_fill(BinaryColor::On);
|
||||
let statusbar_text_style = MonoTextStyleBuilder::new()
|
||||
.font(&FONT_9X15)
|
||||
.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)
|
||||
.draw(display.inner_mut())
|
||||
.unwrap();
|
||||
|
||||
#[cfg(feature = "simulator")]
|
||||
display.update();
|
||||
|
||||
let mut update = true;
|
||||
let mut hm_change = true;
|
||||
loop {
|
||||
let energy_status = energy::get_energy_status();
|
||||
|
||||
if energy_status != state().energy {
|
||||
update = true;
|
||||
|
||||
Text::with_alignment(
|
||||
unsafe { core::str::from_utf8_unchecked(&strf::fmt_energy(&energy_status)) },
|
||||
Point::new(0, 9),
|
||||
statusbar_text_style,
|
||||
Alignment::Left,
|
||||
)
|
||||
.draw(display.inner_mut())
|
||||
.unwrap();
|
||||
|
||||
state_mut().energy = energy_status;
|
||||
}
|
||||
|
||||
let 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;
|
||||
|
||||
Text::with_alignment(
|
||||
unsafe { core::str::from_utf8_unchecked(&strf::fmt_time_hm(hour, minute)) },
|
||||
Point::new(199, 9),
|
||||
statusbar_text_style,
|
||||
Alignment::Right,
|
||||
)
|
||||
.draw(display.inner_mut())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "simulator")]
|
||||
{
|
||||
display.update();
|
||||
std::thread::sleep(core::time::Duration::from_millis(50));
|
||||
}
|
||||
#[cfg(not(feature = "simulator"))]
|
||||
if update {
|
||||
display.update();
|
||||
}
|
||||
// TODO sleep on samd
|
||||
|
||||
update = false;
|
||||
hm_change = false;
|
||||
}
|
||||
}
|
||||
18
src/state.rs
Normal file
18
src/state.rs
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use crate::energy::EnergyStatus;
|
||||
|
||||
/// Global state
|
||||
pub struct State {
|
||||
pub energy: EnergyStatus,
|
||||
pub hour: u8,
|
||||
pub minute: u8,
|
||||
pub mode: Mode,
|
||||
}
|
||||
|
||||
pub enum Mode {
|
||||
Clock {
|
||||
year: i32,
|
||||
month: u8,
|
||||
day: u8,
|
||||
week_day: u8,
|
||||
},
|
||||
}
|
||||
57
src/strf.rs
Normal file
57
src/strf.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use tz::DateTime;
|
||||
|
||||
use crate::energy::EnergyStatus;
|
||||
|
||||
static WEEK_DAYS: [[u8; 3]; 7] = [
|
||||
*b"Sun", *b"Mon", *b"Tue", *b"Wed", *b"Thu", *b"Fri", *b"Sat",
|
||||
];
|
||||
|
||||
/// hh:mm
|
||||
pub fn fmt_time_hm(hour: u8, minute: u8) -> [u8; 5] {
|
||||
let mut buf = *b"00:00";
|
||||
buf[0] += hour / 10;
|
||||
buf[1] += hour % 10;
|
||||
buf[3] += minute / 10;
|
||||
buf[4] += minute % 10;
|
||||
buf
|
||||
}
|
||||
|
||||
/// yyyy-mm-dd Www
|
||||
pub fn fmt_time_ymdw(year: i32, month: u8, day: u8, week_day: u8) -> [u8; 14] {
|
||||
let mut buf = *b"0000-00-00 \x00\x00\x00";
|
||||
|
||||
let mut year = year as u16 % 10_000;
|
||||
buf[3] += (year % 10) as u8;
|
||||
year /= 10;
|
||||
buf[2] += (year % 10) as u8;
|
||||
year /= 10;
|
||||
buf[1] += (year % 10) as u8;
|
||||
year /= 10;
|
||||
buf[0] += year as u8;
|
||||
|
||||
buf[5] += month / 10;
|
||||
buf[6] += month % 10;
|
||||
|
||||
buf[8] += day / 10;
|
||||
buf[9] += day % 10;
|
||||
|
||||
buf[11..14].copy_from_slice(&WEEK_DAYS[week_day as usize]);
|
||||
|
||||
buf
|
||||
}
|
||||
|
||||
pub fn fmt_energy(energy_status: &EnergyStatus) -> [u8; 4] {
|
||||
let mut buf = *b" 0 ";
|
||||
|
||||
buf[3] |= energy_status.charging as u8 * b'+';
|
||||
|
||||
let mut battery = energy_status.battery;
|
||||
buf[2] += energy_status.battery % 10;
|
||||
battery /= 10;
|
||||
buf[1] |= (battery > 9) as u8 * (b'0' + battery % 10);
|
||||
battery /= 10;
|
||||
let d = battery % 10;
|
||||
buf[0] |= d * (b'0' + d);
|
||||
|
||||
buf
|
||||
}
|
||||
24
src/time.rs
Normal file
24
src/time.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
pub fn now() -> tz::DateTime {
|
||||
tz::DateTime::from_timespec(timestamp() as i64, 0, tz::TimeZoneRef::utc()).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(feature = "simulator")]
|
||||
pub fn timestamp() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "simulator"))]
|
||||
pub fn timestamp() -> u64 {
|
||||
1692450980
|
||||
}
|
||||
|
||||
#[cfg(feature = "simulator")]
|
||||
pub fn millis() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_millis() as u64
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue