Skip to content

Commit 058c626

Browse files
author
brentru
committed
library updated for compatability with RGB backlight LCDs!
1 parent 5c59e75 commit 058c626

File tree

3 files changed

+298
-0
lines changed

3 files changed

+298
-0
lines changed

adafruit_character_lcd_RGB.mpy

4.32 KB
Binary file not shown.

example code/rgb_hello_CircuitPython

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import time, math, digitalio
2+
import adafruit_character_lcd_RGB as LCD
3+
import pulseio
4+
from board import *
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+
# Metro m0 Pin Config:
12+
lcd_rs = digitalio.DigitalInOut(D7)
13+
lcd_en = digitalio.DigitalInOut(D8)
14+
lcd_d7 = digitalio.DigitalInOut(D12)
15+
lcd_d6 = digitalio.DigitalInOut(D11)
16+
lcd_d5 = digitalio.DigitalInOut(D10)
17+
lcd_d4 = digitalio.DigitalInOut(D9)
18+
lcd_backlight = digitalio.DigitalInOut(D13)
19+
red = pulseio.PWMOut(D3)
20+
green = pulseio.PWMOut(D4)
21+
blue = pulseio.PWMOut(D5)
22+
23+
# Init the lcd class
24+
lcd = LCD.cirpyth_char_lcd_RGB(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, red, green, blue, lcd_backlight)
25+
26+
# only red
27+
RED = [100, 0, 0]
28+
GREEN = [0, 100, 0]
29+
BLUE = [0, 0, 100]
30+
31+
while True:
32+
lcd.message('CircuitPython\nRGB Test')
33+
lcd.setColor(RED)
34+
time.sleep(1)
35+
lcd.setColor(GREEN)
36+
time.sleep(1)
37+
lcd.setColor(BLUE)
38+
time.sleep(1)

src/adafruit_character_lcd_RGB.py

