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
#![no_std]
#![deny(warnings, missing_docs)]
mod fonts;
use fonts::*;
use embedded_hal::blocking::i2c::{Write, WriteRead};
use ht16k33::{
DisplayDataAddress,
DisplayData,
LedLocation,
HT16K33,
COMMONS_SIZE,
};
pub use ascii::AsciiChar;
#[derive(Debug)]
pub enum Error {
InsufficientDigits,
}
pub trait AlphaNum4<E> {
fn update_buffer_with_digit(&mut self, index: Index, value: u8);
fn update_buffer_with_dot(&mut self, index: Index, dot_on: bool);
fn update_buffer_with_char(&mut self, index: Index, value: AsciiChar);
fn update_buffer_with_float(&mut self, index: Index, value: f32, fractional_digits: u8, base: u8) -> Result<(), Error>;
}
#[derive(Clone, Copy, PartialEq)]
pub enum Index {
One,
Two,
Three,
Four,
}
impl From<Index> for u8 {
fn from(i: Index) -> u8 {
match i {
Index::One => 0,
Index::Two => 1,
Index::Three => 2,
Index::Four => 3,
}
}
}
impl From<u8> for Index {
fn from(v: u8) -> Index {
match v {
0 => Index::One,
1 => Index::Two,
2 => Index::Three,
3 => Index::Four,
_ => panic!("Invalid index > 3"),
}
}
}
fn set_bit<I2C, E>(display: &mut HT16K33<I2C>, index: Index, bit: u8, on: bool)
where
I2C:
Write<Error = E> +
WriteRead<Error = E> +
{
debug_assert!((bit as usize) < (COMMONS_SIZE * 2));
let index = u8::from(index) * 2;
let row = DisplayDataAddress::from_bits_truncate(if bit < 8 {
index
} else {
index + 1
});
let common = DisplayData::from_bits_truncate(1 << (bit % 8));
display.update_display_buffer(
LedLocation { row, common },
on,
);
}
fn update_bits<I2C, E>(display: &mut HT16K33<I2C>, index: Index, bits: u16)
where
I2C:
Write<Error = E> +
WriteRead<Error = E> +
{
for i in 0..16 {
let on = ((bits >> i) & 1) == 1;
set_bit(display, index, i, on);
}
}
impl<I2C, E> AlphaNum4<E> for HT16K33<I2C>
where
I2C:
Write<Error = E> +
WriteRead<Error = E> +
{
fn update_buffer_with_digit(&mut self, index: Index, value: u8) {
let value = value as usize;
assert!(value < NUMBER_FONT_TABLE.len());
let bits = NUMBER_FONT_TABLE[value];
update_bits(self, index, bits);
}
fn update_buffer_with_dot(&mut self, index: Index, dot_on: bool) {
set_bit(self, index, DOT_BIT, dot_on);
}
fn update_buffer_with_char(&mut self, index: Index, value: AsciiChar) {
let bits = ASCII_FONT_TABLE[value.as_byte() as usize];
update_bits(self, index, bits);
}
fn update_buffer_with_float(&mut self, index: Index, mut value: f32, mut fractional_digits: u8, base: u8) -> Result<(), Error> {
let index = u8::from(index);
let mut numeric_digits = 4 - index;
let is_negative = if value < 0. {
numeric_digits -= 1;
value *= -1.;
true
} else {
false
};
let base = base as u32;
let basef = base as f32;
let mut to_int_factor = base.pow(fractional_digits as u32) as f32;
let mut display_number = ((value * to_int_factor) + 0.5) as u32;
let too_big = base.pow(numeric_digits as u32);
while display_number >= too_big {
fractional_digits -= 1;
to_int_factor /= basef;
display_number = ((value * to_int_factor) + 0.5) as u32;
}
if to_int_factor < 1. {
return Err(Error::InsufficientDigits)
}
let mut display_pos = (3 - index) as i8;
if display_number == 0 {
self.update_buffer_with_digit(
(index + (display_pos as u8)).into(),
0,
);
display_pos -= 1;
} else {
let mut i = 0;
while display_number != 0 || i <= fractional_digits {
let digit_index = (index + (display_pos as u8)).into();
self.update_buffer_with_digit(
digit_index,
(display_number % base) as u8,
);
if fractional_digits != 0 && i == fractional_digits {
self.update_buffer_with_dot(
digit_index,
true,
);
}
display_pos -= 1;
display_number /= base;
i += 1;
}
}
if is_negative {
self.update_buffer_with_char(
(index + (display_pos as u8)).into(),
AsciiChar::new('-'),
);
display_pos -= 1;
}
while display_pos >= 0 {
update_bits(self, (index + (display_pos as u8)).into(), 0);
display_pos -= 1;
}
Ok(())
}
}