Created
March 26, 2018 13:32
-
-
Save Breakerz/2ade97a573d7362843c1aceac648db04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
from sys import exit | |
import unicornhat as uh | |
import paho.mqtt.client as mqtt | |
# set the layout | |
uh.set_layout(uh.PHAT) | |
# brightness | |
uh.brightness(0.5) | |
#number of transitions | |
steps = 20 | |
oldr = 0 | |
oldg = 0 | |
oldb = 0 | |
def lightitup(oldr, oldg, oldb, r, g, b): | |
curStep = 0 | |
# next we need to calculate the amount to increment (or decrement) | |
# for each individual color (R G B) from the old color to the new color in the number | |
# of steps (transistions) defined above | |
# ie: | |
# old R color value: 146 | |
# new R color value: 16 | |
# abs is used to have positive values | |
# stepa = abs (16 - 146) / 20 = 100 / 20 = 5 | |
# Which means, in each one of the 20 transistions, we will remove 5 from 146 - to arrive at 16 in 20 loops | |
# This way, we will have a kind of gradient effect when changing colors | |
stepa = abs(r - oldr) / steps | |
stepg = abs(g - oldg) / steps | |
stepb = abs(b - oldb) / steps | |
# This is ugly, but I don't know the terneary operator in Python. | |
# every attempt I've made did not work... | |
while (curStep < steps): | |
if (oldr > r): | |
oldr -= stepa | |
else: | |
oldr += stepa | |
if (oldg > g): | |
oldg -= stepg | |
else: | |
oldg += stepg | |
if (oldb > b): | |
oldb -= stepb | |
else: | |
oldb += stepb | |
for x in range(8): | |
for y in range(4): | |
uh.set_pixel(x, y, oldr, oldg, oldb) | |
uh.show() | |
time.sleep(0.01) | |
curStep += 1 | |
#borrowed from Pimoroni's Cheerlights from blink examples | |
def hex_to_rgb(col_hex): | |
"""Convert hex color to rgb""" | |
col_hex = col_hex.lstrip("#") | |
return bytearray.fromhex(col_hex) | |
#https://gist.github.com/ghostbitmeta/694934062c0814680d52 | |
# The callback for when the client receives a CONNACK response from the server. | |
def on_connect(client, userdata, flag, rc): | |
print("Connected with result code %s" % (str(rc))) | |
# Subscribing in on_connect() means that if we lose the connection and | |
# reconnect then subscriptions will be renewed. | |
client.subscribe("moodlight/#") | |
# The callback for when a PUBLISH message is received from the server. | |
def on_message(client, userdata, msg): | |
print("Topic: ", msg.topic+'\nMessage: '+str(msg.payload)) | |
{ | |
"moodlight/mood": set_mood, | |
"moodlight/turn": set_turn, | |
"moodlight/brighness": set_brigtness, | |
}.get(str(msg.topic), wrong_topic)(client, msg) | |
def wrong_topic(client, msg): | |
print(str(msg.topic)) | |
def set_brigtness(client, msg): | |
print("a coder") | |
def set_turn(client, msg): | |
print("a coder") | |
def set_mood(client, msg): | |
print("set_mood") | |
global oldr | |
global oldg | |
global oldb | |
r, g, b = hex_to_rgb(msg.payload) | |
# print ("old colors: %s %s %s" % (oldr,oldg,oldb)) | |
# print ("New colors: %s %s %s" % (r,g,b)) | |
lightitup(oldr, oldg, oldb, r, g, b) | |
# Wait 10s before another request to the API - be friendly | |
oldr = r | |
oldg = g | |
oldb = b | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.connect('192.168.0.123', 1883, 60) | |
# Blocking call that processes network traffic, dispatches callbacks and | |
# handles reconnecting. | |
# Other loop*() functions are available that give a threaded interface and a | |
# manual interface. | |
client.loop_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment