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
#![no_std]
use embedded_hal::blocking::delay::DelayUs;
use embedded_hal::blocking::spi;
use embedded_hal::digital::OutputPin;
pub struct SegmentDisplay<SPI, PIN> {
back_buffer: [u8; 4],
spi: SPI,
latch_pin: PIN,
current_digit: usize,
}
impl<SPI, PIN> SegmentDisplay<SPI, PIN>
where
SPI: spi::Write<u8>,
PIN: OutputPin,
{
pub fn new(spi: SPI, latch_pin: PIN) -> Self {
Self {
back_buffer: [0xff; 4],
spi,
latch_pin,
current_digit: 0,
}
}
pub fn release(self) -> (SPI, PIN) {
(self.spi, self.latch_pin)
}
pub fn refresh(&mut self) -> Result<(), SPI::Error> {
let segments_and_select: [u8; 2] = [
self.back_buffer[self.current_digit],
1 << (4 - 1 - self.current_digit),
];
self.current_digit = (self.current_digit + 1) & 0b11;
self.latch_pin.set_low();
let res = self.spi.write(&segments_and_select)?;
self.latch_pin.set_high();
Ok(res)
}
pub fn refresh_with_delay<DELAY>(&mut self, delay: &mut DELAY) -> Result<(), SPI::Error>
where
DELAY: DelayUs<u16>,
{
let segments_and_select: [u8; 2] = [
self.back_buffer[self.current_digit],
1 << (4 - 1 - self.current_digit),
];
self.current_digit = (self.current_digit + 1) & 0b11;
self.latch_pin.set_low();
let res = self.spi.write(&segments_and_select)?;
delay.delay_us(100);
self.latch_pin.set_high();
Ok(res)
}
pub fn write_chars(&mut self, buf: [char; 4]) {
for (i, c) in buf.iter().enumerate() {
self.back_buffer[i] = Self::char_to_segment_code(*c);
}
}
pub fn write_str(&mut self, s: &str) {
self.back_buffer.iter_mut().for_each(|b| *b = !0);
for (i, c) in s.chars().take(4).enumerate() {
self.back_buffer[i] = Self::char_to_segment_code(c);
}
}
pub fn write_number(&mut self, num: usize) {
let mut num = num;
if num > 9999 {
num = 9999;
}
for (i, div) in [1000, 100, 10].iter().enumerate() {
let digit;
if num >= i {
digit = num / div;
num -= div * digit;
} else {
digit = 0;
}
self.back_buffer[i] = NUMERALS[digit];
}
self.back_buffer[3] = NUMERALS[num];
}
fn char_to_segment_code(c: char) -> u8 {
if c.is_ascii_digit() {
let cb = c as u8;
let idx = cb - ('0' as u8);
NUMERALS[idx as usize]
} else if c.is_ascii_alphabetic() {
let cb = (c as u8) & !0x20;
let idx = cb - ('A' as u8);
LETTERS[idx as usize]
} else {
match c {
' ' => 0b1111_1111,
'-' => 0b1011_1111,
'_' => 0b1111_0111,
_ => 0b1111_1111,
}
}
}
}
static NUMERALS: [u8; 10] = [
0b1100_0000,
0b1111_1001,
0b1010_0100,
0b1011_0000,
0b1001_1001,
0b1001_0010,
0b1000_0010,
0b1111_1000,
0b1000_0000,
0b1001_1000,
];
static LETTERS: [u8; 26] = [
0b1000_1000,
0b1000_0011,
0b1100_0110,
0b1010_0001,
0b1000_0110,
0b1000_1110,
0b1100_0010,
0b1000_1001,
0b1100_1111,
0b1110_0001,
0b1000_1010,
0b1100_0111,
0b1110_1010,
0b1100_1000,
0b1100_0000,
0b1000_1100,
0b1001_0100,
0b1100_1100,
0b1001_0010,
0b1000_0111,
0b1100_0001,
0b1100_0001,
0b1101_0101,
0b1000_1001,
0b1001_0001,
0b1010_0100,
];