-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime.rs
185 lines (159 loc) · 5.69 KB
/
time.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::time::Duration;
use accelerometer::vector::F32x3;
use anyhow::Result;
use embedded_svc::event_bus::Postbox;
use embedded_svc::timer::{PeriodicTimer, TimerService};
use esp_idf_svc::timer::{EspTimer, EspTimerService};
use ft6x36::{Direction, TouchEvent};
use log::*;
use embedded_graphics::mono_font::MonoTextStyle;
use embedded_graphics::pixelcolor::Rgb565;
use embedded_graphics::prelude::*;
use embedded_graphics::text::Text;
use embedded_graphics::Drawable;
use pcf8563::DateTime;
use profont::{PROFONT_24_POINT, PROFONT_12_POINT};
use u8g2_fonts::{self, fonts, FontRenderer};
use accelerometer::Accelerometer;
use u8g2_fonts::types::{FontColor, HorizontalAlignment, VerticalPosition};
use crate::events::{Kind, TwatchEvent, TwatchRawEvent};
use crate::tiles::WatchTile;
use crate::twatch::Hal;
pub struct TimeTile {
battery_level: f32,
time: DateTime,
accel: F32x3,
timer: Option<EspTimer>,
}
impl Default for TimeTile {
fn default() -> Self {
Self {
battery_level: 0.0,
time: DateTime {
year: 0,
month: 0,
day: 0,
weekday: 0,
hours: 0,
minutes: 0,
seconds: 0,
},
accel: F32x3::default(),
timer: None,
}
}
}
unsafe impl Send for TimeTile {}
impl WatchTile for TimeTile {
fn init(&mut self, hal: &mut Hal<'static>) -> Result<()> {
let mut timer_loop = hal.eventloop.clone();
let mut periodic_timer = EspTimerService::new()?.timer(move || {
let _ = timer_loop.post(
&TwatchRawEvent::Timer.into(),
Some(Duration::from_millis(0)),
);
})?;
periodic_timer.every(Duration::from_secs(1))?;
self.timer = Some(periodic_timer);
Ok(())
}
fn run(&mut self, hal: &mut Hal<'static>) -> Result<()> {
self.update_state(hal);
self.display_tile(hal)?;
hal.display.commit_display()?;
Ok(())
}
fn process_event(
&mut self,
hal: &mut Hal<'static>,
event: crate::events::TwatchEvent,
) -> Option<TwatchEvent> {
match (&event.time, &event.kind) {
(_, Kind::Touch(TouchEvent::Swipe(dir, _info))) => match dir {
Direction::Right => {
let mut ferris_tile = crate::tiles::ferris::FerrisTile::default();
let _ = crate::tiles::move_to_tile(hal, self, &mut ferris_tile, dir);
Some(TwatchEvent::new(Kind::NewTile(Box::new(ferris_tile))))
}
_ => {
info!("Swipe: {dir:?}");
let _ = self
.display_swipe(hal, *dir, Default::default())
.map_err(|e| warn!("Error displaying swipe: {e:?}"));
None
}
},
(_, Kind::Timer) => {
self.update_state(hal);
let _ = self
.display_tile(hal)
.map_err(|e| warn!("Error refreshing state: {e:?}"));
let _ = hal
.display
.commit_display()
.map_err(|e| warn!("Error refreshing state: {e:?}"));
None
}
_ => Some(event),
}
}
fn display_tile(&self, hal: &mut Hal<'static>) -> Result<()> {
let font = FontRenderer::new::<fonts::u8g2_font_logisoso78_tn>();
let style = MonoTextStyle::new(&PROFONT_24_POINT, Rgb565::WHITE);
let small_style = MonoTextStyle::new(&PROFONT_12_POINT, Rgb565::WHITE);
let battery_level = format!("Bat: {:>3}%", self.battery_level.round());
Text::new(&battery_level, Point::new(30, 30), style).draw(&mut hal.display)?;
let time = format!(
"{:02}:{:02}",
self.time.hours,
self.time.minutes //, self.time.seconds
);
font.render_aligned(
time.as_str(),
hal.display.bounding_box().center() + Point::new(0, 16),
VerticalPosition::Baseline,
HorizontalAlignment::Center,
FontColor::Transparent(Rgb565::WHITE),
&mut hal.display,
)
.expect("Unable to render time");
let accel = format!(
"x:{:.2} y:{:.2} z:{:.2}",
self.accel.x, self.accel.y, self.accel.z
);
Text::new(&accel, Point::new(30, 220), small_style).draw(&mut hal.display)?;
Ok(())
}
fn update_state(&mut self, hal: &mut Hal<'static>) {
match hal.pmu.get_battery_percentage() {
Ok(battery_level) => self.battery_level = battery_level,
Err(err) => error!("Error updating battery level: {}", err),
}
match hal.clock.get_datetime() {
Ok(time) => self.time = time,
Err(err) => error!("Error getting time: {:?}", err),
}
match hal.accel.accel_norm() {
Ok(accel) => self.accel = accel,
Err(err) => error!("Error updating accelerometer values: {:?}", err),
}
}
}
impl TimeTile {
fn display_swipe(
&mut self,
hal: &mut Hal<'static>,
direction: ft6x36::Direction,
offset: Point,
) -> Result<()> {
self.update_state(hal);
let text = format!("{direction:?}");
let style = MonoTextStyle::new(&PROFONT_24_POINT, Rgb565::WHITE);
Text::new(&text, Point::new(30, 170) + offset, style).draw(&mut hal.display)?;
self.display_tile(hal)?;
hal.display.commit_display()?;
std::thread::sleep(Duration::from_millis(300));
self.display_tile(hal)?;
hal.display.commit_display()
}
}