Skip to content

Commit 1e48768

Browse files
authored
Merge pull request #10 from fourstix/set_rgb_backlight
Add Set rgb backlight command to set RGB Backlight in one command without flicker
2 parents 2829d05 + 4c689ae commit 1e48768

File tree

3 files changed

+291
-2
lines changed

3 files changed

+291
-2
lines changed
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/*
2+
* This sketch changes the backlight color and displays text using
3+
* the OpenLCD functions.
4+
*
5+
*The circuit:
6+
* Sparkfun RGB OpenLCD Serial display connected through
7+
* a Sparkfun Qwiic adpater to an Ardruino with a
8+
* Qwiic shield or a Sparkfun Blackboard with Qwiic built in.
9+
*
10+
* The Qwiic adapter should be attached to the display as follows:
11+
*
12+
* Display Qwiic Qwiic Cable Color
13+
* GND GND Black
14+
* RAW 3.3V Red
15+
* SDA SDA Blue
16+
* SCL SCL Yellow
17+
*
18+
* Note: If you connect directly to a 5V Arduino instead, you *MUST* use
19+
* a level-shifter to convert the i2c voltage levels down to 3.3V for the display.
20+
21+
OpenLCD is an LCD with Serial/I2C/SPI interfaces.
22+
By: Nathan Seidle
23+
SparkFun Electronics
24+
Date: April 19th, 2015
25+
License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
26+
OpenLCD gives the user multiple interfaces (serial, I2C, and SPI) to control an LCD. SerLCD was the original
27+
serial LCD from SparkFun that ran on the PIC 16F88 with only a serial interface and limited feature set.
28+
This is an updated serial LCD.
29+
30+
This example shows how to change brightness of the three backlight controls.
31+
32+
Please Note: 0x72 is the 7-bit I2C address. If you are using a different language than Arduino you will probably
33+
need to add the Read/Write bit to the end of the address. This means the default read address for the OpenLCD
34+
is 0b.1110.0101 or 0xE5 and the write address is 0b.1110.0100 or 0xE4.
35+
For more information see https://learn.sparkfun.com/tutorials/i2c
36+
Note: This code expects the display to be listening at the default I2C address. If your display is not at 0x72, you can
37+
do a hardware reset. Tie the RX pin to ground and power up OpenLCD. You should see the splash screen
38+
then "System reset Power cycle me" and the backlight will begin to blink. Now power down OpenLCD and remove
39+
the RX/GND jumper. OpenLCD is now reset.
40+
41+
To get this code to work, attached an OpenLCD to an Arduino Uno using the following pins:
42+
SCL (OpenLCD) to A5 (Arduino)
43+
SDA to A4
44+
VIN to 5V
45+
GND to GND
46+
47+
The OpenLCD has 4.7k pull up resistors on SDA and SCL. If you have other devices on the
48+
I2C bus then you may want to disable the pull up resistors by clearing the PU (pull up) jumper.
49+
50+
OpenLCD will work at 400kHz Fast I2C. Use the .setClock() call shown below to set the data rate
51+
faster if needed.
52+
Command cheat sheet:
53+
ASCII / DEC / HEX
54+
'|' / 124 / 0x7C - Put into setting mode
55+
Ctrl+c / 3 / 0x03 - Change width to 20
56+
Ctrl+d / 4 / 0x04 - Change width to 16
57+
Ctrl+e / 5 / 0x05 - Change lines to 4
58+
Ctrl+f / 6 / 0x06 - Change lines to 2
59+
Ctrl+g / 7 / 0x07 - Change lines to 1
60+
Ctrl+h / 8 / 0x08 - Software reset of the system
61+
Ctrl+i / 9 / 0x09 - Enable/disable splash screen
62+
Ctrl+j / 10 / 0x0A - Save currently displayed text as splash
63+
Ctrl+k / 11 / 0x0B - Change baud to 2400bps
64+
Ctrl+l / 12 / 0x0C - Change baud to 4800bps
65+
Ctrl+m / 13 / 0x0D - Change baud to 9600bps
66+
Ctrl+n / 14 / 0x0E - Change baud to 14400bps
67+
Ctrl+o / 15 / 0x0F - Change baud to 19200bps
68+
Ctrl+p / 16 / 0x10 - Change baud to 38400bps
69+
Ctrl+q / 17 / 0x11 - Change baud to 57600bps
70+
Ctrl+r / 18 / 0x12 - Change baud to 115200bps
71+
Ctrl+s / 19 / 0x13 - Change baud to 230400bps
72+
Ctrl+t / 20 / 0x14 - Change baud to 460800bps
73+
Ctrl+u / 21 / 0x15 - Change baud to 921600bps
74+
Ctrl+v / 22 / 0x16 - Change baud to 1000000bps
75+
Ctrl+w / 23 / 0x17 - Change baud to 1200bps
76+
Ctrl+x / 24 / 0x18 - Change the contrast. Follow Ctrl+x with number 0 to 255. 120 is default.
77+
Ctrl+y / 25 / 0x19 - Change the TWI address. Follow Ctrl+x with number 0 to 255. 114 (0x72) is default.
78+
Ctrl+z / 26 / 0x1A - Enable/disable ignore RX pin on startup (ignore emergency reset)
79+
'-' / 45 / 0x2D - Clear display. Move cursor to home position.
80+
/ 128-157 / 0x80-0x9D - Set the primary backlight brightness. 128 = Off, 157 = 100%.
81+
/ 158-187 / 0x9E-0xBB - Set the green backlight brightness. 158 = Off, 187 = 100%.
82+
/ 188-217 / 0xBC-0xD9 - Set the blue backlight brightness. 188 = Off, 217 = 100%.
83+
For example, to change the baud rate to 115200 send 124 followed by 18.
84+
'+' / 43 / 0x2B - Set Backlight to RGB value, follow + by 3 numbers 0 to 255, for the r, g and b values to set.
85+
For example, to change the backlight to yellow send + followe by 255, 255 and 0.
86+
87+
88+
*/
89+
90+
#include <Wire.h>
91+
92+
#define DISPLAY_ADDRESS1 0x72 //This is the default address of the OpenLCD
93+
94+
void setup()
95+
{
96+
Wire.begin(); //Join the bus as master
97+
98+
//By default .begin() will set I2C SCL to Standard Speed mode of 100kHz
99+
Wire.setClock(400000); //Optional - set I2C SCL to High Speed Mode of 400kHz
100+
101+
Serial.begin(9600); //Start serial communication at 9600 for debug statements
102+
Serial.println("OpenLCD Example Code");
103+
104+
//Send the reset command to the display - this forces the cursor to return to the beginning of the display
105+
Wire.beginTransmission(DISPLAY_ADDRESS1);
106+
Wire.write('|'); //Put LCD into setting mode
107+
Wire.write('-'); //Send clear display command
108+
Wire.endTransmission();
109+
Wire.print("Testing Set RGB");
110+
delay(2000);
111+
}
112+
113+
void loop()
114+
{
115+
//Turn off backlight (black)
116+
Serial.println("Setting RGB backlight to black");
117+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
118+
Wire.write('|'); //Put LCD into setting mode
119+
Wire.write('-'); //Send clear display command
120+
Wire.write('|'); //Put LCD into setting mode
121+
Wire.write('+'); //Send the Set RGB command
122+
Wire.write(0x00); //Send the red value
123+
Wire.write(0x00); //Send the green value
124+
Wire.write(0x00); //Send the blue value
125+
Wire.print("Black (Off)!");
126+
Wire.endTransmission(); //Stop I2C transmission
127+
128+
delay(2000);
129+
130+
131+
//Set red backlight
132+
Serial.println("Setting RGB backlight to red");
133+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
134+
Wire.write('|'); //Put LCD into setting mode
135+
Wire.write('-'); //Send clear display command
136+
Wire.write('|'); //Put LCD into setting mode
137+
Wire.write('+'); //Send the Set RGB command
138+
Wire.write(0xFF); //Send the red value
139+
Wire.write(0x00); //Send the green value
140+
Wire.write(0x00); //Send the blue value
141+
Wire.print("Red!");
142+
Wire.endTransmission(); //Stop I2C transmission
143+
144+
delay(2000);
145+
146+
//Set Orange backlight
147+
Serial.println("Setting RGB backlight to orange");
148+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
149+
Wire.write('|'); //Put LCD into setting mode
150+
Wire.write('-'); //Send clear display command
151+
Wire.write('|'); //Put LCD into setting mode
152+
Wire.write('+'); //Send the Set RGB command
153+
Wire.write(0xFF); //Send the red value
154+
Wire.write(0x8C); //Send the green value
155+
Wire.write(0x00); //Send the blue value
156+
Wire.print("Orange!");
157+
Wire.endTransmission(); //Stop I2C transmission
158+
159+
delay(2000);
160+
161+
//Set yellow backlight
162+
Serial.println("Setting RGB backlight to yellow");
163+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
164+
Wire.write('|'); //Put LCD into setting mode
165+
Wire.write('-'); //Send clear display command
166+
Wire.write('|'); //Put LCD into setting mode
167+
Wire.write('+'); //Send the Set RGB command
168+
Wire.write(0xFF); //Send the red value
169+
Wire.write(0xFF); //Send the green value
170+
Wire.write(0x00); //Send the blue value
171+
Wire.print("Yellow!");
172+
Wire.endTransmission(); //Stop I2C transmission
173+
174+
delay(2000);
175+
176+
//Set green backlight
177+
Serial.println("Setting RGB backlight to green");
178+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
179+
Wire.write('|'); //Put LCD into setting mode
180+
Wire.write('-'); //Send clear display command
181+
Wire.write('|'); //Put LCD into setting mode
182+
Wire.write('+'); //Send the Set RGB command
183+
Wire.write(0x00); //Send the red value
184+
Wire.write(0xFF); //Send the green value
185+
Wire.write(0x00); //Send the blue value
186+
Wire.print("Green!");
187+
Wire.endTransmission(); //Stop I2C transmission
188+
delay(2000);
189+
190+
//Set blue backlight
191+
Serial.println("Setting RGB backlight to blue");
192+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
193+
Wire.write('|'); //Put LCD into setting mode
194+
Wire.write('-'); //Send clear display command
195+
Wire.write('|'); //Put LCD into setting mode
196+
Wire.write('+'); //Send the Set RGB command
197+
Wire.write(0x00); //Send the red value
198+
Wire.write(0x00); //Send the green value
199+
Wire.write(0xFF); //Send the blue value
200+
Wire.print("Blue!");
201+
Wire.endTransmission(); //Stop I2C transmission
202+
delay(2000);
203+
204+
//Set violet backlight
205+
Serial.println("Setting RGB backlight to violet");
206+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
207+
Wire.write('|'); //Put LCD into setting mode
208+
Wire.write('-'); //Send clear display command
209+
Wire.write('|'); //Put LCD into setting mode
210+
Wire.write('+'); //Send the Set RGB command
211+
Wire.write(0xA0); //Send the red value
212+
Wire.write(0x20); //Send the green value
213+
Wire.write(0xF0); //Send the blue value
214+
Wire.print("Violet!");
215+
Wire.endTransmission(); //Stop I2C transmission
216+
delay(2000);
217+
218+
//Turn on all (white)
219+
Serial.println("Setting RGB backlight to white");
220+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
221+
Wire.write('|'); //Put LCD into setting mode
222+
Wire.write('-'); //Send clear display command
223+
Wire.write('|'); //Put LCD into setting mode
224+
Wire.write('+'); //Send the Set RGB command
225+
Wire.write(0xFF); //Send the red value
226+
Wire.write(0xFF); //Send the green value
227+
Wire.write(0xFF); //Send the blue value
228+
Wire.print("White!");
229+
Wire.endTransmission(); //Stop I2C transmission
230+
delay(2000);
231+
232+
//Set to Gray
233+
Serial.println("Setting RGB backlight to gray");
234+
Wire.beginTransmission(DISPLAY_ADDRESS1); // transmit to device #1
235+
Wire.write('|'); //Put LCD into setting mode
236+
Wire.write('-'); //Send clear display command
237+
Wire.write('|'); //Put LCD into setting mode
238+
Wire.write('+'); //Send the Set RGB command
239+
Wire.write(0x80); //Send the red value
240+
Wire.write(0x80); //Send the green value
241+
Wire.write(0x80); //Send the blue value
242+
Wire.print("Gray!");
243+
Wire.endTransmission(); //Stop I2C transmission
244+
delay(2000);
245+
}

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[3]; //Records incoming backlight rgb triplet
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 + (43) command to set the backlight rgb values
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)