mirror of
https://github.com/fossasia/badgemagic-rs
synced 2025-06-24 07:43:58 +00:00
Allow usage without embedded graphics
This commit is contained in:
parent
8a885c7d70
commit
03fa9668b5
1 changed files with 47 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
||||||
//! Protocol used to update the badge
|
//! Protocol used to update the badge
|
||||||
|
|
||||||
use std::{convert::Infallible, num::TryFromIntError};
|
use std::num::TryFromIntError;
|
||||||
|
|
||||||
#[cfg(feature = "embedded-graphics")]
|
#[cfg(feature = "embedded-graphics")]
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
|
@ -396,24 +396,57 @@ impl PayloadBuffer {
|
||||||
pub struct MessageBuffer<'a>(&'a mut [[u8; 11]]);
|
pub struct MessageBuffer<'a>(&'a mut [[u8; 11]]);
|
||||||
|
|
||||||
impl MessageBuffer<'_> {
|
impl MessageBuffer<'_> {
|
||||||
#[cfg(feature = "embedded-graphics")]
|
/// Set the state of the pixel at point (`x`, `y`)
|
||||||
fn set(&mut self, point: Point, color: BinaryColor) -> Option<()> {
|
///
|
||||||
let byte = self
|
/// Returns `None` if the pixel was out of bounds.
|
||||||
.0
|
pub fn set(&mut self, (x, y): (usize, usize), state: State) -> Option<()> {
|
||||||
.get_mut(usize::try_from(point.x / 8).ok()?)?
|
let byte = self.0.get_mut(x / 8)?.get_mut(y)?;
|
||||||
.get_mut(usize::try_from(point.y).ok()?)?;
|
let bit = 0x80 >> (x % 8);
|
||||||
|
match state {
|
||||||
let bit = 0x80 >> (point.x % 8);
|
State::Off => {
|
||||||
match color {
|
|
||||||
BinaryColor::Off => {
|
|
||||||
*byte &= !bit;
|
*byte &= !bit;
|
||||||
}
|
}
|
||||||
BinaryColor::On => {
|
State::On => {
|
||||||
*byte |= bit;
|
*byte |= bit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "embedded-graphics")]
|
||||||
|
fn set_embedded_graphics(&mut self, point: Point, color: BinaryColor) -> Option<()> {
|
||||||
|
let x = point.x.try_into().ok()?;
|
||||||
|
let y = point.y.try_into().ok()?;
|
||||||
|
self.set((x, y), color.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// State of a pixel
|
||||||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
pub enum State {
|
||||||
|
#[default]
|
||||||
|
Off,
|
||||||
|
On,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for State {
|
||||||
|
fn from(value: bool) -> Self {
|
||||||
|
if value {
|
||||||
|
Self::On
|
||||||
|
} else {
|
||||||
|
Self::Off
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "embedded-graphics")]
|
||||||
|
impl From<BinaryColor> for State {
|
||||||
|
fn from(value: BinaryColor) -> Self {
|
||||||
|
match value {
|
||||||
|
BinaryColor::Off => Self::Off,
|
||||||
|
BinaryColor::On => Self::On,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "embedded-graphics")]
|
#[cfg(feature = "embedded-graphics")]
|
||||||
|
@ -430,7 +463,7 @@ impl Dimensions for MessageBuffer<'_> {
|
||||||
impl DrawTarget for MessageBuffer<'_> {
|
impl DrawTarget for MessageBuffer<'_> {
|
||||||
type Color = BinaryColor;
|
type Color = BinaryColor;
|
||||||
|
|
||||||
type Error = Infallible;
|
type Error = std::convert::Infallible;
|
||||||
|
|
||||||
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
|
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
|
||||||
where
|
where
|
||||||
|
@ -438,7 +471,7 @@ impl DrawTarget for MessageBuffer<'_> {
|
||||||
{
|
{
|
||||||
for Pixel(point, color) in pixels {
|
for Pixel(point, color) in pixels {
|
||||||
#[allow(clippy::manual_assert)]
|
#[allow(clippy::manual_assert)]
|
||||||
if self.set(point, color).is_none() {
|
if self.set_embedded_graphics(point, color).is_none() {
|
||||||
panic!(
|
panic!(
|
||||||
"tried to draw pixel outside the display area (x: {}, y: {})",
|
"tried to draw pixel outside the display area (x: {}, y: {})",
|
||||||
point.x, point.y
|
point.x, point.y
|
||||||
|
|
Loading…
Reference in a new issue