-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathkeypad_test.py
60 lines (44 loc) · 1.37 KB
/
keypad_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Make sure to have everything set up
# https://github.com/sparkfun/Qwiic_Keypad_Py
# `pip install sparkfun-qwiic-keypad`
# From https://github.com/sparkfun/Qwiic_Keypad_Py/blob/main/examples/qwiic_keypad_ex2.py
from __future__ import print_function
import qwiic_keypad
import time
import sys
def runExample():
print("\nSparkFun qwiic Keypad Example 1\n")
myKeypad = qwiic_keypad.QwiicKeypad()
if myKeypad.connected == False:
print("The Qwiic Keypad device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myKeypad.begin()
print("Initialized. Firmware Version: %s" % myKeypad.version)
print("Press a button: * to do a space. # to go to next line.")
button = 0
while True:
# necessary for keypad to pull button from stack to readable register
myKeypad.update_fifo()
button = myKeypad.get_button()
if button == -1:
print("No keypad detected")
time.sleep(1)
elif button != 0:
# Get the character version of this char
charButton = chr(button)
if charButton == '#':
print()
elif charButton == '*':
print(" ", end="")
else:
print(charButton, end="")
# Flush the stdout buffer to give immediate user feedback
sys.stdout.flush()
time.sleep(.25)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)