53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
|
from samplebase import SampleBase
|
||
|
from rgbmatrix import RGBMatrix, RGBMatrixOptions
|
||
|
import paho.mqtt.client as mqtt
|
||
|
|
||
|
class PulsingColors(SampleBase):
|
||
|
color = 0
|
||
|
def __init__(self, *args, **kwargs):
|
||
|
super(PulsingColors, self).__init__(*args, **kwargs)
|
||
|
|
||
|
def run(self):
|
||
|
self.offscreen_canvas = self.matrix.CreateFrameCanvas()
|
||
|
continuum = 0
|
||
|
|
||
|
while True:
|
||
|
self.usleep(5 * 1000)
|
||
|
|
||
|
if(self.color == 1):
|
||
|
self.offscreen_canvas.Fill(255,255,255)
|
||
|
else:
|
||
|
self.offscreen_canvas_Fill(255,0,0)
|
||
|
self.offscreen_canvas.brightness = 100
|
||
|
self.offscreen_canvas = self.matrix.SwapOnVSync(self.offscreen_canvas)
|
||
|
|
||
|
def toggleColor(self):
|
||
|
if(self.color == 1):
|
||
|
self.color = 0
|
||
|
else:
|
||
|
self.color = 1
|
||
|
|
||
|
def on_connect(client, userdata, flags, rc):
|
||
|
print("Connected to MQTT broker with result code " + str(rc))
|
||
|
client.subscribe("test")
|
||
|
|
||
|
def on_message(client, userdata, msg):
|
||
|
print("Received message: " + str(msg.payload.decode("utf-8")))
|
||
|
pulsing_colors.toggleColor()
|
||
|
pulsing_colors.run()
|
||
|
|
||
|
pulsing_colors = PulsingColors()
|
||
|
|
||
|
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_forever()
|
||
|
|
||
|
# Main function
|
||
|
if __name__ == "__main__":
|
||
|
if (not pulsing_colors.process()):
|
||
|
pulsing_colors.print_help()
|