Simple test

Ensure your device works with this simple test.

examples/qwiicjoystick_simpletest.py
 1# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic Joystick.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15168
 9
10"""
11 Qwiic Joystick Simple Test - qwiicjoystick_simpletest.py
12 Written by Gaston Williams, June 13th, 2019
13 The Qwiic Joystick is a I2C controlled analog joystick
14
15 Simple Test:
16 This program uses the Qwiic Joystick CircuitPython Library to read
17 and print out the joystick position.
18"""
19import sys
20from time import sleep
21import board
22import sparkfun_qwiicjoystick
23
24# Create bus object using our board's I2C port
25i2c = board.I2C()
26
27# Create joystick object
28joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c)
29
30# Check if connected
31if joystick.connected:
32    print("Joystick connected.")
33else:
34    print("Joystick does not appear to be connected. Please check wiring.")
35    sys.exit()
36
37print("Press Joystick button to exit program.")
38
39# joystick.button goes to 0 when pressed
40while joystick.button == 1:
41    print("X = " + str(joystick.horizontal) + " Y = " + str(joystick.vertical))
42    sleep(0.200)  # sleep a bit to slow down messages
43
44print("Button pressed.")

Examples

  1. Basic Readings - Print the joystick position and button status.

examples/example1_basic_readings.py
 1# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic Joystick.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15168
 9
10"""
11 Qwiic Joystick Example 1 - example1_basic_readings.py
12 Written by Gaston Williams, June 13th, 2019
13 Based on Arduino code written by
14 Wes Furuya @ SparkFun Electronics, February 5th, 2019
15 The Qwiic Joystick is a I2C controlled analog joystick
16
17 Example 1 - Basic Readings:
18 This program uses the Qwiic Joystick CircuitPython Library to read and
19 print the current joystick position and button state.
20"""
21import sys
22from time import sleep
23import board
24import sparkfun_qwiicjoystick
25
26# Create bus object using our board's I2C port
27i2c = board.I2C()
28
29# Create joystick object
30joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c)
31
32# Check if connected
33if joystick.connected:
34    print("Joystick connected.")
35else:
36    print("Joystick does not appear to be connected. Please check wiring.")
37    sys.exit()
38
39print("Joystick Version: " + joystick.version)
40print("Type Ctrl-C to exit program.")
41
42try:
43    while True:
44        print(
45            "X: "
46            + str(joystick.horizontal)
47            + " Y: "
48            + str(joystick.vertical)
49            + " Button: "
50            + str(joystick.button)
51        )
52        # sleep a bit to slow down messages
53        sleep(0.200)
54except KeyboardInterrupt:
55    pass
  1. Change I2C Address - Change the device I2C address.

examples/example2_change_i2c_address.py
  1# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams
  2#
  3# SPDX-License-Identifier: MIT
  4
  5#  This is example is for the SparkFun Qwiic Joystick.
  6#  SparkFun sells these at its website: www.sparkfun.com
  7#  Do you like this library? Help support SparkFun. Buy a board!
  8#  https://www.sparkfun.com/products/15168
  9
 10"""
 11  Qwiic Joystick Example 2 - example2_change_i2c_address.py
 12  Written by Gaston Williams, June 13th, 2019
 13  Based on Arduino code written by
 14  Wes Furuya @ SparkFun Electronics, February 5th, 2019
 15  The Qwiic Joystick is a I2C controlled analog joystick
 16
 17  Example 2 - Change I2C Address and Read Firmware Version:
 18  This program uses the Qwiic Joystick CircuitPython Library to change
 19  the I2C address for the device. You enter in the DEC value (8-119) or
 20
 21  HEX value (0x08-0x77) for the new Joystick address.  The address is
 22  changed and the firmware version is printed out to validate the connection.
 23  You can run i2c_scanner.py to validate the address after the change.
 24
 25  Syntax: python3 change_i2c_address.py [address]
 26    where address is an optional address value in decimal or hex
 27    The default value for the address is 32 [0x20]
 28"""
 29
 30import sys
 31import board
 32import sparkfun_qwiicjoystick
 33
 34# The default QwiicJoystick i2c address is 0x20 (32)
 35i2c_address = 0x20
 36
 37# print('Arguement count: ' , len(sys.argv))
 38# print('List: ' + str(sys.argv))
 39
 40# If we were passed an arguement, then use it as the address
 41if len(sys.argv) > 1:
 42    try:
 43        # check to see if hex or decimal arguement
 44        if "0x" in sys.argv[1]:
 45            i2c_address = int(sys.argv[1], 16)
 46        else:
 47            i2c_address = int(sys.argv[1])
 48    except ValueError:
 49        print("Ignoring invalid arguement: " + str(sys.argv[1]))
 50
 51# Show the initial address
 52print("Current i2c address = " + str(i2c_address) + " [" + hex(i2c_address) + "]")
 53
 54# Create library object using our Bus I2C port
 55i2c = board.I2C()
 56joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c, i2c_address)
 57
 58if joystick.connected:
 59    print("Qwiic Joystick Example.")
 60else:
 61    # if we can't connecct, something is wrong so just quit
 62    print("Joystick does not appear to be connected. Please check wiring.")
 63    sys.exit()
 64
 65print(
 66    "Address: "
 67    + str(i2c_address)
 68    + " ["
 69    + hex(i2c_address)
 70    + "]"
 71    + " Version: "
 72    + joystick.version
 73)
 74
 75text = input(
 76    "Enter a new I2C address (as a decimal from 8 to 119 or hex 0x08 to 0x77):"
 77)
 78
 79# check to see if hex or decimal value
 80if "0x" in text:
 81    new_address = int(text, 16)
 82else:
 83    new_address = int(text)
 84
 85print("Changing address to " + str(new_address) + " [" + hex(new_address) + "]")
 86
 87result = joystick.set_i2c_address(new_address)
 88
 89if result:
 90    print("Address changed to " + str(new_address) + " [" + hex(new_address) + "]")
 91    # After the change check the new connection and show firmware version
 92    if joystick.connected:
 93        print("Connected to Joystick after address change.")
 94        print("Firmware Version: " + joystick.version)
 95    else:
 96        print("Error after address change. Cannot connect to Joystick.")
 97
 98else:
 99    print("Address change failed.")
