Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ledmatrix: gamma correction for smoother brightness ramps #99

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions fl16-inputmodules/build.rs
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 2 additions & 0 deletions fl16-inputmodules/src/fl16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ where
Ok(())
}

/// Fills the matrix with a _raw_ brightness value, i.e. without gamma
/// correction, to show the native PWM values.
pub fn fill_brightness(&mut self, brightness: u8) -> Result<(), Error<I2cError>> {
for x in 0..self.device.width {
for y in 0..self.device.height {
Expand Down
15 changes: 13 additions & 2 deletions fl16-inputmodules/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,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();
}
}
}
Expand All @@ -327,9 +328,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();
Expand Down Expand Up @@ -384,3 +387,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"));
}