-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.rs
75 lines (66 loc) · 1.77 KB
/
matrix.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
use core::fmt::Debug;
use cortex_m::asm::delay;
use embedded_hal::digital::v2::OutputPin;
pub struct LedMatrix<ClockPin, DataPin, StrobePin> {
clock: ClockPin,
data: DataPin,
strobe: StrobePin,
// Make size an u16 to prevent overflow on multiplication and avoid
// lots of casting
height: u16,
width: u16,
}
#[allow(dead_code)]
impl<ClockPin, DataPin, StrobePin> LedMatrix<ClockPin, DataPin, StrobePin> where
ClockPin: OutputPin, ClockPin::Error: Debug,
DataPin: OutputPin, DataPin::Error: Debug,
StrobePin: OutputPin, StrobePin::Error: Debug,
{
pub fn new(
clock: ClockPin, data: DataPin, strobe: StrobePin,
height: u16, width: u16,
) -> LedMatrix<ClockPin, DataPin, StrobePin> {
LedMatrix {
clock,
data,
strobe,
height,
width,
}
}
pub fn pulse_clock(&mut self) {
self.clock.set_high().unwrap();
delay(10);
self.clock.set_low().unwrap();
delay(10);
}
pub fn show(&mut self) {
self.strobe.set_high().unwrap();
delay(10);
self.strobe.set_low().unwrap();
delay(10);
}
pub fn clear(&mut self) {
self.data.set_low().unwrap();
for _ in 0 .. self.width * self.height {
self.pulse_clock();
}
}
pub fn pixel_on(&mut self) {
self.data.set_high().unwrap();
self.pulse_clock();
}
pub fn pixel_off(&mut self) {
self.data.set_low().unwrap();
self.pulse_clock();
}
pub fn push_row(&mut self, row: u8) {
for i in (0..=7).rev() {
if (row & (1 << i)) != 0 {
self.pixel_on();
} else {
self.pixel_off();
}
}
}
}