added blink testing script

This commit is contained in:
CiscoTheWolf 2023-05-21 22:56:20 +02:00
parent 81339b159f
commit 912ffd8e5d
9 changed files with 196 additions and 43 deletions

View file

@ -3,4 +3,43 @@ test message
For using the: RGB-Matrix-P3-64x32 by Wavshare with driver chips: FM6047
In order to set up the pi in such a way that the RGB LED matrixes can operate at their rated brightness a init script from the screen manuafacturer needs to be ran. A guide on how to is on the following webpage: https://www.waveshare.com/wiki/RGB--Matrix-P3-64x32-Reginit
In order to set up the pi in such a way that the RGB LED matrixes can operate at their rated brightness a init script from the screen manuafacturer needs to be ran. A guide on how to is on the following webpage: https://www.waveshare.com/wiki/RGB--Matrix-P3-64x32-Reginit
RGB Matrix Display with MQTT Control (roy-test.py)
This script uses the RGBMatrix library to control an RGB matrix display. It also connects to an MQTT broker to receive messages and toggle the display color.
Requirements
Python 3.x
rgbmatrix library
paho.mqtt.client library
PIL library
Installation
Clone or download the script.
Install the required libraries by running the following command:
pip install rgbmatrix paho-mqtt Pillow
Usage
Connect your RGB matrix display to your device.
Update the configuration options in the script to match your display specifications (rows, columns, chain length, etc.).
Modify the MQTT broker address in the client.connect line to match your MQTT broker's address.
Run the script using the following command:
python rgb_matrix_mqtt.py
Description
This script sets up an RGB matrix display using the RGBMatrix library. It also creates an offscreen canvas that can be written to and then displayed on the matrix asynchronously.
The script connects to an MQTT broker using the paho.mqtt.client library. It subscribes to the "test" topic and listens for incoming messages. When a message is received, it toggles the display color between white, red, and an image loaded from a file.
The image is loaded using the PIL library and resized to fit the matrix display. The pure_pil_alpha_to_color_v2 function converts an RGBA image to RGB format by applying an alpha composite with a specified color.
To use this script, you need to provide the appropriate configuration for your RGB matrix display, including the number of rows, columns, chain length, and hardware mapping. You also need to specify the MQTT broker's address to connect to.
License

View file

Before

(image error) Size: 16 KiB

After

(image error) Size: 16 KiB

BIN
faces/prootface2.bmp Normal file

Binary file not shown.

After

(image error) Size: 16 KiB

BIN
faces/prootface3.bmp Normal file

Binary file not shown.

After

(image error) Size: 16 KiB

BIN
faces/prootface4.bmp Normal file

Binary file not shown.

After

(image error) Size: 16 KiB

View file

Before

(image error) Size: 16 KiB

After

(image error) Size: 16 KiB

112
rpi/blinkingTest.py Normal file
View file

@ -0,0 +1,112 @@
from rgbmatrix import RGBMatrix, RGBMatrixOptions
import paho.mqtt.client as mqtt
import time
from PIL import Image
import numpy as np
# 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)
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
def image_interpolation(image1, image2, alpha):
# Ensure both images have the same size
assert image1.shape == image2.shape, "Input images must have the same shape"
# Normalize the alpha value
alpha = max(0, min(1, alpha))
# Perform interpolation
interpolated_image = np.uint8((1 - alpha) * image1 + alpha * image2)
return interpolated_image
image = Image.open("ciscoTheProot/faces/prootface1.bmp")
image.thumbnail((128, 32), Image.ANTIALIAS)
RGBImage = pure_pil_alpha_to_color_v2(image)
noBlinkImage = np.array(RGBImage, dtype=np.uint8)
image = Image.open("ciscoTheProot/faces/prootface2.bmp")
image.thumbnail((128, 32), Image.ANTIALIAS)
RGBImage = pure_pil_alpha_to_color_v2(image)
fullBlinkImage = np.array(RGBImage, dtype=np.uint8)
DesiredBlinkState = 0
currentBlinkState = 0
blinkFrameCanvases = []
for alpha in range(0,1,(1/10)):
offscreen_interpolated_canvas = matrix.CreateFrameCanvas()
interpolated_image = image_interpolation(noBlinkImage, fullBlinkImage, alpha)
blinkFrameCanvases[alpha*10] = offscreen_interpolated_canvas.SetImage(interpolated_image)
# offscreen canvas that can be written to and then set to the matrix asynchronously
offscreen_canvas = matrix.CreateFrameCanvas()
offscreen_canvas.brightness = 50
offscreen_text_canvas = matrix.CreateFrameCanvas()
offscreen_text_canvas.brightness = 50
offscreen_text_canvas.SetImage(noBlinkImage)
def update_screen():
global DesiredBlinkState, currentBlinkState, blinkFrameCanvases, matrix, offscreen_canvas
# open eye again after blink
if currentBlinkState == 10:
DesiredBlinkState = 0
if currentBlinkState == DesiredBlinkState:
return
if currentBlinkState < DesiredBlinkState:
currentBlinkState =+ 1
else:
currentBlinkState =- 1
next_canvas = blinkFrameCanvases[currentBlinkState/10]
next_canvas = matrix.SwapOnVSync(next_canvas)
# functions called by the MQTT listener
def on_connect(client, userdata, flags, response_code):
print("Connected to MQTT broker with result code " + str(response_code))
client.subscribe("test")
def on_message(client, userdata, message):
print("Received message '" + str(message.payload) + "' on topic '"
+ message.topic + "' with QoS " + str(message.qos))
global DesiredBlinkState
DesiredBlinkState = 10
# 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
client.connect(broker_address, broker_port, broker_keepalive)
client.loop_start()
while True:
time.sleep(0.05)
update_screen()

