1
1
//! Protocol used to update the badge
2
2
3
- use std:: { convert :: Infallible , num:: TryFromIntError } ;
3
+ use std:: num:: TryFromIntError ;
4
4
5
5
#[ cfg( feature = "embedded-graphics" ) ]
6
6
use embedded_graphics:: {
@@ -396,24 +396,57 @@ impl PayloadBuffer {
396
396
pub struct MessageBuffer < ' a > ( & ' a mut [ [ u8 ; 11 ] ] ) ;
397
397
398
398
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 => {
409
407
* byte &= !bit;
410
408
}
411
- BinaryColor :: On => {
409
+ State :: On => {
412
410
* byte |= bit;
413
411
}
414
412
}
415
413
Some ( ( ) )
416
414
}
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
+ }
417
450
}
418
451
419
452
#[ cfg( feature = "embedded-graphics" ) ]
@@ -430,15 +463,15 @@ impl Dimensions for MessageBuffer<'_> {
430
463
impl DrawTarget for MessageBuffer < ' _ > {
431
464
type Color = BinaryColor ;
432
465
433
- type Error = Infallible ;
466
+ type Error = std :: convert :: Infallible ;
434
467
435
468
fn draw_iter < I > ( & mut self , pixels : I ) -> Result < ( ) , Self :: Error >
436
469
where
437
470
I : IntoIterator < Item = Pixel < Self :: Color > > ,
438
471
{
439
472
for Pixel ( point, color) in pixels {
440
473
#[ allow( clippy:: manual_assert) ]
441
- if self . set ( point, color) . is_none ( ) {
474
+ if self . set_embedded_graphics ( point, color) . is_none ( ) {
442
475
panic ! (
443
476
"tried to draw pixel outside the display area (x: {}, y: {})" ,
444
477
point. x, point. y
0 commit comments