-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqwiic_keypad.py
220 lines (182 loc) · 7.69 KB
/
qwiic_keypad.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#-----------------------------------------------------------------------------
# qwiic_keypad.py
#
# Python library for the SparkFun qwiic keypad.
#
# https://www.sparkfun.com/products/15290
#
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, July 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem
#
# More information on qwiic is at https:// www.sparkfun.com/qwiic
#
# Do you like this library? Help support SparkFun. Buy a board!
#==================================================================================
# Copyright (c) 2019 SparkFun Electronics
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#==================================================================================
#
# This is mostly a port of existing Arduino functionaly, so pylint is sad.
# The goal is to keep the public interface pthonic, but internal is internal
#
# pylint: disable=line-too-long, bad-whitespace, invalid-name
#
"""!
qwiic_keypad
============
Python module for the[SparkFun Qwiic Keypad - 12 Button Breakout](https://www.sparkfun.com/products/15290)
This python package is a port of the existing [SparkFun Qwiic Keypad Arduino Library](https://github.com/sparkfun/SparkFun_Qwiic_Keypad_Arduino_Library)
This package can be used in conjunction with the overall [SparkFun qwiic Python Package](https://github.com/sparkfun/Qwiic_Py)
New to qwiic? Take a look at the entire [SparkFun qwiic ecosystem](https://www.sparkfun.com/qwiic).
"""
#-----------------------------------------------------------------------------
import qwiic_i2c
# Define the device name and I2C addresses. These are set in the class defintion
# as class variables, making them avilable without having to create a class instance.
# This allows higher level logic to rapidly create a index of qwiic devices at
# runtine
#
# The name of this device
_DEFAULT_NAME = "Qwiic Keypad - 12 Key"
# Some devices have multiple availabel addresses - this is a list of these addresses.
# NOTE: The first address in this list is considered the default I2C address for the
# device.
_AVAILABLE_I2C_ADDRESS = [0x4B]
# Register codes for the keypad
KEYPAD_ID = 0x00
KEYPAD_VERSION1 = 0x01
KEYPAD_VERSION2 = 0x02
KEYPAD_BUTTON = 0x03
KEYPAD_TIME_MSB = 0x04
KEYPAD_TIME_LSB = 0x05
KEYPAD_UPDATE_FIFO = 0x06
KEYPAD_CHANGE_ADDRESS = 0x07
# define the class that encapsulates the device being created. All information associated with this
# device is encapsulated by this class. The device class should be the only value exported
# from this module.
class QwiicKeypad(object):
"""!
QwiicKeypad
@param address: The I2C address to use for the device.
If not provided, the default address is used.
@param i2c_driver: An existing i2c driver object. If not provided
a driver object is created.
@return **Object** The QwiicKeypad device object.
"""
# Constructor
device_name = _DEFAULT_NAME
available_addresses = _AVAILABLE_I2C_ADDRESS
# Constructor
def __init__(self, address=None, i2c_driver=None):
# Did the user specify an I2C address?
if address in self.available_addresses:
self.address = address
else:
self.address = self.available_addresses[0]
# load the I2C driver if one isn't provided
if i2c_driver is None:
self._i2c = qwiic_i2c.getI2CDriver()
if self._i2c is None:
print("Unable to load I2C driver for this platform.")
return
else:
self._i2c = i2c_driver
# ----------------------------------
# is_connected()
#
# Is an actual board connected to our system?
def is_connected(self):
"""!
Determine if a Keypad device is conntected to the system..
@return **bool** True if the device is connected, otherwise False.
"""
return self._i2c.isDeviceConnected(self.address)
connected = property(is_connected)
# ----------------------------------
# begin()
#
# Initialize the system/validate the board.
def begin(self):
"""!
Initialize the operation of the Keypad module
@return **bool** Returns true of the initializtion was successful, otherwise False.
"""
# Basically return True if we are connected...
return self.is_connected()
#----------------------------------------------------------------
# get_button()
#
# Returns the button at the top of the stack (aka the oldest button)
def get_button(self):
"""!
Returns the button at the top of the stack (aka the oldest button).
The return value is the 'ascii' value of th key pressed. To convert
to a character, use the python char() function.
@return **byte as integer** The next button value
"""
value = 0
# bus can throw an issue
try:
value = self._i2c.readByte(self.address, KEYPAD_BUTTON)
except IOError:
pass
return value
#----------------------------------------------------------------
# time_since_pressed()
#
# Returns the number of milliseconds since the current button in FIFO was pressed.
def time_since_pressed(self):
"""!
Returns the number of milliseconds since the current button in FIFO was pressed.
@return **integer** The elapsed time since button was pressed
"""
MSB = self._i2c.readByte(self.address, KEYPAD_TIME_MSB)
LSB = self._i2c.readByte(self.address, KEYPAD_TIME_LSB)
return (MSB << 8) | LSB
#----------------------------------------------------------------
# get_version()
#
# Returns a string of the firmware version number
def get_version(self):
"""!
Returns a string of the firmware version number
@return **string** The firmware version
"""
vMajor = self._i2c.readByte(self.address, KEYPAD_VERSION1)
vMinor = self._i2c.readByte(self.address, KEYPAD_VERSION2)
return "v %d.%d" % (vMajor, vMinor)
version = property(get_version)
#----------------------------------------------------------------
# update_fifo()
#
# "commands" keypad to plug in the next button into the registerMap
# note, this actually sets the bit0 on the updateFIFO register
def update_fifo(self):
"""!
"commands" keypad to plug in the next button into the registerMap
note, this actually sets the bit0 on the updateFIFO register
@return No return value
"""
# set bit0, commanding keypad to update fifo
self._i2c.writeByte(self.address, KEYPAD_UPDATE_FIFO, 0x01)