Skip to content

Commit

Permalink
Merge pull request #147 from GrantM11235/infallible-fill-params-buf
Browse files Browse the repository at this point in the history
Make fill_params_buf infallible
  • Loading branch information
almindor authored Nov 16, 2024
2 parents eb124d5 + 2df07d3 commit f8b268c
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 82 deletions.
4 changes: 2 additions & 2 deletions mipidsi/src/dcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait DcsCommand {
fn instruction(&self) -> u8;

/// Fills the given buffer with the command parameters.
fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error>;
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize;
}

/// Wrapper around [`WriteOnlyDataCommand`] with support for writing DCS commands.
Expand Down Expand Up @@ -66,7 +66,7 @@ where
/// Sends a DCS command to the display interface.
pub fn write_command(&mut self, command: impl DcsCommand) -> Result<(), Error> {
let mut param_bytes: [u8; 16] = [0; 16];
let n = command.fill_params_buf(&mut param_bytes)?;
let n = command.fill_params_buf(&mut param_bytes);
self.write_raw(command.instruction(), &param_bytes[..n])
}

Expand Down
4 changes: 2 additions & 2 deletions mipidsi/src/dcs/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ macro_rules! dcs_basic_command {
$instr
}

fn fill_params_buf(&self, _buffer: &mut [u8]) -> Result<usize, Error> {
Ok(0)
fn fill_params_buf(&self, _buffer: &mut [u8]) -> usize {
0
}
}
};
Expand Down
17 changes: 7 additions & 10 deletions mipidsi/src/dcs/set_address_mode.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! Module for the MADCTL instruction constructors
use crate::error::Error;
use crate::options::{
ColorOrder, HorizontalRefreshOrder, MemoryMapping, ModelOptions, Orientation, RefreshOrder,
VerticalRefreshOrder,
Expand Down Expand Up @@ -79,9 +78,9 @@ impl DcsCommand for SetAddressMode {
0x36
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
buffer[0] = self.0;
Ok(1)
1
}
}

Expand All @@ -101,7 +100,7 @@ mod tests {
use super::*;

#[test]
fn madctl_bit_operations() -> Result<(), Error> {
fn madctl_bit_operations() {
let madctl = SetAddressMode::default()
.with_color_order(ColorOrder::Bgr)
.with_refresh_order(RefreshOrder::new(
Expand All @@ -111,21 +110,19 @@ mod tests {
.with_orientation(Orientation::default().rotate(Rotation::Deg270));

let mut bytes = [0u8];
assert_eq!(madctl.fill_params_buf(&mut bytes)?, 1);
assert_eq!(madctl.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b1011_1100u8]);

let madctl = madctl.with_orientation(Orientation::default());
assert_eq!(madctl.fill_params_buf(&mut bytes)?, 1);
assert_eq!(madctl.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0001_1100u8]);

let madctl = madctl.with_color_order(ColorOrder::Rgb);
assert_eq!(madctl.fill_params_buf(&mut bytes)?, 1);
assert_eq!(madctl.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0001_0100u8]);

let madctl = madctl.with_refresh_order(RefreshOrder::default());
assert_eq!(madctl.fill_params_buf(&mut bytes)?, 1);
assert_eq!(madctl.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0000_0000u8]);

Ok(())
}
}
12 changes: 4 additions & 8 deletions mipidsi/src/dcs/set_column_address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Module for the CASET address window instruction constructors
use crate::error::Error;

use super::DcsCommand;

/// Set Column Address
Expand All @@ -26,11 +24,11 @@ impl DcsCommand for SetColumnAddress {
0x2A
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
buffer[0..2].copy_from_slice(&self.start_column.to_be_bytes());
buffer[2..4].copy_from_slice(&self.end_column.to_be_bytes());

Ok(4)
4
}
}

Expand All @@ -39,13 +37,11 @@ mod tests {
use super::*;

#[test]
fn caset_fills_data_properly() -> Result<(), Error> {
fn caset_fills_data_properly() {
let caset = SetColumnAddress::new(0, 320);

let mut buffer = [0u8; 4];
assert_eq!(caset.fill_params_buf(&mut buffer)?, 4);
assert_eq!(caset.fill_params_buf(&mut buffer), 4);
assert_eq!(buffer, [0, 0, 0x1, 0x40]);

Ok(())
}
}
11 changes: 4 additions & 7 deletions mipidsi/src/dcs/set_invert_mode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::Error;
use crate::options::ColorInversion;

use super::DcsCommand;
Expand All @@ -22,8 +21,8 @@ impl DcsCommand for SetInvertMode {
}
}

