Skip to content

Commit e848301

Browse files
author
Your Name
committed
added backlight setting
1 parent cc3fefc commit e848301

File tree

1 file changed

+47
-28
lines changed

1 file changed

+47
-28
lines changed

Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,24 @@
1111
from Adafruit_MCP230xx import Adafruit_MCP230XX
1212
import smbus
1313

14-
OUTPUT = 0
15-
INPUT = 1
14+
# change busnum = 0 to bbusnum = 1 if you have a rev 2 Pi!
1615
mcp = Adafruit_MCP230XX(busnum = 0, address = 0x20, num_gpios = 16)
1716

18-
1917
class Adafruit_CharLCD:
2018

19+
OUTPUT = 0
20+
INPUT = 1
21+
22+
# LED colors
23+
RED = 0x01
24+
GREEN = 0x02
25+
BLUE = 0x04
26+
YELLOW = 0x03
27+
TEAL = 0x06
28+
VIOLET = 0x05
29+
ON = 0x07
30+
OFF = 0x0
31+
2132
# commands
2233
LCD_CLEARDISPLAY = 0x01
2334
LCD_RETURNHOME = 0x02
@@ -68,14 +79,14 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
6879
self.pin_rw = pin_rw
6980
self.pins_db = pins_db
7081

71-
mcp.config(self.pin_e, OUTPUT)
72-
mcp.config(self.pin_rs, OUTPUT)
73-
mcp.config(self.pin_rw, OUTPUT)
82+
mcp.config(self.pin_e, self.OUTPUT)
83+
mcp.config(self.pin_rs, self.OUTPUT)
84+
mcp.config(self.pin_rw, self.OUTPUT)
7485
mcp.output(self.pin_rw, 0)
7586
mcp.output(self.pin_e, 0)
7687

7788
for pin in self.pins_db:
78-
mcp.config(pin, OUTPUT)
89+
mcp.config(pin, self.OUTPUT)
7990

8091
self.write4bits(0x33) # initialization
8192
self.write4bits(0x32) # initialization
@@ -92,6 +103,14 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
92103
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
93104
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
94105

106+
# turn on backlights!
107+
mcp.config(6, mcp.OUTPUT)
108+
mcp.config(7, mcp.OUTPUT)
109+
mcp.config(8, mcp.OUTPUT)
110+
mcp.output(6, 0) # red
111+
mcp.output(7, 0) # green
112+
mcp.output(8, 0) # blue
113+
95114

96115
def begin(self, cols, lines):
97116
if (lines > 1):
@@ -211,6 +230,10 @@ def message(self, text):
211230
else:
212231
self.write4bits(ord(char),True)
213232

233+
def backlight(self, color):
234+
mcp.output(6, not color & 0x01)
235+
mcp.output(7, not color & 0x02)
236+
mcp.output(8, not color & 0x04)
214237

215238

216239
if __name__ == '__main__':
@@ -222,29 +245,25 @@ def message(self, text):
222245
# while (True):
223246
# for i in range(16):
224247
# print "%d: %x" % (i, mcp.input(i) >> i)
225-
mcp.config(6, OUTPUT)
226-
mcp.config(7, OUTPUT)
227-
mcp.config(8, OUTPUT)
228-
mcp.output(6, 0) # red
229-
mcp.output(7, 0) # green
230-
mcp.output(8, 0) # blue
231248

232249
lcd = Adafruit_CharLCD(15, 13, [12,11,10,9], 14)
233250
lcd.clear()
234251
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
252+
sleep(1)
235253
while 1:
236-
mcp.output(6, 0) # red
237-
mcp.output(7, 1) # green
238-
mcp.output(8, 1) # blue
239-
sleep(1)
240-
mcp.output(7, 0) # green
241-
sleep(1)
242-
mcp.output(6, 1) # red
243-
sleep(1)
244-
mcp.output(8, 0) # blue
245-
sleep(1)
246-
mcp.output(7, 1) # green
247-
sleep(1)
248-
mcp.output(6, 0) # red
249-
sleep(1)
250-
254+
lcd.backlight(lcd.RED)
255+
sleep(1)
256+
lcd.backlight(lcd.YELLOW)
257+
sleep(1)
258+
lcd.backlight(lcd.GREEN)
259+
sleep(1)
260+
lcd.backlight(lcd.TEAL)
261+
sleep(1)
262+
lcd.backlight(lcd.BLUE)
263+
sleep(1)
264+
lcd.backlight(lcd.VIOLET)
265+
sleep(1)
266+
lcd.backlight(lcd.ON)
267+
sleep(1)
268+
lcd.backlight(lcd.OFF)
269+
sleep(1)

0 commit comments

Comments
 (0)