Qwiic_Joystick_Py¶
Python module for the qwiic joystick, which is part of the SparkFun Qwiic Joystick
This python package is a port of the existing SparkFun Qwiic Joystick Arduino Library
This package can be used in conjunction with the overall SparkFun qwiic Python Package
New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.
Dependencies¶
This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py
Documentation¶
The SparkFun qwiic Joystick module documentation is hosted at ReadTheDocs
Installation¶
PyPi Installation¶
This repository is hosted on PyPi as the sparkfun-qwiic-joystick package. On systems that support PyPi installation via pip, this library is installed using the following commands
For all users (note: the user must have sudo privileges):
sudo pip install sparkfun-qwiic-joystick
For the current user:
pip install sparkfun-qwiic-joystick
Local Installation¶
To install, make sure the setuptools package is installed on the system.
Direct installation at the command line:
python setup.py install
To build a package for use with pip:
python setup.py sdist
A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.
cd dist
pip install sparkfun_qwiic_joystick-<version>.tar.gz
Example Use¶
See the examples directory for more detailed use examples.
from __future__ import print_function
import qwiic_joystick
import time
import sys
def runExample():
print("\nSparkFun qwiic Joystick Example 1\n")
myJoystick = qwiic_joystick.QwiicJoystick()
if myJoystick.isConnected() == False:
print("The Qwiic Joystick device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myJoystick.begin()
print("Initialized. Firmware Version: %s" % myJoystick.getVersion())
while True:
print("X: %d, Y: %d, Button: %d" % ( \
myJoystick.getHorizontal(), \
myJoystick.getVertical(), \
myJoystick.getButton()))
time.sleep(.5)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
Table of Contents¶
API Reference¶
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).
-
class
qwiic_joystick.
QwiicJoystick
(address=None, i2c_driver=None)[source]¶ Parameters: - address – The I2C address to use for the device. If not provided, the default address is used.
- i2c_driver – An existing i2c driver object. If not provided a driver object is created.
Returns: The QwiicJoystick device object.
Return type: Object
-
begin
()[source]¶ Initialize the operation of the Joystick module
Returns: Returns true of the initializtion was successful, otherwise False. Return type: bool
Returns 0 button is currently being pressed.
Returns: button status Return type: integer
Returns 1 if button was pressed between reads of .getButton() or .checkButton() the register is then cleared after read.
Returns: button status Return type: integer
-
connected
¶ Determine if a Joystick device is conntected to the system..
Returns: True if the device is connected, otherwise False. Return type: bool
Returns 0 button is currently being pressed.
Returns: button status Return type: integer
-
get_horizontal
()[source]¶ Returns the 10-bit ADC value of the joystick horizontal position
Returns: The next button value Return type: byte as integer
-
get_version
()[source]¶ Returns a string of the firmware version number
Returns: The firmware version Return type: string
-
get_vertical
()[source]¶ Returns the 10-bit ADC value of the joystick vertical position
Returns: The next button value Return type: byte as integer
-
horizontal
¶ Returns the 10-bit ADC value of the joystick horizontal position
Returns: The next button value Return type: byte as integer
-
is_connected
()[source]¶ Determine if a Joystick device is conntected to the system..
Returns: True if the device is connected, otherwise False. Return type: bool
-
version
¶ Returns a string of the firmware version number
Returns: The firmware version Return type: string
-
vertical
¶ Returns the 10-bit ADC value of the joystick vertical position
Returns: The next button value Return type: byte as integer
Read a Position and Button State¶
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 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_env_joystick_ex1.py
#
# Simple Example for the Qwiic Joystick Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# 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.
#==================================================================================
# Example 1
#
from __future__ import print_function
import qwiic_joystick
import time
import sys
def runExample():
print("\nSparkFun qwiic Joystick Example 1\n")
myJoystick = qwiic_joystick.QwiicJoystick()
if myJoystick.connected == False:
print("The Qwiic Joystick device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myJoystick.begin()
print("Initialized. Firmware Version: %s" % myJoystick.version)
while True:
print("X: %d, Y: %d, Button: %d" % ( \
myJoystick.horizontal, \
myJoystick.vertical, \
myJoystick.button))
time.sleep(.5)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
|
Output Direction¶
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 | #!/usr/bin/env python
#-----------------------------------------------------------------------------
# qwiic_env_joystick_ex2.py
#
# Simple Example for the Qwiic Joystick Device
#------------------------------------------------------------------------
#
# Written by SparkFun Electronics, May 2019
#
# This python library supports the SparkFun Electroncis qwiic
# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
# board computers.
#
# 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.
#==================================================================================
# Example 2
#
from __future__ import print_function
import qwiic_joystick
import time
import sys
def runExample():
print("\nSparkFun qwiic Joystick Example 2\n")
myJoystick = qwiic_joystick.QwiicJoystick()
if myJoystick.connected == False:
print("The Qwiic Joystick device isn't connected to the system. Please check your connection", \
file=sys.stderr)
return
myJoystick.begin()
print("Initialized. Firmware Version: %s" % myJoystick.version)
while True:
x = myJoystick.horizontal
y = myJoystick.vertical
b = myJoystick.button
if x > 575:
print("L")
elif x < 450:
print("R")
if y > 575:
print("U")
elif y < 450:
print("D")
if b == 0:
print("Button")
time.sleep(.5)
if __name__ == '__main__':
try:
runExample()
except (KeyboardInterrupt, SystemExit) as exErr:
print("\nEnding Example 1")
sys.exit(0)
|
\ Sort by:\ best rated\ newest\ oldest\
\\
Add a comment\ (markup):
\``code``
, \ code blocks:::
and an indented block after blank line