chore: introduce consts for display height and width

This commit is contained in:
Valentin Weber 2025-07-20 09:38:32 +02:00
parent b4d8c1ab97
commit 159c678e15
No known key found for this signature in database
GPG key ID: 44797000F143F522

View file

@ -136,6 +136,9 @@ fn list_devices(transport: &TransportProtocol) -> Result<()> {
} }
fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> { fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
const DISPLAY_HEIGHT: u32 = 11;
const DISPLAY_WIDTH: u32 = 44;
let config_path = args.config.take().unwrap_or_default(); let config_path = args.config.take().unwrap_or_default();
let config = fs::read_to_string(&config_path) let config = fs::read_to_string(&config_path)
.with_context(|| format!("load config: {config_path:?}"))?; .with_context(|| format!("load config: {config_path:?}"))?;
@ -177,8 +180,9 @@ fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
let lines: Vec<_> = bitstring.trim().lines().collect(); let lines: Vec<_> = bitstring.trim().lines().collect();
anyhow::ensure!( anyhow::ensure!(
lines.len() == 11, lines.len() == DISPLAY_HEIGHT as usize,
"expected 11 lines in bitstring, found {} lines", "expected {} lines in bitstring, found {} lines",
DISPLAY_HEIGHT,
lines.len() lines.len()
); );
let width = lines[0].len(); let width = lines[0].len();
@ -234,10 +238,10 @@ fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
let img_reader = ImageReader::open(img_file)?; let img_reader = ImageReader::open(img_file)?;
let img = img_reader let img = img_reader
.decode()? .decode()?
.resize(u32::MAX, 11, FilterType::Nearest) .resize(u32::MAX, DISPLAY_HEIGHT, FilterType::Nearest)
.into_luma8(); .into_luma8();
let (width, height) = img.dimensions(); let (width, height) = img.dimensions();
let mut buffer = payload.add_message(style, (width as usize).div_ceil(8)); let mut buffer = payload.add_message(style, width.div_ceil(8) as usize);
for y in 0..height { for y in 0..height {
for x in 0..width { for x in 0..width {
if img.get_pixel(x, y).0 > [31] { if img.get_pixel(x, y).0 > [31] {
@ -256,19 +260,30 @@ fn generate_payload(args: &mut Args) -> Result<PayloadBuffer> {
let frame_count = frames.len(); let frame_count = frames.len();
let (width, height) = frames.first().unwrap().buffer().dimensions(); let (width, height) = frames.first().unwrap().buffer().dimensions();
if height != 11 || width != 44 { if height != DISPLAY_HEIGHT || width != DISPLAY_WIDTH {
anyhow::bail!("Expected 44x11 pixel gif file"); anyhow::bail!(
"Expected {}x{} pixel gif file",
DISPLAY_WIDTH,
DISPLAY_WIDTH
);
} }
let mut buffer = payload.add_message(style, (48 * frame_count).div_ceil(8)); let mut buffer = payload.add_message(
style,
((DISPLAY_WIDTH as usize + 4) * frame_count).div_ceil(8),
);
for (i, frame) in frames.iter().enumerate() { for (i, frame) in frames.iter().enumerate() {
let buf = frame.buffer(); let buf = frame.buffer();
for y in 0..11 { for y in 0..DISPLAY_HEIGHT {
for x in 0..44 { for x in 0..DISPLAY_WIDTH {
if buf.get_pixel(x, y).to_luma().0 > [31] { if buf.get_pixel(x, y).to_luma().0 > [31] {
Pixel( Pixel(
Point::new((x as usize + i * 48).try_into()?, y.try_into()?), Point::new(
(x as usize + i * (DISPLAY_WIDTH as usize + 4))
.try_into()?,
y.try_into()?,
),
BinaryColor::On, BinaryColor::On,
) )
.draw(&mut buffer)?; .draw(&mut buffer)?;