Skip to content

Commit d993c42

Browse files
committed
Add function to set backlight to RGB value without flickering
1 parent 2829d05 commit d993c42

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

firmware/OpenLCD/OpenLCD.ino

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,17 @@ byte customCharData[8]; //Records incoming custom character data
7171
byte customCharSpot = 0 ; //Keeps track of where we are in custCharData array
7272
byte customCharNumber = 0; //LCDs can store 8 custom chars, this keeps track
7373

74+
//New variables for Set RGB command
75+
byte rgbData[8]; //Records incoming backlight rgb data
76+
byte rgbSpot = 0 ; //Keeps track of where we are in rgbData array
77+
7478
bool modeCommand = false; //Used to indicate if a command byte has been received
7579
bool modeSetting = false; //Used to indicate if a setting byte has been received
7680
bool modeContrast = false; //First setting mode, then contrast change mode, then the value to change to
7781
bool modeTWI = false; //First setting mode, then TWI change mode, then the value to change to
7882
bool modeRecordCustomChar = false; //First setting mode, then custom char mode, then record 8 bytes
83+
//New command mode for Set RGB
84+
bool modeSetRGB = false; //First setting mode, then RGB mode, then get 3 bytes
7985

8086
// Struct for circular data buffer
8187
// Data received over UART, SPI and I2C are all sent into a single buffer
@@ -146,8 +152,8 @@ void updateDisplay()
146152
buffer.tail = (buffer.tail + 1) % BUFFER_SIZE; // and update the tail to the next oldest
147153

148154
//If the last byte received wasn't special
149-
if (modeCommand == false && modeSetting == false && modeContrast == false && modeTWI == false && modeRecordCustomChar == false)
150-
{
155+
if (modeCommand == false && modeSetting == false && modeContrast == false && modeTWI == false
156+
&& modeRecordCustomChar == false && modeSetRGB == false) {
151157
//Check to see if the incoming byte is special
152158
if (incoming == SPECIAL_SETTING) modeSetting = true; //SPECIAL_SETTING is 127
153159
else if (incoming == SPECIAL_COMMAND) modeCommand = true; //SPECIAL_COMMAND is 254
@@ -275,6 +281,10 @@ void updateDisplay()
275281
currentFrame[characterCount++] = incoming; //Record this character to the display buffer
276282
if (characterCount == settingLCDwidth * settingLCDlines) characterCount = 0; //Wrap condition
277283
}
284+
//Set Backlight RGB in one command to eliminate flicker
285+
else if (incoming == 43) {
286+
modeSetRGB = true;
287+
}
278288
modeSetting = false;
279289
}
280290
else if (modeTWI == true)
@@ -421,6 +431,20 @@ void updateDisplay()
421431
changeContrast(incoming);
422432
modeContrast = false; //Exit this mode
423433
}
434+
else if (modeSetRGB == true)
435+
{
436+
//We get into this mode if the user has sent the correct setting command
437+
rgbData[rgbSpot] = incoming; //Record this byte to the array
438+
439+
rgbSpot++;
440+
if (rgbSpot > 2)
441+
{
442+
//Once we have 3 bytes, stop listening and change the backlight color
443+
rgbSpot = 0;
444+
changeBacklightRGB(rgbData[0], rgbData[1], rgbData[2]);
445+
modeSetRGB = false; //Exit this mode
446+
} //if (rgbSpot > 2)
447+
} // else if modeSetRGB
424448

425449
}
426450

firmware/OpenLCD/Setting_Control.ino

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ void changeBLBrightness(byte color, byte brightness)
143143
displayFrameBuffer(); //Display what was there before
144144
}
145145

146+
//Changes the brightness of all three backlight pins and updates the EEPROM locations
147+
//with their rgb values to eliminate flicker. Incoming brightness values should be 0 to 255
148+
void changeBacklightRGB(byte red, byte green, byte blue) {
149+
//update red
150+
EEPROM.write(LOCATION_RED_BRIGHTNESS, red); //Record new setting
151+
analogWrite(BL_RW, 255 - red); //Controlled by PNP so reverse the brightness value
152+
//update green
153+
EEPROM.write(LOCATION_GREEN_BRIGHTNESS, green); //Record new setting
154+
analogWrite(BL_G, 255 - green); //Controlled by PNP so reverse the brightness value
155+
//update blue (SoftPWM)
156+
EEPROM.write(LOCATION_BLUE_BRIGHTNESS, blue); //Record new setting
157+
//analogWrite(BL_B, 255 - brightness); //Controlled by PNP so reverse the brightness value
158+
SoftPWMSet(BL_B, 255 - blue); //Controlled by software PWM
159+
160+
petSafeDelay(SYSTEM_MESSAGE_DELAY);
161+
162+
displayFrameBuffer(); //Display what was there before
163+
}
164+
165+
146166
//Changes the baud rate setting
147167
//Assumes caller is passing a number 0 to 12
148168
void changeUARTSpeed(byte setting)

0 commit comments

Comments
 (0)