CiscoTheProot/rpi/roy-test.py

78 lines
1.9 KiB
Python
Raw Normal View History

from rgbmatrix import RGBMatrix, RGBMatrixOptions
import paho.mqtt.client as mqtt
import time
2023-05-18 10:49:13 +02:00
from PIL import Image
# Configuration for the matrix
options = RGBMatrixOptions()
options.rows = 32
options.cols = 64
options.chain_length = 2
options.parallel = 1
options.hardware_mapping = 'regular' # If you have an Adafruit HAT: 'adafruit-hat'
matrix = RGBMatrix(options = options)
# Load image and make sure it fits our screen.
2023-05-18 10:49:13 +02:00
image = Image.open("../testImg.bmp")
image.thumbnail((matrix.width, matrix.height), Image.ANTIALIAS)
# offscreen canvas that can be written to which can then be set to the matrix async
offscreen_canvas = matrix.CreateFrameCanvas()
offscreen_canvas.brightness = 50
offscreen__text_canvas = matrix.CreateFrameCanvas()
offscreen__text_canvas.brightness = 50
offscreen__text_canvas.SetImage(image.convert('RGB'))
2023-05-18 10:49:13 +02:00
def updateScreen():
2023-05-18 10:52:04 +02:00
global color, matrix, offscreen_canvas
nextCanvas = offscreen_canvas
2023-05-18 10:49:13 +02:00
if(color == 1):
offscreen_canvas.Fill(255,255,255)
nextCanvas = offscreen_canvas
2023-05-18 10:49:13 +02:00
elif(color == 0):
offscreen_canvas.Fill(255,0,0)
nextCanvas = offscreen_canvas
2023-05-18 10:49:13 +02:00
elif(color == 2):
nextCanvas = offscreen__text_canvas
2023-05-18 10:49:13 +02:00
nextCanvas = matrix.SwapOnVSync(nextCanvas)
2023-05-18 10:49:13 +02:00
color = 0
def toggleColor():
global color
2023-05-18 10:49:13 +02:00
color += 1
color %= 3
updateScreen()
2023-05-18 10:49:13 +02:00
# functions called by the MQTT listener
def on_connect(client, userdata, flags, responseCode):
print("Connected to MQTT broker with result code " + str(responseCode))
client.subscribe("test")
2023-05-18 10:49:13 +02:00
def on_message(client, userdata, message):
print("Received message '" + str(message.payload) + "' on topic '"
+ message.topic + "' with QoS " + str(message.qos))
toggleColor()
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("10.1.13.173", 1883, 60) # Replace with your MQTT broker's address
client.loop_start()
2023-05-18 10:49:13 +02:00
while True:
time.sleep(0.005)
2023-05-18 10:49:13 +02:00