-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_phat_button.py
342 lines (280 loc) · 12.3 KB
/
top_phat_button.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#-----------------------------------------------------------------------------
# qwiic_joystick.py
#
# Python library for the SparkFun qwiic joystick.
#
# https://www.sparkfun.com/products/15168
#
#------------------------------------------------------------------------
#
# 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
#
"""
top_phat_button
===============
Python module for the[SparkFun Qwiic Joystick](https://www.sparkfun.com/products/15168)
This python package is a port of the existing [SparkFun Qwiic Joystick Arduino Library](https://github.com/sparkfun/SparkFun_Qwiic_Joystick_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).
"""
#-----------------------------------------------------------------------------
from __future__ import print_function
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 = "SparkFun Top pHAT Button"
# 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 = [0x71]
# Register codes for the Joystick
BUTTON_ID = 0x00
BUTTON_VERSION1 = 0x01
BUTTON_VERSION2 = 0x02
BUTTON_PRESSED = 0x03
BUTTON_CLICKED = 0x04
BUTTON_INTERRUPT = 0x05
BUTTON_DEBOUNCE = 0x06
BUTTON_CHANGE_ADDREESS = 0x1F
# Define the bit positions of the buttons and interrupt flag
A = 0
B = 1
UP = 2
DOWN = 3
LEFT = 4
RIGHT = 5
CENTER = 6
EVENT_AVAILABLE = 7
# Define the positions of the Interrupt Enable Bits
CLICKED_INTERRUPT_ENABLE = 0
PRESSED_INTERRUPT_ENABLE = 1
# 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 ToppHATButton(object):
"""
ToppHATButton
: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: The ToppHATButton device object.
:rtype: Object
"""
# Constructor
device_name = _DEFAULT_NAME
available_addresses = _AVAILABLE_I2C_ADDRESS
a_pressed = 0
b_pressed = 0
up_pressed = 0
down_pressed = 0
left_pressed = 0
right_pressed = 0
center_pressed = 0
pressed_event_available = 0
a_clicked = 0
b_clicked = 0
up_clicked = 0
down_clicked = 0
left_clicked = 0
right_clicked = 0
center_clicked = 0
clicked_event_available = 0
# Constructor
def __init__(self, address=None, i2c_driver=None):
# Did the user specify an I2C address?
self.address = address if address is not None else 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 the Top pHAT Buttons are connected to the system..
:return: True if the device is connected, otherwise False.
:rtype: bool
"""
return qwiic_i2c.isDeviceConnected(self.address)
connected = property(is_connected)
# ----------------------------------
# begin()
#
# Initialize the system/validate the board.
def begin(self):
"""
Initialize the operation of the button module
:return: Returns true of the initializtion was successful, otherwise False.
:rtype: bool
"""
# Basically return True if we are connected...
return self.is_connected()
#----------------------------------------------------------------
# get_button_pressed()
#
# Updates and returns buffer for all buttons and whether or not they are pressed as well as the pressed interrupt flag
# Reading this register also clears it.
# 7(MSB) 6 5 4 3 2 1 0(LSB)
# INT CTR RGT LFT DWN UP B A
def get_button_pressed(self):
"""
Updates and returns buffer for all buttons and whether or not they are pressed as well as the pressed interrupt flag
Reading this register also clears it.
7(MSB) 6 5 4 3 2 1 0(LSB)
INT CTR RGT LFT DWN UP B A
:return: button status
:rtype: integer
"""
temp = self._i2c.readByte(self.address, BUTTON_PRESSED)
self.a_pressed = (temp & (1 << A)) >> A
self.b_pressed = (temp & (1 << B)) >> B
self.up_pressed = (temp & (1 << UP)) >> UP
self.down_pressed = (temp & (1 << DOWN)) >> DOWN
self.left_pressed = (temp & (1 << LEFT)) >> LEFT
self.right_pressed = (temp & (1 << RIGHT)) >> RIGHT
self.center_pressed = (temp & (1 << CENTER)) >> CENTER
self.pressed_event_available = (temp & (1 << EVENT_AVAILABLE)) >>EVENT_AVAILABLE
return temp
button_pressed = property(get_button_pressed)
#----------------------------------------------------------------
# get_button_clicked()
#
# Updates and returns buffer for all buttons and whether or not they have been clicked as well as the clicked interrupt flag
# Reading this register also clears it.
# 7(MSB) 6 5 4 3 2 1 0(LSB)
# INT CTR RGT LFT DWN UP B A
def get_button_clicked(self):
"""
Returns 1 when a button has received a full click cycle (press and release). The interrupt must be cleared by the user.
7(MSB) 6 5 4 3 2 1 0(LSB)
INT CTR RGT LFT DWN UP B A
:return: Clicked status of all buttons in a byte
:rtype: integer
"""
temp = self._i2c.readByte(self.address, BUTTON_CLICKED)
self.a_clicked = temp & (1 << A)
self.b_clicked = (temp & (1 << B)) >> B
self.up_clicked = (temp & (1 << UP)) >> UP
self.down_clicked = (temp & (1 << DOWN)) >> DOWN
self.left_clicked = (temp & (1 << LEFT)) >> LEFT
self.right_clicked = (temp & (1 << RIGHT)) >> RIGHT
self.center_clicked = (temp & (1 << CENTER)) >> CENTER
self.clicked_event_available = (temp & (1 << EVENT_AVAILABLE)) >> EVENT_AVAILABLE
return temp
button_clicked = property(get_button_clicked)
#----------------------------------------------------------------
# get_version()
#
# Returns a string of the firmware version number
def get_version(self):
"""
Returns a string of the firmware version number
:return: The firmware version
:rtype: string
"""
vMajor = self._i2c.readByte(self.address, BUTTON_VERSION1)
vMinor = self._i2c.readByte(self.address, BUTTON_VERSION2)
return "v %d.%d" % (vMajor, vMinor)
version = property(get_version)
#----------------------------------------------------------------
# get__pressed_interrupt()
#
# Returns the status of the pressed interrupt enable bit
def get_pressed_interrupt(self):
"""
Returns the status of the pressed interrupt enable
:return: The pressed interrupt enable bit
:rtype: bool
"""
interrupt = self._i2c.readByte(self.address, BUTTON_INTERRUPT)
interrupt = (interrupt & (1 << PRESSED_INTERRUPT_ENABLE)) >> PRESSED_INTERRUPT_ENABLE
return interrupt
#----------------------------------------------------------------
# set_pressed_interrupt(bit_setting)
#
# Sets the status of the pressed interrupt enable bit
def set_pressed_interrupt(self, bit_setting):
"""
Sets the status of the pressed interrupt enable bit
:param: The pressed interrupt enable bit
:return: The status of the I2C transaction
:rtype: bool
"""
interrupt = self._i2c.readByte(self.address, BUTTON_INTERRUPT)
interrupt &= ~(1 << PRESSED_INTERRUPT_ENABLE) #Clear enable bit
interrupt |= (bit_setting << PRESSED_INTERRUPT_ENABLE)
return self._i2c.writeWord(self.address, BUTTON_INTERRUPT, interrupt)
pressed_interrupt_enable = property(get_pressed_interrupt, set_pressed_interrupt)
#----------------------------------------------------------------
# get__clicked_interrupt()
#
# Returns the status of the clicked interrupt enable bit
def get_clicked_interrupt(self):
"""
Returns the status of the clicked interrupt enable
:return: The clicked interrupt enable bit
:rtype: bool
"""
interrupt = self._i2c.readByte(self.address, BUTTON_INTERRUPT)
interrupt = (interrupt & (1 << CLICKED_INTERRUPT_ENABLE)) >> CLICKED_INTERRUPT_ENABLE
return interrupt
#----------------------------------------------------------------
# set_clicked_interrupt(bit_setting)
#
# Sets the status of the clicked interrupt enable bit
def set_clicked_interrupt(self, bit_setting):
"""
Sets the status of the clicked interrupt enable bit
:param: The clicked interrupt enable bit
:return: The status of the I2C transaction
:rtype: bool
"""
interrupt = self._i2c.readByte(self.address, BUTTON_INTERRUPT)
interrupt &= ~(1 << CLICKED_INTERRUPT_ENABLE) #Clear enable bit
interrupt |= (bit_setting << CLICKED_INTERRUPT_ENABLE)
return self._i2c.writeWord(self.address, BUTTON_INTERRUPT, interrupt)
clicked_interrupt_enable = property(get_clicked_interrupt, set_clicked_interrupt)