diff --git a/README.rst b/README.rst index 20294d0..7ef7f9b 100644 --- a/README.rst +++ b/README.rst @@ -20,7 +20,7 @@ Introduction :target: https://github.com/psf/black :alt: Code Style: Black -CircuitPython library for Sparkfun Qwiic Joystick. This library is ported from the +CircuitPython library for Sparkfun Qwiic Joystick. This library is ported from the `SparkFun Qwiic Joystick Arduino Library `_ .. image:: https://cdn.sparkfun.com/assets/parts/1/3/5/5/8/15168-SparkFun_Qwiic_Joystick-01.jpg @@ -28,7 +28,7 @@ CircuitPython library for Sparkfun Qwiic Joystick. This library is ported from :alt: SparkFun Qwiic Joystick (COM-15168) `SparkFun Qwiic Joystick (COM-15168) `_ - + Dependencies @@ -49,7 +49,7 @@ Raspberry Pi Setup ------------------ Adafruit has an excellent tutorial on `Installing CircuitPython Libraries on Raspberry Pi `_. - + Quick Start Summary: -------------------- * Start with the latest version of Raspbian with Wifi configured. @@ -200,15 +200,12 @@ Then run the build: License Information ----------------------- -This product is **open source**! +This product is **open source**! -Please review the LICENSE.md file for license information. +Please review the LICENSE.md file for license information. -Please use, reuse, and modify these files as you see fit. +Please use, reuse, and modify these files as you see fit. Please maintain the attributions to SparkFun Electronics and Adafruit and release any derivative under the same license. Distributed as-is; no warranty is given. - - - diff --git a/docs/conf.py b/docs/conf.py index 7b9c55f..870605c 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -29,8 +29,11 @@ intersphinx_mapping = { - "python": ("https://docs.python.org/3.4", None),"BusDevice": ("https://circuitpython.readthedocs.io/projects/busdevice/en/latest/", None), - + "python": ("https://docs.python.org/3.4", None), + "BusDevice": ( + "https://circuitpython.readthedocs.io/projects/busdevice/en/latest/", + None, + ), "CircuitPython": ("https://circuitpython.readthedocs.io/en/latest/", None), } diff --git a/examples/example1_basic_readings.py b/examples/example1_basic_readings.py index fab31e1..2ee2986 100644 --- a/examples/example1_basic_readings.py +++ b/examples/example1_basic_readings.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams +# +# SPDX-License-Identifier: MIT + # This is example is for the SparkFun Qwiic Joystick. # SparkFun sells these at its website: www.sparkfun.com # Do you like this library? Help support SparkFun. Buy a board! @@ -14,33 +18,37 @@ This program uses the Qwiic Joystick CircuitPython Library to read and print the current joystick position and button state. """ - +import sys from time import sleep import board -import busio import sparkfun_qwiicjoystick # Create bus object using our board's I2C port -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() # Create joystick object joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c) # Check if connected if joystick.connected: - print('Joystick connected.') + print("Joystick connected.") else: - print('Joystick does not appear to be connected. Please check wiring.') - exit() + print("Joystick does not appear to be connected. Please check wiring.") + sys.exit() -print('Joystick Version: ' + joystick.version) -print('Type Ctrl-C to exit program.') +print("Joystick Version: " + joystick.version) +print("Type Ctrl-C to exit program.") try: while True: - print('X: ' + str(joystick.horizontal) - + ' Y: ' + str(joystick.vertical) - + ' Button: ' + str(joystick.button)) + print( + "X: " + + str(joystick.horizontal) + + " Y: " + + str(joystick.vertical) + + " Button: " + + str(joystick.button) + ) # sleep a bit to slow down messages sleep(0.200) except KeyboardInterrupt: diff --git a/examples/example2_change_i2c_address.py b/examples/example2_change_i2c_address.py index 08b02bd..7c11e92 100644 --- a/examples/example2_change_i2c_address.py +++ b/examples/example2_change_i2c_address.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams +# +# SPDX-License-Identifier: MIT + # This is example is for the SparkFun Qwiic Joystick. # SparkFun sells these at its website: www.sparkfun.com # Do you like this library? Help support SparkFun. Buy a board! @@ -25,7 +29,6 @@ import sys import board -import busio import sparkfun_qwiicjoystick # The default QwiicJoystick i2c address is 0x20 (32) @@ -38,56 +41,62 @@ if len(sys.argv) > 1: try: # check to see if hex or decimal arguement - if '0x' in sys.argv[1]: + if "0x" in sys.argv[1]: i2c_address = int(sys.argv[1], 16) else: i2c_address = int(sys.argv[1]) except ValueError: - print('Ignoring invalid arguement: ' + str(sys.argv[1])) + print("Ignoring invalid arguement: " + str(sys.argv[1])) # Show the initial address -print('Current i2c address = ' + str(i2c_address) - + ' [' + hex(i2c_address) + ']') +print("Current i2c address = " + str(i2c_address) + " [" + hex(i2c_address) + "]") # Create library object using our Bus I2C port -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c, i2c_address) if joystick.connected: - print('Qwiic Joystick Example.') + print("Qwiic Joystick Example.") else: # if we can't connecct, something is wrong so just quit - print('Joystick does not appear to be connected. Please check wiring.') - exit() - -print('Address: ' + str(i2c_address) + ' [' + hex(i2c_address) + ']' - + ' Version: ' + joystick.version) - -text = input('Enter a new I2C address (as a decimal from 8 to 119 or hex 0x08 to 0x77):') + print("Joystick does not appear to be connected. Please check wiring.") + sys.exit() + +print( + "Address: " + + str(i2c_address) + + " [" + + hex(i2c_address) + + "]" + + " Version: " + + joystick.version +) + +text = input( + "Enter a new I2C address (as a decimal from 8 to 119 or hex 0x08 to 0x77):" +) # check to see if hex or decimal value -if '0x' in text: +if "0x" in text: new_address = int(text, 16) else: new_address = int(text) -print('Changing address to ' + str(new_address) - + ' [' + hex(new_address) + ']') +print("Changing address to " + str(new_address) + " [" + hex(new_address) + "]") result = joystick.set_i2c_address(new_address) if result: - print('Address changed to ' + str(new_address) - + ' [' + hex(new_address) + ']') + print("Address changed to " + str(new_address) + " [" + hex(new_address) + "]") # After the change check the new connection and show firmware version if joystick.connected: - print('Connected to Joystick after address change.') - print('Firmware Version: ' + joystick.version) + print("Connected to Joystick after address change.") + print("Firmware Version: " + joystick.version) else: - print('Error after address change. Cannot connect to Joystick.') + print("Error after address change. Cannot connect to Joystick.") else: - print('Address change failed.') + print("Address change failed.") # good advice whether the address changed worked or not -print('Run example3_i2c_scanner.py to verify the Qwiic Joystick address.') +print("Run example3_i2c_scanner.py to verify the Qwiic Joystick address.") diff --git a/examples/example3_i2c_scanner.py b/examples/example3_i2c_scanner.py index 8f14750..d7efbcc 100644 --- a/examples/example3_i2c_scanner.py +++ b/examples/example3_i2c_scanner.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams +# +# SPDX-License-Identifier: MIT + # This is example is for the SparkFun Qwiic Joystick. # SparkFun sells these at its website: www.sparkfun.com # Do you like this library? Help support SparkFun. Buy a board! @@ -17,21 +21,21 @@ """ import time - import board -import busio -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() while not i2c.try_lock(): pass -print('Press Ctrl-C to exit program') +print("Press Ctrl-C to exit program") try: while True: - print('I2C addresses found:', - [hex(device_address) for device_address in i2c.scan()]) + print( + "I2C addresses found:", + [hex(device_address) for device_address in i2c.scan()], + ) time.sleep(5) except KeyboardInterrupt: pass diff --git a/examples/example4_joystick_output.py b/examples/example4_joystick_output.py index 86bb723..0ed2615 100644 --- a/examples/example4_joystick_output.py +++ b/examples/example4_joystick_output.py @@ -1,3 +1,7 @@ +# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams +# +# SPDX-License-Identifier: MIT + # This is example is for the SparkFun Qwiic Joystick. # SparkFun sells these at its website: www.sparkfun.com # Do you like this library? Help support SparkFun. Buy a board! @@ -14,14 +18,13 @@ This program uses the Qwiic Joystick CircuitPython Library to read the joystick position and button state, and print them out as directions. """ - +import sys from time import sleep import board -import busio import sparkfun_qwiicjoystick # Create bus object using our board's I2C port -i2c = busio.I2C(board.SCL, board.SDA) +i2c = board.I2C() # Create joystick object joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c) @@ -29,13 +32,13 @@ # Check if connected if joystick.connected: - print('Joystick connected.') + print("Joystick connected.") else: - print('Joystick does not appear to be connected. Please check wiring.') - exit() + print("Joystick does not appear to be connected. Please check wiring.") + sys.exit() -print('Joystick Version: ' + joystick.version) -print('Type Ctrl-C to exit program.') +print("Joystick Version: " + joystick.version) +print("Type Ctrl-C to exit program.") try: while True: @@ -45,19 +48,19 @@ # print horizontal direction if x > 575: - print('L') + print("L") if x < 450: - print('R') + print("R") # print vertical direction if y > 575: - print('U') + print("U") if y < 450: - print('D') + print("D") # print button state if b == 0: - print('Button') + print("Button") # sleep a bit to slow down messages sleep(0.200) diff --git a/examples/qwiicjoystick_simpletest.py b/examples/qwiicjoystick_simpletest.py index 02e540d..8c2ebd9 100644 --- a/examples/qwiicjoystick_simpletest.py +++ b/examples/qwiicjoystick_simpletest.py @@ -1,6 +1,6 @@ # SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams # -# SPDX-License-Identifier: Unlicense +# SPDX-License-Identifier: MIT # This is example is for the SparkFun Qwiic Joystick. # SparkFun sells these at its website: www.sparkfun.com @@ -29,15 +29,15 @@ # Check if connected if joystick.connected: - print('Joystick connected.') + print("Joystick connected.") else: - print('Joystick does not appear to be connected. Please check wiring.') + print("Joystick does not appear to be connected. Please check wiring.") sys.exit() -print('Press Joystick button to exit program.') +print("Press Joystick button to exit program.") while joystick.button == 1: - print('X = ' + str(joystick.horizontal) + ' Y = ' + str(joystick.vertical)) + print("X = " + str(joystick.horizontal) + " Y = " + str(joystick.vertical)) sleep(0.200) # sleep a bit to slow down messages -print('Button pressed.') +print("Button pressed.") diff --git a/setup.py b/setup.py index 33818f4..99cec8c 100644 --- a/setup.py +++ b/setup.py @@ -58,7 +58,6 @@ ], # What does your project relate to? keywords="adafruit blinka circuitpython micropython qwiicjoystick sparkfun qwiic joystick", - # You can just specify the packages manually here if your project is # simple. Or you can use find_packages(). # IF LIBRARY FILES ARE A PACKAGE FOLDER, diff --git a/sparkfun_qwiicjoystick.py b/sparkfun_qwiicjoystick.py index 7b12967..0ff2ad6 100644 --- a/sparkfun_qwiicjoystick.py +++ b/sparkfun_qwiicjoystick.py @@ -64,7 +64,7 @@ from adafruit_bus_device.i2c_device import I2CDevice # public constants -QWIIC_JOYSTICK_ADDR = const(0x20) #default I2C Address +QWIIC_JOYSTICK_ADDR = const(0x20) # default I2C Address # private constants _JOYSTICK_ID = const(0x00) @@ -75,7 +75,7 @@ _JOYSTICK_Y_MSB = const(0x05) _JOYSTICK_Y_LSB = const(0x06) _JOYSTICK_BUTTON = const(0x07) -_JOYSTICK_STATUS = const(0x08) #1 - button clicked +_JOYSTICK_STATUS = const(0x08) # 1 - button clicked _JOYSTICK_I2C_LOCK = const(0x09) _JOYSTICK_CHANGE_ADDRESS = const(0x0A) @@ -98,15 +98,14 @@ class Sparkfun_QwiicJoystick: # use QwiicJoystick(i2c, address) for a different address # joystick = QwiicJoystick(i2c, 0x21)""" - def __init__(self, i2c, address=QWIIC_JOYSTICK_ADDR, debug=False): """Initialize Qwiic Joystick for i2c communication.""" self._device = I2CDevice(i2c, address) - #save handle to i2c bus in case address is changed + # save handle to i2c bus in case address is changed self._i2c = i2c self._debug = debug -# public properites + # public properites @property def connected(self): @@ -120,7 +119,7 @@ def version(self): """Firmware version string for joystick.""" major = self._read_register(_JOYSTICK_VERSION1) minor = self._read_register(_JOYSTICK_VERSION2) - return 'v' + str(major) + '.' + str(minor) + return "v" + str(major) + "." + str(minor) @property def horizontal(self): @@ -131,7 +130,7 @@ def horizontal(self): x_lsb = self._read_register(_JOYSTICK_X_LSB) # mask off bytes and combine into 10-bit integer - x = ((x_msb & 0xFF)<<8 | (x_lsb & 0xFF))>>6 + x = ((x_msb & 0xFF) << 8 | (x_lsb & 0xFF)) >> 6 return x @property @@ -143,7 +142,7 @@ def vertical(self): y_lsb = self._read_register(_JOYSTICK_Y_LSB) # mask off bytes and combine into 10-bit integer - y = ((y_msb & 0xFF)<<8 | (y_lsb & 0xFF))>>6 + y = ((y_msb & 0xFF) << 8 | (y_lsb & 0xFF)) >> 6 return y @property @@ -156,50 +155,48 @@ def button(self): @property def button_status(self): """1 if button pressed between reads, cleared after read.""" - #read button status (since last check) + # read button status (since last check) status = self._read_register(_JOYSTICK_STATUS) - #clear button status + # clear button status self._write_register(_JOYSTICK_STATUS, 0x00) return status & 0xFF - -# public functions + # public functions def set_i2c_address(self, new_address): """Change the i2c address of Joystick snd return True if successful.""" # check range of new address - if (new_address < 8 or new_address > 119): - print('ERROR: Address outside 8-119 range') + if new_address < 8 or new_address > 119: + print("ERROR: Address outside 8-119 range") return False # write magic number 0x13 to lock register, to unlock address for update self._write_register(_JOYSTICK_I2C_LOCK, 0x13) # write new address self._write_register(_JOYSTICK_CHANGE_ADDRESS, new_address) - # wait a second for joystick to settle after change + # wait a second for joystick to settle after change sleep(1) # try to re-create new i2c device at new address try: self._device = I2CDevice(self._i2c, new_address) except ValueError as err: - print('Address Change Failure') + print("Address Change Failure") print(err) return False - #if we made it here, everything went fine + # if we made it here, everything went fine return True -# No i2c begin function is needed since I2Cdevice class takes care of that + # No i2c begin function is needed since I2Cdevice class takes care of that -# private functions + # private functions def _read_register(self, addr): # Read and return a byte from the specified 8-bit register address. with self._device as device: - device.write(bytes([addr & 0xFF]), stop=False) result = bytearray(1) - device.readinto(result) + device.write_then_readinto(bytes([addr & 0xFF]), result) if self._debug: print("$%02X => %s" % (addr, [hex(i) for i in result])) return result[0]