Skip to content

Commit f59c1b6

Browse files
committed
Make clippy happy
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent cef2ba6 commit f59c1b6

File tree

3 files changed

+87
-73
lines changed

3 files changed

+87
-73
lines changed

src/control.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn handle_command(command: &Command, state: &mut State, matrix: &mut Foo) {
116116
}
117117
Command::Animate(a) => state.animate = *a,
118118
Command::Panic => panic!("Ahhh"),
119-
Command::Draw(vals) => state.grid = draw(&vals),
119+
Command::Draw(vals) => state.grid = draw(vals),
120120
_ => {}
121121
}
122122
}

src/main.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
//! Lotus LED Matrix Module
22
#![no_std]
33
#![no_main]
4+
#![allow(clippy::needless_range_loop)]
45

56
use bsp::entry;
6-
use cortex_m::delay::{self, Delay};
7+
use cortex_m::delay::Delay;
78
//use defmt::*;
89
use defmt_rtt as _;
910
use embedded_hal::digital::v2::{InputPin, OutputPin};
@@ -129,8 +130,15 @@ fn get_serialnum() -> Option<&'static str> {
129130
}
130131
}
131132

132-
pub type Grid = [[u8; 34]; 9];
133+
#[derive(Clone)]
134+
pub struct Grid([[u8; HEIGHT]; WIDTH]);
135+
impl Default for Grid {
136+
fn default() -> Self {
137+
Grid([[0; HEIGHT]; WIDTH])
138+
}
139+
}
133140

