-
Notifications
You must be signed in to change notification settings - Fork 4
Adding 3x4 OLED demo #7
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# SPDX-FileCopyrightText: Copyright (c) 2022 Liz Clark for Adafruit Industries | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
|
||
import time | ||
import board | ||
import displayio | ||
import terminalio | ||
from adafruit_display_text import label | ||
import adafruit_displayio_ssd1306 | ||
from adafruit_tca8418 import TCA8418 | ||
|
||
displayio.release_displays() | ||
|
||
oled_reset = board.D1 | ||
|
||
i2c = board.I2C() # uses board.SCL and board.SDA | ||
tca = TCA8418(i2c) | ||
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset) | ||
|
||
keymap = (("*", "0", "#"), ("7", "8", "9"), ("4", "5", "6"), ("1", "2", "3")) | ||
|
||
# set up all R0-R2 pins and C0-C3 pins as keypads | ||
KEYPADPINS = ( | ||
TCA8418.R0, | ||
TCA8418.R1, | ||
TCA8418.R2, | ||
TCA8418.C0, | ||
TCA8418.C1, | ||
TCA8418.C2, | ||
TCA8418.C3, | ||
) | ||
|
||
# make them inputs with pullups | ||
for pin in KEYPADPINS: | ||
tca.keypad_mode[pin] = True | ||
# make sure the key pins generate FIFO events | ||
tca.enable_int[pin] = True | ||
# we will stick events into the FIFO queue | ||
tca.event_mode_fifo[pin] = True | ||
|
||
# turn on INT output pin | ||
tca.key_intenable = True | ||
|
||
# display width and height setup | ||
WIDTH = 128 | ||
HEIGHT = 64 | ||
BORDER = 5 | ||
|
||
# display setup | ||
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT) | ||
|
||
splash = displayio.Group() | ||
display.show(splash) | ||
|
||
# text area setup | ||
title_text = "TCA8418 Demo" | ||
title_area = label.Label( | ||
terminalio.FONT, text=title_text, color=0xFFFFFF, x=10, y=10 // 2 + 1 | ||
) | ||
splash.append(title_area) | ||
|
||
key_text = " " | ||
key_area = label.Label( | ||
terminalio.FONT, text=key_text, color=0xFFFFFF, x=10, y=HEIGHT // 2 + 1 | ||
) | ||
splash.append(key_area) | ||
|
||
while True: | ||
if tca.key_int: | ||
# first figure out how big the queue is | ||
events = tca.events_count | ||
# now print keyevent, row, column & key name | ||
for _ in range(events): | ||
keyevent = tca.next_event | ||
# strip keyevent | ||
event = keyevent & 0x7F | ||
event -= 1 | ||
# figure out row | ||
row = int(event / 10) | ||
# figure out column | ||
col = event % 10 | ||
# print event type first | ||
if keyevent & 0x80: | ||
print("Key down") | ||
# print each key pressed to display consecutively | ||
key_area.text = key_area.text + keymap[col][row] | ||
else: | ||
print("Key up") | ||
# use row & column coordinates to print key name | ||
print("Row %d, Column %d, Key %s" % (row, col, keymap[col][row])) | ||
tca.key_int = True # clear the IRQ by writing 1 to it | ||
time.sleep(0.01) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be
event // 10
but I'm not sure if that's as beginner friendly as casting withint()
. Your call!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh yeah I like that, thanks! This example is more to show a good example for matrixes so I'd rather have the most pythonic way.