Skip to content

Commit 92fa7fe

Browse files
cbiffleJohnAZoidberg
authored andcommitted
fl16-inputmodules: add gamma correction.
The brightness values sent to the LED controller actually control a PWM duty cycle. LEDs emit light roughly in proportion to the PWM duty cycle, but human vision perceives brightness on an exponential curve -- generally it takes 2x the physical luminous flux for the eye to perceive something as one step brighter. As a result, brightness ramps (like the one generated by --all-brightnesses) were rapidly brightening to what appeared to be max, and then flattening. This change adds gamma correction, which maps the linear input brightness values to a non-linear function of PWM duty cycles, causing the ramp to look far more ramp-y. The gamma value I've chosen here is arguably a preference, I messed with output on a PVT panel until I found something that looked approximately right.
1 parent a548403 commit 92fa7fe

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

fl16-inputmodules/src/patterns.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ pub fn double_gradient() -> Grid {
313313
pub fn _fill_grid(grid: &Grid, matrix: &mut Foo) {
314314
for y in 0..HEIGHT {
315315
for x in 0..WIDTH {
316-
matrix.device.pixel(x as u8, y as u8, grid.0[x][y]).unwrap();
316+
let p = gamma::correct(grid.0[x][y]);
317+
matrix.device.pixel(x as u8, y as u8, p).unwrap();
317318
}
318319
}
319320
}
@@ -330,9 +331,11 @@ pub fn fill_grid_pixels(state: &LedmatrixState, matrix: &mut Foo) {
330331
for y in 0..HEIGHT {
331332
for x in 0..WIDTH {
332333
let (register, page) = (matrix.device.calc_pixel)(x as u8, y as u8);
333-
brightnesses[(page as usize) * 0xB4 + (register as usize)] =
334+
let uncorrected =
334335
((state.grid.0[x][y] as u64) * (state.brightness as u64)
335336
/ (BRIGHTNESS_LEVELS as u64)) as u8;
337+
brightnesses[(page as usize) * 0xB4 + (register as usize)] =
338+
gamma::correct(uncorrected);
336339
}
337340
}
338341
matrix.device.fill_matrix(&brightnesses).unwrap();
@@ -387,3 +390,11 @@ pub fn every_nth_col(n: usize) -> Grid {
387390

388391
grid
389392
}
393+
394+
pub mod gamma {
395+
pub const fn correct(value: u8) -> u8 {
396+
GAMMA[value as usize]
397+
}
398+
399+
include!(concat!(env!("OUT_DIR"), "/gamma.rs"));
400+
}

0 commit comments

Comments
 (0)