From de65ee3f51352d7c4e8e5f05c9ae7156675f9155 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Tue, 18 May 2021 15:16:15 +0100 Subject: [PATCH] Decorator key test example This example allows you to test each key and LED in turn. 1. At startup all LEDs should be white 2. Press a key and it will turn blue 3. Release that key and it will turn white 4. *Hold* a key and it will turn red 5. Release a *held* key and it will turn green If you can turn all your keys blue -> red -> green, they're good! --- examples/decorator-key-test.py | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/decorator-key-test.py diff --git a/examples/decorator-key-test.py b/examples/decorator-key-test.py new file mode 100644 index 0000000..f52bcff --- /dev/null +++ b/examples/decorator-key-test.py @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: 2021 Philip Howard +# +# SPDX-License-Identifier: MIT + +# This example allows you to test each key and LED in turn +# 1. At startup all LEDs should be white +# 2. Press a key and it will turn blue +# 3. Release that key and it will turn white +# 4. *Hold* a key and it will turn red +# 5. Release a *held* key and it will turn green. +# If you can turn all your keys blue -> red -> green, they're good! + +# Drop the keybow2040.py file into your `lib` folder on your `CIRCUITPY` drive. + +import board +from keybow2040 import Keybow2040 +import time + +i2c = board.I2C() +keybow = Keybow2040(i2c) +keys = keybow.keys + +keybow.set_all(64, 64, 64) + +for key in keys: + @keybow.on_press(key) + def press_handler(key): + print("Key {} pressed".format(key.number)) + key.set_led(0, 0, 255) + + @keybow.on_release(key) + def release_handler(key): + print("Key {} released".format(key.number)) + if key.rgb == [255, 0, 0]: + key.set_led(0, 255, 0) + else: + key.set_led(64, 64, 64) + + @keybow.on_hold(key) + def hold_handler(key): + print("Key {} held".format(key.number)) + key.set_led(255, 0, 0) + +while True: + keybow.update() + time.sleep(1.0 / 60) \ No newline at end of file