From 77222f7b7fec81119996453a7e2e92e2008e16c8 Mon Sep 17 00:00:00 2001 From: sandyjmacdonald Date: Wed, 10 Mar 2021 21:57:52 +0000 Subject: [PATCH] Adding a few basic examples. --- .DS_Store | Bin 0 -> 6148 bytes examples/decorators.py | 34 +++++++++++++++++++ examples/rainbow.py | 67 +++++++++++++++++++++++++++++++++++++ examples/reactive-press.py | 28 ++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 .DS_Store create mode 100644 examples/decorators.py create mode 100644 examples/rainbow.py create mode 100644 examples/reactive-press.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4cbce45ee165364fa7344a150993655ebed0cf73 GIT binary patch literal 6148 zcmeHKJ5Izf5S@W7B2lmE$emX}%{c~X0Evj6M88zvh*@ag)x-nRXg@eil3PtR{J=S#V|e)lcE;jV1HtT|ae)L9PZuyk literal 0 HcmV?d00001 diff --git a/examples/decorators.py b/examples/decorators.py new file mode 100644 index 0000000..78b3c8f --- /dev/null +++ b/examples/decorators.py @@ -0,0 +1,34 @@ +# SPDX-FileCopyrightText: 2021 Sandy Macdonald +# +# SPDX-License-Identifier: MIT + +# This example demonstrates attaching functions to keys using decorators, and +# the ability to turn the LEDs off with led_sleep_enabled and led_sleep_time. + +import time +import board +import random +from keybow2040 import Keybow2040 + +# Set up Keybow +i2c = board.I2C() +keybow = Keybow2040(i2c) +keys = keybow.keys + +# Enable LED sleep and set a time of 5 seconds before the LEDs turn off. +keybow.led_sleep_enabled = True +keybow.led_sleep_time = 5 + +# Loop through the keys and set the RGB colour for the keys to magenta. +for key in keys: + key.rgb = (255, 0, 255) + + # Attach a `on_hold` decorator to the key that toggles the key's LED when + # the key is held (the default hold time is 0.75 seconds). + @keybow.on_hold(key) + def hold_handler(key): + key.toggle_led() + +while True: + # Always remember to call keybow.update()! + keybow.update() diff --git a/examples/rainbow.py b/examples/rainbow.py new file mode 100644 index 0000000..461ab8d --- /dev/null +++ b/examples/rainbow.py @@ -0,0 +1,67 @@ +# SPDX-FileCopyrightText: 2021 Sandy Macdonald +# +# SPDX-License-Identifier: MIT + +# This example displays a rainbow animation on Keybow 2040's keys. + +import time +import math +import board +from keybow2040 import Keybow2040, number_to_xy + + +def hsv_to_rgb(h, s, v): + # Convert an HSV (0.0-1.0) colour to RGB (0-255) + if s == 0.0: + rgb = [v, v, v] + + i = int(h * 6.0) + + f = (h*6.)-i; p,q,t = v*(1.-s), v*(1.-s*f), v*(1.-s*(1.-f)); i%=6 + + if i == 0: + rgb = [v, t, p] + if i == 1: + rgb = [q, v, p] + if i == 2: + rgb = [p, v, t] + if i == 3: + rgb = [p, q, v] + if i == 4: + rgb = [t, p, v] + if i == 5: + rgb = [v, p, q] + + rgb = (int(c * 255) for c in rgb) + + return rgb + +# Set up Keybow +i2c = board.I2C() +keybow = Keybow2040(i2c) +keys = keybow.keys + +# Increment step to shift animation across keys. +step = 0 + +while True: + # Always remember to call keybow.update() on every iteration of your loop! + keybow.update() + + step += 1 + + for i in range(16): + # Convert the key number to an x/y coordinate to calculate the hue + # in a matrix style-y. + x, y = number_to_xy(i) + + # Calculate the hue. + hue = (x + y + (step / 20)) / 8 + hue = hue - int(hue) + hue = hue - math.floor(hue) + + # Convert the hue to RGB values. + r, g, b = hsv_to_rgb(hue, 1, 1) + + # Display it on the key! + keys[i].set_led(r, g, b) diff --git a/examples/reactive-press.py b/examples/reactive-press.py new file mode 100644 index 0000000..b1b15c6 --- /dev/null +++ b/examples/reactive-press.py @@ -0,0 +1,28 @@ +# SPDX-FileCopyrightText: 2021 Sandy Macdonald +# +# SPDX-License-Identifier: MIT + +# This example demonstrates how to light keys when pressed. + +import board +from keybow2040 import Keybow2040 + +# Set up Keybow +i2c = board.I2C() +keybow = Keybow2040(i2c) +keys = keybow.keys + +# Use cyan as the colour. +rgb = (0, 255, 255) + +while True: + # Always remember to call keybow.update() on every iteration of your loop! + keybow.update() + + # Loop through the keys and set the LED to cyan if pressed, otherwise turn + # it off (set it to black). + for key in keys: + if key.pressed: + key.set_led(*rgb) + else: + key.set_led(0, 0, 0) \ No newline at end of file