Skip to content

Commit f050d53

Browse files
author
Brennen Bearnes
committed
enable non-PWM pins for backlight color, add pi-specific simpletest
Rationale: pulseio isn't available on CPython Linux systems (i.e. the Pi), this mimics the design of the original Adafruit_Python_CharLCD. Adds `enable_pwm` parameter to Character_LCD_RGB constructor, and conditionalizes handling of the color pins on this value. It's entirely possible I've messed something up here, so close review is appreciated.
1 parent ee2a9ad commit f050d53

File tree

2 files changed

+96
-32
lines changed

2 files changed

+96
-32
lines changed

adafruit_character_lcd/character_lcd_rgb.py

Lines changed: 48 additions & 32 deletions
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

@@ -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,57 @@ 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,
133+
enable_pwm=True
135134
):
136-
# define columns and lines
137135
self.cols = cols
138136
self.lines = lines
139-
# define pin params
137+
138+
# define pin params
140139
self.reset = rs
141140
self.enable = en
142141
self.dl4 = d4
143142
self.dl5 = d5
144143
self.dl6 = d6
145144
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]
145+
152146
# define backlight pin
153147
self.backlight = backlight
154-
# self.pwn_enabled = enable_pwm
148+
self.pwm_enabled = enable_pwm
149+
155150
# set all pins as outputs
156151
for pin in(rs, en, d4, d5, d6, d7):
157152
pin.direction = digitalio.Direction.OUTPUT
158-
# setup backlight
153+
154+
# setup backlight
159155
if backlight is not None:
160156
self.backlight.direction = digitalio.Direction.OUTPUT
161157
self.backlight.value = 0 # turn backlight on
162-
# initialize the display
158+
159+
# define color params
160+
self.red = red
161+
self.green = green
162+
self.blue = blue
163+
self.rgb_led = [red, green, blue]
164+
165+
if not self.pwm_enabled:
166+
for pin in self.rgb_led:
167+
pin.direction = digitalio.Direction.OUTPUT
168+
169+
# initialize the display
163170
self._write8(0x33)
164171
self._write8(0x32)
165-
# init. display control
172+
# init. display control
166173
self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF
167-
# init display function
174+
# init display function
168175
self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS
169-
# init display mode
176+
# init display mode
170177
self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT
171-
# write to display control
178+
# write to display control
172179
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
173-
# write displayfunction
180+
# write displayfunction
174181
self._write8(_LCD_FUNCTIONSET | self.displayfunction)
175-
# set the entry mode
182+
# set the entry mode
176183
self._write8(_LCD_ENTRYMODESET | self.displaymode)
177184
self.clear()
178185
#pylint: enable-msg=too-many-arguments
@@ -259,25 +266,34 @@ def set_backlight(self, lighton):
259266
self.backlight.value = 1
260267

261268
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
269+
"""Method to set the duty cycle or the on/off value of the RGB LED
270+
:param color: list of 3 integers in range(100). ``[R,G,B]`` 0 is no
271+
color, 100 is maximum color. If PWM is disabled, 0 is off and
272+
non-zero is on.
265273
"""
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))
274+
if self.pwm_enabled:
275+
self.rgb_led[0].duty_cycle = int(_map(color[0], 0, 100, 65535, 0))
276+
self.rgb_led[1].duty_cycle = int(_map(color[1], 0, 100, 65535, 0))
277+
self.rgb_led[2].duty_cycle = int(_map(color[2], 0, 100, 65535, 0))
278+
else:
279+
# If we don't have PWM enabled, all we can do is turn each color
280+
# on / off. Assume a DigitalInOut and write 0 (on) to pin for any
281+
# value greater than 0, or 1 (off) for 0:
282+
self.rgb_led[0].value = 0 if color[0] > 0 else 1
283+
self.rgb_led[1].value = 0 if color[1] > 0 else 1
284+
self.rgb_led[2].value = 0 if color[2] > 0 else 1
269285

270286
def message(self, text):
271287
"""Write text to display, can include \n for newline
272288
:param text: string to display
273289
"""
274290
line = 0
275-
# iterate thru each char
291+
# iterate thru each char
276292
for char in text:
277293
# if character is \n, go to next line
278294
if char == '\n':
279295
line += 1
280-
# move to left/right depending on text direction
296+
# move to left/right depending on text direction
281297
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols-1
282298
self.set_cursor(col, line)
283299
# Write character to display

examples/rpi_rgb.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
enable_pwm=False)
29+
30+
RED = [1, 0, 0]
31+
GREEN = [0, 1, 0]
32+
BLUE = [0, 0, 1]
33+
34+
while True:
35+
lcd.clear()
36+
lcd.message('CircuitPython\nRGB Test: RED')
37+
lcd.set_color(RED)
38+
time.sleep(1)
39+
40+
lcd.clear()
41+
lcd.message('CircuitPython\nRGB Test: GREEN')
42+
lcd.set_color(GREEN)
43+
time.sleep(1)
44+
45+
lcd.clear()
46+
lcd.message('CircuitPython\nRGB Test: BLUE')
47+
lcd.set_color(BLUE)
48+
time.sleep(1)

0 commit comments

Comments
 (0)