100
101# good advice whether the address changed worked or not
102print("Run example3_i2c_scanner.py to verify the Qwiic Joystick address.")
  1. I2C Scanner - Scan the IC2 bus for devices.

examples/example3_i2c_scanner.py
 1# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic Joystick.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15168
 9
10"""
11  Qwiic Joystick Example 3 - example3_i2c_Scanner.py
12  Written by Gaston Williams, June 13th, 2019
13  The Qwiic Joystick is a I2C controlled analog joystick
14
15  Example 3 - I2C Scanner
16  This progam uses CircuitPython BusIO library to find the current
17  address of the Qwiic Joystick. It uses the I2C Scanner Example from
18  https://learn.adafruit.com/circuitpython-basics-i2c-and-spi/i2c-devices
19
20  The factory default address is 0x20.
21"""
22
23import time
24import board
25
26i2c = board.I2C()
27
28while not i2c.try_lock():
29    pass
30
31print("Press Ctrl-C to exit program")
32
33try:
34    while True:
35        print(
36            "I2C addresses found:",
37            [hex(device_address) for device_address in i2c.scan()],
38        )
39        time.sleep(5)
40except KeyboardInterrupt:
41    pass
42
43finally:
44    i2c.unlock()
  1. Joystick Output - Print joystick position and button status as directions.

examples/example4_joystick_output.py
 1# SPDX-FileCopyrightText: Copyright (c) 2019-2021 Gaston Williams
 2#
 3# SPDX-License-Identifier: MIT
 4
 5#  This is example is for the SparkFun Qwiic Joystick.
 6#  SparkFun sells these at its website: www.sparkfun.com
 7#  Do you like this library? Help support SparkFun. Buy a board!
 8#  https://www.sparkfun.com/products/15168
 9
10"""
11 Qwiic Joystick Example 4 - example4_joystick_output.py
12 Written by Gaston Williams, June 13th, 2019
13 Based on Arduino code written by
14 Wes Furuya @ SparkFun Electronics, February 5th, 2019
15 The Qwiic Joystick is a I2C controlled analog joystick
16
17 Example 4 - Joystick Output:
18 This program uses the Qwiic Joystick CircuitPython Library to read the
19 joystick position and button state, and print them out as directions.
20"""
21import sys
22from time import sleep
23import board
24import sparkfun_qwiicjoystick
25
26# Create bus object using our board's I2C port
27i2c = board.I2C()
28
29# Create joystick object
30joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c)
31# joystick = sparkfun_qwiicjoystick.Sparkfun_QwiicJoystick(i2c, other_addr)
32
33# Check if connected
34if joystick.connected:
35    print("Joystick connected.")
36else:
37    print("Joystick does not appear to be connected. Please check wiring.")
38    sys.exit()
39
40print("Joystick Version: " + joystick.version)
41print("Type Ctrl-C to exit program.")
42
43try:
44    while True:
45        x = joystick.horizontal
46        y = joystick.vertical
47        b = joystick.button
48
49        # print horizontal direction
50        if x > 575:
51            print("L")
52        if x < 450:
53            print("R")
54
55        # print vertical direction
56        if y > 575:
57            print("U")
58        if y < 450:
59            print("D")
60
61        # print button state
62        if b == 0:
63            print("Button")
64
65        # sleep a bit to slow down messages
66        sleep(0.200)
67except KeyboardInterrupt:
68    pass