fn fill_params_buf(&self, _buffer: &mut [u8]) -> Result<usize, Error> {
Ok(0)
fn fill_params_buf(&self, _buffer: &mut [u8]) -> usize {
0
}
}

Expand All @@ -32,13 +31,11 @@ mod tests {
use super::*;

#[test]
fn set_invert_mode_chooses_correct_instruction() -> Result<(), Error> {
fn set_invert_mode_chooses_correct_instruction() {
let ste = SetInvertMode(ColorInversion::Inverted);

let mut buffer = [0u8; 0];
assert_eq!(ste.instruction(), 0x21);
assert_eq!(ste.fill_params_buf(&mut buffer)?, 0);

Ok(())
assert_eq!(ste.fill_params_buf(&mut buffer), 0);
}
}
12 changes: 4 additions & 8 deletions mipidsi/src/dcs/set_page_address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Module for the RASET address window instruction constructors
use crate::error::Error;

use super::DcsCommand;

/// Set Page Address
Expand All @@ -23,11 +21,11 @@ impl DcsCommand for SetPageAddress {
0x2B
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
buffer[0..2].copy_from_slice(&self.start_row.to_be_bytes());
buffer[2..4].copy_from_slice(&self.end_row.to_be_bytes());

Ok(4)
4
}
}

Expand All @@ -36,13 +34,11 @@ mod tests {
use super::*;

#[test]
fn raset_fills_data_properly() -> Result<(), Error> {
fn raset_fills_data_properly() {
let raset = SetPageAddress::new(0, 320);

let mut buffer = [0u8; 4];
assert_eq!(raset.fill_params_buf(&mut buffer)?, 4);
assert_eq!(raset.fill_params_buf(&mut buffer), 4);
assert_eq!(buffer, [0, 0, 0x1, 0x40]);

Ok(())
}
}
24 changes: 8 additions & 16 deletions mipidsi/src/dcs/set_pixel_format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Module for the COLMOD instruction constructors
use crate::error::Error;

use super::DcsCommand;

/// Set Pixel Format
Expand All @@ -20,9 +18,9 @@ impl DcsCommand for SetPixelFormat {
0x3A
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
buffer[0] = self.0.as_u8();
Ok(1)
1
}
}

Expand Down Expand Up @@ -85,45 +83,39 @@ mod tests {
use super::*;

#[test]
fn colmod_rgb565_is_16bit() -> Result<(), Error> {
fn colmod_rgb565_is_16bit() {
let colmod = SetPixelFormat::new(PixelFormat::new(
BitsPerPixel::Sixteen,
BitsPerPixel::Sixteen,
));

let mut bytes = [0u8];
assert_eq!(colmod.fill_params_buf(&mut bytes)?, 1);
assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0101_0101u8]);

Ok(())
}

#[test]
fn colmod_rgb666_is_18bit() -> Result<(), Error> {
fn colmod_rgb666_is_18bit() {
let colmod = SetPixelFormat::new(PixelFormat::new(
BitsPerPixel::Eighteen,
BitsPerPixel::Eighteen,
));

let mut bytes = [0u8];
assert_eq!(colmod.fill_params_buf(&mut bytes)?, 1);
assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0110_0110u8]);

Ok(())
}

