-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.lua
More file actions
1118 lines (981 loc) · 26.7 KB
/
init.lua
File metadata and controls
1118 lines (981 loc) · 26.7 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local socket = require("socket")
local class = require("middleclass")
local bit
if type(jit) == "table" then
bit = require("bit")
bxor = bit.bxor
bor = bit.bor
rshift = bit.rshift
lshift = bit.lshift
end
local status, brotli = pcall(require, "brotli")
if not status then
print("Can't load brotli")
end
local upack
local pack
local gsub = string.gsub
if love then
pack = function(format, ...)
format = gsub(format, "A", "z")
format = gsub(format, "b", "B")
format = gsub(format, "c", "b")
return love.data.pack("string", format, ...)
end
upack = function(datastring, format, pos) return 0, love.data.unpack(format, datastring, pos) end
else
local lpack = require("pack")
pack = string.pack
upack = string.unpack
end
local UDP_PORT = 6454
local MAX_UPDATE_SIZE = 1280 -- max 1280 after the driver explode == 426 RGB LEDs
local LEDS_BY_UNI = 170
local ART_POLL = 0x2000
local ART_REPLY = 0x2100
local ART_DMX = 0x5000
local ART_SYNC = 0x5200
local ARTNET_VERSION = 0x0E00
local ART_HEAD = love and "Art-Net" or "Art-Net\0"
local LED_RGB_888 = 0
local LED_RGB_888_UPDATE = 1
local LED_RGB_565 = 2
local LED_RGB_565_UPDATE = 3
local STOP = 11
-- local LED_RLE_888 = 11
-- local LED_RLE_888_UPDATE = 12
-- local LED_BRO_888 = 13
-- local LED_BRO_888_UPDATE = 14
local LED_Z_888 = 15
local LED_Z_888_UPDATE = 16
local LED_Z_565 = 17
local LED_Z_565_UPDATE = 18
local LED_UPDATE = 4
local GET_INFO = 5
local LED_TEST = 6
local LED_RGB_SET = 7
local LED_LERP = 8
local SET_MODE = 9
local REBOOT = 10
local LEDsController = class("LEDsController")
function LEDsController:initialize(t)
self.led_nb = t.led_nb or LEDS_BY_UNI
self.ip = t.ip
self.port = t.remote_port or UDP_PORT
self.debug = t.debug or false
self.count = 0
self.rgbw = t.rgbw
self.bright = t.bright or 1
self.rgbw_mode = t.rgbw_mode or 0
if t.protocol == "BRO888" and not brotli then
t.protocol = "RGB888"
end
if t.protocol == "Z888" and not love then
t.protocol = "RGB888"
end
self.protocols = {
artnet = self.sendArtnetDMX_ext,
artnet_big = self.sendAllArtnetDMX,
RGB888 = self.sendAll888,
RGB565 = self.sendAll565,
RLE888 = self.sendAllRLE888,
BRO888 = self.sendAllBRO888,
Z888 = self.sendAllZ888,
Z565 = self.sendAllZ565,
UDPX = self.sendAllUDPX,
UDPX565= self.sendAllUDPX565,
DRGB = self.sendDRGB,
DNRGB = self.sendAllDNRGB,
}
if t.udp then
self.udp = t.udp
else
self.udp = assert(socket.udp())
assert(self.udp:setsockname("0.0.0.0", 0))
self.udp:settimeout(0)
self.udp:setoption("broadcast", true)
end
self.protocol = self.protocols[t.protocol] or self.sendAllArtnetDMX
-- print(t.protocol, self.protocols.RLE888, self.protocol )
local size = t.led_nb * (self.rgbw and 4 or 3)
if size < 512 then
self.leds_by_uni = self.led_nb
else
self.leds_by_uni = t.leds_by_uni or math.floor(512 / (self.rgbw and 4 or 3))
end
self.leds = {}
for i=1, self.led_nb do self.leds[i] = {0,0,0,0} end
self.map = t.map
self.uni = t.uni or 0
self.net = t.net or 0
-- print("LEDs controller start using "..self.protocol.." protocol")
end
-------------------------------------------------------------------------------
function LEDsController:printD(...)
if self.debug then print(...) end
end
--------------------------------- ART-NET -------------------------------------
function LEDsController:sendArtnetDMX(net, sub_uni, off)
self:printD("ART-NET DMX:", net, sub_uni, off)
local to_send = pack(
"AHHbbbb>H",
ART_HEAD,
ART_DMX,
ARTNET_VERSION,
self.count%256,
0,
sub_uni,
net,
self.leds_by_uni*(self.rgbw and 4 or 3)
)
self.count = self.count + 1
local data = self.leds
local size = self.leds_by_uni
local mode = self.rgbw and "bbbb" or "bbb"
local mode_s = self.rgbw and 4 or 3
for i=1, size do
local d = data[off*size+i]
to_send = to_send..pack(
mode,
d and d[1] or 0,
d and d[2] or 0,
d and d[3] or 0,
d and d[4] or 0
)
end
self.udp:sendto(to_send, self.ip, self.port)
end
function LEDsController:sendArtnetDMX_ext(nb_led, update, delay)
self:printD("ART-NET ext DMX:", self.uni, self.net)
local to_send = pack(
"AHHbbbb>H",
ART_HEAD,
ART_DMX,
ARTNET_VERSION,
self.count%256,
0,
self.uni + (bit.lshift(self.subnet or 0, 4)),
self.net,
self.leds_by_uni*(self.rgbw and 4 or 3)
)
self.count = self.count + 1
local data = self.leds
local size = self.leds_by_uni
local mode = self.rgbw and "bbbb" or "bbb"
for i=1, size do
local d = data[i]
to_send = to_send..pack(
mode,
d and d[1] or 0,
d and d[2] or 0,
d and d[3] or 0,
d and d[4] or 0
)
end
self.udp:sendto(to_send, self.ip, self.port)
if delay then
socket.sleep(delay)
end
end
function LEDsController:sendArtnetSync()
self:printD("ART-NET Sync")
local to_send = pack(
"AHHH",
ART_HEAD,
ART_SYNC,
ARTNET_VERSION,
0x0000
)
self.udp:sendto(to_send, "255.255.255.255", self.port)
end
function LEDsController:sendAllArtnetDMX(nb_led, update, delay)
-- local max_update = math.floor(512 / (self.rgbw and 4 or 3))
local nb_update = math.ceil(nb_led / self.leds_by_uni)
if update then
delay = delay / (nb_update+1)
else
delay = delay / nb_update
end
self:printD("#artnet", nb_led, nb_update+(update and 1 or 0))
for i=0, nb_update-1 do
self:sendArtnetDMX(self.net, self.uni+i, i)
if delay then
socket.sleep(delay)
end
end
if update then
self:sendArtnetSync()
if delay then
socket.sleep(delay)
end
end
end
function LEDsController:decodeArtnetDMX(data)
local _,_,_,_,seq,_,uni,net,size = upack(data, "zHHbbbb>H")
-- if input[net] == nil then input[net] = {} end
-- input[net][(uni.."")] = data:sub(19)
print(uni,net,seq)
local dmx = data:sub(19)
local leds = self.leds
for i=1,LEDS_BY_UNI do
local _,r,g,b = upack(dmx, "bbb")
leds[i+(uni*LEDS_BY_UNI)] = {r,g,b}
dmx = dmx:sub(4)
end
return uni,net,seq
end
local function str(s,size)
local s_size = s:len()
if s_size < size then
for i=1,size-s_size do
s = s .. "\0"
end
end
return s
end
function LEDsController:sendArtnetPollReply(ip, port)
for i=1,24 do
local to_send = pack(
"AHb4HHbbHbbHAAAHI5b7Ab4bbA",
ART_HEAD,
ART_REPLY, -- Opcode
192,168,1,i, -- ip
6454, -- eth port
0x0001, -- firmware version
0, -- net
i, -- subnet
0x00ff, -- OEM
0, -- Ubea version
0xd2, -- Status1
0, -- EstaMan
str("Test\0", 18), -- short name
str(string.format("Driver %d",i), 64), -- long name
str("status\0", 64), -- text status
1, -- num port
0xc0c0c0c0, -- port type
0x08808080, --Good input
0x80808080, -- Good output
0x00000000, -- SwIn
0x00000000, -- SwOut
0, -- SwVideo
0, -- SwMacro
0, -- SwRemote
0, -- Spare1
0, -- Spare2
0, -- Spare3
0, -- Style
"\1\2\3\4\5\6", -- mac
192,168,1,245,
0, --BindIndex
8, -- Status
str("",26)
)
self.udp:sendto(to_send, ip, port)
end
end
function LEDsController:decodeArtnetReply(data)
local _,
id,
opp,
ip1,ip2,ip3,ip4, -- ip
port, -- eth port
firmware, -- firmware version
net, -- net
subnet, -- subnet
OEM, -- OEM
Ubea, -- Ubea version
Status1, -- Status1
EstaMan, -- EstaMan
short_name, -- short name
long_name, -- long name
status, -- text status
nb_port, -- num port
port_type, -- port type
good_input, --Good input
good_output, -- Good output
SwIn, -- SwIn
SwOut, -- SwOut
SwVideo, -- SwVideo
SwMacro, -- SwMacro
SwRemote, -- SwRemote
Spare1, -- Spare1
Spare2, -- Spare2
Spare3, -- Spare3
Style, -- Style
mac1,mac2,mac3,mac4,mac5,mac6, -- mac
remoteIp1,remoteIp2,remoteIp3,remoteIp4,
bindIndex, --BindIndex
Status2 = upack(data, "zHBBBBHHBBHBBHc18c64c64>HIIIIIBBBBBBBBBBBBBBBBBBB")
-- print(id , ip1,ip2,ip3,ip4)
_, short_name = upack(short_name,"z")
_, long_name = upack(long_name,"z")
_, status = upack(status,"z")
local t = {
id = id,
opp = opp,
ip = {ip1,ip2,ip3,ip4},
port = port,
firmware = firmware,
net = net,
subnet = subnet,
OEM = OEM,
Ubea = Ubea,
Status1 = Status1,
EstaMan = EstaMan,
short_name = short_name,
long_name = long_name,
status = status,
nb_port = nb_port,
port_type = port_type,
good_input = good_input,
good_output = good_output,
SwIn = SwIn,
SwOut = SwOut,
SwVideo = SwVideo,
SwMacro = SwMacro,
SwRemote = SwRemote,
Spare1 = Spare1,
Spare2 = Spare2,
Spare3 = Spare3,
Style = Style,
mac = {mac1,mac2,mac3,mac4,mac5,mac6},
remoteIp = {remoteIp1,remoteIp2,remoteIp3,remoteIp4},
bindIndex = bindIndex
}
self:printD(string.format([[
ip: %d.%d.%d.%d
port: %d
art-vers: 0x%04x
net: %d
subset: %d
Oem: 0x%04x
short_name: '%s'
long_name: '%s'
report: '%s'
]],
t.ip[1], t.ip[2], t.ip[3], t.ip[4],
t.port,
t.firmware,
t.net,
t.subnet,
t.OEM,
t.short_name,
t.long_name,
t.status
))
return t
end
function LEDsController:sendArtnetPoll()
local to_send = pack(
"AHHbb",
ART_HEAD,
ART_POLL, -- Opcode
ARTNET_VERSION,
06,
00
)
self.udp:sendto(to_send, "255.255.255.255", self.port)
end
function LEDsController:receiveArtnet(receive_data, remote_ip, remote_port)
local _,_, opcode = upack(receive_data, "zH")
-- self:printD("RECEIVE from", remote_ip, remote_port)
-- self:printD(string.format("0x%04x",opcode))
if opcode == ART_POLL then
-- self:printD("Art-Net POLL from:", remote_ip, remote_port)
-- self:sendArtnetPollReply(remote_ip, remote_port)
return "poll"
elseif opcode == ART_REPLY then
self:printD("Art-Net POLL_REPLY from:", remote_ip, remote_port)
local d = self:decodeArtnetReply(receive_data)
return "reply", d
elseif opcode == ART_DMX then
self:printD("Art-Net DMX from:", remote_ip, remote_port)
local uni,net,seq = self:decodeArtnetDMX(receive_data)
-- if uni == 3 then
-- return "sync"
-- end
return "dmx"
elseif opcode == ART_SYNC then
self:printD("Art-Net SYNC from:", remote_ip, remote_port)
return "sync"
end
end
function LEDsController:setLED(m, r, g, b, w)
if self.rgbw and self.rgbw_mode ~= 0 then
if self.rgbw_mode == 1 then
w = math.min(r,g,b)
elseif self.rgbw_mode == 2 then
w = math.min(r,g,b)
r,g,b = r-w, g-w, b-w
elseif self.rgbw_mode == 3 then
w = (math.max(r,g,b) + math.min(r,g,b)) / 2
end
else
w = 0
end
r = r * self.bright
g = g * self.bright
b = b * self.bright
w = w * self.bright
self.leds[m.id+1] = {r,g,b,w}
end
------------------------------- RGB888 ----------------------------------------
function LEDsController:sendLED888(off, len, show)
-- self:printD("sendLED888", off, len, show)
local to_send = pack("bbHH", (show and LED_RGB_888_UPDATE or LED_RGB_888), self.count%256, off, len*3)
self.count = self.count + 1
local data = self.leds
for i=0,len-1 do
to_send = to_send..pack(
"bbb",
data[off+i+1][1],
data[off+i+1][2],
data[off+i+1][3]
)
end
self.udp:sendto(to_send, self.ip, self.port)
end
function LEDsController:sendAll888(nb_led, update, delay)
local max_update = math.floor(MAX_UPDATE_SIZE / 3)
local nb_update = math.ceil(nb_led / max_update)
self:printD("#RGB888", nb_led*3, nb_update)
for i=0, nb_update-2 do
self:sendLED888(i * max_update, max_update, false)
socket.sleep(delay/nb_update)
end
local last_off = max_update * (nb_update-1)
self:sendLED888(last_off, nb_led - last_off, update)
socket.sleep(delay/nb_update)
return nb_led*3, nb_update
end
------------------------------- RGB565 ----------------------------------------
local function conv888to565(color)
local r = color[1]
local g = color[2]
local b = color[3]
r = rshift(r, 3)
r = lshift(r, 6)
g = rshift(g, 2)
r = bor(r,g)
r = lshift(r, 5)
b = rshift(b, 3)
r = bor(r,b)
-- print(color[1],color[2],color[3],r)
return r
end
function LEDsController:send565LED(off, len, show)
-- self:printD(off, len)
local to_send = pack("bbHH", (show and LED_RGB_565_UPDATE or LED_RGB_565), self.count%256, off, len*2)
self.count = self.count + 1
local data = self.leds
for i=0,len-1 do
to_send = to_send..pack("H", conv888to565(data[off+i+1]))
end
self.udp:sendto(to_send, self.ip, self.port)
end
function LEDsController:sendAll565(nb_led, update, delay)
local max_update = math.floor(MAX_UPDATE_SIZE / 2)
local nb_update = math.ceil(nb_led / max_update)
self:printD("#565",nb_led, nb_update)
for i=0, nb_update-2 do
self:send565LED(i * max_update, max_update, false)
socket.sleep(delay/nb_update)
end
local last_off = max_update * (nb_update-1)
self:send565LED(last_off, nb_led - last_off, update)
socket.sleep(delay/nb_update)
end
---------------------------------- RLE888 -------------------------------------
local function cpm_color(c1, c2)
return (c1[1] == c2[1] and c1[2] == c2[2] and c1[3] == c2[3])
end
function LEDsController:compressRLE888(led_nb)
local color = self.leds[1]
local nb = 0
local data = {}
local size = 0
local pqt = 1
for i=1,led_nb+1 do
if i == led_nb+1 then
-- print("lst",i)
if not data[pqt] then
data[pqt] = {}
data[pqt].str = ""
data[pqt].led_nb = nb
if (data[pqt-1]) then
data[pqt].off = data[pqt-1].off + data[pqt-1].led_nb
else
data[pqt].off = 0
end
end
data[pqt].str = data[pqt].str..pack("bbbb",
(nb),
color[1],
color[2],
color[3]
)
size = size + 4
data[pqt].led_nb = data[pqt].led_nb + nb
elseif cpm_color(color, self.leds[i]) and nb < 255 then
nb = nb + 1
else
if not data[pqt] then
data[pqt] = {}
data[pqt].str = ""
data[pqt].led_nb = 0
if (data[pqt-1]) then
data[pqt].off = data[pqt-1].off + data[pqt-1].led_nb
else
data[pqt].off = 0
end
end
data[pqt].str = data[pqt].str..pack(
"bbbb",
nb,
color[1],
color[2],
color[3]
)
data[pqt].led_nb = data[pqt].led_nb + nb
color = self.leds[i]
nb = 1
size = size + 4
if size % MAX_UPDATE_SIZE == 0 then
pqt = pqt+1
end
end
end
return data, size
end
function LEDsController:sendRLE888(data, show, delay)
self:printD("#RLE888", #data)
-- for k,v in pairs(data) do
-- print("=",k,v.led_nb,v.off)
-- end
local nb = #data
for i=1,nb-1 do
local to_send = pack(
"bbHH",
LED_RLE_888,
self.count%256,
data[i].off,
data[i].led_nb)..data[i].str
self.count = self.count + 1
self.udp:sendto(to_send, self.ip, self.port)
end
local to_send = pack(
"bbHH",
(show and LED_RLE_888_UPDATE or LED_RLE_888),
self.count%256,
data[nb].off,
data[nb].led_nb)..data[nb].str
self.count = self.count + 1
self.udp:sendto(to_send, self.ip, self.port)
socket.sleep(delay)
end
function LEDsController:sendAllRLE888(led_nb, leds_show, delay_pqt)
local data, size = self:compressRLE888(led_nb)
if #data < math.ceil(led_nb / (MAX_UPDATE_SIZE/3)) then
self:sendRLE888(data, leds_show, delay_pqt)
else
self:sendAll888(led_nb, leds_show, delay_pqt)
end
-- self:printD(string.format([[
-- ART-NET: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RGB-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RLE-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S; %0.03f%% (RLE-888 VS RGB-888)
-- ]],
--
-- math.ceil(led_nb / LEDS_BY_UNI)+1,
-- led_nb*3/1024,
-- (math.ceil(led_nb / LEDS_BY_UNI)+1)*60,
--
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3)),
-- (led_nb*3)/1024,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3))*60,
--
-- #data,
-- size/1024,
-- #data * 60,
-- size/(led_nb*3)*100
-- ))
end
---------------------------------- BRO888 --------------------------------------
function LEDsController:compressBRO888()
-- self:printD("compressBRO888")
local data = self.leds
local to_compress = ""
for i=0,self.led_nb-1 do
to_compress = to_compress..pack(
"bbb",
data[i+1][1],
data[i+1][2],
data[i+1][3]
)
end
local options = {
mode = brotli.MODE_GENERIC,
quality = 1,
lgwin = 10,
lgblock = 16,
}
local cmp = brotli.compress(to_compress,options)
return cmp, #cmp
end
function LEDsController:sendBRO888(data, leds_show, delay_pqt)
self:printD("#BRO888", #data)
local to_send = pack("bbHH", (leds_show and LED_BRO_888_UPDATE or LED_BRO_888), self.count%256, 0, self.led_nb)..data
self.count = self.count + 1
self.udp:sendto(to_send, self.ip, self.port)
socket.sleep(delay_pqt)
end
function LEDsController:sendAllBRO888(led_nb, leds_show, delay_pqt)
local bro_data, bro_size = self:compressBRO888()
-- local z_data, z_size = self:compressZ888()
if bro_size > 1400 then
self:sendAll888(led_nb, leds_show, delay_pqt)
else
self:sendBRO888(bro_data, leds_show, delay_pqt)
end
-- self:printD(string.format([[
-- ART-NET: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RGB-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RGB-565: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RLE-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S; %0.03f%% (RLE-888 VS RGB-888)
-- BRO-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S; %0.03f%% (BRO-888 VS RGB-888)
-- Z-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S; %0.03f%% (Z-888 VS RGB-888)
-- ]],
--
-- math.ceil(led_nb / LEDS_BY_UNI)+1,
-- led_nb*3/1024,
-- (math.ceil(led_nb / LEDS_BY_UNI)+1)*60,
--
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3)),
-- (led_nb*3)/1024,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3))*60,
--
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/2)),
-- (led_nb*2)/1024,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/2))*60,
--
-- math.ceil(rle_size / MAX_UPDATE_SIZE),
-- rle_size/1024,
-- math.ceil(rle_size / MAX_UPDATE_SIZE)*60,
-- rle_size/(led_nb*3)*100,
--
-- math.ceil(bro_size / MAX_UPDATE_SIZE),
-- bro_size/1024,
-- math.ceil(bro_size / MAX_UPDATE_SIZE)*60,
-- bro_size/(led_nb*3)*100,
--
-- math.ceil(z_size / MAX_UPDATE_SIZE),
-- z_size/1024,
-- math.ceil(z_size / MAX_UPDATE_SIZE)*60,
-- z_size/(led_nb*3)*100
-- ))
end
---------------------------------- Z888 ----------------------------------------
function LEDsController:compressZ888(nb, off)
nb = nb or self.led_nb
off = off or 0
local data = self.leds
local t = {}
for i=off,off+nb-1 do
table.insert(t, pack(
"bbb",
data[i+1][1],
data[i+1][2],
data[i+1][3]
))
end
local cmp = love.data.compress("string", "zlib", table.concat(t))
return cmp, #cmp
end
function LEDsController:sendZ888(data, offset, leds_show, delay_pqt)
self:printD("#Z888 size:", #data, offset, leds_show, delay_pqt)
local to_send = pack("bbHH", (leds_show and LED_Z_888_UPDATE or LED_Z_888), self.count%256, offset, #data)..data
self.count = self.count + 1
self.udp:sendto(to_send, self.ip, self.port)
socket.sleep(delay_pqt)
end
function LEDsController:sendAllZ888(led_nb, leds_show, delay_pqt)
local z_data, z_size = self:compressZ888()
local split = math.ceil(z_size/1400)
if split > 1 then
local off = 0
for i=0, split-2 do
local l_data = z_data:sub(off+1, off+1400)
self:sendZ888(l_data, off, false, delay_pqt)
off = off + #l_data
end
local l_data = z_data:sub(off+1)
self:sendZ888(l_data, off, true, delay_pqt)
else
self:sendZ888(z_data, 0, leds_show, delay_pqt)
end
-- self:printD(string.format([[
-- ART-NET: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RGB-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- Z-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S; %0.03f%% (Z-888 VS RGB-888)
-- ]],
-- math.ceil(led_nb / LEDS_BY_UNI)+1,
-- led_nb*3/1024,
-- (math.ceil(led_nb / LEDS_BY_UNI)+1)*60,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3)),
-- (led_nb*3)/1024,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3))*60,
-- math.ceil(z_size / MAX_UPDATE_SIZE),
-- z_size/1024,
-- math.ceil(z_size / MAX_UPDATE_SIZE)*60,
-- z_size/(led_nb*3)*100
-- ))
end
function LEDsController:compressZ565(nb, off, header)
nb = nb or self.led_nb
off = off or 0
local data = self.leds
local t = {}
for i=off,off+nb-1 do
table.insert(t, pack("H", conv888to565(data[i+1])))
end
local cmp = love.data.compress("string", "zlib", table.concat(t))
return cmp, #cmp
end
function LEDsController:sendZ565(data, offset, leds_show, delay_pqt)
self:printD("#Z565 size:", #data, offset, leds_show, delay_pqt)
local to_send = pack("bbHH",
(leds_show and LED_Z_565_UPDATE or LED_Z_565),
self.count%256,
offset,
#data
)..data
self.count = self.count + 1
self.udp:sendto(to_send, self.ip, self.port)
socket.sleep(delay_pqt or 0)
end
function LEDsController:sendAllZ565(led_nb, leds_show, delay_pqt)
-- local start = socket.gettime()
local z_data, z_size = self:compressZ565()
local split = math.ceil(z_size/1400)
if split > 1 then
local off = 0
for i=0, split-2 do
local l_data = z_data:sub(off+1, off+1400)
self:sendZ565(l_data, off, false, delay_pqt)
off = off + #l_data
end
local l_data = z_data:sub(off+1)
self:sendZ565(l_data, off, true, delay_pqt)
else
self:sendZ565(z_data, 0, leds_show, delay_pqt)
end
-- print(1/(socket.gettime() - start))
-- self:printD(string.format([[
-- ART-NET: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- RGB-888: %02d pqt/frame; %0.3f Ko; %02d pqt/S
-- Z-565: %02d pqt/frame; %0.3f Ko; %02d pqt/S; %0.03f%% (Z-565 VS RGB-888)
-- ]],
-- math.ceil(led_nb / LEDS_BY_UNI)+1,
-- led_nb*3/1024,
-- (math.ceil(led_nb / LEDS_BY_UNI)+1)*60,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3)),
-- (led_nb*3)/1024,
-- math.ceil(led_nb / (MAX_UPDATE_SIZE/3))*60,
-- math.ceil(z_size / MAX_UPDATE_SIZE),
-- z_size/1024,
-- math.ceil(z_size / MAX_UPDATE_SIZE)*60,
-- z_size/(led_nb*3)*100
-- ))
end
---------------------------------- UDPX ----------------------------------------
function LEDsController:sendUDPX(led_nb)
self:printD("#UDPX")
local to_send = pack("bbHH", 0x50, 0, 1, self.led_nb)
self.count = self.count + 1
local data = self.leds
-- local crc = 0
for i=0,led_nb-1 do
to_send = to_send..pack(
"bbb",
data[i+1][1],
data[i+1][2],
data[i+1][3]
)
end
-- for i=0,led_nb-1 do
-- crc = bxor(crc, to_send:byte(1+5+i))
-- end
-- to_send = to_send..pack("b", crc)
self.udp:sendto(to_send, self.ip, self.port)
end
function LEDsController:sendAllUDPX(led_nb, leds_show, delay_pqt)
self:sendUDPX(led_nb)
socket.sleep(delay_pqt)
end
function LEDsController:sendUDPX565(led_nb)
self:printD("#UDPX565")
local to_send = pack("bb>H>H", 0x52, self.count%256, self.led_nb/2, self.led_nb)
self.count = self.count + 1
local z_data = self:compressZ565(led_nb, 0, to_send)
self.udp:sendto(z_data, self.ip, self.port)
end
function LEDsController:sendAllUDPX565(led_nb, leds_show, delay_pqt)
self:sendUDPX565(led_nb)
socket.sleep(delay_pqt)
end
------------------------------- DRGB ----------------------------------------
function LEDsController:sendDRGB(nb_led, update, delay)
self:printD("sendDRGB", nb_led, update, delay)
local t = {pack("bb", 2, 2)}
self.count = self.count + 1
local data = self.leds
for i=1,nb_led do
t[i+1] = pack(
"bbb",
data[i][1],
data[i][2],
data[i][3]
)
end
self.udp:sendto(table.concat(t), self.ip, self.port)
socket.sleep(delay)
end
------------------------------- DNRGB ----------------------------------------
function LEDsController:sendDNRGB(off, len, show)
-- self:printD("sendLED888", off, len, show)
local to_send = pack("bbH", 4, 2, off)
self.count = self.count + 1
local data = self.leds
for i=0,len-1 do
to_send = to_send..pack(
"bbb",
data[off+i+1][1],
data[off+i+1][2],
data[off+i+1][3]
)
end
self.udp:sendto(to_send, self.ip, self.port)
end
function LEDsController:sendAllDNRGB(nb_led, update, delay)
local max_update = 489
local nb_update = math.ceil(nb_led / max_update)
self:printD("#DNRGB", nb_led*3, nb_update)
for i=0, nb_update-2 do
self:sendDNRGB(i * max_update, max_update, false)
socket.sleep(delay/nb_update)
end
local last_off = max_update * (nb_update-1)
self:sendDNRGB(last_off, nb_led - last_off, false)
socket.sleep(delay/nb_update)
return nb_led*3, nb_update
end
--------------------------------------------------------------------------------
function LEDsController:start_dump(pro, name)
local protocol
if pro == "RGB888" then
self.dump = self.dump_888
protocol = LED_RGB_888
elseif pro == "RGB565" then
self.dump = self.dump_565