@@ -16,6 +16,16 @@ use rp2040_hal::{
16
16
//use panic_probe as _;
17
17
use rp2040_panic_usb_boot as _;
18
18
19
+ /// Static configuration whether sleep shohld instantly turn all LEDs on/off or
20
+ /// slowly fade themm on/off
21
+ const INSTANT_SLEEP : bool = false ;
22
+
23
+ const MAX_CURRENT : usize = 500 ;
24
+
25
+ /// Maximum brightness out of 255
26
+ /// Set to 94 because that results in just below 500mA current draw.
27
+ const MAX_BRIGHTNESS : u8 = 94 ;
28
+
19
29
// TODO: Doesn't work yet, unless I panic right at the beginning of main
20
30
//#[cfg(not(debug_assertions))]
21
31
//use core::panic::PanicInfo;
@@ -65,9 +75,9 @@ use rp2040_panic_usb_boot as _;
65
75
// .setup(&mut delay)
66
76
// .expect("failed to setup rgb controller");
67
77
//
68
- // matrix.set_scaling(100).expect("failed to set scaling" );
78
+ // set_brightness(state, 255, &mut matrix );
69
79
// let grid = display_panic();
70
- // fill_grid_pixels(grid , &mut matrix);
80
+ // fill_grid_pixels(state , &mut matrix);
71
81
//
72
82
// loop {}
73
83
//}
@@ -166,7 +176,7 @@ fn main() -> ! {
166
176
. manufacturer ( "Framework Computer Inc" )
167
177
. product ( "LED Matrix Input Module" )
168
178
. serial_number ( serialnum)
169
- . max_power ( 200 ) // Device uses roughly 164mW when all LEDs are at full brightness
179
+ . max_power ( MAX_CURRENT )
170
180
. device_release ( device_release ( ) )
171
181
. device_class ( USB_CLASS_CDC )
172
182
. build ( ) ;
@@ -191,22 +201,22 @@ fn main() -> ! {
191
201
grid : percentage ( 0 ) ,
192
202
col_buffer : Grid :: default ( ) ,
193
203
animate : false ,
194
- brightness : 120 ,
204
+ brightness : 51 , // Default to 51/255 = 20% brightness
195
205
sleeping : SleepState :: Awake ,
196
206
game : None ,
197
- animation_period : 31_250 , // 32 FPS
207
+ animation_period : 31_250 , // 31,250 us = 32 FPS
198
208
} ;
199
209
200
210
let mut matrix = LedMatrix :: configure ( i2c) ;
201
211
matrix
202
212
. setup ( & mut delay)
203
- . expect ( "failed to setup rgb controller" ) ;
213
+ . expect ( "failed to setup RGB controller" ) ;
204
214
205
215
matrix
206
- . set_scaling ( state . brightness )
216
+ . set_scaling ( MAX_BRIGHTNESS )
207
217
. expect ( "failed to set scaling" ) ;
208
218
209
- fill_grid_pixels ( & state. grid , & mut matrix) ;
219
+ fill_grid_pixels ( & state, & mut matrix) ;
210
220
211
221
let timer = Timer :: new ( pac. TIMER , & mut pac. RESETS ) ;
212
222
let mut prev_timer = timer. get_counter ( ) . ticks ( ) ;
@@ -251,7 +261,7 @@ fn main() -> ! {
251
261
_ => { }
252
262
}
253
263
254
- fill_grid_pixels ( & state. grid , & mut matrix) ;
264
+ fill_grid_pixels ( & state, & mut matrix) ;
255
265
if state. animate {
256
266
for x in 0 ..WIDTH {
257
267
state. grid . 0 [ x] . rotate_right ( 1 ) ;
@@ -309,7 +319,7 @@ fn main() -> ! {
309
319
buf[ 0 ] , buf[ 1 ] , buf[ 2 ] , buf[ 3 ]
310
320
)
311
321
. unwrap ( ) ;
312
- fill_grid_pixels ( & state. grid , & mut matrix) ;
322
+ fill_grid_pixels ( & state, & mut matrix) ;
313
323
}
314
324
_ => { }
315
325
}
@@ -378,21 +388,23 @@ fn handle_sleep(
378
388
match ( state. sleeping . clone ( ) , go_sleeping) {
379
389
( SleepState :: Awake , false ) => ( ) ,
380
390
( SleepState :: Awake , true ) => {
381
- state. sleeping = SleepState :: Sleeping ( state. grid . clone ( ) ) ;
382
- //state.grid = display_sleep();
383
- fill_grid_pixels ( & state. grid , matrix) ;
391
+ state. sleeping = SleepState :: Sleeping ( ( state. grid . clone ( ) , state. brightness ) ) ;
392
+ // Perhaps we could have a sleep pattern. Probbaly not Or maybe
393
+ // just for the first couple of minutes?
394
+ // state.grid = display_sleep();
395
+ // fill_grid_pixels(&state, matrix);
384
396
385
397
// Slowly decrease brightness
386
- delay . delay_ms ( 1000 ) ;
387
- let mut brightness = state . brightness ;
388
- loop {
389
- delay . delay_ms ( 100 ) ;
390
- brightness = if brightness <= 5 { 0 } else { brightness - 5 } ;
391
- matrix
392
- . set_scaling ( brightness)
393
- . expect ( "failed to set scaling" ) ;
394
- if brightness == 0 {
395
- break ;
398
+ if ! INSTANT_SLEEP {
399
+ delay . delay_ms ( 1000 ) ;
400
+ let mut brightness = state . brightness ;
401
+ loop {
402
+ delay . delay_ms ( 100 ) ;
403
+ brightness = if brightness <= 5 { 0 } else { brightness - 5 } ;
404
+ set_brightness ( state , brightness, matrix ) ;
405
+ if brightness == 0 {
406
+ break ;
407
+ }
396
408
}
397
409
}
398
410
@@ -403,30 +415,30 @@ fn handle_sleep(
403
415
//cortex_m::asm::wfi();
404
416
}
405
417
( SleepState :: Sleeping ( _) , true ) => ( ) ,
406
- ( SleepState :: Sleeping ( old_grid) , false ) => {
418
+ ( SleepState :: Sleeping ( ( old_grid, old_brightness ) ) , false ) => {
407
419
// Restore back grid before sleeping
408
420
state. sleeping = SleepState :: Awake ;
409
421
state. grid = old_grid;
410
- fill_grid_pixels ( & state. grid , matrix) ;
422
+ fill_grid_pixels ( state, matrix) ;
411
423
412
424
// Power LED controller back on
413
425
led_enable. set_high ( ) . unwrap ( ) ;
414
426
415
427
// Slowly increase brightness
416
- delay . delay_ms ( 1000 ) ;
417
- let mut brightness = 0 ;
418
- loop {
419
- delay . delay_ms ( 100 ) ;
420
- brightness = if brightness >= state . brightness - 5 {
421
- state . brightness
422
- } else {
423
- brightness + 5
424
- } ;
425
- matrix
426
- . set_scaling ( brightness)
427
- . expect ( "failed to set scaling" ) ;
428
- if brightness == state . brightness {
429
- break ;
428
+ if ! INSTANT_SLEEP {
429
+ delay . delay_ms ( 1000 ) ;
430
+ let mut brightness = 0 ;
431
+ loop {
432
+ delay . delay_ms ( 100 ) ;
433
+ brightness = if brightness >= old_brightness - 5 {
434
+ old_brightness
435
+ } else {
436
+ brightness + 5
437
+ } ;
438
+ set_brightness ( state , brightness, matrix ) ;
439
+ if brightness == old_brightness {
440
+ break ;
441
+ }
430
442
}
431
443
}
432
444
}
0 commit comments