90
90
_LCD_5X8DOTS = const (0x00 )
91
91
92
92
# Offset for up to 4 rows.
93
- LCD_ROW_OFFSETS = (0x00 , 0x40 , 0x14 , 0x54 )
93
+ LCD_ROW_OFFSETS = (0x00 , 0x40 , 0x14 , 0x54 )
94
94
95
95
#pylint: enable-msg=bad-whitespace
96
96
@@ -107,7 +107,7 @@ def _map(xval, in_min, in_max, out_min, out_max):
107
107
108
108
109
109
#pylint: disable-msg=too-many-instance-attributes
110
- class Character_LCD_RGB ( object ) :
110
+ class Character_LCD_RGB :
111
111
""" Interfaces with a character LCD
112
112
:param ~digitalio.DigitalInOut rs: The reset data line
113
113
:param ~digitalio.DigitalInOut en: The enable data line
@@ -117,9 +117,9 @@ class Character_LCD_RGB(object):
117
117
:param ~digitalio.DigitalInOut d7: The data line 7
118
118
:param cols: The columns on the charLCD
119
119
: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
123
123
:param ~digitalio.DigitalInOut backlight: The backlight pin, usually the last pin.
124
124
Consult the datasheet. Note that Pin value 0 means backlight is lit.
125
125
@@ -129,50 +129,61 @@ 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 = False,
134
- #initial_backlight = 1.0
132
+ backlight = None
135
133
):
136
- # define columns and lines
137
134
self .cols = cols
138
135
self .lines = lines
139
- # define pin params
136
+
137
+ # define pin params
140
138
self .reset = rs
141
139
self .enable = en
142
140
self .dl4 = d4
143
141
self .dl5 = d5
144
142
self .dl6 = d6
145
143
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
+
152
145
# define backlight pin
153
146
self .backlight = backlight
154
- # self.pwn_enabled = enable_pwm
147
+
155
148
# set all pins as outputs
156
149
for pin in (rs , en , d4 , d5 , d6 , d7 ):
157
150
pin .direction = digitalio .Direction .OUTPUT
158
- # setup backlight
151
+
152
+ # setup backlight
159
153
if backlight is not None :
160
154
self .backlight .direction = digitalio .Direction .OUTPUT
161
155
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
163
174
self ._write8 (0x33 )
164
175
self ._write8 (0x32 )
165
- # init. display control
176
+ # init. display control
166
177
self .displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF
167
- # init display function
178
+ # init display function
168
179
self .displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5X8DOTS
169
- # init display mode
180
+ # init display mode
170
181
self .displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT
171
- # write to display control
182
+ # write to display control
172
183
self ._write8 (_LCD_DISPLAYCONTROL | self .displaycontrol )
173
- # write displayfunction
184
+ # write displayfunction
174
185
self ._write8 (_LCD_FUNCTIONSET | self .displayfunction )
175
- # set the entry mode
186
+ # set the entry mode
176
187
self ._write8 (_LCD_ENTRYMODESET | self .displaymode )
177
188
self .clear ()
178
189
#pylint: enable-msg=too-many-arguments
@@ -259,25 +270,32 @@ def set_backlight(self, lighton):
259
270
self .backlight .value = 1
260
271
261
272
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.
265
277
"""
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
269
287
270
288
def message (self , text ):
271
289
"""Write text to display, can include \n for newline
272
290
:param text: string to display
273
291
"""
274
292
line = 0
275
- # iterate thru each char
293
+ # iterate thru each char
276
294
for char in text :
277
295
# if character is \n, go to next line
278
296
if char == '\n ' :
279
297
line += 1
280
- # move to left/right depending on text direction
298
+ # move to left/right depending on text direction
281
299
col = 0 if self .displaymode & _LCD_ENTRYLEFT > 0 else self .cols - 1
282
300
self .set_cursor (col , line )
283
301
# Write character to display
0 commit comments