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(); +} 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")); +} 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