Skip to content

Commit 9eb5b55

Browse files
committed
Adding 3x4 OLED demo
Adding an example for the 3x4 numpad with an OLED to copy the Arduino lib example
1 parent f68b509 commit 9eb5b55

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

examples/tca8418_3x4_OLED.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2022 Liz Clark for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
import time
6+
import board
7+
import displayio
8+
import terminalio
9+
from adafruit_display_text import label
10+
import adafruit_displayio_ssd1306
11+
from adafruit_tca8418 import TCA8418
12+
13+
displayio.release_displays()
14+
15+
oled_reset = board.D1
16+
17+
i2c = board.I2C() # uses board.SCL and board.SDA
18+
tca = TCA8418(i2c)
19+
display_bus = displayio.I2CDisplay(i2c, device_address=0x3D, reset=oled_reset)
20+
21+
keymap = (('*', '0', '#'),
22+
('7', '8', '9'),
23+
('4', '5', '6'),
24+
('1', '2', '3'))
25+
26+
# set up all R0-R2 pins and C0-C3 pins as keypads
27+
KEYPADPINS = (
28+
TCA8418.R0,
29+
TCA8418.R1,
30+
TCA8418.R2,
31+
TCA8418.C0,
32+
TCA8418.C1,
33+
TCA8418.C2,
34+
TCA8418.C3,
35+
)
36+
37+
# make them inputs with pullups
38+
for pin in KEYPADPINS:
39+
tca.keypad_mode[pin] = True
40+
# make sure the key pins generate FIFO events
41+
tca.enable_int[pin] = True
42+
# we will stick events into the FIFO queue
43+
tca.event_mode_fifo[pin] = True
44+
45+
# turn on INT output pin
46+
tca.key_intenable = True
47+
48+
# display width and height setup
49+
WIDTH = 128
50+
HEIGHT = 64
51+
BORDER = 5
52+
53+
# display setup
54+
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)
55+
56+
splash = displayio.Group()
57+
display.show(splash)
58+
59+
# text area setup
60+
title_text = "TCA8418 Demo"
61+
title_area = label.Label(
62+
terminalio.FONT, text=title_text, color=0xFFFFFF, x=10, y=10 // 2+1)
63+
splash.append(title_area)
64+
65+
key_text = " "
66+
key_area = label.Label(
67+
terminalio.FONT, text=key_text, color=0xFFFFFF, x=10, y=HEIGHT // 2+1)
68+
splash.append(key_area)
69+
70+
while True:
71+
if tca.key_int:
72+
# first figure out how big the queue is
73+
events = tca.events_count
74+
# now print keyevent, row, column & key name
75+
for _ in range(events):
76+
keyevent = tca.next_event
77+
# strip keyevent
78+
event = keyevent & 0x7F
79+
event -= 1
80+
# figure out row
81+
row = int(event / 10)
82+
# figure out column
83+
col = event % 10
84+
# print event type first
85+
if keyevent & 0x80:
86+
print("Key down")
87+
# print each key pressed to display consecutively
88+
key_area.text = key_area.text + keymap[col][row]
89+
else:
90+
print("Key up")
91+
# use row & column coordinates to print key name
92+
print("Row %d, Column %d, Key %s" % (row, col, keymap[col][row]))
93+
tca.key_int = True # clear the IRQ by writing 1 to it
94+
time.sleep(0.01)

0 commit comments

Comments
 (0)