Simple test

Ensure your device works with this simple test.

examples/qwiickeypad_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 Keypad.
 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/15290
 9
10"""
11 Qwiic Keypad Simple Test - qwiickeypad_simpletest.py
12 Written by Gaston Williams, July 2nd, 2019
13 The Qwiic Keypad is an I2C controlled 12-Button Keypad produced by Sparkfun.
14
15 Qwiic Keypad - Simple Test:
16 This program uses the Qwiic Keypad CircuitPython Library to
17 control the Qwiic Keypad over I2C to read the button.
18"""
19import sys
20from time import sleep
21import board
22import sparkfun_qwiickeypad
23
24# Create bus object using our board's I2C port
25i2c = board.I2C()
26
27# Create keypad object
28keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c)
29
30print("Qwicc Keypad Simple Test")
31
32# Check if connected
33if keypad.connected:
34    print("Keypad connected. Firmware: ", keypad.version)
35else:
36    print("Keypad does not appear to be connected. Please check wiring.")
37    sys.exit()
38
39print("Press any button on the keypad.")
40
41# button value -1 is error/busy, 0 is no key pressed
42button = -1
43
44# while no key is pressed
45while button <= 0:
46    # request a button
47    keypad.update_fifo()
48    button = keypad.button
49    # Display the button value
50    if button > 0:
51        print("Button '" + chr(button) + "' was pressed.")
52    # wait a bit before trying again
53    sleep(0.100)

Examples

  1. Read Button - Print the button value when a key is pressed.

examples/example1_read_button.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 Keypad.
 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/15290
 9
10"""
11 Qwiic Keypad Example 1 - example1_read_button.py
12 Written by Gaston Williams, July 1st, 2019
13 Based on Arduino code written by
14 Nathan Seidle @ Sparkfun, January 21, 2018
15 and updated by
16 Pete Lewis @ Sparkfun, March 16, 2019
17
18 The Qwiic Keypad is an I2C controlled 12-button keypad produced by sparkfun
19
20 Example 1 - Read Button:
21 This program uses the Qwiic Keypad CircuitPython Library to
22 control the Qwiic Keypad over I2C and prints which button was pressed.
23 Qwiic KeyPad records any button presses to a stack. It can remember up to
24 15 button presses. The master I2C device (for example, an Uno) can ask for
25 the oldest button pressed. If the master continues to read in button presses,
26 it will receive the entire stack (from oldest to newest). This is handy if
27 you need to go and do something else with your code, you can then come back
28 to the keypad and pull in the last 15 button presses.
29"""
30
31import sys
32from time import sleep
33import board
34import sparkfun_qwiickeypad
35
36# Create bus object using our board's I2C port
37i2c = board.I2C()
38
39# Create relay object
40keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c)
41
42print("Qwicc Keypad Example 1 Read Button")
43
44# Check if connected
45if keypad.connected:
46    print("Keypad connected. Firmware: ", keypad.version)
47else:
48    print("Keypad does not appear to be connected. Please check wiring.")
49    sys.exit()
50
51print("Type Ctrl-C to exit program.")
52print("Press a button. Press * to do a space. Press # to go to next line.")
53
54try:
55    while True:
56        # request the next key pressed
57        keypad.update_fifo()
58        button = keypad.button
59
60        if button == -1:
61            print("Keypad error. Try again.")
62            sleep(1)
63        elif button != 0:
64            c = chr(button)
65            if c == "#":
66                print()
67            elif c == "*":
68                sys.stdout.write(" ")
69                sys.stdout.flush()
70            else:
71                sys.stdout.write(c)
72                sys.stdout.flush()
73
74        sleep(0.250)
75
76except KeyboardInterrupt:
77    pass
  1. Read With Time - Print the button value and time elapsed when a key is pressed.

examples/example2_read_with_time.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 Keypad.
 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/15290
 9
