CiscoTheProot/rpi/roy-test.py

86 lines
2.2 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'
2023-05-21 22:56:20 +02:00
matrix = RGBMatrix(options=options)
# Load image and make sure it fits our screen.
def pure_pil_alpha_to_color_v2(image, color=(0, 0, 0)):
image.load() # needed for split()
background = Image.new('RGB', image.size, color)
background.paste(image, mask=image.split()[3]) # 3 is the alpha channel
return background
image = Image.open("../prootface.bmp")
2023-05-21 22:56:20 +02:00
image.thumbnail((128, 62), Image.ANTIALIAS)
2023-05-18 11:39:16 +02:00
RGBImage = pure_pil_alpha_to_color_v2(image)
2023-05-21 22:56:20 +02:00
# offscreen canvas that can be written to and then set to the matrix asynchronously
offscreen_canvas = matrix.CreateFrameCanvas()
offscreen_canvas.brightness = 50
2023-05-21 22:56:20 +02:00
offscreen_text_canvas = matrix.CreateFrameCanvas()
offscreen_text_canvas.brightness = 50
offscreen_text_canvas.SetImage(RGBImage)
2023-05-18 10:49:13 +02:00
2023-05-21 22:56:20 +02:00
def update_screen():
2023-05-18 10:52:04 +02:00
global color, matrix, offscreen_canvas
2023-05-21 22:56:20 +02:00
next_canvas = offscreen_canvas
if color == 1:
next_canvas.Fill(255, 255, 255)
elif color == 0:
next_canvas.Fill(255, 0, 0)
elif color == 2:
next_canvas = offscreen_text_canvas
next_canvas = matrix.SwapOnVSync(next_canvas)
2023-05-18 10:49:13 +02:00
color = 0
2023-05-21 22:56:20 +02:00
def toggle_color():
global color
2023-05-18 10:49:13 +02:00
color += 1
color %= 3
2023-05-21 22:56:20 +02:00
update_screen()
2023-05-18 10:49:13 +02:00
# functions called by the MQTT listener
2023-05-21 22:56:20 +02:00
def on_connect(client, userdata, flags, response_code):
print("Connected to MQTT broker with result code " + str(response_code))
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 '"
2023-05-21 22:56:20 +02:00
+ message.topic + "' with QoS " + str(message.qos))
toggle_color()
2023-05-21 22:56:20 +02:00
# MQTT broker configuration
broker_address = "10.1.13.173" # Replace with your MQTT broker's address
broker_port = 1883
broker_keepalive = 60
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
2023-05-21 22:56:20 +02:00
client.connect(broker_address, broker_port, broker_keepalive)
client.loop_start()
2023-05-18 10:49:13 +02:00
while True:
2023-05-21 22:56:20 +02:00
time.sleep(0.005)