+260
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
# The MIT License (MIT)
2+
#
3+
# Copyright (c) 2017 Brent Rubell for Adafruit Industries
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
"""
23+
`adafruit_CircuitPython_CharLCD`
24+
====================================================
25+
26+
TODO(description)
27+
28+
* Author(s):
29+
-Brent Rubell
30+
-Asher Lieber
31+
-Tony DiCola for the original python charLCD library
32+
"""
33+
34+
"""
35+
`adafruit_character_lcd_RGB` - RGB character lcd module
36+
=================================================
37+
module for interfacing with RGB character lcds
38+
"""
39+
import time
40+
import math
41+
import digitalio
42+
import pulseio
43+
from board import *
44+
from micropython import const
45+
46+
# Commands
47+
_LCD_CLEARDISPLAY = const(0x01)
48+
_LCD_RETURNHOME = const(0x02)
49+
_LCD_ENTRYMODESET = const(0x04)
50+
_LCD_DISPLAYCONTROL = const(0x08)
51+
_LCD_CURSORSHIFT = const(0x10)
52+
_LCD_FUNCTIONSET = const(0x20)
53+
_LCD_SETCGRAMADDR = const(0x40)
54+
_LCD_SETDDRAMADDR = const(0x80)
55+
56+
# Entry flags
57+
_LCD_ENTRYRIGHT = const(0x00)
58+
_LCD_ENTRYLEFT = const(0x02)
59+
_LCD_ENTRYSHIFTINCREMENT = const(0x01)
60+
_LCD_ENTRYSHIFTDECREMENT = const(0x00)
61+
62+
# Control flags
63+
_LCD_DISPLAYON = const(0x04)
64+
_LCD_DISPLAYOFF = const(0x00)
65+
LCD_CURSORON = const(0x02)
66+
_LCD_CURSOROFF = const(0x00)
67+
_LCD_BLINKON = const(0x01)
68+
_LCD_BLINKOFF = const(0x00)
69+
70+
# Move flags
71+
_LCD_DISPLAYMOVE = const(0x08)
72+
_LCD_CURSORMOVE = const(0x00)
73+
_LCD_MOVERIGHT = const(0x04)
74+
_LCD_MOVELEFT = const(0x00)
75+
76+
# Function set flags
77+
_LCD_8BITMODE = const(0x10)
78+
_LCD_4BITMODE = const(0x00)
79+
_LCD_2LINE = const(0x08)
80+
_LCD_1LINE = const(0x00)
81+
_LCD_5x10DOTS = const(0x04)
82+
_LCD_5x8DOTS = const(0x00)
83+
84+
# Offset for up to 4 rows.
85+
LCD_ROW_OFFSETS = (0x00, 0x40, 0x14, 0x54)
86+
87+
class cirpyth_char_lcd_RGB(object):
88+
""" Interfaces with a character LCD
89+
:param ~microcontroller.Pin rs: The reset data line
90+
:param ~microcontroller.Pin en: The enable data line
91+
:param ~microcontroller.Pin d4, d5, d6, d7: The data lines 4 thru 7
92+
:param ~microcontroller.Pin cols: The columns on the charLCD
93+
:param ~microcontroller.Pin lines: The lines on the charLCD
94+
:param ~microcontroller.Pin red: Red RGB Anode
95+
:param ~microcontroller.Pin green: Green RGB Anode
96+
:param ~microcontroller.Pin blue: Blue RGB Anode
97+
:param ~microcontroller.Pin backlight: The backlight pin, usually the last pin. Consult the datasheet.
98+
:param enable_pwm: PulseIO Control
99+
:param initial_backlight: THE initial backlight status (on/off)
100+
"""
101+
def __init__(self, rs, en, d4, d5, d6, d7, cols, lines,
102+
red,
103+
green,
104+
blue,
105+
backlight = None,
106+
enable_pwm = False,
107+
initial_backlight = 1.0):
108+
# define columns and lines
109+
self.cols = cols
110+
self.lines = lines
111+
# define pin params
112+
self.rs = rs
113+
self.en = en
114+
self.d4 = d4
115+
self.d5 = d5
116+
self.d6 = d6
117+
self.d7 = d7
118+
# define color params
119+
self.red = red
120+
self.green = green
121+
self.blue = blue
122+
# define rgb led
123+
self.RGBLED = [red, green, blue]
124+
# define backlight pin
125+
self.backlight = backlight
126+
# save backlight state
127+
self.backlight = backlight
128+
self.pwn_enabled = enable_pwm
129+
# set all pins as outputs
130+
for pin in(rs, en, d4, d5, d6, d7):
131+
pin.direction = digitalio.Direction.OUTPUT
132+
# setup backlight
133+
if backlight is not None:
134+
self.backlight.direction = digitalio.Direction.OUTPUT
135+
self.backlight.value = 0 # turn backlight on
136+
# initialize the display
137+
self._write8(0x33)
138+
self._write8(0x32)
139+
# init. display control
140+
self.displaycontrol = _LCD_DISPLAYON | _LCD_CURSOROFF | _LCD_BLINKOFF
141+
# init display function
142+
self.displayfunction = _LCD_4BITMODE | _LCD_1LINE | _LCD_2LINE | _LCD_5x8DOTS
143+
# init display mode
144+
self.displaymode = _LCD_ENTRYLEFT | _LCD_ENTRYSHIFTDECREMENT
145+
# write to display control
146+
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
147+
# write displayfunction
148+
self._write8(_LCD_FUNCTIONSET | self.displayfunction)
149+
# set the entry mode
150+
self._write8(_LCD_ENTRYMODESET | self.displaymode)
151+
self.clear()
152+
153+
def home(self):
154+
"""Moves the cursor back home pos(1,1)"""
155+
self._write8(_LCD_RETURNHOME)
156+
time.sleep(0.003)
157+
158+
def clear(self):
159+
"""Clears the LCD"""
160+
self._write8(_LCD_CLEARDISPLAY)
161+
time.sleep(0.003)
162+
163+
def show_cursor(self, show):
164+
"""Show or hide the cursor"""
165+
if show:
166+
self.displaycontrol |= LCD_CURSORON
167+
else:
168+
self.displaycontrol &= ~_LCD_DISPLAYON
169+
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
170+
171+
def set_cursor(self, col, row):
172+
"""Sets the cursor to ``row`` and ``col``
173+
:param col: column location
174+
:param row: row location
175+
"""
176+
# Clamp row to the last row of the display
177+
if row > self.lines:
178+
row = self.lines - 1
179+
# Set location
180+
self._write8(_LCD_SETDDRAMADDR | (col + LCD_ROW_OFFSETS[row]))
181+
182+
def enable_display(self, enable):
183+
"""Enable or disable the display. Set enable to True to enable."""
184+
if enable:
185+
self.displaycontrol |= _LCD_DISPLAYON
186+
else:
187+
self.displaycontrol &= ~_LCD_DISPLAYON
188+
self._write8(_LCD_DISPLAYCONTROL | self.displaycontrol)
189+
190+
def _write8(self,value, char_mode = False):
191+
"""Sends 8b ``value`` in ``char_mode``.
192+
:param value: bytes
193+
:param char_mode: character/data mode selector. False (default) for
194+
data only, True for character bits.
195+
"""
196+
# one ms delay to prevent writing too quickly.
197+
time.sleep(0.001)
198+
# set character/data bit. (charmode = False)
199+
self.rs.value = char_mode
200+
# WRITE upper 4 bits
201+
self.d4.value = ((value >> 4) & 1) > 0
202+
self.d5.value = ((value >> 5) & 1) > 0
203+
self.d6.value = ((value >> 6) & 1) > 0
204+
self.d7.value = ((value >> 7) & 1) > 0
205+
# send command
206+
self._pulse_enable()
207+
# WRITE lower 4 bits
208+
self.d4.value = (value & 1) > 0
209+
self.d5.value = ((value >> 1) & 1) > 0
210+
self.d6.value = ((value >> 2) & 1) > 0
211+
self.d7.value = ((value >> 3) & 1) > 0
212+
self._pulse_enable()
213+
214+
def _pulse_enable(self):
215+
""" Pulses (lo->hi->lo) to send commands. """
216+
self.en.value = False
217+
# 1microsec pause
218+
time.sleep(0.0000001)
219+
self.en.value = True
220+
time.sleep(0.0000001)
221+
self.en.value = False
222+
time.sleep(0.0000001)
223+
224+
def set_backlight(self, lighton):
225+
""" Set lighton to turn the charLCD backlight on. """
226+
if lighton:
227+
self.backlight.value = 0
228+
else:
229+
self.backlight.value = 1
230+
231+
def _map(self, x, in_min, in_max, out_min, out_max):
232+
""" Affine transfer/map with constrained output. """
233+
outrange = float(out_max - out_min)
234+
inrange = float(in_max - in_min)
235+
ret = (x - in_min) * (outrange / inrange) + out_min
236+
if (out_max > out_min):
237+
return max(min(ret, out_max), out_min)
238+
else:
239+
return max(min(ret, out_min), out_max)
240+
241+
def setColor(self, color):
242+
""" Method to set the duty cycle of the RGB LED """
243+
self.RGBLED[0].duty_cycle = int(self._map(color[0], 0, 100, 65535, 0))
244+
self.RGBLED[1].duty_cycle = int(self._map(color[1], 0, 100, 65535, 0))
245+
self.RGBLED[2].duty_cycle = int(self._map(color[2], 0, 100, 65535, 0))
246+
247+
def message(self, text):
248+
"""Write text to display, can include \n for newline"""
249+
line = 0
250+
# iterate thru each char
251+
for char in text:
252+
# if character is \n, go to next line
253+
if char == '\n':
254+
line += 1
255+
# move to left/right depending on text direction
256+
col = 0 if self.displaymode & _LCD_ENTRYLEFT > 0 else self.cols-1
257+
self.set_cursor(col, line)
258+
# Write character to display
259+
else:
260+
self._write8(ord(char), True)

0 commit comments

Comments
 (0)