Last active
May 19, 2020 18:12
-
-
Save Kartoffel/7c31d3daace1c01abf03aa0bcdcf716c to your computer and use it in GitHub Desktop.
Desktop viewer for https://revspace.nl/M5stack_Atom_Matrix#Matrixflut
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
#!/usr/bin/python3 | |
import paho.mqtt.client as mqtt | |
from sty import fg, bg, rs | |
width = height = 5 | |
mqtt_server = "test.mosquitto.org" | |
mqtt_topic = "matrixflut" | |
def on_connect(client, userdata, flags, rc): | |
print("Connected (rc={})".format(rc)) | |
client.subscribe(mqtt_topic) | |
def on_subscribe(client, userdata, mid, granted_qos): | |
print('Subscribed') | |
def on_message(client, userdata, msg): | |
values = list(msg.payload) | |
# "Validation" of data | |
if len(values) != (width * height * 3): | |
return print("Invalid payload ({} bytes)".format(len(values))) | |
display = bg(0,0,0) | |
for i in range(width * height): | |
ii = i * 3 | |
if (ii % (width * 3)) == 0: | |
display += rs.bg + '\n' + bg(0,0,0) | |
rgb = values[ii:(ii+3)] | |
if max(rgb) > 255: | |
return print("Invalid RGB value") | |
display += fg(*rgb) + '■ ' | |
display += rs.fg + rs.bg | |
print(display) | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_subscribe = on_subscribe | |
client.on_message = on_message | |
client.connect(mqtt_server) | |
client.loop_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment