Skip to content

Commit f278916

Browse files
committed
Improve performance (FPS) by drawing all pixels in two cmds
Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 5ac5511 commit f278916

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/control.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ pub fn handle_command(command: &Command, state: &mut State, matrix: &mut Foo) {
105105
PatternVals::ZigZag => state.grid = zigzag(),
106106
PatternVals::FullBrightness => {
107107
state.grid = percentage(100);
108-
full_brightness(matrix);
109108
state.brightness = 0xFF;
110109
matrix
111110
.set_scaling(state.brightness)

src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ use rp2040_panic_usb_boot as _;
6464
//
6565
// matrix.set_scaling(100).expect("failed to set scaling");
6666
// let grid = display_panic();
67-
// fill_grid(grid, &mut matrix);
67+
// fill_grid_pixels(grid, &mut matrix);
6868
//
6969
// loop {}
7070
//}
@@ -90,7 +90,7 @@ use fugit::RateExtU32;
9090
use usb_device::{class_prelude::*, prelude::*};
9191

9292
// USB Communications Class Device support
93-
use usbd_serial::SerialPort;
93+
use usbd_serial::{SerialPort, USB_CLASS_CDC};
9494

9595
// Used to demonstrate writing formatted strings
9696
use core::fmt::Write;
@@ -202,7 +202,7 @@ fn main() -> ! {
202202
.manufacturer("Framework")
203203
.product("Lotus LED Matrix")
204204
.serial_number(serialnum)
205-
.device_class(2) // Communications and CDC Control. From: https://www.usb.org/defined-class-codes
205+
.device_class(USB_CLASS_CDC)
206206
.build();
207207

208208
// Enable LED controller
@@ -241,7 +241,7 @@ fn main() -> ! {
241241

242242
let mut said_hello = false;
243243

244-
fill_grid(&state.grid, &mut matrix);
244+
fill_grid_pixels(&state.grid, &mut matrix);
245245

246246
let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
247247
let mut prev_timer = timer.get_counter().ticks();
@@ -254,7 +254,7 @@ fn main() -> ! {
254254

255255
// Handle period display updates. Don't do it too often
256256
if timer.get_counter().ticks() > prev_timer + 20_000 {
257-
fill_grid(&state.grid, &mut matrix);
257+
fill_grid_pixels(&state.grid, &mut matrix);
258258
if state.animate {
259259
for x in 0..WIDTH {
260260
state.grid.0[x].rotate_right(1);
@@ -297,7 +297,7 @@ fn main() -> ! {
297297
handle_sleep(go_sleeping, &mut state, &mut matrix, &mut delay);
298298
}
299299

300-
fill_grid(&state.grid, &mut matrix);
300+
fill_grid_pixels(&state.grid, &mut matrix);
301301
}
302302
}
303303
}
@@ -311,7 +311,7 @@ fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &
311311
(SleepState::Awake, true) => {
312312
state.sleeping = SleepState::Sleeping(state.grid.clone());
313313
//state.grid = display_sleep();
314-
fill_grid(&state.grid, matrix);
314+
fill_grid_pixels(&state.grid, matrix);
315315

316316
// Slowly decrease brightness
317317
delay.delay_ms(1000);
@@ -335,7 +335,7 @@ fn handle_sleep(go_sleeping: bool, state: &mut State, matrix: &mut Foo, delay: &
335335
// Restore back grid before sleeping
336336
state.sleeping = SleepState::Awake;
337337
state.grid = old_grid;
338-
fill_grid(&state.grid, matrix);
338+
fill_grid_pixels(&state.grid, matrix);
339339

340340
// Slowly increase brightness
341341
delay.delay_ms(1000);

src/patterns.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,24 @@ pub fn double_gradient() -> Grid {
252252
grid
253253
}
254254

255-
pub fn fill_grid(grid: &Grid, matrix: &mut Foo) {
255+
/// Same as fill_grid_pixels but does each pixel individually
256+
/// So it's much slower because it has to send 306 I2C commands
257+
pub fn _fill_grid(grid: &Grid, matrix: &mut Foo) {
256258
for y in 0..HEIGHT {
257259
for x in 0..WIDTH {
258260
matrix.device.pixel(x as u8, y as u8, grid.0[x][y]).unwrap();
259261
}
260262
}
261263
}
262264

265+
/// Just sends two I2C commands for the entire grid
263266
pub fn fill_grid_pixels(grid: &Grid, matrix: &mut Foo) {
264267
// B4 LEDs on the first page, 0xAB on the second page
265268
let mut brightnesses = [0x00; 0xB4 + 0xAB];
266269
for y in 0..HEIGHT {
267270
for x in 0..WIDTH {
268271
let (register, page) = (matrix.device.calc_pixel)(x as u8, y as u8);
269-
brightnesses[(page * 0xAA + register) as usize] = grid.0[x][y];
272+
brightnesses[(page as usize) * 0xB4 + (register as usize)] = grid.0[x][y];
270273
}
271274
}
272275
matrix.device.fill_matrix(&brightnesses).unwrap();

0 commit comments

Comments
 (0)