10"""
11 Qwiic Keypad Example 2 - example2_read_with_time.py
12 Written by Gaston Williams, July 1st, 2019
13 Based on Arduino code written by
14 Nathan Seidle @ Sparkfun, January 21, 2018
15 and updated by
16 Pete Lewis @ Sparkfun, March 16, 2019
17
18 The Qwiic Keypad is an I2C controlled 12-button keypad produced by sparkfun.
19
20 Example 2 - Read With Time:
21 This program uses the Qwiic Keypad CircuitPython Library to control the Qwiic
22 Keypad over I2C and prints which button was pressed and when it was pressed.
23 Qwiic KeyPad records any button presses to a stack. It can remember up to
24 15 button presses. The master I2C device (for example, an Uno) can ask for
25 the oldest button pressed. If the master continues to read in button presses,
26 it will receive the entire stack (from oldest to newest). This is handy if
27 you need to go and do something else with your code, you can then come back
28 to the keypad and pull in the last 15 button presses.
29"""
30import sys
31from time import sleep
32import board
33import sparkfun_qwiickeypad
34
35# Create bus object using our board's I2C port
36i2c = board.I2C()
37
38# Create relay object
39keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c)
40
41print("Qwicc Keypad Example 2 Read With Time")
42
43# Check if connected
44if keypad.connected:
45    print("Keypad connected. Firmware: ", keypad.version)
46else:
47    print("Keypad does not appear to be connected. Please check wiring.")
48    sys.exit()
49
50print("Type Ctrl-C to exit program.")
51print("Press a button.")
52
53
54try:
55    while True:
56        # request the next key pressed
57        keypad.update_fifo()
58        button = keypad.button
59        duration = keypad.time_since_pressed
60
61        if button == -1:
62            print("Keypad error. Try again.")
63            sleep(1)
64        elif button == 0:
65            print("No button has been pressed.")
66            sleep(1)
67        else:
68            print(
69                "Button '"
70                + chr(button)
71                + "' was pressed "
72                + str(duration)
73                + " milliseconds ago."
74            )
75
76        sleep(0.250)
77
78except KeyboardInterrupt:
79    pass
  1. Change I2C Address - Change the device I2C address.

examples/example3_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 Keypad.
 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/15290
 9
10"""
11  Qwiic Keypad Example 3 - example3_change_i2c_address.py
12  Written by Gaston Williams, July 1st, 2019
13  Based on Arduino code written by
14  Kevin Kuwata @ SparkX, March 21, 2019
15  The Qwiic Keypad is an I2C controlled 12-button keypad from Sparkfun.
16
17  Example 3 - Change I2C Address:
18  This program uses the Qwiic Keypad CircuitPython Library to change
19  the I2C address for the device. You enter in the DEC value (8-119) or
20  HEX value (0x08-0x77) for the new Keypad address.  After the i2c address
21  is changed, you can run the example4_i2c_scanner.py program to validate
22  the i2c address.
23
24  Syntax: python3 change_i2c_address.py [address]
25    where address is an optional current address value in decimal or hex
26
27    The default value for the address is 75 [0x4B]
28"""
29
30import sys
31import board
32import sparkfun_qwiickeypad
33
34# The default QwiicKeypad i2c address is 0x4B (75)
35i2c_address = 0x4B
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()
56keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c, i2c_address)
57
58if keypad.connected:
59    print("Qwiic Keypad Example.")
60else:
61    # if we can't connecct, something is wrong so just quit
62    print("Keypad does not appear to be connected. Please check wiring.")
63    sys.exit()
64
65print("Address: " + str(i2c_address) + " [" + hex(i2c_address) + "]")
66
67text = input(
68    "Enter a new I2C address (as a decimal from 8 to 119 or hex 0x08 to 0x77):"
69)
70
71# check to see if hex or decimal value
72if "0x" in text:
73    new_address = int(text, 16)
74else:
75    new_address = int(text)
76
77print("Changing address to " + str(new_address) + " [" + hex(new_address) + "]")
78
79result = keypad.set_i2c_address(new_address)
80
81if result:
82    print("Address changed to " + str(new_address) + " [" + hex(new_address) + "]")
83    # After the change check the new connection and show firmware version
84    if keypad.connected:
85        print("Connected to Keypad after address change.")
86    else:
87        print("Error after address change. Cannot connect to Keypad.")
88
89else:
90    print("Address change failed.")
91
92# good advice whether the address changed worked or not
93print("Run example4_i2c_scanner.py to verify the Qwiic Keypad address.")
  1. I2C Scanner - Scan the IC2 bus for devices.

examples/example4_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 Keypad.
 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/15290
 9
10"""
11  Qwiic Keypad Example 4 - example4_i2c_Scanner.py
12  Written by Gaston Williams, July 1st, 2019
13  The Qwiic Keypad is an I2C controlled 12-button Keypad from Sparkfun.
14
15  Example 4 - I2C Scanner
16  This progam uses CircuitPython BusIO library to find the current
17  address of the Qwiic Keypad. 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 0x4B.
21"""
22import time
23import board
24
25i2c = board.I2C()
26
27while not i2c.try_lock():
28    pass
29
30print("Press Ctrl-C to exit program")
31
32try:
33    while True:
34        print(
35            "I2C addresses found:",
36            [hex(device_address) for device_address in i2c.scan()],
37        )
38        time.sleep(5)
39except KeyboardInterrupt:
40    pass
41
42finally:
43    i2c.unlock()
  1. Interrupt Read - Show the button pressed when interrupt pin goes low.

examples/example5_interrupt_read.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 Keypad.
 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/15290
 9
