-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServeAnimation.py
More file actions
195 lines (160 loc) · 6.58 KB
/
ServeAnimation.py
File metadata and controls
195 lines (160 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# Copyright (c) 2020-2037 duxman.
#
# This file is part of Duxman Luces
# (see https://github.com/duxman/luces).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Credits to Andrew Oakley www.aoakley.com
# Thanks a lot for the inspiration of this program
# By parameter takes resolution and image and create animation
import getopt
import json
import time, sys, os
import paho.mqtt.client as mqtt
from config import configurationLedMatrix, animation, calculateMatrix
from Util.LedPanelMessage import led, display
MAX_PACKET = 1024
class ServeAnimation():
Logger = None
im = None
txtlines = []
myMatrix = []
token = ""
clienteMqtt: mqtt.Client = None
ani: animation
_protocol = mqtt.MQTTv311
def __init__(self, test=False):
self.clienteMqtt = mqtt.Client("ServeAnimation", True)
if test == False:
self.Config = configurationLedMatrix()
for mtx in self.Config.Matrix:
self.initializeMQTT(mtx.MQTT_HOST, int(mtx.MQTT_PORT), mtx.MQTT_TOKEN)
for anim in mtx.Animations:
anim.panelsV = mtx.VerticalPanels
anim.panelsH= mtx.HorizontalPanels
self.startAnimation( anim,mtx.myMatrix)
def initializeMQTT(self, host, port, token):
self.token = token
self.clienteMqtt.on_connect = self.on_connect
self.clienteMqtt.on_message = self.on_message
self.clienteMqtt.on_publish = self.on_publish
self.clienteMqtt.connect(host, port, 15)
self.clienteMqtt.loop_start()
def on_connect(self, mqttc, obj, flags, rc):
print("rc: " + str(rc))
mqttc.subscribe(self.token)
def decodeMsg(self, msg):
print("New frame")
#ledpanel = display()
#ledpanel.ParseFromString(msg.payload)
#for l in ledpanel.frame:
# print("Pin {}, Color {}".format(l.Pin, l.Color))
def on_message(self, mqttc, obj, msg):
#print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
self.decodeMsg(msg)
def on_publish(self, mqttc, obj, mid):
print("mid: " + str(mid))
def allonecolour(self, strip, colour):
# Paint the entire matrix one colour
strip.fill(colour)
strip.show()
def colour(self, r, g, b):
# Fix for Neopixel RGB->GRB, also British spelling
return self.colourTuple([g, r, b])
def colourTuple(self, rgbTuple):
red = rgbTuple[0]
green = rgbTuple[1]
blue = rgbTuple[2]
RGBint = (green << 16) + (red << 8) + blue
return RGBint
def startAnimation(self, Anim: animation, myMatrix):
# And here we go.
try:
rep = Anim.Repetitions
if Anim.Repetitions < 0:
rep = sys.maxsize
for i in range(rep):
# Loop through the image widthways
# Can't use a for loop because Python is dumb
# and won't jump values for FLIP command
x = 0
# Initialise a pointer for the current line in the text file
tx = 0
while x < Anim.image.size[0] - Anim.width:
# Set the sleep period for this frame
# This might get changed by a textfile command
thissleep = Anim.Speed
# Set the increment for this frame
# Typically advance 1 pixel at a time but
# the FLIP command can change this
thisincrement = 1
rg = Anim.image.crop((x, 0, x + Anim.width * Anim.panelsH, Anim.height * Anim.panelsV))
dots = list(rg.getdata())
cont_paquete = 0
ledpixel = led()
ledpanel = display()
ledpanel.Ini = True
for i in range(len(dots)):
id = myMatrix[i]
if self.colourTuple(dots[i]) != 0:
if cont_paquete >= MAX_PACKET:
self.clienteMqtt.publish("InData", ledpanel.SerializeToString(), 2, False)
ledpanel = display()
ledpanel.Ini = False
cont_paquete = 0
ledpixel.Pin = id
ledpixel.Color = self.colourTuple(dots[i])
ledpanel.frame.append(ledpixel)
cont_paquete = cont_paquete + 1
ledpanel.Fin = True
self.clienteMqtt.publish("InData", ledpanel.SerializeToString(), 2, False)
x = x + thisincrement
time.sleep(thissleep)
except (KeyboardInterrupt, SystemExit):
print("Stopped")
def main(argv):
inputfile = ""
host = ""
port = ""
token = ""
##"{\"ImageFile\": \"prueba.png\", \"CommandFile\": \"\", \"width\": 12, \"heigth\": 8, \"Repetitions\": 5, \"Speed\": 0.1 }"
##{"ImageFile": "prueba.png", "CommandFile": "", "width": 12, "heigth": 8, "Repetitions": 5, "Speed": 0.1 }
try:
opts, args = getopt.getopt(argv, "a:h:p:t:", ["AnimationData=", "host=", "port=", "token="])
except getopt.GetoptError:
print('LedMatrixAnimation.py -a <json animation data>')
sys.exit(2)
for opt, arg in opts:
if opt in ("-a", "--AnimationData"):
inputfile = arg
elif opt in ("-h", "--host"):
host = arg
elif opt in ("-p", "--port"):
port = arg
elif opt in ("-t", "--token"):
token = arg
if (host != ""):
ani = animation(inputfile)
ani.panelsV=1
ani.panelsH=2
myMatrix = calculateMatrix(ani.height, ani.width,"LEFT",1,2)
ma = ServeAnimation(True)
ma.initializeMQTT(host, int(port), token)
ma.startAnimation(ani, myMatrix)
else:
ma = ServeAnimation(False)
if __name__ == "__main__":
main(sys.argv[1:])