Skip to content

Commit 818691b

Browse files
Merge pull request #49 from FrameworkComputer/max-brightness
2 parents 8bdaf2a + bdf3e9f commit 818691b

File tree

5 files changed

+67
-50
lines changed

5 files changed

+67
-50
lines changed

Diff for: fl16-inputmodules/src/control.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,7 @@ pub fn handle_command(
479479
}
480480
Command::SetBrightness(br) => {
481481
//let _ = serial.write("Brightness".as_bytes());
482-
state.brightness = *br;
483-
matrix
484-
.set_scaling(state.brightness)
485-
.expect("failed to set scaling");
482+
set_brightness(state, *br, matrix);
486483
None
487484
}
488485
Command::Percentage(p) => {
@@ -499,10 +496,7 @@ pub fn handle_command(
499496
PatternVals::ZigZag => state.grid = zigzag(),
500497
PatternVals::FullBrightness => {
501498
state.grid = percentage(100);
502-
state.brightness = 0xFF;
503-
matrix
504-
.set_scaling(state.brightness)
505-
.expect("failed to set scaling");
499+
set_brightness(state, BRIGHTNESS_LEVELS, matrix);
506500
}
507501
PatternVals::DisplayPanic => state.grid = display_panic(),
508502
PatternVals::DisplayLotus2 => state.grid = display_lotus2(),

Diff for: fl16-inputmodules/src/fl16.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ where
2020
self.device.i2c
2121
}
2222

23+
// TODO: Maybe make this private and set it once in the constructor
2324
pub fn set_scaling(&mut self, scale: u8) -> Result<(), I2cError> {
2425
self.device.set_scaling(scale)
2526
}

Diff for: fl16-inputmodules/src/matrix.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub struct LedmatrixState {
2828
#[derive(Clone)]
2929
pub enum SleepState {
3030
Awake,
31-
Sleeping(Grid),
31+
Sleeping((Grid, u8)),
3232
}
3333

3434
#[allow(clippy::large_enum_variant)]

Diff for: fl16-inputmodules/src/patterns.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ use crate::matrix::*;
1212
/// math.ceil(WIDTH * HEIGHT / 8)
1313
pub const DRAW_BYTES: usize = 39;
1414

15+
/// Maximum number of brightneses levels
16+
pub const BRIGHTNESS_LEVELS: u8 = 255;
17+
1518
pub type Foo = LedMatrix<
1619
bsp::hal::I2C<
1720
I2C1,
@@ -270,14 +273,21 @@ pub fn _fill_grid(grid: &Grid, matrix: &mut Foo) {
270273
}
271274
}
272275

276+
pub fn set_brightness(state: &mut LedmatrixState, brightness: u8, matrix: &mut Foo) {
277+
state.brightness = brightness;
278+
fill_grid_pixels(state, matrix);
279+
}
280+
273281
/// Just sends two I2C commands for the entire grid
274-
pub fn fill_grid_pixels(grid: &Grid, matrix: &mut Foo) {
282+
pub fn fill_grid_pixels(state: &LedmatrixState, matrix: &mut Foo) {
275283
// 0xB4 LEDs on the first page, 0xAB on the second page
276284
let mut brightnesses = [0x00; 0xB4 + 0xAB];
277285
for y in 0..HEIGHT {
278286
for x in 0..WIDTH {
279287
let (register, page) = (matrix.device.calc_pixel)(x as u8, y as u8);
280-
brightnesses[(page as usize) * 0xB4 + (register as usize)] = grid.0[x][y];
288+
brightnesses[(page as usize) * 0xB4 + (register as usize)] =
289+
((state.grid.0[x][y] as u64) * (state.brightness as u64)
290+
/ (BRIGHTNESS_LEVELS as u64)) as u8;
281291
}
282292
}
283293
matrix.device.fill_matrix(&brightnesses).unwrap();

Diff for: ledmatrix/src/main.rs

+51-39
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ use rp2040_hal::{
1616
//use panic_probe as _;
1717
use 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

Comments
 (0)