-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlight.rs
126 lines (109 loc) · 3.98 KB
/
light.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
use std::cmp::{max, min};
use embedded_graphics::{
mono_font::MonoTextStyle,
pixelcolor::Rgb565,
prelude::*,
primitives::{PrimitiveStyleBuilder, Rectangle},
text::Text,
Drawable,
};
use ft6x36::{Direction, TouchEvent};
use profont::PROFONT_18_POINT;
use log::*;
use u8g2_fonts::{
fonts,
types::{FontColor, HorizontalAlignment, VerticalPosition},
FontRenderer,
};
use crate::{
events::{Kind, TwatchEvent},
tiles::WatchTile,
};
#[derive(Default)]
pub struct LightTile {}
unsafe impl Send for LightTile {}
impl WatchTile for LightTile {
fn run(&mut self, hal: &mut crate::twatch::Hal<'static>) -> anyhow::Result<()> {
self.display_tile(hal)?;
hal.display.commit_display()?;
Ok(())
}
fn display_tile(&self, hal: &mut crate::twatch::Hal<'static>) -> anyhow::Result<()> {
let style = MonoTextStyle::new(&PROFONT_18_POINT, Rgb565::WHITE);
let font = FontRenderer::new::<fonts::u8g2_font_logisoso92_tn>();
let level = format!("Light level: {}", hal.display.get_display_level());
Text::new(&level, Point::new(0, 30), style).draw(&mut hal.display)?;
let rect_style = PrimitiveStyleBuilder::new()
.stroke_width(2)
.stroke_color(Rgb565::BLUE)
.build();
Rectangle::new(Point::new(20, 80), Size::new(90, 90))
.into_styled(rect_style)
.draw(&mut hal.display)?;
font.render_aligned(
"-",
Point::new(65, 160),
VerticalPosition::Baseline,
HorizontalAlignment::Center,
FontColor::Transparent(Rgb565::WHITE),
&mut hal.display,
)
.expect("-");
Rectangle::new(Point::new(130, 80), Size::new(90, 90))
.into_styled(rect_style)
.draw(&mut hal.display)?;
font.render_aligned(
"+",
Point::new(175, 160),
VerticalPosition::Baseline,
HorizontalAlignment::Center,
FontColor::Transparent(Rgb565::WHITE),
&mut hal.display,
)
.expect("+");
Ok(())
}
fn process_event(
&mut self,
hal: &mut crate::twatch::Hal<'static>,
event: crate::events::TwatchEvent,
) -> Option<crate::events::TwatchEvent> {
match (&event.time, &event.kind) {
(_, Kind::Touch(TouchEvent::Swipe(dir, _info))) => match dir {
Direction::Left => {
let mut hello_tile = crate::tiles::hello::HelloTile::default();
let _ = crate::tiles::move_to_tile(hal, self, &mut hello_tile, dir);
Some(TwatchEvent::new(Kind::NewTile(Box::new(hello_tile))))
}
Direction::Right => {
let mut motor_tile = crate::tiles::motor::MotorTile::default();
let _ = crate::tiles::move_to_tile(hal, self, &mut motor_tile, dir);
Some(TwatchEvent::new(Kind::NewTile(Box::new(motor_tile))))
}
_ => {
info!("Swipe: {dir:?}");
None
}
},
(_, Kind::Touch(TouchEvent::TouchOnePoint(p))) => {
if p.y >= 80 && p.y <= 170 {
let mut level = hal.display.get_display_level();
if p.x <= 120 {
level = min(100, level + 15);
} else {
level = max(10, level.saturating_sub(15));
}
hal.display
.set_display_level(level)
.unwrap_or_else(|e| warn!("Unable to change backlight level: {e:?}"));
self.display_tile(hal).expect("Unable to update tile");
hal.display.commit_display().expect("Unable to commit fb");
None
} else {
Some(event)
}
}
_ => Some(event),
}
}
}