-
Notifications
You must be signed in to change notification settings - Fork 3
/
qwiic_joystick.py
255 lines (202 loc) · 8.38 KB
/
qwiic_joystick.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
#-----------------------------------------------------------------------------
# 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
#
"""
qwiic_joystick
===============
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).
"""
#-----------------------------------------------------------------------------
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 Qwiic Joystick"
# 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 = [0x20]
# Register codes for the Joystick
JOYSTICK_ID = 0x00
JOYSTICK_VERSION1 = 0x01
JOYSTICK_VERSION2 = 0x02
JOYSTICK_X_MSB = 0x03
JOYSTICK_X_LSB = 0x04
JOYSTICK_Y_MSB = 0x05
JOYSTICK_Y_LSB = 0x06
JOYSTICK_BUTTON = 0x07
JOYSTICK_STATUS = 0x08 # 1 -> button clicked
JOYSTICK_I2C_LOCK = 0x09
JOYSTICK_CHANGE_ADDREESS = 0x0A
# 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 QwiicJoystick(object):
"""
QwiicJoystick
: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 QwiicJoystick device object.
:rtype: 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 Joystick device is conntected to the system..
:return: True if the device is connected, otherwise False.
:rtype: bool
"""
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 Joystick 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_horizontal()
#
# Returns the 10-bit ADC value of the joystick horizontal position
def get_horizontal(self):
"""
Returns the 10-bit ADC value of the joystick horizontal position
:return: The next button value
:rtype: byte as integer
"""
msb = self._i2c.readByte(self.address, JOYSTICK_X_MSB)
lsb = self._i2c.readByte(self.address, JOYSTICK_X_LSB)
return ((msb << 8) | lsb)>>6
horizontal = property(get_horizontal)
#----------------------------------------------------------------
# get_vertical()
#
# Returns the 10-bit ADC value of the joystick vertical position
def get_vertical(self):
"""
Returns the 10-bit ADC value of the joystick vertical position
:return: The next button value
:rtype: byte as integer
"""
msb = self._i2c.readByte(self.address, JOYSTICK_Y_MSB)
lsb = self._i2c.readByte(self.address, JOYSTICK_Y_LSB)
return ((msb << 8) | lsb)>>6
vertical = property(get_vertical)
#----------------------------------------------------------------
# get_button()
#
# Returns 0 button is currently being pressed.
def get_button(self):
"""
Returns 0 button is currently being pressed.
:return: button status
:rtype: integer
"""
return self._i2c.readByte(self.address, JOYSTICK_BUTTON)
button = property(get_button)
#----------------------------------------------------------------
# check_button()
#
# Returns 1 if button was pressed between reads of .getButton() or .checkButton()
# the register is then cleared after read.
def check_button(self):
"""
Returns 1 if button was pressed between reads of .getButton() or .checkButton()
the register is then cleared after read.
:return: button status
:rtype: integer
"""
status = self._i2c.readByte(self.address, JOYSTICK_STATUS)
# We've read this status bit, now clear it
self._i2c.writeByte(self.address, JOYSTICK_STATUS, 0x00)
return status
#----------------------------------------------------------------
# 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, JOYSTICK_VERSION1)
vMinor = self._i2c.readByte(self.address, JOYSTICK_VERSION2)
return "v %d.%d" % (vMajor, vMinor)
version = property(get_version)