@@ -44,8 +44,10 @@ pub enum PatternVals {
44
44
45
45
// TODO: Reduce size for modules that don't require other commands
46
46
pub enum Command {
47
+ /// Get current brightness scaling
48
+ GetBrightness ,
47
49
/// Set brightness scaling
48
- Brightness ( u8 ) ,
50
+ SetBrightness ( u8 ) ,
49
51
/// Display pre-programmed pattern
50
52
Pattern ( PatternVals ) ,
51
53
/// Reset into bootloader
@@ -54,8 +56,10 @@ pub enum Command {
54
56
Percentage ( u8 ) ,
55
57
/// Go to sleepe or wake up
56
58
Sleep ( bool ) ,
59
+ IsSleeping ,
57
60
/// Start/stop animation (vertical scrolling)
58
- Animate ( bool ) ,
61
+ SetAnimate ( bool ) ,
62
+ GetAnimate ,
59
63
/// Panic. Just to test what happens
60
64
Panic ,
61
65
/// Draw black/white on the grid
@@ -74,17 +78,21 @@ pub fn parse_command(count: usize, buf: &[u8]) -> Option<Command> {
74
78
}
75
79
76
80
// Parse the generic commands common to all modules
77
- if count >= 4 && buf[ 0 ] == 0x32 && buf[ 1 ] == 0xAC {
81
+ if count >= 3 && buf[ 0 ] == 0x32 && buf[ 1 ] == 0xAC {
78
82
let command = buf[ 2 ] ;
79
- let arg = buf[ 3 ] ;
83
+ let arg = if count <= 3 { None } else { Some ( buf[ 3 ] ) } ;
80
84
81
85
//let mut text: String<64> = String::new();
82
86
//writeln!(&mut text, "Command: {command}, arg: {arg}").unwrap();
83
87
//let _ = serial.write(text.as_bytes());
84
88
85
89
match command {
86
90
0x02 => Some ( Command :: BootloaderReset ) ,
87
- 0x03 => Some ( Command :: Sleep ( arg == 1 ) ) ,
91
+ 0x03 => Some ( if let Some ( go_to_sleep) = arg {
92
+ Command :: Sleep ( go_to_sleep == 1 )
93
+ } else {
94
+ Command :: IsSleeping
95
+ } ) ,
88
96
0x05 => Some ( Command :: Panic ) ,
89
97
_ => None , //Some(Command::Unknown),
90
98
}
@@ -95,30 +103,39 @@ pub fn parse_command(count: usize, buf: &[u8]) -> Option<Command> {
95
103
96
104
#[ cfg( feature = "ledmatrix" ) ]
97
105
pub fn parse_module_command ( count : usize , buf : & [ u8 ] ) -> Option < Command > {
98
- if count >= 4 && buf[ 0 ] == 0x32 && buf[ 1 ] == 0xAC {
106
+ if count >= 3 && buf[ 0 ] == 0x32 && buf[ 1 ] == 0xAC {
99
107
let command = buf[ 2 ] ;
100
- let arg = buf[ 3 ] ;
108
+ let arg = if count <= 3 { None } else { Some ( buf[ 3 ] ) } ;
101
109
102
110
match command {
103
- 0x00 => Some ( Command :: Brightness ( arg) ) ,
111
+ 0x00 => Some ( if let Some ( brightness) = arg {
112
+ Command :: SetBrightness ( brightness)
113
+ } else {
114
+ Command :: GetBrightness
115
+ } ) ,
104
116
0x01 => match arg {
105
- 0x00 => {
117
+ Some ( 0x00 ) => {
106
118
if count >= 5 {
107
119
Some ( Command :: Percentage ( buf[ 4 ] ) )
108
120
} else {
109
121
None
110
122
}
111
123
}
112
- 0x01 => Some ( Command :: Pattern ( PatternVals :: Gradient ) ) ,
113
- 0x02 => Some ( Command :: Pattern ( PatternVals :: DoubleGradient ) ) ,
114
- 0x03 => Some ( Command :: Pattern ( PatternVals :: DisplayLotus ) ) ,
115
- 0x04 => Some ( Command :: Pattern ( PatternVals :: ZigZag ) ) ,
116
- 0x05 => Some ( Command :: Pattern ( PatternVals :: FullBrightness ) ) ,
117
- 0x06 => Some ( Command :: Pattern ( PatternVals :: DisplayPanic ) ) ,
118
- 0x07 => Some ( Command :: Pattern ( PatternVals :: DisplayLotus2 ) ) ,
119
- _ => None ,
124
+ Some ( 0x01 ) => Some ( Command :: Pattern ( PatternVals :: Gradient ) ) ,
125
+ Some ( 0x02 ) => Some ( Command :: Pattern ( PatternVals :: DoubleGradient ) ) ,
126
+ Some ( 0x03 ) => Some ( Command :: Pattern ( PatternVals :: DisplayLotus ) ) ,
127
+ Some ( 0x04 ) => Some ( Command :: Pattern ( PatternVals :: ZigZag ) ) ,
128
+ Some ( 0x05 ) => Some ( Command :: Pattern ( PatternVals :: FullBrightness ) ) ,
129
+ Some ( 0x06 ) => Some ( Command :: Pattern ( PatternVals :: DisplayPanic ) ) ,
130
+ Some ( 0x07 ) => Some ( Command :: Pattern ( PatternVals :: DisplayLotus2 ) ) ,
131
+ Some ( _) => None ,
132
+ None => None ,
120
133
} ,
121
- 0x04 => Some ( Command :: Animate ( arg == 1 ) ) ,
134
+ 0x04 => Some ( if let Some ( run_animation) = arg {
135
+ Command :: SetAnimate ( run_animation == 1 )
136
+ } else {
137
+ Command :: GetAnimate
138
+ } ) ,
122
139
0x06 => {
123
140
if count >= 3 + DRAW_BYTES {
124
141
let mut bytes = [ 0 ; DRAW_BYTES ] ;
@@ -194,9 +211,14 @@ pub fn handle_generic_command(command: &Command) {
194
211
}
195
212
196
213
#[ cfg( feature = "ledmatrix" ) ]
197
- pub fn handle_command ( command : & Command , state : & mut State , matrix : & mut Foo ) {
214
+ pub fn handle_command ( command : & Command , state : & mut State , matrix : & mut Foo ) -> Option < [ u8 ; 32 ] > {
198
215
match command {
199
- Command :: Brightness ( br) => {
216
+ Command :: GetBrightness => {
217
+ let mut response: [ u8 ; 32 ] = [ 0 ; 32 ] ;
218
+ response[ 0 ] = state. brightness ;
219
+ return Some ( response) ;
220
+ }
221
+ Command :: SetBrightness ( br) => {
200
222
//let _ = serial.write("Brightness".as_bytes());
201
223
state. brightness = * br;
202
224
matrix
@@ -226,7 +248,12 @@ pub fn handle_command(command: &Command, state: &mut State, matrix: &mut Foo) {
226
248
_ => { }
227
249
}
228
250
}
229
- Command :: Animate ( a) => state. animate = * a,
251
+ Command :: SetAnimate ( a) => state. animate = * a,
252
+ Command :: GetAnimate => {
253
+ let mut response: [ u8 ; 32 ] = [ 0 ; 32 ] ;
254
+ response[ 0 ] = state. animate as u8 ;
255
+ return Some ( response) ;
256
+ }
230
257
Command :: Draw ( vals) => state. grid = draw ( vals) ,
231
258
Command :: StageGreyCol ( col, vals) => {
232
259
draw_grey_col ( & mut state. col_buffer , * col, vals) ;
@@ -237,8 +264,19 @@ pub fn handle_command(command: &Command, state: &mut State, matrix: &mut Foo) {
237
264
// Zero the old staging buffer, just for good measure.
238
265
state. col_buffer = percentage ( 0 ) ;
239
266
}
267
+ // TODO: Move to handle_generic_command
268
+ Command :: IsSleeping => {
269
+ let mut response: [ u8 ; 32 ] = [ 0 ; 32 ] ;
270
+ response[ 0 ] = match state. sleeping {
271
+ SleepState :: Sleeping ( _) => 1 ,
272
+ SleepState :: Awake => 0 ,
273
+ } ;
274
+ return Some ( response) ;
275
+ }
276
+ // TODO: Make it return something
240
277
_ => handle_generic_command ( command) ,
241
278
}
279
+ None
242
280
}
243
281
244
282
#[ cfg( feature = "b1display" ) ]
0 commit comments