@@ -7,6 +7,8 @@ use crate::graphics::*;
7
7
#[ cfg( feature = "b1display" ) ]
8
8
use core:: fmt:: { Debug , Write } ;
9
9
#[ cfg( feature = "b1display" ) ]
10
+ use embedded_graphics:: Pixel ;
11
+ #[ cfg( feature = "b1display" ) ]
10
12
use embedded_graphics:: {
11
13
pixelcolor:: Rgb565 ,
12
14
prelude:: { Point , RgbColor } ,
@@ -50,6 +52,8 @@ pub enum _CommandVals {
50
52
SetColor = 0x13 ,
51
53
DisplayOn = 0x14 ,
52
54
InvertScreen = 0x15 ,
55
+ SetPixelColumn = 0x16 ,
56
+ FlushFramebuffer = 0x17 ,
53
57
Version = 0x20 ,
54
58
}
55
59
@@ -117,6 +121,8 @@ pub enum Command {
117
121
SetColor ( RGB8 ) ,
118
122
DisplayOn ( bool ) ,
119
123
InvertScreen ( bool ) ,
124
+ SetPixelColumn ( usize , [ u8 ; 50 ] ) ,
125
+ FlushFramebuffer ,
120
126
_Unknown,
121
127
}
122
128
@@ -243,29 +249,49 @@ pub fn parse_module_command(count: usize, buf: &[u8]) -> Option<Command> {
243
249
244
250
#[ cfg( feature = "b1display" ) ]
245
251
pub fn parse_module_command ( count : usize , buf : & [ u8 ] ) -> Option < Command > {
246
- if count >= 4 && buf[ 0 ] == 0x32 && buf[ 1 ] == 0xAC {
252
+ if count >= 3 && buf[ 0 ] == 0x32 && buf[ 1 ] == 0xAC {
247
253
let command = buf[ 2 ] ;
248
- let arg = buf[ 3 ] ;
254
+ let arg = if count <= 3 { None } else { Some ( buf[ 3 ] ) } ;
249
255
250
256
match command {
251
257
0x09 => {
252
- let available_len = count - 4 ;
253
- let str_len = arg as usize ;
254
- assert ! ( str_len <= available_len) ;
258
+ if let Some ( arg) = arg {
259
+ let available_len = count - 4 ;
260
+ let str_len = arg as usize ;
261
+ assert ! ( str_len <= available_len) ;
255
262
256
- assert ! ( str_len < 32 ) ;
257
- let mut bytes = [ 0 ; 32 ] ;
258
- bytes[ ..str_len] . copy_from_slice ( & buf[ 4 ..4 + str_len] ) ;
263
+ assert ! ( str_len < 32 ) ;
264
+ let mut bytes = [ 0 ; 32 ] ;
265
+ bytes[ ..str_len] . copy_from_slice ( & buf[ 4 ..4 + str_len] ) ;
259
266
260
- let text_str = core:: str:: from_utf8 ( & bytes[ ..str_len] ) . unwrap ( ) ;
261
- let mut text: String < 64 > = String :: new ( ) ;
262
- writeln ! ( & mut text, "{}" , text_str) . unwrap ( ) ;
267
+ let text_str = core:: str:: from_utf8 ( & bytes[ ..str_len] ) . unwrap ( ) ;
268
+ let mut text: String < 64 > = String :: new ( ) ;
269
+ writeln ! ( & mut text, "{}" , text_str) . unwrap ( ) ;
263
270
264
- Some ( Command :: SetText ( text) )
271
+ Some ( Command :: SetText ( text) )
272
+ } else {
273
+ None
274
+ }
265
275
}
266
- 0x14 => Some ( Command :: DisplayOn ( arg == 1 ) ) ,
267
- 0x15 => Some ( Command :: InvertScreen ( arg == 1 ) ) ,
268
- _ => None ,
276
+ 0x14 => Some ( Command :: DisplayOn ( arg == Some ( 1 ) ) ) ,
277
+ 0x15 => Some ( Command :: InvertScreen ( arg == Some ( 1 ) ) ) ,
278
+ 0x16 => {
279
+ // 3B for magic and command
280
+ // 2B for column (u16)
281
+ // 50B for 400 pixels (400/8=50)
282
+ if count == 3 + 2 + 50 {
283
+ let column = u16:: from_le_bytes ( [ buf[ 3 ] , buf[ 4 ] ] ) ;
284
+ //panic!("SetPixelColumn. Col: {}", column);
285
+ let mut pixels: [ u8 ; 50 ] = [ 0 ; 50 ] ;
286
+ pixels. clone_from_slice ( & buf[ 5 ..55 ] ) ;
287
+ Some ( Command :: SetPixelColumn ( column as usize , pixels) )
288
+ } else {
289
+ panic ! ( "Failed to parse SetPixelColumn. count: {}" , count) ;
290
+ None
291
+ }
292
+ }
293
+ 0x17 => Some ( Command :: FlushFramebuffer ) ,
294
+ _ => panic ! ( "Invalid command: {command}" ) ,
269
295
}
270
296
} else {
271
297
None
@@ -453,6 +479,34 @@ where
453
479
}
454
480
None
455
481
}
482
+ Command :: SetPixelColumn ( column, pixel_bytes) => {
483
+ let mut pixels: [ bool ; 400 ] = [ false ; 400 ] ;
484
+ for ( i, byte) in pixel_bytes. iter ( ) . enumerate ( ) {
485
+ pixels[ 8 * i + 0 ] = byte & 0b00000001 != 0 ;
486
+ pixels[ 8 * i + 1 ] = byte & 0b00000010 != 0 ;
487
+ pixels[ 8 * i + 2 ] = byte & 0b00000100 != 0 ;
488
+ pixels[ 8 * i + 3 ] = byte & 0b00001000 != 0 ;
489
+ pixels[ 8 * i + 4 ] = byte & 0b00010000 != 0 ;
490
+ pixels[ 8 * i + 5 ] = byte & 0b00100000 != 0 ;
491
+ pixels[ 8 * i + 6 ] = byte & 0b01000000 != 0 ;
492
+ pixels[ 8 * i + 7 ] = byte & 0b10000000 != 0 ;
493
+ }
494
+ disp. draw_pixels (
495
+ pixels. iter ( ) . enumerate ( ) . map ( |( y, black) | {
496
+ Pixel (
497
+ Point :: new ( * column as i32 , y as i32 ) ,
498
+ if * black { Rgb565 :: BLACK } else { Rgb565 :: WHITE } ,
499
+ )
500
+ } ) ,
501
+ false ,
502
+ )
503
+ . unwrap ( ) ;
504
+ None
505
+ }
506
+ Command :: FlushFramebuffer => {
507
+ disp. flush ( ) ;
508
+ None
509
+ }
456
510
_ => handle_generic_command ( command) ,
457
511
}
458
512
}
0 commit comments