-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlights.rs
More file actions
113 lines (101 loc) · 3.45 KB
/
lights.rs
File metadata and controls
113 lines (101 loc) · 3.45 KB
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
extern crate sysfs_gpio;
use on_export;
use std::thread::sleep;
use std::time::Duration;
use sysfs_gpio::{Direction, Pin};
pub const BLUE_PIN: u64 = 17;
pub const YELLOW_PIN: u64 = 5;
pub const RED_PIN: u64 = 26;
pub fn blink() {
let blue_pin = Pin::new(BLUE_PIN);
blue_pin
.with_exported(|| {
// give the system a little time
// to catch up to export
on_export::wait();
blue_pin.set_direction(Direction::Out).unwrap();
loop {
blue_pin.set_value(0).unwrap();
sleep(Duration::from_millis(200));
blue_pin.set_value(1).unwrap();
sleep(Duration::from_millis(200));
}
})
.unwrap();
}
pub fn blink3() {
let blue_pin = Pin::new(BLUE_PIN);
let yellow_pin = Pin::new(YELLOW_PIN);
let red_pin = Pin::new(RED_PIN);
let duration = 125;
blue_pin
.with_exported(|| {
yellow_pin.with_exported(|| {
red_pin.with_exported(|| {
on_export::wait();
blue_pin.set_direction(Direction::Out).unwrap();
yellow_pin.set_direction(Direction::Out).unwrap();
red_pin.set_direction(Direction::Out).unwrap();
loop {
blue_pin.set_value(1).unwrap();
yellow_pin.set_value(1).unwrap();
red_pin.set_value(1).unwrap();
sleep(Duration::from_millis(duration));
blue_pin.set_value(0).unwrap();
yellow_pin.set_value(0).unwrap();
red_pin.set_value(0).unwrap();
sleep(Duration::from_millis(duration));
}
})
})
})
.unwrap();
}
pub fn flashy() {
let blue_pin = Pin::new(BLUE_PIN);
let yellow_pin = Pin::new(YELLOW_PIN);
let red_pin = Pin::new(RED_PIN);
let blue_flag = 0b0_001u32;
let yellow_flag = 0b0_010u32;
let red_flag = 0b0_100u32;
let duration = 31;
let mut count = 0;
blue_pin
.with_exported(|| {
yellow_pin.with_exported(|| {
red_pin.with_exported(|| {
on_export::wait();
blue_pin.set_direction(Direction::Out).unwrap();
yellow_pin.set_direction(Direction::Out).unwrap();
red_pin.set_direction(Direction::Out).unwrap();
loop {
count = (count + 1) % 8;
blue_pin.set_value((count & blue_flag > 0) as u8).unwrap();
yellow_pin
.set_value((count & yellow_flag > 0) as u8)
.unwrap();
red_pin.set_value((count & red_flag > 0) as u8).unwrap();
sleep(Duration::from_millis(duration));
}
})
})
})
.unwrap();
}
/// Flash some lights after a given amount of time
///
/// # Examples
///
/// ```
/// use std::time::Duration;
///
/// let duration = Duration::from_seconds(5);
/// let blink_fn = () -> blink()
///
/// timer(duration, blink_fn)
/// ```
pub fn timer(duration: Duration, blink_fn: &Fn() -> ()) {
println!("See you in {} seconds\n... zzz ...\n. . . z z z . . . \n. . . z z z . . .\n", duration.as_secs());
sleep(duration);
blink_fn()
}