@@ -129,8 +129,7 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
129
129
red ,
130
130
green ,
131
131
blue ,
132
- backlight = None ,
133
- enable_pwm = True
132
+ backlight = None
134
133
):
135
134
self .cols = cols
136
135
self .lines = lines
@@ -145,7 +144,6 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
145
144
146
145
# define backlight pin
147
146
self .backlight = backlight
148
- self .pwm_enabled = enable_pwm
149
147
150
148
# set all pins as outputs
151
149
for pin in (rs , en , d4 , d5 , d6 , d7 ):
@@ -162,9 +160,16 @@ def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
162
160
self .blue = blue
163
161
self .rgb_led = [red , green , blue ]
164
162
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:
167
166
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
+ )
168
173
169
174
# initialize the display
170
175
self ._write8 (0x33 )
@@ -268,20 +273,18 @@ def set_backlight(self, lighton):
268
273
def set_color (self , color ):
269
274
"""Method to set the duty cycle or the on/off value of the RGB LED
270
275
: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
272
277
non-zero is on.
273
278
"""
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
285
288
286
289
def message (self , text ):
287
290
"""Write text to display, can include \n for newline
0 commit comments