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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use std::fmt::{Display, Formatter};
use std::sync::Arc;
use parking_lot::RwLock;
use crate::devices::{Device, Input, InputEvent};
use crate::errors::Error;
use crate::hardware::Hardware;
use crate::io::{IoProtocol, PinIdOrName, PinModeId};
use crate::pause;
use crate::utils::{task, EventHandler, EventManager, State, TaskHandler};
/// Represents a simple push button as an input of the board.
/// <https://docs.arduino.cc/built-in-examples/digital/Button>
///
/// This structure is very similar to [`DigitalInput`](crate::devices::DigitalInput) but exposes convenience methods to handle two sorts of buttons:
/// - pull-down: when the button is configured with a pin to ground by default (ie button press => pin becomes high)
/// - pull-up: when the button is configured with a pin to +Vin by default (ie button press => pin becomes low)
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug)]
pub struct Button {
// ########################################
// # Basics
/// The pin (id) of the [`Board`] used to read the button value.
pin: u8,
/// The current Button state.
#[cfg_attr(feature = "serde", serde(with = "crate::devices::arc_rwlock_serde"))]
state: Arc<RwLock<bool>>,
/// Inverts the true/false state value.
invert: bool,
/// Defines a PULL-UP mode button.
pullup: bool,
// ########################################
// # Volatile utility data.
#[cfg_attr(feature = "serde", serde(skip))]
protocol: Box<dyn IoProtocol>,
/// Inner handler to the task running the button value check.
#[cfg_attr(feature = "serde", serde(skip))]
handler: Arc<RwLock<Option<TaskHandler>>>,
/// The event manager for the button.
#[cfg_attr(feature = "serde", serde(skip))]
events: EventManager,
}
impl Button {
/// Creates an instance of a PULL-DOWN button attached to a given board:
/// <https://docs.arduino.cc/built-in-examples/digital/Button/>
///
/// - Button pressed => pin state HIGH
/// - Button released => pin state LOW
///
/// # Errors
/// * `UnknownPin`: this function will bail an error if the Button pin does not exist for this board.
/// * `IncompatiblePin`: this function will bail an error if the Button pin does not support INPUT mode.
pub fn new<T: Into<PinIdOrName>>(board: &dyn Hardware, pin: T) -> Result<Self, Error> {
Self {
pin: 0,
state: Arc::new(RwLock::new(false)),
invert: false,
pullup: false,
protocol: board.get_protocol(),
handler: Arc::new(RwLock::new(None)),
events: Default::default(),
}
.start_with(board, pin)
}
/// Creates an instance of an inverted PULL-DOWN button attached to a given board:
/// <https://docs.arduino.cc/built-in-examples/digital/Button/>
///
/// /!\ The state value is inverted compared to HIGH/LOW electrical value of the pin.
/// - Inverted button pressed => pin state LOW
/// - Inverted button released => pin state HIGH
///
/// # Errors
/// * `UnknownPin`: this function will bail an error if the Button pin does not exist for this board.
/// * `IncompatiblePin`: this function will bail an error if the Button pin does not support INPUT mode.
pub fn new_inverted<T: Into<PinIdOrName>>(board: &dyn Hardware, pin: T) -> Result<Self, Error> {
Self {
pin: 0,
state: Arc::new(RwLock::new(false)),
invert: true,
pullup: false,
protocol: board.get_protocol(),
handler: Arc::new(RwLock::new(None)),
events: Default::default(),
}
.start_with(board, pin)
}
/// Creates an instance of a PULL-UP button attached to a given board:
/// <https://docs.arduino.cc/tutorials/generic/digital-input-pullup/>
///
/// - Pullup button pressed => pin state LOW
/// - Pullup button released => pin state HIGH
///
/// # Errors
/// * `UnknownPin`: this function will bail an error if the Button pin does not exist for this board.
/// * `IncompatiblePin`: this function will bail an error if the Button pin does not support INPUT mode.
pub fn new_pullup<T: Into<PinIdOrName>>(board: &dyn Hardware, pin: T) -> Result<Self, Error> {
Self {
pin: 0,
state: Arc::new(RwLock::new(false)),
invert: false,
pullup: true,
protocol: board.get_protocol(),
handler: Arc::new(RwLock::new(None)),
events: Default::default(),
}
.start_with(board, pin)
}
/// Creates an instance of an inverted PULL-UP button attached to a given board:
/// <https://docs.arduino.cc/tutorials/generic/digital-input-pullup/>
///
/// /!\ The state value is inverted compared to HIGH/LOW electrical value of the pin
/// (therefore equivalent to a standard pull-down button)
/// - Inverted pullup button pressed => pin state HIGH
/// - Inverted pullup button released => pin state LOW
///
/// # Errors
/// * `UnknownPin`: this function will bail an error if the Button pin does not exist for this board.
/// * `IncompatiblePin`: this function will bail an error if the Button pin does not support INPUT mode.
pub fn new_inverted_pullup<T: Into<PinIdOrName>>(
board: &dyn Hardware,
pin: T,
) -> Result<Self, Error> {
Self {
pin: 0,
state: Arc::new(RwLock::new(false)),
invert: true,
pullup: true,
protocol: board.get_protocol(),
handler: Arc::new(RwLock::new(None)),
events: Default::default(),
}
.start_with(board, pin)
}
/// Private helper method shared by constructors.
fn start_with<T: Into<PinIdOrName>>(
mut self,
board: &dyn Hardware,
pin: T,
) -> Result<Self, Error> {
let pin = board.get_io().read().get_pin(pin)?.clone();
// Set pin ID and state from pin.
self.pin = pin.id;
*self.state.write() = pin.value != 0;
// Set pin mode to INPUT/PULLUP.
match self.pullup {
true => {
self.protocol.set_pin_mode(self.pin, PinModeId::PULLUP)?;
self.protocol.get_io().write().get_pin_mut(self.pin)?.value = 1;
}
false => {
self.protocol.set_pin_mode(self.pin, PinModeId::INPUT)?;
}
};
// Set reporting for this pin.
self.protocol.report_digital(self.pin, true)?;
// Create a task to listen hardware value and emit events accordingly.
self.attach();
Ok(self)
}
// ########################################
/// Returns the pin (id) used by the device.
pub fn get_pin(&self) -> u8 {
self.pin
}
/// Returns if the button is configured in PULL-UP mode.
pub fn is_pullup(&self) -> bool {
self.pullup
}
/// Returns if the logical button value is inverted.
pub fn is_inverted(&self) -> bool {
self.invert
}
// ########################################
// Event related functions
/// Manually attaches the button with the value change events.
/// This should never be needed unless you manually `detach()` the button first for some reason
/// and want it to start being reactive to events again.
pub fn attach(&self) {
if self.handler.read().is_none() {
let self_clone = self.clone();
*self.handler.write() = Some(
task::run(async move {
loop {
let pin_value = self_clone
.protocol
.get_io()
.read()
.get_pin(self_clone.pin)?
.value
!= 0;
let state_value = *self_clone.state.read();
if pin_value != state_value {
*self_clone.state.write() = pin_value;
// Depending on logical inversion mode, pin_value is inverted.
match self_clone.invert {
false => self_clone.events.emit(InputEvent::OnChange, pin_value),
true => self_clone.events.emit(InputEvent::OnChange, !pin_value),
};
match self_clone.pullup {
true => match pin_value {
true => self_clone.events.emit(InputEvent::OnRelease, ()),
false => self_clone.events.emit(InputEvent::OnPress, ()),
},
false => match pin_value {
true => self_clone.events.emit(InputEvent::OnPress, ()),
false => self_clone.events.emit(InputEvent::OnRelease, ()),
},
};
}
// Change can only be done 10x a sec. to avoid bouncing.
pause!(100);
}
#[allow(unreachable_code)]
Ok(())
})
.unwrap(),
);
}
}
/// Detaches the interval associated with the button.
/// This means the button won't react anymore to value changes.
pub fn detach(&self) {
if let Some(handler) = self.handler.read().as_ref() {
handler.abort();
}
*self.handler.write() = None
}
/// Registers a callback to be executed on a given event on the Button.
///
/// Available events for a button are:
/// - **`InputEvent::OnChange` | `change`:** Triggered when the button value changes.
/// _The callback must receive the following parameter: `|value: u16| { ... }`_
/// - **`InputEvent::OnRelease` | `released`:** Triggered when the button value changes.
/// _The callback must receive the void parameter: `|_:()| { ... }`_
/// - **`InputEvent::OnPress` | `pressed`:** Triggered when the button value changes.
/// _The callback must receive the void parameter: `|_:()| { ... }`_
///
/// # Example
///
/// ```
/// use hermes_five::devices::{Button, InputEvent};
/// use hermes_five::hardware::{Board, BoardEvent};
///
/// #[hermes_five::runtime]
/// async fn main() {
/// let board = Board::run();
/// board.on(BoardEvent::OnReady, |board: Board| async move {
///
/// // Register a Button on pin 2.
/// let button = Button::new(&board, 2)?;
/// // Triggered function when the button is pressed.
/// button.on(InputEvent::OnPress, |_: ()| async move {
/// println!("Push button pressed");
/// Ok(())
/// });
///
/// // The above code will run forever.
/// // <do something useful>
///
/// // The above code will run forever runs a listener on the pin state under-the-hood.
/// // It means the program will run forever listening to the InputEvent,
/// // until we detach the device and close the board.
/// button.detach();
/// board.close();
///
/// Ok(())
/// });
/// }
/// ```
pub fn on<S, F, T, Fut>(&self, event: S, callback: F) -> EventHandler
where
S: Into<String>,
T: 'static + Send + Sync + Clone,
F: FnMut(T) -> Fut + Send + 'static,
Fut: std::future::Future<Output = Result<(), Error>> + Send + 'static,
{
self.events.on(event, callback)
}
}
impl Display for Button {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Button (pin={}) [state={}, pullup={}, inverted={}]",
self.pin,
self.state.read(),
self.pullup,
self.invert
)
}
}
#[cfg_attr(feature = "serde", typetag::serde)]
impl Device for Button {}
#[cfg_attr(feature = "serde", typetag::serde)]
impl Input for Button {
fn get_state(&self) -> State {
match self.invert {
false => State::from(*self.state.read()),
true => State::from(!*self.state.read()),
}
}
}
#[cfg(test)]
mod tests {
use std::sync::atomic::{AtomicBool, Ordering};
use crate::hardware::Board;
use crate::mocks::plugin_io::MockIoProtocol;
use super::*;
#[hermes_five_macros::test]
fn test_new_button_creation() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new(&board, 4);
assert!(button.is_ok());
let button = button.unwrap();
assert_eq!(button.get_pin(), 4);
assert_eq!(button.get_state().as_bool(), true);
assert!(!button.is_inverted());
assert!(!button.is_pullup());
button.detach();
board.close();
}
#[hermes_five_macros::test]
fn test_new_inverted_button_creation() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new_inverted(&board, 4);
assert!(button.is_ok());
let button = button.unwrap();
assert_eq!(button.get_pin(), 4);
assert_eq!(button.get_state().as_bool(), false);
assert!(button.is_inverted());
assert!(!button.is_pullup());
button.detach();
board.close();
}
#[hermes_five_macros::test]
fn test_new_pullup_button_creation() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new_pullup(&board, 4);
assert!(button.is_ok());
let button = button.unwrap();
assert_eq!(button.get_pin(), 4);
assert_eq!(button.get_state().as_bool(), true);
assert!(!button.is_inverted());
assert!(button.is_pullup());
button.detach();
board.close();
}
#[hermes_five_macros::test]
fn test_new_inverted_pullup_button_creation() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new_inverted_pullup(&board, 4);
assert!(button.is_ok());
let button = button.unwrap();
assert_eq!(button.get_pin(), 4);
assert_eq!(button.get_state().as_bool(), false);
assert!(button.is_inverted());
assert!(button.is_pullup());
button.detach();
board.close();
}
#[hermes_five_macros::test]
fn test_button_helper() {
let board = Board::new(MockIoProtocol::default());
let button = Button::start_with(
Button {
pin: 0,
state: Arc::new(RwLock::new(false)),
invert: true,
pullup: false,
protocol: board.get_protocol(),
handler: Arc::new(RwLock::new(None)),
events: Default::default(),
},
&board,
13,
);
assert!(button.is_ok());
let button = button.unwrap();
assert_eq!(button.get_pin(), 13);
assert!(button.handler.read().is_some());
button.detach();
assert!(button.handler.read().is_none());
board.close();
}
#[hermes_five_macros::test]
fn test_button_inverted_state_logic() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new_inverted(&board, 5).unwrap();
assert_eq!(button.get_state().as_bool(), true);
button.state.write().clone_from(&true); // Simulate a pressed button
assert_eq!(button.get_state().as_bool(), false);
button.detach();
board.close();
}
#[hermes_five_macros::test]
fn test_button_events() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new(&board, 5).unwrap();
// CHANGE
let change_flag = Arc::new(AtomicBool::new(false));
let moved_change_flag = change_flag.clone();
button.on(InputEvent::OnChange, move |new_state: bool| {
let captured_flag = moved_change_flag.clone();
async move {
captured_flag.store(new_state, Ordering::SeqCst);
Ok(())
}
});
// PRESSED
let pressed_flag = Arc::new(AtomicBool::new(false));
let moved_pressed_flag = pressed_flag.clone();
button.on(InputEvent::OnPress, move |_: ()| {
let captured_flag = moved_pressed_flag.clone();
async move {
captured_flag.store(true, Ordering::SeqCst);
Ok(())
}
});
// RELEASED
let released_flag = Arc::new(AtomicBool::new(false));
let moved_released_flag = released_flag.clone();
button.on(InputEvent::OnRelease, move |_: ()| {
let captured_flag = moved_released_flag.clone();
async move {
captured_flag.store(true, Ordering::SeqCst);
Ok(())
}
});
assert!(!change_flag.load(Ordering::SeqCst));
assert!(!pressed_flag.load(Ordering::SeqCst));
assert!(!released_flag.load(Ordering::SeqCst));
// Simulate pin state change in the protocol => take value 0xFF
button
.protocol
.get_io()
.write()
.get_pin_mut(5)
.unwrap()
.value = 0xFF;
pause!(500);
assert!(change_flag.load(Ordering::SeqCst));
assert!(pressed_flag.load(Ordering::SeqCst));
assert!(!released_flag.load(Ordering::SeqCst));
// Simulate pin state change in the protocol => takes value 0
button
.protocol
.get_io()
.write()
.get_pin_mut(5)
.unwrap()
.value = 0;
pause!(500);
assert!(!change_flag.load(Ordering::SeqCst)); // change switched back to 0
assert!(released_flag.load(Ordering::SeqCst));
button.detach();
board.close();
}
#[hermes_five_macros::test]
fn test_inverted_button_events() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new_inverted(&board, 5).unwrap();
// CHANGE
let change_flag = Arc::new(AtomicBool::new(true));
let moved_change_flag = change_flag.clone();
button.on(InputEvent::OnChange, move |new_state: bool| {
let captured_flag = moved_change_flag.clone();
async move {
captured_flag.store(new_state, Ordering::SeqCst);
Ok(())
}
});
// PRESSED
let pressed_flag = Arc::new(AtomicBool::new(false));
let moved_pressed_flag = pressed_flag.clone();
button.on(InputEvent::OnPress, move |_: ()| {
let captured_flag = moved_pressed_flag.clone();
async move {
captured_flag.store(true, Ordering::SeqCst);
Ok(())
}
});
// RELEASED
let released_flag = Arc::new(AtomicBool::new(false));
let moved_released_flag = released_flag.clone();
button.on(InputEvent::OnRelease, move |_: ()| {
let captured_flag = moved_released_flag.clone();
async move {
captured_flag.store(true, Ordering::SeqCst);
Ok(())
}
});
assert!(change_flag.load(Ordering::SeqCst)); // true by default
assert!(!pressed_flag.load(Ordering::SeqCst));
assert!(!released_flag.load(Ordering::SeqCst));
// Simulate pin state change in the protocol => take value 0xFF
button
.protocol
.get_io()
.write()
.get_pin_mut(5)
.unwrap()
.value = 0xFF;
pause!(500);
assert!(!change_flag.load(Ordering::SeqCst)); // changed to false
assert!(pressed_flag.load(Ordering::SeqCst));
assert!(!released_flag.load(Ordering::SeqCst));
// Simulate pin state change in the protocol => takes value 0
button
.protocol
.get_io()
.write()
.get_pin_mut(5)
.unwrap()
.value = 0;
pause!(500);
assert!(change_flag.load(Ordering::SeqCst)); // change switched back to true
assert!(released_flag.load(Ordering::SeqCst));
button.detach();
board.close();
}
// #[hermes_five_macros::test]
// fn test_pullup_button_events() {
// let board = Board::new(MockIoProtocol::default());
// let button = Button::new_pullup(&board, 5).unwrap();
//
// // CHANGE
// let change_flag = Arc::new(AtomicBool::new(true));
// let moved_change_flag = change_flag.clone();
// button.on(InputEvent::OnChange, move |new_state: bool| {
// let captured_flag = moved_change_flag.clone();
// async move {
// captured_flag.store(new_state, Ordering::SeqCst);
// Ok(())
// }
// });
//
// // PRESSED
// let pressed_flag = Arc::new(AtomicBool::new(false));
// let moved_pressed_flag = pressed_flag.clone();
// button.on(InputEvent::OnPress, move |_: ()| {
// let captured_flag = moved_pressed_flag.clone();
// async move {
// captured_flag.store(true, Ordering::SeqCst);
// Ok(())
// }
// });
//
// // RELEASED
// let released_flag = Arc::new(AtomicBool::new(false));
// let moved_released_flag = released_flag.clone();
// button.on(InputEvent::OnRelease, move |_: ()| {
// let captured_flag = moved_released_flag.clone();
// async move {
// captured_flag.store(true, Ordering::SeqCst);
// Ok(())
// }
// });
//
// assert!(change_flag.load(Ordering::SeqCst)); // true by default
// assert!(!pressed_flag.load(Ordering::SeqCst));
// assert!(!released_flag.load(Ordering::SeqCst));
//
// // Simulate pin state change in the protocol => take value 0xFF
// button
// .protocol
// .get_io()
// .write()
// .get_pin_mut(5)
// .unwrap()
// .value = 0;
//
// pause!(500);
//
// assert!(!change_flag.load(Ordering::SeqCst)); // changed to false
// assert!(pressed_flag.load(Ordering::SeqCst));
// assert!(!released_flag.load(Ordering::SeqCst));
//
// // Simulate pin state change in the protocol => takes value 0
// button
// .protocol
// .get_io()
// .write()
// .get_pin_mut(5)
// .unwrap()
// .value = 0xFF;
//
// pause!(500);
//
// assert!(change_flag.load(Ordering::SeqCst)); // change switched back to true
// assert!(released_flag.load(Ordering::SeqCst));
//
// button.detach();
// board.close();
// }
#[hermes_five_macros::test]
fn test_button_display() {
let board = Board::new(MockIoProtocol::default());
let button = Button::new(&board, 4).unwrap();
assert_eq!(
format!("{}", button),
String::from("Button (pin=4) [state=true, pullup=false, inverted=false]")
);
button.detach();
board.close();
}
}