@@ -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 ( ) ;
@@ -193,22 +203,22 @@ fn main() -> ! {
193
203
grid : percentage ( 0 ) ,
194
204
col_buffer : Grid :: default ( ) ,
195
205
animate : false ,
196
- brightness : 120 ,
206
+ brightness : 51 , // Default to 51/255 = 20% brightness
197
207
sleeping : SleepState :: Awake ,
198
208
game : None ,
199
- animation_period : 31_250 , // 32 FPS
209
+ animation_period : 31_250 , // 31,250 us = 32 FPS
200
210
} ;
201
211
202
212
let mut matrix = LedMatrix :: configure ( i2c) ;
203
213
matrix
204
214
. setup ( & mut delay)
205
- . expect ( "failed to setup rgb controller" ) ;
215
+ . expect ( "failed to setup RGB controller" ) ;
206
216
207
217
matrix
208
- . set_scaling ( state . brightness )
218
+ . set_scaling ( MAX_BRIGHTNESS )
209
219
. expect ( "failed to set scaling" ) ;
210
220
211
- fill_grid_pixels ( & state. grid , & mut matrix) ;
221
+ fill_grid_pixels ( & state, & mut matrix) ;
212
222
213
223
let timer = Timer :: new ( pac. TIMER , & mut pac. RESETS ) ;
214
224
let mut prev_timer = timer. get_counter ( ) . ticks ( ) ;
@@ -239,7 +249,7 @@ fn main() -> ! {
239
249
_ => { }
240
250
}
241
251
242
- fill_grid_pixels ( & state. grid , & mut matrix) ;
252
+ fill_grid_pixels ( & state, & mut matrix) ;
243
253
if state. animate {
244
254
for x in 0 ..WIDTH {
245
255
state. grid . 0 [ x] . rotate_right ( 1 ) ;
@@ -297,7 +307,7 @@ fn main() -> ! {
297
307
buf[ 0 ] , buf[ 1 ] , buf[ 2 ] , buf[ 3 ]
298
308
)
299
309
. unwrap ( ) ;
300
- fill_grid_pixels ( & state. grid , & mut matrix) ;
310
+ fill_grid_pixels ( & state, & mut matrix) ;
301
311
}
302
312
_ => { }
303
313
}
@@ -366,21 +376,23 @@ fn handle_sleep(
366
376
match ( state. sleeping . clone ( ) , go_sleeping) {
367
377
( SleepState :: Awake , false ) => ( ) ,
368
378
( SleepState :: Awake , true ) => {
369
- state. sleeping = SleepState :: Sleeping ( state. grid . clone ( ) ) ;
370
- //state.grid = display_sleep();
371
- fill_grid_pixels ( & state. grid , matrix) ;
379
+ state. sleeping = SleepState :: Sleeping ( ( state. grid . clone ( ) , state. brightness ) ) ;
380
+ // Perhaps we could have a sleep pattern. Probbaly not Or maybe
381
+ // just for the first couple of minutes?
382
+ // state.grid = display_sleep();
383
+ // fill_grid_pixels(&state, matrix);
372
384
373
385
// Slowly decrease brightness
374
- delay . delay_ms ( 1000 ) ;
375
- let mut brightness = state . brightness ;
376
- loop {
377
- delay . delay_ms ( 100 ) ;
378
- brightness = if brightness <= 5 { 0 } else { brightness - 5 } ;
379
- matrix
380
- . set_scaling ( brightness)
381
- . expect ( "failed to set scaling" ) ;
382
- if brightness == 0 {
383
- break ;
386
+ if ! INSTANT_SLEEP {
387
+ delay . delay_ms ( 1000 ) ;
388
+ let mut brightness = state . brightness ;
389
+ loop {
390
+ delay . delay_ms ( 100 ) ;
391
+ brightness = if brightness <= 5 { 0 } else { brightness - 5 } ;
392
+ set_brightness ( state , brightness, matrix ) ;
393
+ if brightness == 0 {
394
+ break ;
395
+ }
384
396
}
385
397
}
386
398
@@ -391,30 +403,30 @@ fn handle_sleep(
391
403
//cortex_m::asm::wfi();
392
404
}
393
405
( SleepState :: Sleeping ( _) , true ) => ( ) ,
394
- ( SleepState :: Sleeping ( old_grid) , false ) => {
406
+ ( SleepState :: Sleeping ( ( old_grid, old_brightness ) ) , false ) => {
395
407
// Restore back grid before sleeping
396
408
state. sleeping = SleepState :: Awake ;
397
409
state. grid = old_grid;
398
- fill_grid_pixels ( & state. grid , matrix) ;
410
+ fill_grid_pixels ( state, matrix) ;
399
411
400
412
// Power LED controller back on
401
413
led_enable. set_high ( ) . unwrap ( ) ;
402
414
403
415
// Slowly increase brightness
404
- delay . delay_ms ( 1000 ) ;
405
- let mut brightness = 0 ;
406
- loop {
407
- delay . delay_ms ( 100 ) ;
408
- brightness = if brightness >= state . brightness - 5 {
409
- state . brightness
410
- } else {
411
- brightness + 5
412
- } ;
413
- matrix
414
- . set_scaling ( brightness)
415
- . expect ( "failed to set scaling" ) ;
416
- if brightness == state . brightness {
417
- break ;
416
+ if ! INSTANT_SLEEP {
417
+ delay . delay_ms ( 1000 ) ;
418
+ let mut brightness = 0 ;
419
+ loop {
420
+ delay . delay_ms ( 100 ) ;
421
+ brightness = if brightness >= old_brightness - 5 {
422
+ old_brightness
423
+ } else {
424
+ brightness + 5
425
+ } ;
426
+ set_brightness ( state , brightness, matrix ) ;
427
+ if brightness == old_brightness {
428
+ break ;
429
+ }
418
430
}
419
431
}
420
432
}
0 commit comments