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