Skip to content

Commit 375ce69

Browse files
authored
Merge pull request #16 from adafruit/pi_rgb_toggle
enable non-PWM pins for backlight color, add kattni's pi-specific simpletest
2 parents ee2a9ad + 8750a95 commit 375ce69

File tree

2 files changed

+98
-33
lines changed

2 files changed

+98
-33
lines changed

adafruit_character_lcd/character_lcd_rgb.py

+51-33
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
_LCD_5X8DOTS = const(0x00)
9191

9292
# Offset for up to 4 rows.
93-
LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54)
93+
LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54)
9494

9595
#pylint: enable-msg=bad-whitespace
9696

@@ -107,7 +107,7 @@ def _map(xval, in_min, in_max, out_min, out_max):
107107

108108

109109
#pylint: disable-msg=too-many-instance-attributes
110-
class Character_LCD_RGB(object):
110+
class Character_LCD_RGB:
111111
""" Interfaces with a character LCD
112112
:param ~digitalio.DigitalInOut rs: The reset data line
113113
:param ~digitalio.DigitalInOut en: The enable data line
@@ -117,9 +117,9 @@ class Character_LCD_RGB(object):
117117
:param ~digitalio.DigitalInOut d7: The data line 7
118118
:param cols: The columns on the charLCD
119119
:param lines: The lines on the charLCD
120-
:param ~pulseio.PWMOut red: Red RGB Anode
121-
:param ~pulseio.PWMOut green: Green RGB Anode
122-
:param ~pulseio.PWMOut blue: Blue RGB Anode
120+
:param ~pulseio.PWMOut, ~digitalio.DigitalInOut red: Red RGB Anode
121+
:param ~pulseio.PWMOut, ~digitalio.DigitalInOut green: Green RGB Anode
122+
:param ~pulseio.PWMOut, ~digitalio.DigitalInOut blue: Blue RGB Anode
123123
:param ~digitalio.DigitalInOut backlight: The backlight pin, usually the last pin.
124124
Consult the datasheet. Note that Pin value 0 means backlight is lit.
125125
@@ -129,50 +129,61 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
129129
red,
130130
green,
131131
blue,
132-
backlight=None #,
133-
#enable_pwm = False,
134-
#initial_backlight = 1.0
132+
backlight=None
135133
):
136-
# define columns and lines
137134
self.cols = cols
138135
self.lines = lines
139-
# define pin params
136+
137+
# define pin params
140138
self.reset = rs
141139
self.enable = en
142140
self.dl4 = d4
143141
self.dl5 = d5
144142
self.dl6 = d6
145143
self.dl7 = d7
146-
# define color params
147-
self.red = red
148-
self.green = green
149-
self.blue = blue
150-
# define rgb led
151-
self.rgb_led = [red, green, blue]
144+
152145
# define backlight pin
153146
self.backlight = backlight
154-
# self.pwn_enabled = enable_pwm
147+
155148
# set all pins as outputs
156149
for pin in(rs, en, d4, d5, d6, d7):
157150
pin.direction = digitalio.Direction.OUTPUT
158-
# setup backlight
151+
152+
# setup backlight
159153
if backlight is not None:
160154
self.backlight.direction = digitalio.Direction.OUTPUT
161155
self.backlight.value = 0 # turn backlight on
162-
# initialize the display
156+
157+
# define color params
158+
self.red = red
159+
self.green = green
160+
self.blue = blue
161+
self.rgb_led = [red, green, blue]
162+
163+
for pin in self.rgb_led:
164+
if hasattr(pin, 'direction'):
165+
# Assume a digitalio.DigitalInOut or compatible interface:
166+
pin.direction = digitalio.Direction.OUTPUT
167+
elif not hasattr(pin, 'duty_cycle'):
168+
raise TypeError(
169+
'RGB LED objects must be instances of digitalio.DigitalInOut'
170+
' or pulseio.PWMOut, or provide a compatible interface.'
171+
)
172+
173+
# initialize the display
163174
self._write8(0x33)
164175
self._write8(0x32)
165-
# init. display control
176+
# init. display control
166177
self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF
167-
# init display function
178+
# init display function
168179
self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS
169-
# init display mode
180+
# init display mode
170181
self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT
171-
# write to display control
182+
# write to display control
172183
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
173-
# write displayfunction
184+
# write displayfunction
174185
self._write8(_LCD_FUNCTIONSET | self.displayfunction)
175-
# set the entry mode
186+
# set the entry mode
176187
self._write8(_LCD_ENTRYMODESET | self.displaymode)
177188
self.clear()
178189
#pylint: enable-msg=too-many-arguments
@@ -259,25 +270,32 @@ def set_backlight(self, lighton):
259270
self.backlight.value = 1
260271

261272
def set_color(self, color):
262-
""" Method to set the duty cycle of the RGB LED
263-
:param color: list of 3 integers in range(100). ``[R,G,B]`` 0 is no
264-
color, 100 it maximum color
273+
"""Method to set the duty cycle or the on/off value of the RGB LED
274+
:param color: list of 3 integers in range(100). ``[R,G,B]`` 0 is no
275+
color, 100 is maximum color. If PWM is unavailable, 0 is off and
276+
non-zero is on.
265277
"""
266-
self.rgb_led[0].duty_cycle = int(_map(color[0], 0, 100, 65535, 0))
267-
self.rgb_led[1].duty_cycle = int(_map(color[1], 0, 100, 65535, 0))
268-
self.rgb_led[2].duty_cycle = int(_map(color[2], 0, 100, 65535, 0))
278+
for number, pin in enumerate(self.rgb_led):
279+
if hasattr(pin, 'duty_cycle'):
280+
# Assume a pulseio.PWMOut or compatible interface and set duty cycle:
281+
pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0))
282+
elif hasattr(pin, 'value'):
283+
# If we don't have a PWM interface, all we can do is turn each color
284+
# on / off. Assume a DigitalInOut (or compatible interface) and write
285+
# 0 (on) to pin for any value greater than 0, or 1 (off) for 0:
286+
pin.value = 0 if color[number] > 0 else 1
269287

