Browse Source

Improve Stuff mentioned by clippy:

- use range.contains instead of >=  && <=
- Use better error type
main
Caemor 5 years ago
parent
commit
bccfc81b13
  1. 13
      CHANGELOG.md
  2. 15
      src/color.rs
  3. 14
      src/epd2in13_v2/command.rs

13
CHANGELOG.md

@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]
### Added
### Changed
- Use specific ParseColorError instead of ()
### Fixed
## [v0.5.0]
TODO!
## [v0.4.0] - 2020-04-06
### Added

15
src/color.rs

@ -10,6 +10,15 @@ pub use BinaryColor::Off as White;
#[cfg(feature = "graphics")]
pub use BinaryColor::On as Black;
/// When trying to parse u8 to one of the color types
#[derive(Debug)]
pub struct OutOfColorRangeParseError(u8);
impl core::fmt::Display for OutOfColorRangeParseError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "Outside of possible Color Range: {}", self.0)
}
}
/// Only for the Black/White-Displays
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Color {
@ -73,7 +82,7 @@ impl OctColor {
}
///Take the nibble (lower 4 bits) and convert to an OctColor if possible
pub fn from_nibble(nibble: u8) -> Result<OctColor, ()> {
pub fn from_nibble(nibble: u8) -> Result<OctColor, OutOfColorRangeParseError> {
match nibble & 0xf {
0x00 => Ok(OctColor::Black),
0x01 => Ok(OctColor::White),
@ -83,11 +92,11 @@ impl OctColor {
0x05 => Ok(OctColor::Yellow),
0x06 => Ok(OctColor::Orange),
0x07 => Ok(OctColor::HiZ),
_ => Err(()),
e => Err(OutOfColorRangeParseError(e)),
}
}
///Split the nibbles of a single byte and convert both to an OctColor if possible
pub fn split_byte(byte: u8) -> Result<(OctColor, OctColor), ()> {
pub fn split_byte(byte: u8) -> Result<(OctColor, OctColor), OutOfColorRangeParseError> {
let low = OctColor::from_nibble(byte & 0xf)?;
let high = OctColor::from_nibble((byte >> 4) & 0xf)?;
Ok((high, low))

14
src/epd2in13_v2/command.rs

@ -221,7 +221,7 @@ impl I32Ext for i32 {
// This is really not very nice. Until I find something better, this will be
// a placeholder.
fn vcom(self) -> VCOM {
assert!(self >= -30 && self <= -2);
assert!((-30..=-2).contains(&self));
let u = match -self {
2 => 0x08,
3 => 0x0B,
@ -258,20 +258,16 @@ impl I32Ext for i32 {
}
fn gate_driving_decivolt(self) -> GateDrivingVoltage {
assert!(self >= 100 && self <= 210 && self % 5 == 0);
assert!((100..=210).contains(&self) && self % 5 == 0);
GateDrivingVoltage(((self - 100) / 5 + 0x03) as u8)
}
fn source_driving_decivolt(self) -> SourceDrivingVoltage {
assert!(
(self >= 24 && self <= 88)
|| (self >= 90 && self <= 180 && self % 5 == 0)
|| (self >= -180 && self <= -90 && self % 5 == 0)
);
assert!((24..=88).contains(&self) || (self % 5 == 0 && (90..=180).contains(&self.abs())));
if self >= 24 && self <= 88 {
if (24..=88).contains(&self) {
SourceDrivingVoltage(((self - 24) + 0x8E) as u8)
} else if self >= 90 && self <= 180 {
} else if (90..=180).contains(&self) {
SourceDrivingVoltage(((self - 90) / 2 + 0x23) as u8)
} else {
SourceDrivingVoltage((((-self - 90) / 5) * 2 + 0x1A) as u8)

Loading…
Cancel
Save