Skip to content

Commit 0d4ed59

Browse files
authored
Merge pull request #7 from adafruit/3x4_oled
Adding 3x4 OLED demo. Thanks @tekktrik !
2 parents f68b509 + 17de6eb commit 0d4ed59

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

examples/tca8418_3x4_OLED.py

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

0 commit comments

Comments
 (0)