Skip to content

Adding keypad support and example #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions adafruit_character_lcd/character_lcd_rgb_i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@

"* `RGB LCD Shield Kit w/ 16x2 Character Display - Negative Display
<https://www.adafruit.com/product/714>`_"

"* `RGB LCD Shield Kit w/ 16x2 Character Display - Positive Display
<https://www.adafruit.com/product/716>`_"

"* `Adafruit RGB Negative 16x2 LCD+Keypad Kit for Raspberry Pi
<https://www.adafruit.com/product/1110>`_"

"* `Adafruit RGB Positive 16x2 LCD+Keypad Kit for Raspberry Pi
<https://www.adafruit.com/product/1109>`_"

Expand All @@ -50,6 +53,7 @@

"""

import digitalio
from adafruit_character_lcd.character_lcd import Character_LCD_RGB

__version__ = "0.0.0-auto.0"
Expand Down Expand Up @@ -91,5 +95,127 @@ def __init__(self, i2c, columns, lines):
red = self._mcp.get_pin(6)
green = self._mcp.get_pin(7)
blue = self._mcp.get_pin(8)
self._left_button = self._mcp.get_pin(4)
self._up_button = self._mcp.get_pin(3)
self._down_button = self._mcp.get_pin(2)
self._right_button = self._mcp.get_pin(1)
self._select_button = self._mcp.get_pin(0)

self._buttons = [self._left_button, self._up_button, self._down_button, self._right_button,
self._select_button]

for pin in self._buttons:
pin.switch_to_input(pull=digitalio.Pull.UP)

super().__init__(reset, enable, db4, db5, db6, db7, columns, lines, red, green, blue,
read_write)

@property
def left_button(self):
"""The left button on the RGB Character LCD I2C Shield or Pi plate.

The following example prints "Left!" to the LCD when the left button is pressed:

.. code-block:: python

import board
import busio
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C

i2c = busio.I2C(board.SCL, board.SDA)
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)

while True:
if lcd.left_button:
lcd.message = "Left!"

"""
return not self._left_button.value

@property
def up_button(self):
"""The up button on the RGB Character LCD I2C Shield or Pi plate.

The following example prints "Up!" to the LCD when the up button is pressed:

.. code-block:: python

import board
import busio
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C

i2c = busio.I2C(board.SCL, board.SDA)
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)

while True:
if lcd.up_button:
lcd.message = "Up!"

"""
return not self._up_button.value

@property
def down_button(self):
"""The down button on the RGB Character LCD I2C Shield or Pi plate.

The following example prints "Down!" to the LCD when the down button is pressed:

.. code-block:: python

import board
import busio
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C

i2c = busio.I2C(board.SCL, board.SDA)
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)

while True:
if lcd.down_button:
lcd.message = "Down!"

"""
return not self._down_button.value

@property
def right_button(self):
"""The right button on the RGB Character LCD I2C Shield or Pi plate.

The following example prints "Right!" to the LCD when the right button is pressed:

.. code-block:: python

import board
import busio
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C

i2c = busio.I2C(board.SCL, board.SDA)
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)

while True:
if lcd.right_button:
lcd.message = "Right!"

"""
return not self._right_button.value

@property
def select_button(self):
"""The select button on the RGB Character LCD I2C Shield or Pi plate.

The following example prints "Select!" to the LCD when the select button is pressed:

.. code-block:: python

import board
import busio
from adafruit_character_lcd.character_lcd_rgb_i2c import Character_LCD_RGB_I2C

i2c = busio.I2C(board.SCL, board.SDA)
lcd = Character_LCD_RGB_I2C(i2c, 16, 2)

while True:
if lcd.select_button:
lcd.message = "Select!"

"""
return not self._select_button.value
4 changes: 4 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ Ensure your device works with this simple test.
.. literalinclude:: ../examples/charlcd_spi_mono_simpletest.py
:caption: examples/charlcd_spi_mono_simpletest.py
:linenos:

.. literalinclude:: ../examples/charlcd_keypad_simpletest.py
:caption: examples/charlcd_keypad_simpletest.py
:linenos:
42 changes: 42 additions & 0 deletions examples/charlcd_keypad_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Simple test for keypad on I2C RGB character LCD Shield or Pi Plate kits"""
import time
import board
import busio
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd

# Modify this if you have a different sized Character LCD
lcd_columns = 16
lcd_rows = 2

# Initialise I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)

# Initialise the LCD class
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)

lcd.clear()
lcd.color = [100, 0, 0]
while True:
if lcd.left_button:
print("Left!")
lcd.message = "Left!"

elif lcd.up_button:
print("Up!")
lcd.message = "Up!"

elif lcd.down_button:
print("Down!")
lcd.message = "Down!"

elif lcd.right_button:
print("Right!")
lcd.message = "Right!"

elif lcd.select_button:
print("Select!")
lcd.message = "Select!"

else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this else only matches the select button's if - y ou want elifs instead of if's for all the buttons.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code ran successfully when I tested it. I updated it and tested it successfully again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kk do you grok why you want elifs instead of if's? z good thing to know!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! It came out of testing shorter code snippets while iterating on the lib update and it stayed into the final. I was simply pointing out that I had tested the code. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good! more testing tends to reveal many mysteries

time.sleep(0.1)
lcd.clear()