141+
#[allow(clippy::large_enum_variant)]
134142
#[derive(Clone)]
135143
enum SleepState {
136144
Awake,
@@ -204,7 +212,7 @@ fn main() -> ! {
204212
// INTB. Currently ignoring
205213
pins.intb.into_floating_input();
206214

207-
let _sleep = pins.sleep.into_pull_down_input();
215+
let sleep = pins.sleep.into_pull_down_input();
208216

209217
let i2c = bsp::hal::I2C::i2c1(
210218
pac.I2C1,
@@ -233,23 +241,23 @@ fn main() -> ! {
233241

234242
let mut said_hello = false;
235243

236-
fill_grid(state.grid, &mut matrix);
244+
fill_grid(&state.grid, &mut matrix);
237245

238246
let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
239247
let mut prev_timer = timer.get_counter().ticks();
240248

241249
loop {
242250
// TODO: Current hardware revision does not have the sleep pin wired up :(
243251
// Go to sleep if the host is sleeping
244-
//let host_sleeping = sleep.is_low().unwrap();
252+
let _host_sleeping = sleep.is_low().unwrap();
245253
//handle_sleep(host_sleeping, &mut state, &mut matrix, &mut delay);
246254

247255
// Handle period display updates. Don't do it too often
248256
if timer.get_counter().ticks() > prev_timer + 20_000 {
249-
fill_grid(state.grid, &mut matrix);
257+
fill_grid(&state.grid, &mut matrix);
250258
if state.animate {
251-
for x in 0..9 {
252-
state.grid[x].rotate_right(1);
259+
for x in 0..WIDTH {
260+
state.grid.0[x].rotate_right(1);
253261
}
254262
}
255263
prev_timer = timer.get_counter().ticks();
@@ -289,7 +297,7 @@ fn main() -> ! {
289297
handle_sleep(go_sleeping, &mut state, &mut matrix, &mut delay);
290298
}
291299

292-
fill_grid(state.grid, &mut matrix);
300+
fill_grid(&state.grid, &mut matrix);
293301
}
294302
}
295303
}
@@ -299,11 +307,11 @@ fn main() -> ! {
299307

300308
fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &mut Delay) {
301309
match (state.sleeping.clone(), go_sleeping) {
302-
(SleepState::Awake, false) => return,
310+
(SleepState::Awake, false) => (),
303311
(SleepState::Awake, true) => {
304-
state.sleeping = SleepState::Sleeping(state.grid);
312+
state.sleeping = SleepState::Sleeping(state.grid.clone());
305313
//state.grid = display_sleep();
306-
fill_grid(state.grid, matrix);
314+
fill_grid(&state.grid, matrix);
307315

308316
// Slowly decrease brightness
309317
delay.delay_ms(1000);
@@ -322,12 +330,12 @@ fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &
322330
// TODO: Set up SLEEP# pin as interrupt and wfi
323331
//cortex_m::asm::wfi();
324332
}
325-
(SleepState::Sleeping(_), true) => return,
333+
(SleepState::Sleeping(_), true) => (),
326334
(SleepState::Sleeping(old_grid), false) => {
327335
// Restore back grid before sleeping
328336
state.sleeping = SleepState::Awake;
329337
state.grid = old_grid;
330-
fill_grid(state.grid, matrix);
338+
fill_grid(&state.grid, matrix);
331339

332340
// Slowly increase brightness
333341
delay.delay_ms(1000);

src/patterns.rs

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use crate::lotus_led_hal as bsp;
77
use crate::mapping::*;
88
use crate::{lotus::LotusLedMatrix, Grid};
99

10+
pub const WIDTH: usize = 9;
11+
pub const HEIGHT: usize = 34;
12+
1013
pub type Foo = LotusLedMatrix<
1114
bsp::hal::I2C<
1215
I2C1,
@@ -18,26 +21,26 @@ pub type Foo = LotusLedMatrix<
1821
>;
1922

2023
pub fn draw(bytes: &[u8; 39]) -> Grid {
21-
let mut grid: Grid = [[0; 34]; 9];
24+
let mut grid = Grid::default();
2225

23-
for y in 0..34 {
24-
for x in 0..9 {
25-
let index = x + 9 * y;
26+
for y in 0..HEIGHT {
27+
for x in 0..WIDTH {
28+
let index = x + WIDTH * y;
2629
let byte = index / 8;
2730
let bit = index % 8;
2831
let val = if bytes[byte] & (1 << bit) > 0 {
2932
0xFF
3033
} else {
3134
0x00
3235
};
33-
grid[8 - x][y] = val;
36+
grid.0[8 - x][y] = val;
3437
}
3538
}
3639

3740
grid
3841
}
3942
pub fn display_sleep() -> Grid {
40-
[
43+
Grid([
4144
[
4245
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4346
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -83,11 +86,11 @@ pub fn display_sleep() -> Grid {
8386
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8487
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
8588
],
86-
]
89+
])
8790
}
8891

8992
pub fn display_panic() -> Grid {
90-
let grid: Grid = [
93+
Grid([
9194
[
9295
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9396
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -133,13 +136,11 @@ pub fn display_panic() -> Grid {
133136
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134137
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135138
],
136-
];
137-
138-
grid
139+
])
139140
}
140141

141142
pub fn display_lotus() -> Grid {
142-
let mut grid: Grid = [[0; 34]; 9];
143+
let mut grid = Grid::default();
143144

144145
display_letter(26, &mut grid, CAP_L);
145146
display_letter(20, &mut grid, CAP_O);
@@ -151,7 +152,7 @@ pub fn display_lotus() -> Grid {
151152
}
152153

153154
pub fn display_lotus2() -> Grid {
154-
[
155+
Grid([
155156
[
156157
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157158
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -197,107 +198,112 @@ pub fn display_lotus2() -> Grid {
197198
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
198199
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199200
],
200-
]
201+
])
201202
}
202203

203204
pub fn display_letter(pos: usize, grid: &mut Grid, letter: SingleDisplayData) {
204-
for x in 0..8 {
205-
for y in 0..8 {
205+
let letter_size = 8;
206+
for x in 0..letter_size {
207+
for y in 0..letter_size {
206208
let val = if letter[x] & (1 << y) > 0 { 0xFF } else { 0 };
207-
grid[8 - x][y + pos] = val;
209+
grid.0[letter_size - x][y + pos] = val;
208210
}
209211
}
210212
}
211213

212214
/// Gradient getting brighter from top to bottom
213215
pub fn gradient() -> Grid {
214-
let mut grid: Grid = [[0; 34]; 9];
215-
for y in 0..34 {
216-
for x in 0..9 {
217-
grid[x][y] = (1 * (y + 1)) as u8;
216+
let gradient_drop = 1; // Brightness drop between rows
217+
let mut grid = Grid::default();
218+
for y in 0..HEIGHT {
219+
for x in 0..WIDTH {
220+
grid.0[x][y] = (gradient_drop * (y + 1)) as u8;
218221
}
219222
}
220223
grid
221224
}
222225

223226
/// Fill a percentage of the rows from the bottom up
224227
pub fn percentage(percentage: u16) -> Grid {
225-
let mut grid: Grid = [[0; 34]; 9];
226-
let first_row = 34 * percentage / 100;
227-
for y in (34 - first_row)..34 {
228-
for x in 0..9 {
229-
grid[x][y as usize] = 0xFF;
228+
let mut grid = Grid::default();
229+
let first_row = HEIGHT * (percentage as usize) / 100;
230+
for y in (HEIGHT - first_row)..HEIGHT {
231+
for x in 0..WIDTH {
232+
grid.0[x][y] = 0xFF;
230233
}
231234
}
232235
grid
233236
}
234237

235238
/// Double sided gradient, bright in the middle, dim top and bottom
236239
pub fn double_gradient() -> Grid {
237-
let mut grid: Grid = [[0; 34]; 9];
238-
for y in 0..(34 / 2) {
239-
for x in 0..9 {
240-
grid[x][y] = (1 * (y + 1)) as u8;
240+
let gradient_drop = 1; // Brightness drop between rows
241+
let mut grid = Grid::default();
242+
for y in 0..(HEIGHT / 2) {
243+
for x in 0..WIDTH {
244+
grid.0[x][y] = (gradient_drop * (y + 1)) as u8;
241245
}
242246
}
243-
for y in (34 / 2)..34 {
244-
for x in 0..9 {
245-
grid[x][y] = 34 - (1 * (y + 1)) as u8;
247+
for y in (HEIGHT / 2)..HEIGHT {
248+
for x in 0..WIDTH {
249+
grid.0[x][y] = (HEIGHT - gradient_drop * (y + 1)) as u8;
246250
}
247251
}
248252
grid
249253
}
250254

251-
pub fn fill_grid(grid: Grid, matrix: &mut Foo) {
252-
for y in 0..34 {
253-
for x in 0..9 {
254-
matrix
255-
.device
256-
.pixel(x, y, grid[x as usize][y as usize])
257-
.unwrap();
255+
pub fn fill_grid(grid: &Grid, matrix: &mut Foo) {
256+
for y in 0..HEIGHT {
257+
for x in 0..WIDTH {
258+
matrix.device.pixel(x as u8, y as u8, grid.0[x][y]).unwrap();
258259
}
259260
}
260261
}
261262

262-
pub fn fill_grid_pixels(grid: Grid, matrix: &mut Foo) {
263+
pub fn fill_grid_pixels(grid: &Grid, matrix: &mut Foo) {
264+
// B4 LEDs on the first page, 0xAB on the second page
263265
let mut brightnesses = [0x00; 0xB4 + 0xAB];
264-
for y in 0..34 {
265-
for x in 0..9 {
266-
let (register, page) = (matrix.device.calc_pixel)(x, y);
267-
brightnesses[(page * 0xAA + register) as usize] = grid[x as usize][y as usize];
266+
for y in 0..HEIGHT {
267+
for x in 0..WIDTH {
268+
let (register, page) = (matrix.device.calc_pixel)(x as u8, y as u8);
269+
brightnesses[(page * 0xAA + register) as usize] = grid.0[x][y];
268270
}
269271
}
270272
matrix.device.fill_matrix(&brightnesses).unwrap();
271273
}
272274

273275
pub fn full_brightness(matrix: &mut Foo) {
274276
// Fills every pixel individually
275-
matrix.fill_brightness(0xFF).unwrap();
277+
//matrix.fill_brightness(0xFF).unwrap();
276278

277279
// Fills full page at once
278-
//matrix.device.fill(0xFF).unwrap();
280+
matrix.device.fill(0xFF).unwrap();
279281
}
280282

281283
pub fn zigzag() -> Grid {
282-
let mut grid: Grid = [[0; 34]; 9];
284+
let mut grid = Grid::default();
285+
283286
// 1st Right to left
284-
for i in 0..9 {
285-
grid[i][i] = 0xFF;
287+
for i in 0..WIDTH {
288+
grid.0[i][i] = 0xFF;
286289
}
287290
// 1st Left to right
288-
for i in 0..9 {
289-
grid[8 - i][9 + i] = 0xFF;
291+
for i in 0..WIDTH {
292+
grid.0[WIDTH - 1 - i][WIDTH + i] = 0xFF;
290293
}
291294
// 2nd right to left
292-
for i in 0..9 {
293-
grid[i][18 + i] = 0xFF;
295+
for i in 0..WIDTH {
296+
grid.0[i][2 * WIDTH + i] = 0xFF;
294297
}
295298
// 2nd left to right
296-
for i in 0..9 {
297-
if 27 + i < 34 {
298-
grid[8 - i][27 + i] = 0xFF;
299+
for i in 0..WIDTH {
300+
if 3 * WIDTH + i < HEIGHT {
301+
grid.0[WIDTH - 1 - i][3 * WIDTH + i] = 0xFF;
299302
}
300303
}
301-
grid[1][33] = 0xFF;
304+
305+
// Finish it off nicely
306+
grid.0[1][33] = 0xFF;
307+
302308
grid
303309
}

0 commit comments

Comments
 (0)