Skip to content

Commit

Permalink
Added cheerlights example and closed request
Browse files Browse the repository at this point in the history
  • Loading branch information
ZodiusInfuser committed Oct 10, 2024
1 parent 7326553 commit 657baf1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/tiny_fx_w/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ For examples that show off the rest of the board's functions, refer to the regul

- [Wireless Examples](#wireless-examples)
- [Random](#random)
- [CheerLights](#cheerlights)


## Wireless Examples
Expand All @@ -19,4 +20,8 @@ Show the state of TinyFX's Boot button on its RGB output.
Show random colours and patterns obtained from the internet on TinyFX's outputs.


### CheerLights
[wireless/cheerlights.py](examples/wireless/cheerlights.py)

Obtain the current CheerLights colour from the internet and show it on TinyFX's RGB output.
For more information about CheerLights, visit: [https://cheerlights.com/](https://cheerlights.com/)
68 changes: 68 additions & 0 deletions examples/tiny_fx_w/examples/wireless/cheerlights.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import time
import network
import requests
from tiny_fx import TinyFX

"""
Obtain the current CheerLights colour from the internet and show it on TinyFX's RGB output.
For more information about CheerLights, visit: https://cheerlights.com/
This example requires a secrets.py file to be on your board's file system with the credentials of your WiFi network.
Press "Boot" to exit the program.
"""

try:
from secrets import WIFI_SSID, WIFI_PASSWORD
except ImportError:
print("Create secrets.py with your WiFi credentials")
raise


# Constants
COLOUR_NAMES = ("R", "G", "B")
CONNECTION_INTERVAL = 1.0 # The time to sleep between each connection check
REQUEST_INTERVAL = 5.0 # The time to sleep between each internet request

# Variables
tiny = TinyFX() # Create a new TinyFX object to interact with the board
wlan = network.WLAN(network.STA_IF) # Create a new network object for interacting with WiFI


# Wrap the code in a try block, to catch any exceptions (including KeyboardInterrupt)
try:
# Connect to WLAN
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)

# Wait until the connection is established
while not wlan.isconnected():
print('Waiting for connection...')
time.sleep(CONNECTION_INTERVAL)

# Print out our IP address
print(f'Connected on {wlan.ifconfig()[0]}')

# Loop forever
while True:
# Get the current CheerLights colour from the internet
req = requests.get("http://api.thingspeak.com/channels/1417/field/2/last.json")
json = req.json()
req.close()

# Use the second to get the colour components for the RGB output
colour = tuple(int(json['field2'][i:i + 2], 16) for i in (1, 3, 5))

# Set the colour output, and print the values
tiny.rgb.set_rgb(*colour)
for i in range(len(colour)):
print(f"{COLOUR_NAMES[i]} = {colour[i]}", end=", ")

print()

time.sleep(REQUEST_INTERVAL)

# Turn off all the outputs
finally:
tiny.shutdown()
wlan.disconnect()
8 changes: 5 additions & 3 deletions examples/tiny_fx_w/examples/wireless/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
# Loop forever
while True:
# Get two colours from the internet
req = requests.get("https://random-flat-colors.vercel.app/api/random?count=2").json()
req = requests.get("https://random-flat-colors.vercel.app/api/random?count=2")
json = req.json()
req.close()

# Use the first to get brightness values for the six mono outputs
mono = tuple(int(req['colors'][0][i:i + 1], 16) / 15 for i in range(1, 7))
mono = tuple(int(json['colors'][0][i:i + 1], 16) / 15 for i in range(1, 7))

# Use the second to get the colour components for the RGB output
colour = tuple(int(req['colors'][1][i:i + 2], 16) for i in (1, 3, 5))
colour = tuple(int(json['colors'][1][i:i + 2], 16) for i in (1, 3, 5))

# Set the mono outputs, and print the values
for i in range(len(tiny.outputs)):
Expand Down

0 comments on commit 657baf1

Please sign in to comment.