-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_set.rs
285 lines (265 loc) · 8.62 KB
/
time_set.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use core::{
cell::RefCell,
ops::{AddAssign, SubAssign},
};
use arduino_hal::{delay_ms, hal::Atmega, usart::UsartOps, Delay};
use embedded_hal::digital::v2::InputPin;
use hd44780_driver::{bus::DataBus, HD44780};
use ufmt::{derive::uDebug, uwrite};
use crate::{
error::RuntimeError,
lcd_writer::LcdWriter,
serial::{SerialHandler, SerialMsg},
LCD_LINE_LENGTH,
};
const BLINK_DURATION: u16 = 100;
const HOLD_THRESHOLD: u16 = 150;
const REPEAT_THRESHOLD: u16 = 20;
const LOOP_DELAY: u16 = 5;
#[derive(uDebug, PartialEq, Eq, Clone, Copy)]
pub enum TimeSetPart {
P1SetMin,
P1SetSec,
P2SetMin,
P2SetSec,
}
#[derive(uDebug, PartialEq, Eq, Clone, Copy)]
pub struct TimeSetting(u16);
impl TimeSetting {
const MAX_TIME: u16 = (60 * 60 * 10) - 1;
pub fn new(seconds: u16) -> TimeSetting {
TimeSetting(seconds)
}
pub fn into_hrs_mins_secs(&self) -> (u8, u8, u8) {
return (
(self.0 / (60 * 60)) as u8,
((self.0 / 60) % 60) as u8,
(self.0 % 60) as u8,
);
}
pub fn into_millis(&self) -> u32 {
self.0 as u32 * 1000
}
}
impl AddAssign<u16> for TimeSetting {
fn add_assign(&mut self, rhs: u16) {
// If overflow or too high, go back to zero
match self.0.checked_add(rhs) {
Some(r) if r <= TimeSetting::MAX_TIME => self.0 = r,
Some(_) | None => self.0 = 0,
}
}
}
impl SubAssign<u16> for TimeSetting {
fn sub_assign(&mut self, rhs: u16) {
// If underflow, wrap to highest value
match self.0.checked_sub(rhs) {
Some(r) => self.0 = r,
None => self.0 = TimeSetting::MAX_TIME,
}
}
}
/// Prompts the user to set the time using the provided pins and LCD. Blocks.
///
/// # Usage
/// ```
/// time_set(
/// pins.d1.into_input(), // Down button
/// pins.d2.into_input(), // Up button
/// pins.d3.into_input() // Select button
/// );
/// ```
pub fn time_set<
DP: InputPin,
UP: InputPin,
SP: InputPin,
B: DataBus,
USART: UsartOps<Atmega, RX, TX>,
RX,
TX,
>(
down_pin: &mut DP,
up_pin: &mut UP,
start_pin: &mut SP,
serial_handler: &mut SerialHandler<USART, RX, TX>,
delay: &mut Delay,
lcd: &RefCell<HD44780<B>>,
writer: &mut LcdWriter<'_, B>,
) -> Result<(TimeSetting, TimeSetting), RuntimeError> {
lcd.borrow_mut()
.set_cursor_pos(0, delay)
.map_err(|_| RuntimeError::LcdError)?;
uwrite!(writer, "P1 Set time P2").map_err(|_| RuntimeError::LcdError)?;
lcd.borrow_mut()
.set_cursor_pos(LCD_LINE_LENGTH * 1, delay)
.map_err(|_| RuntimeError::LcdError)?;
uwrite!(writer, "0:00:00 0:00:00").map_err(|_| RuntimeError::LcdError)?;
let mut state = TimeSetPart::P1SetMin;
let mut p1_setting = TimeSetting::new(0);
let mut p2_setting = TimeSetting::new(0);
let mut down = debouncr::debounce_4(false);
let mut down_hold_count: u16 = 0;
let mut up = debouncr::debounce_4(false);
let mut up_hold_count: u16 = 0;
let mut start = debouncr::debounce_4(false);
let mut blink_count = 0;
let mut last_p1_setting = TimeSetting::new(u16::MAX);
let mut last_p2_setting = TimeSetting::new(u16::MAX);
let mut last_blink = Some(TimeSetPart::P1SetMin);
loop {
// Change blinks
blink_count += 1;
let blink = blink_count >= BLINK_DURATION;
if blink_count >= BLINK_DURATION * 2 {
blink_count = 0;
}
// Update states
if up.update(up_pin.is_low().map_err(|_| RuntimeError::PinReadError)?)
== Some(debouncr::Edge::Rising)
{
// Up press
match state {
TimeSetPart::P1SetMin => p1_setting += 60,
TimeSetPart::P1SetSec => p1_setting += 1,
TimeSetPart::P2SetMin => p2_setting += 60,
TimeSetPart::P2SetSec => p2_setting += 1,
}
up_hold_count = 0;
blink_count = 0;
}
if up_hold_count == HOLD_THRESHOLD {
// Up hold
match state {
TimeSetPart::P1SetMin => p1_setting += 60,
TimeSetPart::P1SetSec => p1_setting += 5,
TimeSetPart::P2SetMin => p2_setting += 60,
TimeSetPart::P2SetSec => p2_setting += 5,
}
blink_count = 0;
up_hold_count += 1;
}
if up.is_high() {
up_hold_count += 1;
}
if up_hold_count >= HOLD_THRESHOLD + REPEAT_THRESHOLD {
up_hold_count = HOLD_THRESHOLD
}
if down.update(down_pin.is_low().map_err(|_| RuntimeError::PinReadError)?)
== Some(debouncr::Edge::Rising)
{
// Down press
match state {
TimeSetPart::P1SetMin => p1_setting -= 60,
TimeSetPart::P1SetSec => p1_setting -= 1,
TimeSetPart::P2SetMin => p2_setting -= 60,
TimeSetPart::P2SetSec => p2_setting -= 1,
}
down_hold_count = 0;
blink_count = 0;
}
if down_hold_count == HOLD_THRESHOLD {
// Down hold
match state {
TimeSetPart::P1SetMin => p1_setting -= 60,
TimeSetPart::P1SetSec => p1_setting -= 5,
TimeSetPart::P2SetMin => p2_setting -= 60,
TimeSetPart::P2SetSec => p2_setting -= 5,
}
blink_count = 0;
down_hold_count += 1;
}
if down.is_high() {
down_hold_count += 1;
}
if down_hold_count >= HOLD_THRESHOLD + REPEAT_THRESHOLD {
down_hold_count = HOLD_THRESHOLD
}
if start.update(start_pin.is_low().map_err(|_| RuntimeError::PinReadError)?)
== Some(debouncr::Edge::Falling)
{
// Start button released; go to next portion
state = match state {
TimeSetPart::P1SetMin => TimeSetPart::P1SetSec,
TimeSetPart::P1SetSec => TimeSetPart::P2SetMin,
TimeSetPart::P2SetMin => TimeSetPart::P2SetSec,
TimeSetPart::P2SetSec => break,
}
}
// Render results
lcd.borrow_mut()
.set_cursor_pos(LCD_LINE_LENGTH * 1, delay)
.map_err(|_| RuntimeError::LcdError)?;
let new_blink = if blink { Some(state) } else { None };
if p1_setting != last_p1_setting || p2_setting != last_p2_setting {
serial_handler.write(SerialMsg::Sync {
p1_time: p1_setting.into_millis(),
p2_time: p2_setting.into_millis(),
});
}
if p1_setting != last_p1_setting || p2_setting != last_p2_setting || new_blink != last_blink
{
render_time(&p1_setting, &p2_setting, new_blink, writer)
.map_err(|_| RuntimeError::LcdError)?;
last_p1_setting = p1_setting;
last_p2_setting = p2_setting;
last_blink = new_blink;
} else {
delay_ms(LOOP_DELAY);
}
}
Ok((p1_setting, p2_setting))
}
pub fn render_time<B: DataBus>(
p1_time: &TimeSetting,
p2_time: &TimeSetting,
blink_off_part: Option<TimeSetPart>,
writer: &mut LcdWriter<'_, B>,
) -> Result<(), hd44780_driver::error::Error> {
let p1_parts = p1_time.into_hrs_mins_secs();
if !blink_off_part.is_some_and(|b| b == TimeSetPart::P1SetMin) {
// Hour
uwrite!(writer, "{}:", p1_parts.0.min(9))?;
// Minute
if p1_parts.1 > 9 {
uwrite!(writer, "{}:", p1_parts.1)?;
} else {
uwrite!(writer, "0{}:", p1_parts.1)?;
}
} else {
uwrite!(writer, " : :")?;
}
if !blink_off_part.is_some_and(|b| b == TimeSetPart::P1SetSec) {
// Second
if p1_parts.2 > 9 {
uwrite!(writer, "{} ", p1_parts.2)?;
} else {
uwrite!(writer, "0{} ", p1_parts.2)?;
}
} else {
uwrite!(writer, " ")?;
}
let p2_parts = p2_time.into_hrs_mins_secs();
if !blink_off_part.is_some_and(|b| b == TimeSetPart::P2SetMin) {
// Hour
uwrite!(writer, "{}:", p2_parts.0.min(9))?;
// Minute
if p2_parts.1 > 9 {
uwrite!(writer, "{}:", p2_parts.1)?;
} else {
uwrite!(writer, "0{}:", p2_parts.1)?;
}
} else {
uwrite!(writer, " : :")?;
}
if !blink_off_part.is_some_and(|b| b == TimeSetPart::P2SetSec) {
// Second
if p2_parts.2 > 9 {
uwrite!(writer, "{}", p2_parts.2)?;
} else {
uwrite!(writer, "0{}", p2_parts.2)?;
}
} else {
uwrite!(writer, " ")?;
}
Ok(())
}