From a5484033ede1440ba411b12621214a1955ef9fd0 Mon Sep 17 00:00:00 2001 From: "Cliff L. Biffle" Date: Wed, 20 Mar 2024 09:46:54 -0700 Subject: [PATCH 1/3] fl16-inputmodules: generate gamma table during build This costs ~256 bytes of flash (once referenced). I've tried to keep the generated code readable. --- fl16-inputmodules/build.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 fl16-inputmodules/build.rs diff --git a/fl16-inputmodules/build.rs b/fl16-inputmodules/build.rs new file mode 100644 index 00000000..597775c4 --- /dev/null +++ b/fl16-inputmodules/build.rs @@ -0,0 +1,29 @@ +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::Path; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let gamma_path = Path::new(&out_dir).join("gamma.rs"); + let mut f = File::create(gamma_path).unwrap(); + + // Determined empirically for a PVT panel. May need to become conditional + // on features, TBD. + const GAMMA: f32 = 3.2; + + let corrected: [f32; 256] = + std::array::from_fn(|i| f32::powf((i as f32) / 255., GAMMA) * 255. + 0.5); + + writeln!(f, "const GAMMA: [u8; 256] = [").unwrap(); + + const LINE_LEN: usize = 8; + for line in corrected.chunks(LINE_LEN) { + write!(f, " ").unwrap(); + for element in line { + write!(f, " {:>3},", *element as u8).unwrap(); + } + writeln!(f).unwrap(); + } + writeln!(f, "];").unwrap(); +} From 92fa7fe4bc4543c2d79743d355a59845b8446165 Mon Sep 17 00:00:00 2001 From: "Cliff L. Biffle" Date: Wed, 20 Mar 2024 10:17:26 -0700 Subject: [PATCH 2/3] 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. --- fl16-inputmodules/src/patterns.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/fl16-inputmodules/src/patterns.rs b/fl16-inputmodules/src/patterns.rs index e8ff33d6..fc3c84dd 100644 --- a/fl16-inputmodules/src/patterns.rs +++ b/fl16-inputmodules/src/patterns.rs @@ -313,7 +313,8 @@ pub fn double_gradient() -> Grid { pub fn _fill_grid(grid: &Grid, matrix: &mut Foo) { for y in 0..HEIGHT { for x in 0..WIDTH { - matrix.device.pixel(x as u8, y as u8, grid.0[x][y]).unwrap(); + let p = gamma::correct(grid.0[x][y]); + matrix.device.pixel(x as u8, y as u8, p).unwrap(); } } } @@ -330,9 +331,11 @@ pub fn fill_grid_pixels(state: &LedmatrixState, matrix: &mut Foo) { for y in 0..HEIGHT { for x in 0..WIDTH { let (register, page) = (matrix.device.calc_pixel)(x as u8, y as u8); - brightnesses[(page as usize) * 0xB4 + (register as usize)] = + let uncorrected = ((state.grid.0[x][y] as u64) * (state.brightness as u64) / (BRIGHTNESS_LEVELS as u64)) as u8; + brightnesses[(page as usize) * 0xB4 + (register as usize)] = + gamma::correct(uncorrected); } } matrix.device.fill_matrix(&brightnesses).unwrap(); @@ -387,3 +390,11 @@ pub fn every_nth_col(n: usize) -> Grid { grid } + +pub mod gamma { + pub const fn correct(value: u8) -> u8 { + GAMMA[value as usize] + } + + include!(concat!(env!("OUT_DIR"), "/gamma.rs")); +} From 4dbdac6ae239e8ca438d89719d5e41636a63fb16 Mon Sep 17 00:00:00 2001 From: Daniel Schaefer Date: Thu, 22 Jan 2026 03:35:18 +0800 Subject: [PATCH 3/3] Update default brightness With gamma scaling, 51 is very dim, need to increase the default brightness to be able to see the bootup animation. Signed-off-by: Daniel Schaefer --- ledmatrix/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ledmatrix/src/main.rs b/ledmatrix/src/main.rs index 9337f053..2a7b540f 100644 --- a/ledmatrix/src/main.rs +++ b/ledmatrix/src/main.rs @@ -242,7 +242,7 @@ fn main() -> ! { grid: percentage(0), col_buffer: Grid::default(), animate: false, - brightness: 51, // Default to 51/255 = 20% brightness + brightness: 150, sleeping: SleepState::Awake, game: None, animation_period: 31_250, // 31,250 us = 32 FPS