Skip to content

Commit 238dbf0

Browse files
author
Brennen Bearnes
committed
remove enable_pwm, check attrs of pin objects instead
1 parent f050d53 commit 238dbf0

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

adafruit_character_lcd/character_lcd_rgb.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
129129
red,
130130
green,
131131
blue,
132-
backlight=None,
133-
enable_pwm=True
132+
backlight=None
134133
):
135134
self.cols = cols
136135
self.lines = lines
@@ -145,7 +144,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
145144

146145
# define backlight pin
147146
self.backlight = backlight
148-
self.pwm_enabled = enable_pwm
149147

150148
# set all pins as outputs
151149
for pin in(rs, en, d4, d5, d6, d7):
@@ -162,9 +160,16 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
162160
self.blue = blue
163161
self.rgb_led = [red, green, blue]
164162

165-
if not self.pwm_enabled:
166-
for pin in self.rgb_led:
163+
for pin in self.rgb_led:
164+
if hasattr(pin, 'direction'):
165+
# Assume a digitalio.DigitalInOut or compatible interface:
167166
pin.direction = digitalio.Direction.OUTPUT
167+
else:
168+
if not hasattr(pin, 'duty_cycle'):
169+
raise TypeError(
170+
'RGB LED objects must be instances of digitalio.DigitalInOut'
171+
' or pulseio.PWMOut, or provide a compatible interface.'
172+
)
168173

169174
# initialize the display
170175
self._write8(0x33)
@@ -268,20 +273,18 @@ def set_backlight(self, lighton):
268273
def set_color(self, color):
269274
"""Method to set the duty cycle or the on/off value of the RGB LED
270275
: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
276+
color, 100 is maximum color. If PWM is unavailable, 0 is off and
272277
non-zero is on.
273278
"""
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
279+
for number, pin in enumerate(self.rgb_led):
280+
if hasattr(pin, 'duty_cycle'):
281+
# Assume a pulseio.PWMOut or compatible interface and set duty cycle:
282+
pin.duty_cycle = int(_map(color[number], 0, 100, 65535, 0))
283+
elif hasattr(pin, 'value'):
284+
# If we don't have a PWM interface, all we can do is turn each color
285+
# on / off. Assume a DigitalInOut (or compatible interface) and write
286+
# 0 (on) to pin for any value greater than 0, or 1 (off) for 0:
287+
pin.value = 0 if color[number] > 0 else 1
285288

286289
def message(self, text):
287290
"""Write text to display, can include \n for newline

examples/rpi_rgb.py renamed to examples/rpi_charlcd_rgb_simpletest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
# Init the lcd class
2525
lcd = adafruit_character_lcd.Character_LCD_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5,
2626
lcd_d6, lcd_d7, lcd_columns, lcd_rows,
27-
red, green, blue, lcd_backlight,
28-
enable_pwm=False)
27+
red, green, blue, lcd_backlight)
2928

3029
RED = [1, 0, 0]
3130
GREEN = [0, 1, 0]

0 commit comments

Comments
 (0)