Skip to content

Commit 03fa966

Browse files
committed
Allow usage without embedded graphics
1 parent 8a885c7 commit 03fa966

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

src/protocol.rs

+47-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Protocol used to update the badge
22
3-
use std::{convert::Infallible, num::TryFromIntError};
3+
use std::num::TryFromIntError;
44

55
#[cfg(feature = "embedded-graphics")]
66
use embedded_graphics::{
@@ -396,24 +396,57 @@ impl PayloadBuffer {
396396
pub struct MessageBuffer<'a>(&'a mut [[u8; 11]]);
397397

398398
impl MessageBuffer<'_> {
399-
#[cfg(feature = "embedded-graphics")]
400-
fn set(&mut self, point: Point, color: BinaryColor) -> Option<()> {
401-
let byte = self
402-
.0
403-
.get_mut(usize::try_from(point.x / 8).ok()?)?
404-
.get_mut(usize::try_from(point.y).ok()?)?;
405-
406-
let bit = 0x80 >> (point.x % 8);
407-
match color {
408-
BinaryColor::Off => {
399+
/// Set the state of the pixel at point (`x`, `y`)
400+
///
401+
/// Returns `None` if the pixel was out of bounds.
402+
pub fn set(&mut self, (x, y): (usize, usize), state: State) -> Option<()> {
403+
let byte = self.0.get_mut(x / 8)?.get_mut(y)?;
404+
let bit = 0x80 >> (x % 8);
405+
match state {
406+
State::Off => {
409407
*byte &= !bit;
410408
}
411-
BinaryColor::On => {
409+
State::On => {
412410
*byte |= bit;
413411
}
414412
}
415413
Some(())
416414
}
415+
416+
#[cfg(feature = "embedded-graphics")]
417+
fn set_embedded_graphics(&mut self, point: Point, color: BinaryColor) -> Option<()> {
418+
let x = point.x.try_into().ok()?;
419+
let y = point.y.try_into().ok()?;
420+
self.set((x, y), color.into())
421+
}
422+
}
423+
424+
/// State of a pixel
425+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
426+
pub enum State {
427+
#[default]
428+
Off,
429+
On,
430+
}
431+
432+
impl From<bool> for State {
433+
fn from(value: bool) -> Self {
434+
if value {
435+
Self::On
436+
} else {
437+
Self::Off
438+
}
439+
}
440+
}
441+
442+
#[cfg(feature = "embedded-graphics")]
443+
impl From<BinaryColor> for State {
444+
fn from(value: BinaryColor) -> Self {
445+
match value {
446+
BinaryColor::Off => Self::Off,
447+
BinaryColor::On => Self::On,
448+
}
449+
}
417450
}
418451

419452
#[cfg(feature = "embedded-graphics")]
@@ -430,15 +463,15 @@ impl Dimensions for MessageBuffer<'_> {
430463
impl DrawTarget for MessageBuffer<'_> {
431464
type Color = BinaryColor;
432465

433-
type Error = Infallible;
466+
type Error = std::convert::Infallible;
434467

435468
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
436469
where
437470
I: IntoIterator<Item = Pixel<Self::Color>>,
438471
{
439472
for Pixel(point, color) in pixels {
440473
#[allow(clippy::manual_assert)]
441-
if self.set(point, color).is_none() {
474+
if self.set_embedded_graphics(point, color).is_none() {
442475
panic!(
443476
"tried to draw pixel outside the display area (x: {}, y: {})",
444477
point.x, point.y

0 commit comments

Comments
 (0)