Skip to content

Commit ea66c56

Browse files
committed
Simplifying mode logic
This reduces the decision tree time a bit.
1 parent 48cf7aa commit ea66c56

File tree

1 file changed

+43
-49
lines changed

1 file changed

+43
-49
lines changed

firmware/OpenLCD/OpenLCD.ino

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,16 @@ byte customCharNumber = 0; //LCDs can store 8 custom chars, this keeps track
5353
byte rgbData[3]; //Records incoming backlight rgb triplet
5454
byte rgbSpot = 0 ; //Keeps track of where we are in rgbData array
5555

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;
6366

6467
// Struct for circular data buffer
6568
// Data received over UART, SPI and I2C are all sent into a single buffer
@@ -130,11 +133,17 @@ void updateDisplay()
130133
buffer.tail = (buffer.tail + 1) % BUFFER_SIZE; // and update the tail to the next oldest
131134

132135
//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+
{
135138
//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+
}
138147
else if (incoming == 8) //Backspace
139148
{
140149
if (characterCount == 0) characterCount = settingLCDwidth * settingLCDlines; //Special edge case
@@ -152,61 +161,54 @@ void updateDisplay()
152161
if (characterCount == settingLCDwidth * settingLCDlines) characterCount = 0; //Wrap condition
153162
}
154163
}
155-
else if (modeSetting == true)
164+
else if (currentMode == MODE_SETTING)
156165
{
166+
currentMode = MODE_NORMAL; //We assume we will be returning to normal
157167

158168
//LCD width and line settings
159169
if (incoming >= 3 && incoming <= 7) //Ctrl+c to Ctrl+g
160170
{
161171
//Convert incoming value down to 0 to 4
162172
changeLinesWidths(incoming - 3);
163173
}
164-
165174
//Software reset
166175
else if (incoming == 8) //Ctrl+h
167176
{
168177
while (1); //Hang out and let the watchdog punish us
169178
}
170-
171179
//Enable / disable splash setting
172180
else if (incoming == 9) //Ctrl+i
173181
{
174182
changeSplashEnable();
175183
}
176-
177184
//Save current buffer as splash
178185
else if (incoming == 10) //Ctrl+j
179186
{
180187
changeSplashContent();
181188
}
182-
183189
//Set baud rate
184190
else if (incoming >= 11 && incoming <= 23) //Ctrl+k to ctrl+w
185191
{
186192
//Convert incoming value down to 0
187193
changeUARTSpeed(incoming - 11);
188194
}
189-
190195
//Set contrast
191196
else if (incoming == 24) //Ctrl+x
192197
{
193-
modeContrast = true;
198+
currentMode = MODE_CONTRAST; //Go to new mode
194199
//We now grab the next character on the next loop and use it to change the contrast
195200
}
196-
197201
//Set TWI address
198202
else if (incoming == 25) //Ctrl+y
199203
{
200-
modeTWI = true;
204+
currentMode = MODE_TWI; //Go to new mode
201205
//We now grab the next character on the next loop and use it to change the TWI address
202206
}
203-
204207
//Control ignore RX on boot
205208
else if (incoming == 26) //Ctrl+z
206209
{
207210
changeIgnore();
208211
}
209-
210212
//Clear screen and buffer
211213
else if (incoming == 45) //'-'
212214
{
@@ -215,44 +217,40 @@ void updateDisplay()
215217

216218
clearFrameBuffer(); //Get rid of all characters in our buffer
217219
}
218-
219220
//Backlight Red or standard white
220221
else if (incoming >= SPECIAL_RED_MIN && incoming <= (SPECIAL_RED_MIN + 29))
221222
{
222223
byte brightness = map(incoming, SPECIAL_RED_MIN, SPECIAL_RED_MIN + 29, 0, 255); //Covert 30 digit value to 255 digits
223224
changeBLBrightness(RED, brightness);
224225
}
225-
226226
//Backlight Green
227227
else if (incoming >= SPECIAL_GREEN_MIN && incoming <= (SPECIAL_GREEN_MIN + 29))
228228
{
229229
byte brightness = map(incoming, SPECIAL_GREEN_MIN, SPECIAL_GREEN_MIN + 29, 0, 255); //Covert 30 digit value to 255 digits
230230
changeBLBrightness(GREEN, brightness);
231231
}
232-
233232
//Backlight Blue
234233
else if (incoming >= SPECIAL_BLUE_MIN && incoming <= (SPECIAL_BLUE_MIN + 29))
235234
{
236235
byte brightness = map(incoming, SPECIAL_BLUE_MIN, SPECIAL_BLUE_MIN + 29, 0, 255); //Covert 30 digit value to 255 digits
237236
changeBLBrightness(BLUE, brightness);
238237
}
239-
240238
//Record custom characters
241239
else if (incoming >= 27 && incoming <= 34)
242240
{
243241
//User can record up to 8 custom chars
244242
customCharNumber = incoming - 27; //Get the custom char spot to record to
245243

246-
modeRecordCustomChar = true; //Change to this special mode
244+
currentMode = MODE_RECORD_CUSTOM_CHAR; //Change to this special mode
247245
}
248246

249247
//Display custom characters, 8 characters allowed, 35 to 42 inclusive
250248
else if (incoming >= 35 && incoming <= 42)
251249
{
252250
SerLCD.write(byte(incoming - 35)); //You write location zero to display customer char 0
253251
}
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
256254
else if (incoming == SPECIAL_SETTING) {
257255
SerLCD.write(incoming);
258256

@@ -261,19 +259,20 @@ void updateDisplay()
261259
}
262260
//Set Backlight RGB in one command to eliminate flicker
263261
else if (incoming == 43) {
264-
modeSetRGB = true;
262+
currentMode = MODE_SET_RGB; //Go to new mode
265263
}
266-
modeSetting = false;
267264
}
268-
else if (modeTWI == true)
265+
else if (currentMode == MODE_TWI)
269266
{
270267
//Custom TWI address
271268
changeTWIAddress(incoming);
272269

273-
modeTWI = false;
270+
currentMode = MODE_NORMAL; //Return to normal operation
274271
}
275-
else if (modeCommand == true) //Deal with lower level commands
272+
else if (currentMode == MODE_COMMAND) //Deal with lower level commands
276273
{
274+
currentMode = MODE_NORMAL; //In general, return to normal mode
275+
277276
if (incoming >> 7 == 1) //This is a cursor position command
278277
{
279278
incoming &= 0x7F; //Get rid of the leading 1
@@ -303,7 +302,6 @@ void updateDisplay()
303302

304303
SerLCD.setCursor(spot, line); //(x, y) - Set to X spot on the given line
305304
}
306-
307305
else if (incoming >> 6 == 1) //This is Set CGRAM address command
308306
{
309307
//User is trying to create custom character
@@ -313,7 +311,7 @@ void updateDisplay()
313311
//User can record up to 8 custom chars
314312
customCharNumber = incoming - 27; //Get the custom char spot to record to
315313

316-
modeRecordCustomChar = true; //Change to this special mode
314+
currentMode = MODE_RECORD_CUSTOM_CHAR; //modeRecordCustomChar = true; //Change to this special mode
317315
}
318316
else if (incoming >> 4 == 1) //This is a scroll/shift command
319317
{
@@ -344,7 +342,6 @@ void updateDisplay()
344342
SerLCD.setCursor(characterCount % settingLCDwidth, characterCount / settingLCDwidth); //Move the cursor
345343
}
346344
}
347-
348345
else if (incoming >> 3 == 1) //This is a cursor or display on/off control command
349346
{
350347
/*See page 24 of the datasheet: https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
@@ -368,18 +365,15 @@ void updateDisplay()
368365
if (incoming & 1 << 2) SerLCD.display();
369366
else SerLCD.noDisplay();
370367
}
371-
372368
else if (incoming >> 4 != 0b00000011) //If not the data length (DL) command then send it to LCD
373369
{
374370
//We ignore the command that could set LCD to 8bit mode
375371
//But otherwise give the user the ability to pass commands directly
376372
//into the LCD.
377373
SerLCD.command(incoming);
378374
}
379-
380-
modeCommand = false; //Clear flag
381375
}
382-
else if (modeRecordCustomChar == true)
376+
else if (currentMode == MODE_RECORD_CUSTOM_CHAR)
383377
{
384378
//We get into this mode if the user has sent the correct setting or system command
385379

@@ -400,27 +394,27 @@ void updateDisplay()
400394
//For some reason you need to re-init the LCD after a custom char is created
401395
SerLCD.begin(settingLCDwidth, settingLCDlines);
402396

403-
modeRecordCustomChar = false; //Exit this mode
397+
currentMode = MODE_NORMAL; //Exit this mode
404398
}
405399
}
406-
else if (modeContrast == true)
400+
else if (currentMode == MODE_CONTRAST)
407401
{
408402
//We get into this mode if the user has sent the ctrl+x (24) command to change contast
409403
changeContrast(incoming);
410-
modeContrast = false; //Exit this mode
404+
currentMode = MODE_NORMAL; //Exit this mode
411405
}
412-
else if (modeSetRGB == true)
406+
else if (currentMode == MODE_SET_RGB)
413407
{
414408
//We get into this mode if the user has sent the + (43) command to set the backlight rgb values
415409
rgbData[rgbSpot] = incoming; //Record this byte to the array
416410

417411
rgbSpot++;
418412
if (rgbSpot > 2)
419413
{
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
424418
} //if (rgbSpot > 2)
425419
} // else if modeSetRGB
426420

0 commit comments

Comments
 (0)