View file

@ -11,88 +11,76 @@ 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)
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)):
"""Alpha composite an RGBA Image with a specified color.
Simpler, faster version than the solutions above.
Source: http://stackoverflow.com/a/9459208/284318
Keyword Arguments:
image -- PIL RGBA Image object
color -- Tuple r, g, b (default 255, 255, 255)
"""
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")
image.thumbnail((128,62), Image.ANTIALIAS)
image.thumbnail((128, 62), Image.ANTIALIAS)
RGBImage = pure_pil_alpha_to_color_v2(image)
# offscreen canvas that can be written to which can then be set to the matrix async
# offscreen canvas that can be written to and then set to the matrix asynchronously
offscreen_canvas = matrix.CreateFrameCanvas()
offscreen_canvas.brightness = 50
offscreen__text_canvas = matrix.CreateFrameCanvas()
offscreen__text_canvas.brightness = 50
offscreen__text_canvas.SetImage(RGBImage)
offscreen_text_canvas = matrix.CreateFrameCanvas()
offscreen_text_canvas.brightness = 50
offscreen_text_canvas.SetImage(RGBImage)
def updateScreen():
def update_screen():
global color, matrix, offscreen_canvas
nextCanvas = offscreen_canvas
if(color == 1):
offscreen_canvas.Fill(255,255,255)
nextCanvas = offscreen_canvas
elif(color == 0):
offscreen_canvas.Fill(255,0,0)
nextCanvas = offscreen_canvas
elif(color == 2):
nextCanvas = offscreen__text_canvas
nextCanvas = matrix.SwapOnVSync(nextCanvas)
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)
color = 0
def toggleColor():
def toggle_color():
global color
color += 1
color %= 3
updateScreen()
update_screen()
# functions called by the MQTT listener
def on_connect(client, userdata, flags, responseCode):
print("Connected to MQTT broker with result code " + str(responseCode))
def on_connect(client, userdata, flags, response_code):
print("Connected to MQTT broker with result code " + str(response_code))
client.subscribe("test")
def on_message(client, userdata, message):
print("Received message '" + str(message.payload) + "' on topic '"
+ message.topic + "' with QoS " + str(message.qos))
toggleColor()
+ message.topic + "' with QoS " + str(message.qos))
toggle_color()
# 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
client.connect("10.1.13.173", 1883, 60) # Replace with your MQTT broker's address
client.connect(broker_address, broker_port, broker_keepalive)
client.loop_start()
while True:
time.sleep(0.005)
time.sleep(0.005)

View file

@ -0,0 +1,14 @@
from PIL import Image
import numpy as np
svg_file = 'CiscoTheProot/testImg.svg'
from xml.dom import minidom
doc = minidom.parse(svg_file) # parseString also exists
path_strings = [path.getAttribute('d') for path
in doc.getElementsByTagName('path')]
doc.unlink()
print("hello")