270288
def message(self, text):
271289
"""Write text to display, can include \n for newline
272290
:param text: string to display
273291
"""
274292
line = 0
275-
# iterate thru each char
293+
# iterate thru each char
276294
for char in text:
277295
# if character is \n, go to next line
278296
if char == '\n':
279297
line += 1
280-
# move to left/right depending on text direction
298+
# move to left/right depending on text direction
281299
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols-1
282300
self.set_cursor(col, line)
283301
# Write character to display
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import time
2+
import board
3+
import digitalio
4+
import adafruit_character_lcd
5+
6+
# Character LCD Config:
7+
# modify this if you have a different sized charlcd
8+
lcd_columns = 16
9+
lcd_rows = 2
10+
11+
# Raspberry Pi Pin Config:
12+
lcd_rs = digitalio.DigitalInOut(board.D26) # pin 4
13+
lcd_en = digitalio.DigitalInOut(board.D19) # pin 6
14+
lcd_d7 = digitalio.DigitalInOut(board.D27) # pin 14
15+
lcd_d6 = digitalio.DigitalInOut(board.D22) # pin 13
16+
lcd_d5 = digitalio.DigitalInOut(board.D24) # pin 12
17+
lcd_d4 = digitalio.DigitalInOut(board.D25) # pin 11
18+
lcd_backlight = digitalio.DigitalInOut(board.D4)
19+
20+
red = digitalio.DigitalInOut(board.D21)
21+
green = digitalio.DigitalInOut(board.D12)
22+
blue = digitalio.DigitalInOut(board.D18)
23+
24+
# Init the lcd class
25+
lcd = adafruit_character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5,
26+
lcd_d6, lcd_d7, lcd_columns, lcd_rows,
27+
red, green, blue, lcd_backlight)
28+
29+
RED = [1, 0, 0]
30+
GREEN = [0, 1, 0]
31+
BLUE = [0, 0, 1]
32+
33+
while True:
34+
lcd.clear()
35+
lcd.message('CircuitPython\nRGB Test: RED')
36+
lcd.set_color(RED)
37+
time.sleep(1)
38+
39+
lcd.clear()
40+
lcd.message('CircuitPython\nRGB Test: GREEN')
41+
lcd.set_color(GREEN)
42+
time.sleep(1)
43+
44+
lcd.clear()
45+
lcd.message('CircuitPython\nRGB Test: BLUE')
46+
lcd.set_color(BLUE)
47+
time.sleep(1)

0 commit comments

Comments
 (0)