@@ -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 : 50 , // Default to 50% 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 ( ) ;
@@ -238,7 +248,7 @@ fn main() -> ! {
238
248
_ => { }
239
249
}
240
250
241
- fill_grid_pixels ( & state. grid , & mut matrix) ;
251
+ fill_grid_pixels ( & state, & mut matrix) ;
242
252
if state. animate {
243
253
for x in 0 ..WIDTH {
244
254
state. grid . 0 [ x] . rotate_right ( 1 ) ;
@@ -296,7 +306,7 @@ fn main() -> ! {
296
306
buf[ 0 ] , buf[ 1 ] , buf[ 2 ] , buf[ 3 ]
297
307
)
298
308
. unwrap ( ) ;
299
- fill_grid_pixels ( & state. grid , & mut matrix) ;
309
+ fill_grid_pixels ( & state, & mut matrix) ;
300
310
}
301
311
_ => { }
302
312
}
@@ -365,21 +375,23 @@ fn handle_sleep(
365
375
match ( state. sleeping . clone ( ) , go_sleeping) {
366
376
( SleepState :: Awake , false ) => ( ) ,
367
377
( SleepState :: Awake , true ) => {
368
- state. sleeping = SleepState :: Sleeping ( state. grid . clone ( ) ) ;
369
- //state.grid = display_sleep();
370
- fill_grid_pixels ( & state. grid , matrix) ;
378
+ state. sleeping = SleepState :: Sleeping ( ( state. grid . clone ( ) , state. brightness ) ) ;
379
+ // Perhaps we could have a sleep pattern. Probbaly not Or maybe
380
+ // just for the first couple of minutes?
381
+ // state.grid = display_sleep();
382
+ // fill_grid_pixels(&state, matrix);
371
383
372
384
// Slowly decrease brightness
373
- delay . delay_ms ( 1000 ) ;
374
- let mut brightness = state . brightness ;
375
- loop {
376
- delay . delay_ms ( 100 ) ;
377
- brightness = if brightness <= 5 { 0 } else { brightness - 5 } ;
378
- matrix
379
- . set_scaling ( brightness)
380
- . expect ( "failed to set scaling" ) ;
381
- if brightness == 0 {
382
- break ;
385
+ if ! INSTANT_SLEEP {
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
+ set_brightness ( state , brightness, matrix ) ;
392
+ if brightness == 0 {
393
+ break ;
394
+ }
383
395
}
384
396
}
385
397
@@ -390,30 +402,30 @@ fn handle_sleep(
390
402
//cortex_m::asm::wfi();
391
403
}
392
404
( SleepState :: Sleeping ( _) , true ) => ( ) ,
393
- ( SleepState :: Sleeping ( old_grid) , false ) => {
405
+ ( SleepState :: Sleeping ( ( old_grid, old_brightness ) ) , false ) => {
394
406
// Restore back grid before sleeping
395
407
state. sleeping = SleepState :: Awake ;
396
408
state. grid = old_grid;
397
- fill_grid_pixels ( & state. grid , matrix) ;
409
+ fill_grid_pixels ( state, matrix) ;
398
410
399
411
// Power LED controller back on
400
412
led_enable. set_high ( ) . unwrap ( ) ;
401
413
402
414
// Slowly increase brightness
403
- delay . delay_ms ( 1000 ) ;
404
- let mut brightness = 0 ;
405
- loop {
406
- delay . delay_ms ( 100 ) ;
407
- brightness = if brightness >= state . brightness - 5 {
408
- state . brightness
409
- } else {
410
- brightness + 5
411
- } ;
412
- matrix
413
- . set_scaling ( brightness)
414
- . expect ( "failed to set scaling" ) ;
415
- if brightness == state . brightness {
416
- break ;
415
+ if ! INSTANT_SLEEP {
416
+ delay . delay_ms ( 1000 ) ;
417
+ let mut brightness = 0 ;
418
+ loop {
419
+ delay . delay_ms ( 100 ) ;
420
+ brightness = if brightness >= old_brightness - 5 {
421
+ old_brightness
422
+ } else {
423
+ brightness + 5
424
+ } ;
425
+ set_brightness ( state , brightness, matrix ) ;
426
+ if brightness == old_brightness {
427
+ break ;
428
+ }
417
429
}
418
430
}
419
431
}
0 commit comments