Skip to content

Commit 2714510

Browse files
authored
Merge pull request #27 from kattni/add-buttons
Adding keypad support and example
2 parents 281bd43 + be794b8 commit 2714510

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

adafruit_character_lcd/character_lcd_rgb_i2c.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434
3535
"* `RGB LCD Shield Kit w/ 16x2 Character Display - Negative Display
3636
<https://www.adafruit.com/product/714>`_"
37+
3738
"* `RGB LCD Shield Kit w/ 16x2 Character Display - Positive Display
3839
<https://www.adafruit.com/product/716>`_"
40+
3941
"* `Adafruit RGB Negative 16x2 LCD+Keypad Kit for Raspberry Pi
4042
<https://www.adafruit.com/product/1110>`_"
43+
4144
"* `Adafruit RGB Positive 16x2 LCD+Keypad Kit for Raspberry Pi
4245
<https://www.adafruit.com/product/1109>`_"
4346
@@ -50,6 +53,7 @@
5053
5154
"""
5255

56+
import digitalio
5357
from adafruit_character_lcd.character_lcd import Character_LCD_RGB
5458

5559
__version__ = "0.0.0-auto.0"
@@ -91,5 +95,127 @@ def __init__(self, i2c, columns, lines):
9195
red = self._mcp.get_pin(6)
9296
green = self._mcp.get_pin(7)
9397
blue = self._mcp.get_pin(8)
98+
self._left_button = self._mcp.get_pin(4)
99+
self._up_button = self._mcp.get_pin(3)
100+
self._down_button = self._mcp.get_pin(2)
101+
self._right_button = self._mcp.get_pin(1)
102+
self._select_button = self._mcp.get_pin(0)
103+
104+
self._buttons = [self._left_button, self._up_button, self._down_button, self._right_button,
105+
self._select_button]
106+
107+
for pin in self._buttons:
108+
pin.switch_to_input(pull=digitalio.Pull.UP)
109+
94110
super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue,
95111
read_write)
112+
113+
@property
114+
def left_button(self):
115+
"""The left button on the RGB Character LCD I2C Shield or Pi plate.
116+
117+
The following example prints "Left!" to the LCD when the left button is pressed:
118+
119+
.. code-block:: python
120+
121+
import board
122+
import busio
123+
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C
124+
125+
i2c = busio.I2C(board.SCL, board.SDA)
126+
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)
127+
128+
while True:
129+
if lcd.left_button:
130+
lcd.message = "Left!"
131+
132+
"""
133+
return not self._left_button.value
134+
135+
@property
136+
def up_button(self):
137+
"""The up button on the RGB Character LCD I2C Shield or Pi plate.
138+
139+
The following example prints "Up!" to the LCD when the up button is pressed:
140+
141+
.. code-block:: python
142+
143+
import board
144+
import busio
145+
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C
146+
147+
i2c = busio.I2C(board.SCL, board.SDA)
148+
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)
149+
150+
while True:
151+
if lcd.up_button:
152+
lcd.message = "Up!"
153+
154+
"""
155+
return not self._up_button.value
156+
157+
@property
158+
def down_button(self):
159+
"""The down button on the RGB Character LCD I2C Shield or Pi plate.
160+
161+
The following example prints "Down!" to the LCD when the down button is pressed:
162+
163+
.. code-block:: python
164+
165+
import board
166+
import busio
167+
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C
168+
169+
i2c = busio.I2C(board.SCL, board.SDA)
170+
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)
171+
172+
while True:
173+
if lcd.down_button:
174+
lcd.message = "Down!"
175+
176+
"""
177+
return not self._down_button.value
178+
179+
@property
180+
def right_button(self):
181+
"""The right button on the RGB Character LCD I2C Shield or Pi plate.
182+
183+
The following example prints "Right!" to the LCD when the right button is pressed:
184+
185+
.. code-block:: python
186+
187+
import board
188+
import busio
189+
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C
190+
191+
i2c = busio.I2C(board.SCL, board.SDA)
192+
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)
193+
194+
while True:
195+
if lcd.right_button:
196+
lcd.message = "Right!"
197+
198+
"""
199+
return not self._right_button.value
200+
201+
@property
202+
def select_button(self):
203+
"""The select button on the RGB Character LCD I2C Shield or Pi plate.
204+
205+
The following example prints "Select!" to the LCD when the select button is pressed:
206+
207+
.. code-block:: python
208+
209+
import board
210+
import busio
211+
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C
212+
213+
i2c = busio.I2C(board.SCL, board.SDA)
214+
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)
215+
216+
while True:
217+
if lcd.select_button:
218+
lcd.message = "Select!"
219+
220+
"""
221+
return not self._select_button.value

docs/examples.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ Ensure your device works with this simple test.
1818
.. literalinclude:: ../examples/charlcd_spi_mono_simpletest.py
1919
:caption: examples/charlcd_spi_mono_simpletest.py
2020
:linenos:
21+
22+
.. literalinclude:: ../examples/charlcd_keypad_simpletest.py
23+
:caption: examples/charlcd_keypad_simpletest.py
24+
:linenos:

examples/charlcd_keypad_simpletest.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Simple test for keypad on I2C RGB character LCD Shield or Pi Plate kits"""
2+
import time
3+
import board
4+
import busio
5+
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd
6+
7+
# Modify this if you have a different sized Character LCD
8+
lcd_columns = 16
9+
lcd_rows = 2
10+
11+
# Initialise I2C bus.
12+
i2c = busio.I2C(board.SCL, board.SDA)
13+
14+
# Initialise the LCD class
15+
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)
16+
17+
lcd.clear()
18+
lcd.color = [100, 0, 0]
19+
while True:
20+
if lcd.left_button:
21+
print("Left!")
22+
lcd.message = "Left!"
23+
24+
elif lcd.up_button:
25+
print("Up!")
26+
lcd.message = "Up!"
27+
28+
elif lcd.down_button:
29+
print("Down!")
30+
lcd.message = "Down!"
31+
32+
elif lcd.right_button:
33+
print("Right!")
34+
lcd.message = "Right!"
35+
36+
elif lcd.select_button:
37+
print("Select!")
38+
lcd.message = "Select!"
39+
40+
else:
41+
time.sleep(0.1)
42+
lcd.clear()

0 commit comments

Comments
 (0)