@@ -16,6 +16,16 @@ use rp2040_hal::{
1616//use panic_probe as _;
1717use rp2040_panic_usb_boot as _;
1818
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+
1929// TODO: Doesn't work yet, unless I panic right at the beginning of main
2030//#[cfg(not(debug_assertions))]
2131//use core::panic::PanicInfo;
@@ -65,9 +75,9 @@ use rp2040_panic_usb_boot as _;
6575// .setup(&mut delay)
6676// .expect("failed to setup rgb controller");
6777//
68- // matrix.set_scaling(100).expect("failed to set scaling" );
78+ // set_brightness(state, 255, &mut matrix );
6979// let grid = display_panic();
70- // fill_grid_pixels(grid , &mut matrix);
80+ // fill_grid_pixels(state , &mut matrix);
7181//
7282// loop {}
7383//}
@@ -166,7 +176,7 @@ fn main() -> ! {
166176 . manufacturer ( "Framework Computer Inc" )
167177 . product ( "LED Matrix Input Module" )
168178 . serial_number ( serialnum)
169- . max_power ( 200 ) // Device uses roughly 164mW when all LEDs are at full brightness
179+ . max_power ( MAX_CURRENT )
170180 . device_release ( device_release ( ) )
171181 . device_class ( USB_CLASS_CDC )
172182 . build ( ) ;
@@ -191,22 +201,22 @@ fn main() -> ! {
191201 grid : percentage ( 0 ) ,
192202 col_buffer : Grid :: default ( ) ,
193203 animate : false ,
194- brightness : 120 ,
204+ brightness : 51 , // Default to 51/255 = 20% brightness
195205 sleeping : SleepState :: Awake ,
196206 game : None ,
197- animation_period : 31_250 , // 32 FPS
207+ animation_period : 31_250 , // 31,250 us = 32 FPS
198208 } ;
199209
200210 let mut matrix = LedMatrix :: configure ( i2c) ;
201211 matrix
202212 . setup ( & mut delay)
203- . expect ( "failed to setup rgb controller" ) ;
213+ . expect ( "failed to setup RGB controller" ) ;
204214
205215 matrix
206- . set_scaling ( state . brightness )
216+ . set_scaling ( MAX_BRIGHTNESS )
207217 . expect ( "failed to set scaling" ) ;
208218
209- fill_grid_pixels ( & state. grid , & mut matrix) ;
219+ fill_grid_pixels ( & state, & mut matrix) ;
210220
211221 let timer = Timer :: new ( pac. TIMER , & mut pac. RESETS ) ;
212222 let mut prev_timer = timer. get_counter ( ) . ticks ( ) ;
@@ -251,7 +261,7 @@ fn main() -> ! {
251261 _ => { }
252262 }
253263
254- fill_grid_pixels ( & state. grid , & mut matrix) ;
264+ fill_grid_pixels ( & state, & mut matrix) ;
255265 if state. animate {
256266 for x in 0 ..WIDTH {
257267 state. grid . 0 [ x] . rotate_right ( 1 ) ;
@@ -309,7 +319,7 @@ fn main() -> ! {
309319 buf[ 0 ] , buf[ 1 ] , buf[ 2 ] , buf[ 3 ]
310320 )
311321 . unwrap ( ) ;
312- fill_grid_pixels ( & state. grid , & mut matrix) ;
322+ fill_grid_pixels ( & state, & mut matrix) ;
313323 }
314324 _ => { }
315325 }
@@ -378,21 +388,23 @@ fn handle_sleep(
378388 match ( state. sleeping . clone ( ) , go_sleeping) {
379389 ( SleepState :: Awake , false ) => ( ) ,
380390 ( 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);
384396
385397 // 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+ }
396408 }
397409 }
398410
@@ -403,30 +415,30 @@ fn handle_sleep(
403415 //cortex_m::asm::wfi();
404416 }
405417 ( SleepState :: Sleeping ( _) , true ) => ( ) ,
406- ( SleepState :: Sleeping ( old_grid) , false ) => {
418+ ( SleepState :: Sleeping ( ( old_grid, old_brightness ) ) , false ) => {
407419 // Restore back grid before sleeping
408420 state. sleeping = SleepState :: Awake ;
409421 state. grid = old_grid;
410- fill_grid_pixels ( & state. grid , matrix) ;
422+ fill_grid_pixels ( state, matrix) ;
411423
412424 // Power LED controller back on
413425 led_enable. set_high ( ) . unwrap ( ) ;
414426
415427 // 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+ }
430442 }
431443 }
432444 }
0 commit comments