#[test]
fn colmod_rgb888_is_24bit() -> Result<(), Error> {
fn colmod_rgb888_is_24bit() {
let colmod = SetPixelFormat::new(PixelFormat::new(
BitsPerPixel::Eighteen,
BitsPerPixel::TwentyFour,
));

let mut bytes = [0u8];
assert_eq!(colmod.fill_params_buf(&mut bytes)?, 1);
assert_eq!(colmod.fill_params_buf(&mut bytes), 1);
assert_eq!(bytes, [0b0110_0111u8]);

Ok(())
}

#[test]
Expand Down
12 changes: 4 additions & 8 deletions mipidsi/src/dcs/set_scroll_area.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Module for the VSCRDEF visual scroll definition instruction constructors
use crate::error::Error;

use super::DcsCommand;

/// Set Scroll Area
Expand All @@ -26,7 +24,7 @@ impl DcsCommand for SetScrollArea {
0x33
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
let tfa_bytes = self.tfa.to_be_bytes();
let vsa_bytes = self.vsa.to_be_bytes();
let bfa_bytes = self.bfa.to_be_bytes();
Expand All @@ -38,7 +36,7 @@ impl DcsCommand for SetScrollArea {
buffer[4] = bfa_bytes[0];
buffer[5] = bfa_bytes[1];

Ok(6)
6
}
}

Expand All @@ -47,13 +45,11 @@ mod tests {
use super::*;

#[test]
fn vscrdef_fills_buffer_properly() -> Result<(), Error> {
fn vscrdef_fills_buffer_properly() {
let vscrdef = SetScrollArea::new(0, 320, 0);

let mut buffer = [0u8; 6];
assert_eq!(vscrdef.fill_params_buf(&mut buffer)?, 6);
assert_eq!(vscrdef.fill_params_buf(&mut buffer), 6);
assert_eq!(buffer, [0, 0, 0x1, 0x40, 0, 0]);

Ok(())
}
}
12 changes: 4 additions & 8 deletions mipidsi/src/dcs/set_scroll_start.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Module for the VSCAD visual scroll offset instruction constructors
use crate::error::Error;

use super::DcsCommand;

/// Set Scroll Start
Expand All @@ -20,12 +18,12 @@ impl DcsCommand for SetScrollStart {
0x37
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
let bytes = self.0.to_be_bytes();
buffer[0] = bytes[0];
buffer[1] = bytes[1];

Ok(2)
2
}
}

Expand All @@ -34,13 +32,11 @@ mod tests {
use super::*;

#[test]
fn vscad_fills_offset_properly() -> Result<(), Error> {
fn vscad_fills_offset_properly() {
let vscad = SetScrollStart::new(320);

let mut buffer = [0u8; 2];
assert_eq!(vscad.fill_params_buf(&mut buffer)?, 2);
assert_eq!(vscad.fill_params_buf(&mut buffer), 2);
assert_eq!(buffer, [0x1, 0x40]);

Ok(())
}
}
21 changes: 8 additions & 13 deletions mipidsi/src/dcs/set_tearing_effect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::error::Error;
use crate::options::TearingEffect;

use super::DcsCommand;
Expand All @@ -23,16 +22,16 @@ impl DcsCommand for SetTearingEffect {
}
}

fn fill_params_buf(&self, buffer: &mut [u8]) -> Result<usize, Error> {
fn fill_params_buf(&self, buffer: &mut [u8]) -> usize {
match self.0 {
TearingEffect::Off => Ok(0),
TearingEffect::Off => 0,
TearingEffect::Vertical => {
buffer[0] = 0x0;
Ok(1)
1
}
TearingEffect::HorizontalAndVertical => {
buffer[0] = 0x1;
Ok(1)
1
}
}
}
Expand All @@ -43,25 +42,21 @@ mod tests {
use super::*;

#[test]
fn set_tearing_effect_both_fills_param_properly() -> Result<(), Error> {
fn set_tearing_effect_both_fills_param_properly() {
let ste = SetTearingEffect(TearingEffect::HorizontalAndVertical);

let mut buffer = [0u8; 1];
assert_eq!(ste.instruction(), 0x35);
assert_eq!(ste.fill_params_buf(&mut buffer)?, 1);
assert_eq!(ste.fill_params_buf(&mut buffer), 1);
assert_eq!(buffer, [0x1]);

Ok(())
}

#[test]
fn set_tearing_effect_off_fills_param_properly() -> Result<(), Error> {
fn set_tearing_effect_off_fills_param_properly() {
let ste = SetTearingEffect(TearingEffect::Off);

let mut buffer = [0u8; 0];
assert_eq!(ste.instruction(), 0x34);
assert_eq!(ste.fill_params_buf(&mut buffer)?, 0);

Ok(())
assert_eq!(ste.fill_params_buf(&mut buffer), 0);
}
}

0 comments on commit f8b268c

Please sign in to comment.