-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-LEDs.lua
466 lines (425 loc) · 13.3 KB
/
custom-LEDs.lua
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
PluginInfo =
{
Name = "Room Combiner Add-ons~Custom LEDs",
Version = "0.1.0",
Id = "1ad2ee4f-f857-4aaa-8b4b-5af20df5d40a",
Description = "Set your own colours for the Room Combiner!",
Author = "Joshua Cerecke - Ceretech Audiovisual Solutions",
URL = "https://github.com/jcerecke/room-combiner-custom-LEDs"
}
function GetPrettyName(props)
return "Custom LEDs for Room Combiner V" .. PluginInfo.Version
end
function GetColor(props)
return { 191, 226, 249 } --Same colour as Room Combiner component
end
local Colors = {
White = {255, 255, 255},
Black = {0, 0, 0},
Red = {255, 0, 0},
Green = {0, 255, 0},
Transparent = "FFFFFFFF"
}
function GetProperties()
props = {
{
Name = "Rooms",
Type = "integer",
Value = 8,
Min = 2,
Max = 256
},
{
Name = "Custom Off Colors",
Type = "boolean",
Value = "false"
}
}
return props
end
function GetControls(props)
ctls = {
{
Name = "RoomCombineTarget",
ControlType = "Text",
TextBoxStyle = "ComboBox",
Count = 1,
PinStyle = "Input",
UserPin = true
},
{
Name = "Status",
ControlType = "Indicator",
IndicatorType = "Status",
Count = 1,
PinStyle = "Output",
UserPin = true
},
{
Name = "Colors",
ControlType = "Text",
TextBoxStyle = "Normal",
Count = props["Rooms"].Value,
PinStyle = "Input",
UserPin = true
},
{
Name = "LEDs",
ControlType = "Indicator",
IndicatorType = "Led",
Count = props["Rooms"].Value,
PinStyle = "Output",
UserPin = true
}
}
if props["Custom Off Colors"].Value then
table.insert(ctls, {
Name = "OffColors",
ControlType = "Text",
TextBoxStyle = "Normal",
Count = props.Rooms.Value,
PinStyle = "Input",
UserPin = true
}
)
table.insert(ctls, {
Name = "LEDState",
ControlType = "Button",
ButtonType = "Toggle",
Count = 1,
})
end
return ctls
end
-- This function allows you to layout pages in your plugin.
function GetControlLayout(props)
local roomQty = props["Rooms"].Value
-- layout holds representaiton of Controls
local layout = {}
-- graphics holds aesthetic & design items
local graphics = {}
-- x,y allows an easy method of knowing where you are relative to the section being designed
table.insert(
graphics, {
Type = "Text",
Text = "Room Combiner Name",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Right",
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {20, 20},
Size = {136, 20},
ZOrder = 1
}
)
layout["RoomCombineTarget"] = {
PrettyName = "Room Combiner Name",
Style = "ComboBox",
Position = {166, 20},
Size = {288, 20},
FontSize = 11
}
table.insert(
graphics, {
Type = "Text",
Text = "Status",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Right",
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {20, 44},
Size = {136, 48},
ZOrder = 1
}
)
layout["Status"] = {
PrettyName = "Status",
Style = "Indicator",
IndicatorStyle = "Status",
Position = {166, 44},
Size = {288, 48},
FontSize = 11,
ZOrder = 16
}
local x, y = 162, 106
if props["Custom Off Colors"].Value then
table.insert(
graphics, {
Type = "Text",
Text = "LED State",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Right",
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {20, 96},
Size = {136, 16},
ZOrder = 1
}
)
layout["LEDState"] = {
PrettyName = "LED State",
Style = "Button",
ButtonStyle = "Toggle",
Position = {166, 96},
Size = {40, 20},
FontSize = 11,
ZOrder = 16
}
y = 126
end
local sizeY = props["Custom Off Colors"].Value and 110 or 90
table.insert(
graphics, {
Type = "GroupBox",
Text = "Rooms",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Left",
StrokeColor = Colors.Stroke,
StrokeWidth = 1,
CornerRadius = 8,
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {x, y},
Size = {roomQty * 36 + 8, sizeY},
ZOrder = 1
}
)
x, y = 20, y + 20
table.insert(
graphics, {
Type = "Text",
Text = "Custom LED",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Right",
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {x, y + 16},
Size = {136, 16},
ZOrder = 1
}
)
table.insert(
graphics, {
Type = "Text",
Text = "Color",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Right",
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {x, y + 32},
Size = {136, 16},
ZOrder = 1
}
)
if props["Custom Off Colors"].Value then
table.insert(
graphics, {
Type = "Text",
Text = "Off Color",
FontSize = 11,
FontStyle = "Regular",
HTextAlign = "Right",
Color = {51, 51, 51, 255},
Fill = {0, 0, 0, 0},
Position = {x, y + 48},
Size = {136, 16},
ZOrder = 1
}
)
end
x = 166
for i = 1, roomQty do
table.insert(
graphics,{
Type = "Text",
Text = tostring(i),
FontSize = 11,
HTextAlign = "Center",
Color = Colors.Black,
Position = {x, y},
Size = {36, 16}
}
)
layout["LEDs " .. tostring(i)] = {
PrettyName = "LEDs~Room " .. tostring(i),
Style = "Led",
Position = {x + 10, y + 16},
Size = {16, 16}
}
layout["Colors " .. tostring(i)] = {
PrettyName = "Colors~Room " .. tostring(i),
Style = "Text",
TextBoxStyle = "Normal",
Position = {x, y + 32},
Size = {36, 16},
HTextAlign = "Center",
FontSize = 9,
Color = Colors.White,
StrokeColor = Colors.Stroke,
StrokeWidth = 1
}
if props["Custom Off Colors"].Value then
layout["OffColors "..tostring(i)] = {
PrettyName = "Off Colors~Room " .. tostring(i),
Style = "Text",
TextBoxStyle = "Normal",
Position = {x, y + 48},
Size = {36, 16},
HTextAlign = "Center",
FontSize = 9,
Color = Colors.White,
StrokeColor = Colors.Stroke,
StrokeWidth = 1
}
end
x = x + 36
end
return layout, graphics
end
if Controls then
--[[ ******************************* ]]--
--[[ ******* Global Variables ****** ]]--
--[[ ******************************* ]]--
roomCombinerNames = {}
for i, v in pairs(Component.GetComponents()) do
if v.Type == "room_combiner" then
table.insert(roomCombinerNames, v.Name)
end
end
Controls.RoomCombineTarget.Choices = roomCombinerNames
rooms = {} -- key = room number, value = table of LED controls (color shows group membership), color Text Box controls, LEDs
groups = {} -- sort of reverse table of rooms[room][group] info. Keys are group names (LED colors), Values are room numbers that are members of that group.
walls = {} -- key = index, value = control
--[[ ******************************* ]]--
--[[ ********** Functions ********** ]]--
--[[ ******************************* ]]--
function UpdateStatus(val, msg) -- Updates status, "val" can be passed a number or string.
if type(val) == "string" then
val = val == "OK" and 0 or val
val = val == "Compromised" and 1 or val
val = val == "Fault" and 2 or val
val = val == "Not Present" and 3 or val
val = val == "Missing" and 4 or val
val = val == "Initializing" and 5 or val
if type(val) ~= "number" then
error("UpdateStatus val is incorrectly formatted: Expecting integer 0-5 or valid status string, Received: "..val)
end
end
Controls.Status.Value = val
if msg ~= nil then
Controls.Status.String = msg
end
end
function IsComponentValid(name) -- returns true or false if a named component is found.
return #Component.GetControls(name) ~= 0 and true or false
end
function UpdateTextBoxColor(ctl, valid) -- Updates textbox color
valid = ctl.String == "" or valid -- Textbox is white if the Text box is blank.
ctl.Color = valid and "White" or "Red" -- Textbox is white if the entry is valid, red if it's not valid.
end
function UpdateGroupsTable()
groups = {}
local groupNo = 0 -- Room Number of the first room in a group
for room, t in ipairs(rooms) do
if groupNo == 0 or t.LEDs.Color ~= rooms[groupNo].LEDs.Color then -- If there's no GroupNo or the color is different
groupNo = room
end
rooms[room]["groupNo"] = groupNo
if groups["groupNo"] == nil then
groups["groupNo"] = {room}
else
table.insert(groups["groupNo"], room)
end
end
end
function UpdateLEDs()
for room, roomTbl in ipairs(rooms) do
if roomTbl.rcLED.Boolean then -- the room is combined - set it to the on state
if Properties["Custom Off Colors"].Value then
roomTbl.myLED.Color = rooms[roomTbl.groupNo].myColor.String
else
roomTbl.myLED.Boolean = true
end
else
if Properties["Custom Off Colors"].Value then
roomTbl.myLED.Color = roomTbl.myOffColor.String
else
roomTbl.myLED.Boolean = false
end
end
end
end
function WallHasChanged() -- Room Combiner Walls Control EventHandler Function
UpdateGroupsTable()
UpdateLEDs()
end
function GetFirstRoom(group) -- returns the first room number
table.sort(group)
return #group > 0 and group[1] or false
end
function ColorChange(ctl)
print("Color changed to "..ctl.String)
end
function InitialiseAll()
UpdateStatus("Initializing")
local componentValid = IsComponentValid(Controls.RoomCombineTarget.String) --Check Room Combiner Component Exists
UpdateTextBoxColor(Controls.RoomCombineTarget, componentValid)
if componentValid then
roomCombiner = Component.New(Controls.RoomCombineTarget.String)
for room = 1, Properties.Rooms.Value do -- Initialise rooms table with plugin controls
rooms[room] = {}
rooms[room]["myLED"] = Controls.LEDs[room]
rooms[room]["myColor"] = Control.Colors[room]
if Properties["Custom Off Colors"].Value then
rooms[room]["myOffColor"] = Controls.OffColors[room]
end
end
for name, control in pairs(roomCombiner) do -- Initialise room combiner controls
if name:find("output%.%d+%.combined") then
local room = tonumber(name:match("output%.(%d+)%.combined"))
if room <= Properties.Rooms.Value then
rooms[room]["rcLED"] = control
rooms[room]["group"] = control.Color
else
UpdateStatus("Fault", "Room Combiner number of rooms does not match this plugin - Room Combiner: "..tostring(roomCombinerRoomQty)..", This Plugin: "..tostring(Properties.Rooms.Value))
end
elseif name:find("wall%.%d+%.open") then -- Add each wall's open button into an array and assign eventhandler to watch it
local wall = tonumber(name:match("wall%.(%d+)%.open"))
walls[wall] = control
control.EventHandler = function()
WallHasChanged()
end
end
end
WallHasChanged()
if Controls.Status.String == "Initializing" then -- Finished initialising with no faults, update status to OK.
UpdateStatus("OK")
--SyncroniseAllGroups()
end
else
UpdateStatus("Fault", "Room Combiner target not a valid component. Check component name and try again.")
end
end
--Run all eventhandler code on startup:
InitialiseAll()
for i = 1, Properties.rooms.Value do
Controls.Colors.EventHandler = ColorChange
if Properties["Custom Off Colors"].Value then
Controls.OffColors.EventHandler = WallHasChanged
end
end
if Properties["Custom Off Colors"].Value then
Controls["LED State"].EventHandler = function()
for room, roomTbl in ipairs(rooms) do
roomTbl.myLED.Boolean = Controls["LED State"].boolean
end
WallHasChanged()
end
end
end