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
#![deny(
clippy::all,
clippy::cargo,
clippy::nursery,
)]
#![allow(
clippy::fallible_impl_from,
clippy::needless_doctest_main,
clippy::redundant_pub_crate,
clippy::suboptimal_flops
)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(rustdoc::all)]
#![no_std]
#[cfg(feature = "std")]
extern crate std;
#[macro_use]
extern crate alloc;
use alloc::vec::Vec;
#[cfg(feature = "std")]
use crate::setup::Max7219;
#[cfg(feature = "std")]
use std::{thread::sleep, time::Duration};
#[cfg(feature = "std")]
use crate::encoding::encode_string;
use crate::mappings::SingleDisplayData;
#[cfg(feature = "std")]
use max7219::DecodeMode;
pub const LED_SQUARE_MATRIX_DIM: usize = 8;
pub const MAX_DISPLAYS: usize = 16;
pub mod encoding;
pub mod mappings;
#[cfg(feature = "std")]
mod setup;
#[cfg(feature = "std")]
pub use setup::{setup as setup_adapter, Max7219 as Max7219Adapter};
pub fn shift_all_rows_one_bit_left(moving_bits: &mut [SingleDisplayData] ) {
let len = moving_bits.len();
for display_i in 0..len {
for row_i in 0..8 {
if moving_bits[display_i][row_i] & 0b10000000 != 0 {
if display_i == 0
{
moving_bits[len - 1][row_i] |= 1;
} else {
moving_bits[display_i - 1][row_i] |= 1;
}
}
moving_bits[display_i][row_i] <<= 1;
}
}
}
#[cfg(feature = "std")]
pub fn prepare_display(display: &mut Max7219, display_count: usize, intensity: u8) {
let display_count = display_count % MAX_DISPLAYS;
display.power_on().unwrap();
for i in 0..display_count {
display.set_decode_mode(i, DecodeMode::NoDecode).unwrap();
display.clear_display(i).unwrap();
display.set_intensity(i, intensity).unwrap();
}
}
#[cfg(feature = "std")]
pub fn show_moving_text_in_loop(
display: &mut Max7219,
text: &str,
display_count: usize,
ms_sleep: u64,
gap_width: Option<usize>,
) {
let display_count = display_count % MAX_DISPLAYS;
let raw_bits = encode_string(text);
let mut display_data_vec: Vec<SingleDisplayData> = if let Some(gap_width) = gap_width {
remove_gaps_in_display_text(&raw_bits, gap_width)
} else {
raw_bits
};
loop {
for (i, display_data) in display_data_vec.iter().enumerate().take(display_count) {
display.write_raw(i, display_data).unwrap();
}
sleep(Duration::from_millis(ms_sleep));
shift_all_rows_one_bit_left(&mut display_data_vec);
}
}
pub fn remove_gaps_in_display_text(
display_data_arr: &[SingleDisplayData],
min_gap_size: usize,
) -> Vec<SingleDisplayData> {
let display_data_expanded: Vec<u8> = display_data_arr
.iter()
.map(transpose_single_display_data)
.flat_map(|display_data| display_data.into_iter())
.collect();
let preserve_at_begin = display_data_expanded
.iter()
.take_while(|col| **col == 0)
.count();
let preserve_at_end = display_data_expanded
.iter()
.rev()
.take_while(|col| **col == 0)
.count();
let mut shrinked_display_data_expanded = vec![0_u8; preserve_at_begin];
let mut count_since_last_not_empty = 0;
let skip_begin = preserve_at_begin;
let skip_end = preserve_at_end;
let take_elements = display_data_expanded.len() - skip_end - skip_begin + 1;
for col in display_data_expanded.iter().take(take_elements).copied() {
if col == 0 {
count_since_last_not_empty += 1;
} else {
count_since_last_not_empty = 0;
}
if count_since_last_not_empty <= min_gap_size {
shrinked_display_data_expanded.push(col);
}
}
shrinked_display_data_expanded.extend_from_slice(&vec![0; preserve_at_end]);
let add_to_multiple_of_8 = 8 - (shrinked_display_data_expanded.len() % 8);
shrinked_display_data_expanded.extend_from_slice(&vec![0; add_to_multiple_of_8]);
let shrinked_display_data = shrinked_display_data_expanded
.as_slice()
.chunks_exact(8)
.map(|cols_8| {
let mut transposed_display: SingleDisplayData = [0; 8];
cols_8
.iter()
.copied()
.enumerate()
.for_each(|(i, col)| transposed_display[i] = col);
transpose_single_display_data(&transposed_display)
})
.collect();
shrinked_display_data
}
#[allow(clippy::needless_range_loop)]
pub fn transpose_single_display_data(data: &SingleDisplayData) -> SingleDisplayData {
let mut transposed_data: SingleDisplayData = [0; 8];
for col_i in 0..8 {
let mut col = 0;
for row_i in 0..8 {
let col_bit = (data[row_i] >> (7 - col_i)) & 1;
col = (col << 1) | col_bit;
}
transposed_data[col_i] = col;
}
transposed_data
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_move_all_bits_one_col_left() {
let data_dis_0 = [
0b01000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000,
];
let data_dis_1 = [
0b11000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000,
0b00000000,
];
let mut data = Vec::new();
data.push(data_dis_0);
data.push(data_dis_1);
shift_all_rows_one_bit_left(&mut data);
let first_row_dis_0_expected = 0b10000001;
let first_row_dis_1_expected = 0b10000000;
let first_row_dis_0_actual = data[0][0];
let first_row_dis_1_actual = data[1][0];
assert_eq!(first_row_dis_0_actual, first_row_dis_0_expected);
assert_eq!(first_row_dis_1_actual, first_row_dis_1_expected);
}
#[test]
fn test_transpose_single_display_data() {
let input = [
0b0100_0000,
0b0100_0000,
0b0100_0000,
0b0100_0000,
0b0000_0011,
0b0000_0011,
0b0000_0011,
0b0000_0011,
];
let expected = [
0b0000_0000,
0b1111_0000,
0b0000_0000,
0b0000_0000,
0b0000_0000,
0b0000_0000,
0b0000_1111,
0b0000_1111,
];
let actual = transpose_single_display_data(&input);
for i in 0..input.len() {
assert_eq!(
actual[i], expected[i],
"swap_cols_to_rows() doesn't transposed the matrix properly at index {}! is={:#b}, expected={:#b}",
i, actual[i], expected[i]
);
}
}
#[test]
fn test_remove_gaps_in_display_text() {
let vec = vec![
[
0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000,
0b10000000,
],
[
0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000, 0b10000000,
0b10000000,
],
];
let expected = vec![
[
0b10010000, 0b10010000, 0b10010000, 0b10010000, 0b10010000, 0b10010000, 0b10010000,
0b10010000,
],
[
0, 0, 0, 0, 0, 0, 0, 0,
],
];
let actual = remove_gaps_in_display_text(&vec, 2);
for i in 0..2 {
for j in 0..8 {
assert_eq!(
actual[i][j], expected[i][j],
"expected: {:#b}, is: {:#b}",
expected[i][j], vec[i][j]
);
}
}
}
}