@@ -53,13 +53,16 @@ byte customCharNumber = 0; //LCDs can store 8 custom chars, this keeps track
53
53
byte rgbData[3 ]; // Records incoming backlight rgb triplet
54
54
byte rgbSpot = 0 ; // Keeps track of where we are in rgbData array
55
55
56
- bool modeCommand = false ; // Used to indicate if a command byte has been received
57
- bool modeSetting = false ; // Used to indicate if a setting byte has been received
58
- bool modeContrast = false ; // First setting mode, then contrast change mode, then the value to change to
59
- bool modeTWI = false ; // First setting mode, then TWI change mode, then the value to change to
60
- bool modeRecordCustomChar = false ; // First setting mode, then custom char mode, then record 8 bytes
61
- // New command mode for Set RGB
62
- bool modeSetRGB = false ; // First setting mode, then RGB mode, then get 3 bytes
56
+ enum displayMode
57
+ {
58
+ MODE_NORMAL, // No mode, just print
59
+ MODE_COMMAND, // Used to indicate if a command byte has been received
60
+ MODE_SETTING, // Used to indicate if a setting byte has been received
61
+ MODE_CONTRAST, // First setting mode, then contrast change mode, then the value to change to
62
+ MODE_TWI, // First setting mode, then custom char mode, then record 8 bytes
63
+ MODE_RECORD_CUSTOM_CHAR, // First setting mode, then custom char mode, then record 8 bytes
64
+ MODE_SET_RGB // First setting mode, then RGB mode, then get 3 bytes
65
+ } currentMode = MODE_NORMAL;
63
66
64
67
// Struct for circular data buffer
65
68
// Data received over UART, SPI and I2C are all sent into a single buffer
@@ -130,11 +133,17 @@ void updateDisplay()
130
133
buffer.tail = (buffer.tail + 1 ) % BUFFER_SIZE; // and update the tail to the next oldest
131
134
132
135
// If the last byte received wasn't special
133
- if (modeCommand == false && modeSetting == false && modeContrast == false && modeTWI == false
134
- && modeRecordCustomChar == false && modeSetRGB == false ) {
136
+ if (currentMode == MODE_NORMAL)
137
+ {
135
138
// Check to see if the incoming byte is special
136
- if (incoming == SPECIAL_SETTING) modeSetting = true ; // SPECIAL_SETTING is 127
137
- else if (incoming == SPECIAL_COMMAND) modeCommand = true ; // SPECIAL_COMMAND is 254
139
+ if (incoming == SPECIAL_SETTING) // SPECIAL_SETTING is 127
140
+ {
141
+ currentMode = MODE_SETTING;
142
+ }
143
+ else if (incoming == SPECIAL_COMMAND) // SPECIAL_COMMAND is 254
144
+ {
145
+ currentMode = MODE_COMMAND;
146
+ }
138
147
else if (incoming == 8 ) // Backspace
139
148
{
140
149
if (characterCount == 0 ) characterCount = settingLCDwidth * settingLCDlines; // Special edge case
@@ -152,61 +161,54 @@ void updateDisplay()
152
161
if (characterCount == settingLCDwidth * settingLCDlines) characterCount = 0 ; // Wrap condition
153
162
}
154
163
}
155
- else if (modeSetting == true )
164
+ else if (currentMode == MODE_SETTING )
156
165
{
166
+ currentMode = MODE_NORMAL; // We assume we will be returning to normal
157
167
158
168
// LCD width and line settings
159
169
if (incoming >= 3 && incoming <= 7 ) // Ctrl+c to Ctrl+g
160
170
{
161
171
// Convert incoming value down to 0 to 4
162
172
changeLinesWidths (incoming - 3 );
163
173
}
164
-
165
174
// Software reset
166
175
else if (incoming == 8 ) // Ctrl+h
167
176
{
168
177
while (1 ); // Hang out and let the watchdog punish us
169
178
}
170
-
171
179
// Enable / disable splash setting
172
180
else if (incoming == 9 ) // Ctrl+i
173
181
{
174
182
changeSplashEnable ();
175
183
}
176
-
177
184
// Save current buffer as splash
178
185
else if (incoming == 10 ) // Ctrl+j
179
186
{
180
187
changeSplashContent ();
181
188
}
182
-
183
189
// Set baud rate
184
190
else if (incoming >= 11 && incoming <= 23 ) // Ctrl+k to ctrl+w
185
191
{
186
192
// Convert incoming value down to 0
187
193
changeUARTSpeed (incoming - 11 );
188
194
}
189
-
190
195
// Set contrast
191
196
else if (incoming == 24 ) // Ctrl+x
192
197
{
193
- modeContrast = true ;
198
+ currentMode = MODE_CONTRAST; // Go to new mode
194
199
// We now grab the next character on the next loop and use it to change the contrast
195
200
}
196
-
197
201
// Set TWI address
198
202
else if (incoming == 25 ) // Ctrl+y
199
203
{
200
- modeTWI = true ;
204
+ currentMode = MODE_TWI; // Go to new mode
201
205
// We now grab the next character on the next loop and use it to change the TWI address
202
206
}
203
-
204
207
// Control ignore RX on boot
205
208
else if (incoming == 26 ) // Ctrl+z
206
209
{
207
210
changeIgnore ();
208
211
}
209
-
210
212
// Clear screen and buffer
211
213
else if (incoming == 45 ) // '-'
212
214
{
@@ -215,44 +217,40 @@ void updateDisplay()
215
217
216
218
clearFrameBuffer (); // Get rid of all characters in our buffer
217
219
}
218
-
219
220
// Backlight Red or standard white
220
221
else if (incoming >= SPECIAL_RED_MIN && incoming <= (SPECIAL_RED_MIN + 29 ))
221
222
{
222
223
byte brightness = map (incoming, SPECIAL_RED_MIN, SPECIAL_RED_MIN + 29 , 0 , 255 ); // Covert 30 digit value to 255 digits
223
224
changeBLBrightness (RED, brightness);
224
225
}
225
-
226
226
// Backlight Green
227
227
else if (incoming >= SPECIAL_GREEN_MIN && incoming <= (SPECIAL_GREEN_MIN + 29 ))
228
228
{
229
229
byte brightness = map (incoming, SPECIAL_GREEN_MIN, SPECIAL_GREEN_MIN + 29 , 0 , 255 ); // Covert 30 digit value to 255 digits
230
230
changeBLBrightness (GREEN, brightness);
231
231
}
232
-
233
232
// Backlight Blue
234
233
else if (incoming >= SPECIAL_BLUE_MIN && incoming <= (SPECIAL_BLUE_MIN + 29 ))
235
234
{
236
235
byte brightness = map (incoming, SPECIAL_BLUE_MIN, SPECIAL_BLUE_MIN + 29 , 0 , 255 ); // Covert 30 digit value to 255 digits
237
236
changeBLBrightness (BLUE, brightness);
238
237
}
239
-
240
238
// Record custom characters
241
239
else if (incoming >= 27 && incoming <= 34 )
242
240
{
243
241
// User can record up to 8 custom chars
244
242
customCharNumber = incoming - 27 ; // Get the custom char spot to record to
245
243
246
- modeRecordCustomChar = true ; // Change to this special mode
244
+ currentMode = MODE_RECORD_CUSTOM_CHAR ; // Change to this special mode
247
245
}
248
246
249
247
// Display custom characters, 8 characters allowed, 35 to 42 inclusive
250
248
else if (incoming >= 35 && incoming <= 42 )
251
249
{
252
250
SerLCD.write (byte (incoming - 35 )); // You write location zero to display customer char 0
253
251
}
254
- // If we get a second special setting character, then write it to the display
255
- // This allows us to print a pipe by escaping it as a double
252
+ // If we get a second special setting character, then write it to the display
253
+ // This allows us to print a pipe by escaping it as a double
256
254
else if (incoming == SPECIAL_SETTING) {
257
255
SerLCD.write (incoming);
258
256
@@ -261,19 +259,20 @@ void updateDisplay()
261
259
}
262
260
// Set Backlight RGB in one command to eliminate flicker
263
261
else if (incoming == 43 ) {
264
- modeSetRGB = true ;
262
+ currentMode = MODE_SET_RGB; // Go to new mode
265
263
}
266
- modeSetting = false ;
267
264
}
268
- else if (modeTWI == true )
265
+ else if (currentMode == MODE_TWI )
269
266
{
270
267
// Custom TWI address
271
268
changeTWIAddress (incoming);
272
269
273
- modeTWI = false ;
270
+ currentMode = MODE_NORMAL; // Return to normal operation
274
271
}
275
- else if (modeCommand == true ) // Deal with lower level commands
272
+ else if (currentMode == MODE_COMMAND ) // Deal with lower level commands
276
273
{
274
+ currentMode = MODE_NORMAL; // In general, return to normal mode
275
+
277
276
if (incoming >> 7 == 1 ) // This is a cursor position command
278
277
{
279
278
incoming &= 0x7F ; // Get rid of the leading 1
@@ -303,7 +302,6 @@ void updateDisplay()
303
302
304
303
SerLCD.setCursor (spot, line); // (x, y) - Set to X spot on the given line
305
304
}
306
-
307
305
else if (incoming >> 6 == 1 ) // This is Set CGRAM address command
308
306
{
309
307
// User is trying to create custom character
@@ -313,7 +311,7 @@ void updateDisplay()
313
311
// User can record up to 8 custom chars
314
312
customCharNumber = incoming - 27 ; // Get the custom char spot to record to
315
313
316
- modeRecordCustomChar = true ; // Change to this special mode
314
+ currentMode = MODE_RECORD_CUSTOM_CHAR; // modeRecordCustomChar = true; //Change to this special mode
317
315
}
318
316
else if (incoming >> 4 == 1 ) // This is a scroll/shift command
319
317
{
@@ -344,7 +342,6 @@ void updateDisplay()
344
342
SerLCD.setCursor (characterCount % settingLCDwidth, characterCount / settingLCDwidth); // Move the cursor
345
343
}
346
344
}
347
-
348
345
else if (incoming >> 3 == 1 ) // This is a cursor or display on/off control command
349
346
{
350
347
/* See page 24 of the datasheet: https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
@@ -368,18 +365,15 @@ void updateDisplay()
368
365
if (incoming & 1 << 2 ) SerLCD.display ();
369
366
else SerLCD.noDisplay ();
370
367
}
371
-
372
368
else if (incoming >> 4 != 0b00000011 ) // If not the data length (DL) command then send it to LCD
373
369
{
374
370
// We ignore the command that could set LCD to 8bit mode
375
371
// But otherwise give the user the ability to pass commands directly
376
372
// into the LCD.
377
373
SerLCD.command (incoming);
378
374
}
379
-
380
- modeCommand = false ; // Clear flag
381
375
}
382
- else if (modeRecordCustomChar == true )
376
+ else if (currentMode == MODE_RECORD_CUSTOM_CHAR )
383
377
{
384
378
// We get into this mode if the user has sent the correct setting or system command
385
379
@@ -400,27 +394,27 @@ void updateDisplay()
400
394
// For some reason you need to re-init the LCD after a custom char is created
401
395
SerLCD.begin (settingLCDwidth, settingLCDlines);
402
396
403
- modeRecordCustomChar = false ; // Exit this mode
397
+ currentMode = MODE_NORMAL ; // Exit this mode
404
398
}
405
399
}
406
- else if (modeContrast == true )
400
+ else if (currentMode == MODE_CONTRAST )
407
401
{
408
402
// We get into this mode if the user has sent the ctrl+x (24) command to change contast
409
403
changeContrast (incoming);
410
- modeContrast = false ; // Exit this mode
404
+ currentMode = MODE_NORMAL ; // Exit this mode
411
405
}
412
- else if (modeSetRGB == true )
406
+ else if (currentMode == MODE_SET_RGB )
413
407
{
414
408
// We get into this mode if the user has sent the + (43) command to set the backlight rgb values
415
409
rgbData[rgbSpot] = incoming; // Record this byte to the array
416
410
417
411
rgbSpot++;
418
412
if (rgbSpot > 2 )
419
413
{
420
- // Once we have 3 bytes, stop listening and change the backlight color
421
- rgbSpot = 0 ;
422
- changeBacklightRGB (rgbData[0 ], rgbData[1 ], rgbData[2 ]);
423
- modeSetRGB = false ; // Exit this mode
414
+ // Once we have 3 bytes, stop listening and change the backlight color
415
+ rgbSpot = 0 ;
416
+ changeBacklightRGB (rgbData[0 ], rgbData[1 ], rgbData[2 ]);
417
+ currentMode = MODE_NORMAL ; // Exit this mode
424
418
} // if (rgbSpot > 2)
425
419
} // else if modeSetRGB
426
420
0 commit comments