10"""
11 Qwiic Keypad Example 5 - example5_interrupt_read.py
12 Written by Gaston Williams, July 2nd, 2019
13 Based on Arduino code written by
14 Nathan Seidle @ Sparkfun, January 21st, 2018
15 And updated by
16 Pete Lewis @ Sparkfun, March 21st, 2019
17 The Qwiic Keypad is an I2C controlled 12-Button Keypad produced by Sparkfun.
18
19 Example 5 - Interrupt Read:
20 This program uses the Qwiic Keypad CircuitPython Library to
21 control the Qwiic Keypad over I2C to enable the button interrupts.
22 For this example you will need to  connect the INT pin on Qwiic to
23 GPIO D6 on the Raspberry Pi.
24
25 CircuitPython does not support callback functions for interrupts,
26 so this example polls the interrupt line instead.
27"""
28import sys
29import board
30import digitalio
31import sparkfun_qwiickeypad
32
33# Create bus object using our board's I2C port
34i2c = board.I2C()
35
36# Set up Interrupt pin on GPIO D6 with a pull-up resistor
37keypad_interrupt_pin = digitalio.DigitalInOut(board.D6)
38keypad_interrupt_pin.direction = digitalio.Direction.INPUT
39keypad_interrupt_pin.pull = digitalio.Pull.UP
40
41# Create keypad object
42keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c)
43
44print("Qwicc Keypad Example 5 Interrupt Read")
45
46# Check if connected
47if keypad.connected:
48    print("Keypad connected. Firmware: ", keypad.version)
49else:
50    print("Keypad does not appear to be connected. Please check wiring.")
51    sys.exit()
52
53print("Press a button on the keypad and it will print here.")
54print("Type Ctrl-C to exit program.")
55
56try:
57    while True:
58        # When the interrupt pin goes low
59        if not keypad_interrupt_pin.value:
60            keypad.update_fifo()
61            button = keypad.button
62            # Display the button value
63            if button > 0:
64                print("Button '" + chr(button) + "' was pressed.")
65
66
67except KeyboardInterrupt:
68    pass
  1. Key Code - Prompt user to input the correct 4 digit code.

examples/example6_key_code.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 Keypad.
 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/15290
 9
10"""
11 Qwiic Keypad Example 6 - example6_key_code.py
12 Written by Gaston Williams, July 1st, 2019
13 Based on Arduino code written by
14 Nathan Seidle @ Sparkfun, January 21, 2018
15 and updated by
16 Pete Lewis @ Sparkfun, March 16, 2019
17
18 The Qwiic Keypad is an I2C controlled 12-button keypad produced by Sparkfun.
19
20 Example 6 - Key Code:
21 This program uses the Qwiic Keypad CircuitPython Library to control the Qwiic
22 Keypad over I2C and waits for the user to input the correct 4 digit keycode.
23 It then responds with a nice message on serial if they get it right.
24
25 Qwiic KeyPad records any button presses to a stack. It can remember up to
26 15 button presses. The master I2C device (for example, an Uno) can ask for
27 the oldest button pressed. If the master continues to read in button presses,
28 it will receive the entire stack (from oldest to newest). This is handy if
29 you need to go and do something else with your code, you can then come back
30 to the keypad and pull in the last 15 button presses.
31"""
32import sys
33from time import sleep
34import board
35import sparkfun_qwiickeypad
36
37# List with secret keycode (Hey, that's the same code as my luggage!)
38code = ["1", "2", "3", "4"]
39
40# List for user input
41guess = []
42
43# Number of tries
44count = 0
45
46#
47# Create bus object using our board's I2C port
48i2c = board.I2C()
49
50# Create relay object
51keypad = sparkfun_qwiickeypad.Sparkfun_QwiicKeypad(i2c)
52
53print("Qwicc Keypad Example 6 Key Code")
54
55# Check if connected
56if keypad.connected:
57    print("Keypad connected. Firmware: ", keypad.version)
58else:
59    print("Keypad does not appear to be connected. Please check wiring.")
60    sys.exit()
61
62print("Type the correct Keycode or Ctrl-C to exit program.")
63print("Please type in the correct 4 digit Keycode.")
64
65
66try:
67    while True:
68        # request the next key pressed
69        keypad.update_fifo()
70        button = keypad.button
71
72        if button > 0:
73            guess.append(chr(button))
74
75        if len(guess) == 4:
76            count += 1
77            if guess == code:
78                print("Keycode correct. Wahooooooooooo!")
79                break
80            else:
81                print("Sorry, please try again.")
82                guess.clear()
83                print("Please type in the correct 4 digit Keycode.")
84                if count > 2:
85                    print("Hint: try", code)
86
87        sleep(0.250)
88
89
90except KeyboardInterrupt:
91    pass