Skip to content

Instantly share code, notes, and snippets.

@Lukse
Created January 8, 2017 16:47
Show Gist options
  • Save Lukse/ff4297ee1d7ac4c531e089c37c205462 to your computer and use it in GitHub Desktop.
Save Lukse/ff4297ee1d7ac4c531e089c37c205462 to your computer and use it in GitHub Desktop.
LED panel control over MQTT for Node-RED
# -*- coding: utf-8 -*-
import json
import paho.mqtt.client as paho
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import time
from rgbmatrix import RGBMatrix
#from samplebase import SampleBase
from rgbmatrix import graphics
matrix = RGBMatrix(32, 2, 1) # rows, chain, parallel
#canvas = matrix
#canvas2 = matrix.CreateFrameCanvas()
image = Image.new("RGB", (64, 32)) # Can be larger than matrix if wanted!!
draw = ImageDraw.Draw(image) # Declare Draw instance before prims
background = (0, 0, 0)
print '> loading fonts'
font_list = [
['Objective-Bold.ttf', 12],
['Objective-Bold.ttf', 14],
['Objective-Bold.ttf', 16],
['Objective-Bold.ttf', 18],
['Objective-Bold.ttf', 20],
['Objective-Bold.ttf', 22],
['Objective-Bold.ttf', 24],
['Objective-Bold.ttf', 25],
['Objective-Bold.ttf', 26],
['Objective-Bold.ttf', 28],
['Objective-Bold.ttf', 30],
['The Gould St.ttf', 20],
['gomarice_usuazi_hosomozi.ttf', 28],
['helvetica-normal.ttf', 10], # 13
['helvetica-normal.ttf', 12],
['helvetica-normal.ttf', 14],
['helvetica-normal.ttf', 16],
['helvetica-normal.ttf', 18],
['helvetica-normal.ttf', 20],
['helvetica-normal.ttf', 22], #18
]
fonts = []
for f in font_list:
print ' >', f[0], f[1]
fonts.append(ImageFont.truetype(f[0], f[1]))
print '> fonts loaded'
def on_message(clnt, userdata, msg):
global background
global matrix
j = json.loads(str(msg.payload))
if 'function' in j:
if j['function'] == 'set_text':
canvas = matrix.CreateFrameCanvas()
json_text = j['text']
json_red = int(j['red'])
json_green = int(j['green'])
json_blue = int(j['blue'])
json_font = int(j['font'])
json_x = int(j['x'])
json_y = int(j['y'])
font = fonts[json_font]
text_w, text_h = draw.textsize(json_text, font=font)
draw.rectangle((json_x, json_y, json_x+text_w, json_y+text_h), fill=background)
draw.text((json_x, json_y), json_text, font=font, fill=(json_red, json_green, json_blue))
canvas.SetImage(image, 0, 0)
canvas = matrix.SwapOnVSync(canvas)
if j['function'] == 'clear':
#print '> clear'
canvas = matrix.CreateFrameCanvas()
json_red = int(j['red'])
json_green = int(j['green'])
json_blue = int(j['blue'])
background = (json_red, json_green, json_blue)
draw.rectangle((0, 0, matrix.width, matrix.height), fill=background)
canvas.SetImage(image, 0, 0)
canvas = matrix.SwapOnVSync(canvas)
mqtt = paho.Client()
mqtt.on_message = on_message
mqtt.username_pw_set("admin", "admin") # credentials from Node-RED installation
mqtt.connect("192.168.0.68", 1883)
mqtt.subscribe("ledpanel/1")
print '> Ready'
mqtt.loop_forever()
@trlafleur
Copy link

trlafleur commented Dec 1, 2018

Interesting idea, have you expanded on this in the last few years???

where do the fonts come from?? where are they loaded on Linux??

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment