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
#![no_std]
#[macro_use]
extern crate alloc;
pub mod mappings;
#[cfg(feature = "fourdigit7segdis")]
pub mod fourdigit7segdis;
pub mod gpio_api;
use alloc::boxed::Box;
use alloc::vec::Vec;
use crate::mappings::{NumCharBits, UpCharBits, SpecialCharBits, LoCharBits};
pub const DISPLAY_REGISTERS_COUNT: usize = 6;
#[repr(u8)]
#[derive(Clone, Copy)]
pub enum GpioPinValue {
LOW,
HIGH,
}
impl From<u8> for GpioPinValue {
fn from(x: u8) -> Self {
if x == 0 { GpioPinValue::LOW } else { GpioPinValue::HIGH }
}
}
pub struct TM1637Adapter {
pin_clock_write_fn: Box<dyn Fn(GpioPinValue)>,
pin_dio_write_fn: Box<dyn Fn(GpioPinValue)>,
pin_dio_read_fn: Box<dyn Fn() -> GpioPinValue>,
bit_delay_fn: Box<dyn Fn() -> ()>,
brightness: u8,
}
#[repr(u8)]
pub enum Brightness {
L0 = 0b000,
L1 = 0b001,
L2 = 0b010,
L3 = 0b011,
L4 = 0b100,
L5 = 0b101,
L6 = 0b110,
L7 = 0b111,
}
#[repr(u8)]
pub enum DisplayState {
OFF = 0b0000,
ON = 0b1000,
}
#[repr(u8)]
pub enum ISA {
DataCommandWriteToDisplay = 0b0100_0000,
AddressCommandD0 = 0b1100_0000,
AddressCommandD1 = 0b1100_0001,
AddressCommandD2 = 0b1100_0010,
AddressCommandD3 = 0b1100_0011,
DisplayControlOff = 0b1000_0000,
DisplayControlOnL0 = 0b1000_1000,
DisplayControlOnL1 = 0b1000_1001,
DisplayControlOnL2 = 0b1000_1010,
DisplayControlOnL3 = 0b1000_1011,
DisplayControlOnL4 = 0b1000_1100,
DisplayControlOnL5 = 0b1000_1101,
DisplayControlOnL6 = 0b1000_1110,
DisplayControlOnL7 = 0b1000_1111,
}
impl TM1637Adapter {
pub fn new(pin_clock_write_fn: Box<dyn Fn(GpioPinValue)>,
pin_dio_write_fn: Box<dyn Fn(GpioPinValue)>,
pin_dio_read_fn: Box<dyn Fn() -> GpioPinValue>,
bit_delay_fn: Box<dyn Fn() -> ()>) -> TM1637Adapter {
(pin_clock_write_fn)(GpioPinValue::LOW);
(pin_dio_write_fn)(GpioPinValue::LOW);
TM1637Adapter {
pin_clock_write_fn,
pin_dio_write_fn,
pin_dio_read_fn,
bit_delay_fn,
brightness: DisplayState::ON as u8 | Brightness::L7 as u8,
}
}
pub fn set_display_state(&mut self, ds: DisplayState) {
let old_brightness = self.brightness & 0b0000_0111;
self.brightness = ds as u8 | old_brightness;
}
pub fn set_brightness(&mut self, brightness: Brightness) {
let display_on = self.brightness as u8 & 0b0000_1000;
self.brightness = display_on | brightness as u8;
}
pub fn write_segments_raw(&self, segments: &[u8], pos: u8) {
let mut n = segments.len() as u8;
if n == 0 { return; }
let pos = pos % DISPLAY_REGISTERS_COUNT as u8;
if n + pos > DISPLAY_REGISTERS_COUNT as u8 {
n = DISPLAY_REGISTERS_COUNT as u8 - pos;
}
self.start();
self.write_byte_and_wait_ack(ISA::DataCommandWriteToDisplay as u8);
self.stop();
self.start();
self.write_byte_and_wait_ack(ISA::AddressCommandD0 as u8 | pos);
for i in 0..n {
self.write_byte_and_wait_ack(segments[i as usize]);
}
self.stop();
self.write_display_state();
}
pub fn write_segment_raw(&self, segments: u8, position: u8) {
self.write_segments_raw(&[segments], position)
}
pub fn write_display_state(&self) {
self.start();
self.write_byte_and_wait_ack(ISA::DisplayControlOff as u8 | self.brightness);
self.stop();
}
pub fn clear(&self) {
self.write_segments_raw(&[0, 0, 0, 0, 0, 0], 0);
}
fn write_byte_and_wait_ack(&self, byte: u8) {
let mut data = byte;
for _ in 0..8 {
(self.pin_clock_write_fn)(GpioPinValue::LOW);
(self.pin_dio_write_fn)(GpioPinValue::from(data & 0x01));
self.bit_delay();
(self.pin_clock_write_fn)(GpioPinValue::HIGH);
self.bit_delay();
data = data >> 1;
}
self.recv_ack();
}
pub fn encode_number(num: u16) -> [u8; 4] {
let mut num = num % 10000;
let mut bits: [u8; 4] = [0; 4];
for i in 0..4 {
let digit = (num % 10) as u8;
bits[3 - i] = TM1637Adapter::encode_digit(digit);
num = num / 10;
}
bits
}
pub fn encode_digit(digit: u8) -> u8 {
let digit = digit % 10;
if digit == 0 { NumCharBits::Zero as u8 }
else if digit == 1 { NumCharBits::One as u8 }
else if digit == 2 { NumCharBits::Two as u8 }
else if digit == 3 { NumCharBits::Three as u8 }
else if digit == 4 { NumCharBits::Four as u8 }
else if digit == 5 { NumCharBits::Five as u8 }
else if digit == 6 { NumCharBits::Six as u8 }
else if digit == 7 { NumCharBits::Seven as u8 }
else if digit == 8 { NumCharBits::Eight as u8 }
else { NumCharBits::Nine as u8 }
}
pub fn encode_char(c: char) -> u8 {
if c == '0' { NumCharBits::Zero as u8 }
else if c == '1' { NumCharBits::One as u8 }
else if c == '2' { NumCharBits::Two as u8 }
else if c == '3' { NumCharBits::Three as u8 }
else if c == '4' { NumCharBits::Four as u8 }
else if c == '5' { NumCharBits::Five as u8 }
else if c == '6' { NumCharBits::Six as u8 }
else if c == '7' { NumCharBits::Seven as u8 }
else if c == '8' { NumCharBits::Eight as u8 }
else if c == '9' { NumCharBits::Nine as u8 }
else if c == 'A' { UpCharBits::UpA as u8 }
else if c == 'a' { LoCharBits::LoA as u8 }
else if c == 'B' { LoCharBits::LoB as u8 }
else if c == 'b' { LoCharBits::LoB as u8 }
else if c == 'C' { UpCharBits::UpC as u8 }
else if c == 'c' { UpCharBits::UpC as u8 }
else if c == 'D' { LoCharBits::LoD as u8 }
else if c == 'd' { LoCharBits::LoD as u8 }
else if c == 'E' { UpCharBits::UpE as u8 }
else if c == 'e' { UpCharBits::UpE as u8 }
else if c == 'F' { UpCharBits::UpF as u8 }
else if c == 'f' { UpCharBits::UpF as u8 }
else if c == 'G' { UpCharBits::UpG as u8 }
else if c == 'g' { UpCharBits::UpG as u8 }
else if c == 'H' { UpCharBits::UpH as u8 }
else if c == 'h' { LoCharBits::LoH as u8 }
else if c == 'I' { UpCharBits::UpI as u8 }
else if c == 'i' { UpCharBits::UpI as u8 }
else if c == 'J' { UpCharBits::UpJ as u8 }
else if c == 'j' { UpCharBits::UpJ as u8 }
else if c == 'L' { UpCharBits::UpL as u8 }
else if c == 'l' { UpCharBits::UpL as u8 }
else if c == 'N' { LoCharBits::LoN as u8 }
else if c == 'n' { LoCharBits::LoN as u8 }
else if c == 'O' { UpCharBits::UpO as u8 }
else if c == 'o' { LoCharBits::LoO as u8 }
else if c == 'P' { UpCharBits::UpP as u8 }
else if c == 'p' { UpCharBits::UpP as u8 }
else if c == 'Q' { LoCharBits::LoQ as u8 }
else if c == 'q' { LoCharBits::LoQ as u8 }
else if c == 'R' { LoCharBits::LoR as u8 }
else if c == 'r' { LoCharBits::LoR as u8 }
else if c == 'S' { UpCharBits::UpS as u8 }
else if c == 's' { UpCharBits::UpS as u8 }
else if c == 'T' { LoCharBits::LoT as u8 }
else if c == 't' { LoCharBits::LoT as u8 }
else if c == 'U' { UpCharBits::UpU as u8 }
else if c == 'u' { LoCharBits::LoU as u8 }
else if c == 'Y' { LoCharBits::LoY as u8 }
else if c == 'y' { LoCharBits::LoY as u8 }
else if c == ' ' { SpecialCharBits::Space as u8 }
else if c == '?' { SpecialCharBits::QuestionMark as u8 }
else if c == '-' { SpecialCharBits::Minus as u8 }
else if c == '_' { SpecialCharBits::Underscore as u8 }
else if c == '=' { SpecialCharBits::Equals as u8 }
else if c == '.' { SpecialCharBits::Dot as u8 }
else { SpecialCharBits::Space as u8 }
}
pub fn encode_string(str: &str) -> Vec<u8> {
str.chars().into_iter()
.map(|c| TM1637Adapter::encode_char(c))
.collect()
}
#[inline]
fn start(&self) {
(self.pin_dio_write_fn)(GpioPinValue::HIGH);
(self.pin_clock_write_fn)(GpioPinValue::HIGH);
self.bit_delay();
(self.pin_dio_write_fn)(GpioPinValue::LOW);
self.bit_delay();
}
#[inline]
fn stop(&self) {
(self.pin_dio_write_fn)(GpioPinValue::LOW);
(self.pin_clock_write_fn)(GpioPinValue::HIGH);
self.bit_delay();
(self.pin_dio_write_fn)(GpioPinValue::HIGH);
self.bit_delay();
}
fn recv_ack(&self) {
(self.pin_clock_write_fn)(GpioPinValue::LOW);
(self.pin_dio_write_fn)(GpioPinValue::LOW);
self.bit_delay();
(self.pin_clock_write_fn)(GpioPinValue::HIGH);
let ack: GpioPinValue = (self.pin_dio_read_fn)();
for _ in 0..10 {
if ack as u8 == 0 { break; }
else {
}
}
(self.pin_clock_write_fn)(GpioPinValue::LOW);
(self.pin_dio_write_fn)(GpioPinValue::LOW);
self.bit_delay();
}
#[inline]
fn bit_delay(&self) {
(self.bit_delay_fn)()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_number() {
let f = TM1637Adapter::encode_digit;
assert_eq!([f(1), f(2), f(3), f(4)], TM1637Adapter::encode_number(1234));
assert_eq!([f(9), f(9), f(9), f(9)], TM1637Adapter::encode_number(9999));
assert_eq!([f(0), f(0), f(0), f(0)], TM1637Adapter::encode_number(10000));
assert_eq!([f(7), f(6), f(5), f(4)], TM1637Adapter::encode_number(7654));
}
}