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
|
if type(mesecon.luacontroller_libraries) == "table" then
local tslib = {
getColorEscapeSequence = function(color)
return(string.char(0x1b).."(c@"..color..")")
end,
colorize = function(color,message)
return(string.char(0x1b).."(c@"..color..")"..message..string.char(0x1b).."(c@#FFFFFF)")
end,
explode_textlist_event = function(event)
local ret = {type = "INV"}
if string.sub(event,1,3) == "CHG" then
local index = tonumber(string.sub(event,5,-1))
if index then
ret.type = "CHG"
ret.index = index
end
elseif string.sub(event,1,3) == "DCL" then
local index = tonumber(string.sub(event,5,-1))
if index then
ret.type = "DCL"
ret.index = index
end
end
return ret
end,
_mt = {
setChannel = function(self,channel)
self._channel = channel
end,
getChannel = function(self)
return(self._channel)
end,
draw = function(self)
digiline_send(self._channel,self._commands)
end,
clear = function(self)
self._commands = {{command="clear"}}
end,
setLock = function(self,lock)
if lock then
table.insert(self._commands,{command="lock"})
else
table.insert(self._commands,{command="unlock"})
end
end,
addLabel = function(self,x,y,label,vertical)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
if type(label) ~= "string" then
label = tostring(label)
end
local cmd = {
command = "addlabel",
X = x,
Y = y,
label = label,
}
if vertical then cmd.command = "addvertlabel" end
table.insert(self._commands,cmd)
end,
addImage = function(self,x,y,w,h,tex)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(tex) ~= "string" then
tex = tostring(tex)
end
local cmd = {
command = "addimage",
X = x,
Y = y,
W = w,
H = h,
texture_name = tex,
}
table.insert(self._commands,cmd)
end,
addButton = function(self,x,y,w,h,name,label,exit)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
local cmd = {
command = "addbutton",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
}
if exit then cmd.command = "addbutton_exit" end
table.insert(self._commands,cmd)
end,
addImageButton = function(self,x,y,w,h,name,label,tex,exit)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
if type(tex) ~= "string" then
tex = tostring(tex)
end
local cmd = {
command = "addimage_button",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
image = tex,
}
if exit then cmd.command = "addimage_button_exit" end
table.insert(self._commands,cmd)
end,
addField = function(self,x,y,w,h,name,label,default,password)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
if type(default) ~= "string" then
default = tostring(default)
end
local cmd = {
command = "addfield",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
default = default,
}
if password then cmd.command = "addpwdfield" end
table.insert(self._commands,cmd)
end,
addTextArea = function(self,x,y,w,h,name,label,default)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if type(name) ~= "string" then
name = tostring(name)
end
if type(label) ~= "string" then
label = tostring(label)
end
if type(default) ~= "string" then
default = tostring(default)
end
local cmd = {
command = "addtextarea",
X = x,
Y = y,
W = w,
H = h,
name = name,
label = label,
default = default,
}
table.insert(self._commands,cmd)
end,
addDropdown = function(self,x,y,w,h,name,choices,selected)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if not selected then selected = 1 end
assert((type(selected))=="number","Invalid selection index")
if type(name) ~= "string" then
name = tostring(name)
end
assert((type(choices) == "table" and #choices >= 1),"Invalid choices list")
local cmd = {
command = "adddropdown",
X = x,
Y = y,
W = w,
H = h,
name = name,
choices = choices,
selected_id = selected,
}
table.insert(self._commands,cmd)
end,
addTextlist = function(self,x,y,w,h,name,choices,selected,transparent)
assert((type(x))=="number","Invalid X position")
assert((type(y))=="number","Invalid Y position")
assert((type(w))=="number","Invalid width")
assert((type(h))=="number","Invalid height")
if not selected then selected = 1 end
assert((type(selected))=="number","Invalid selection index")
if type(name) ~= "string" then
name = tostring(name)
end
assert((type(transparent)) == "boolean","Invalid transparent flag")
assert((type(choices) == "table" and #choices >= 1),"Invalid choices list")
local cmd = {
command = "addtextlist",
X = x,
Y = y,
W = w,
H = h,
name = name,
listelements = choices,
selected_id = selected,
transparent = transparent,
}
table.insert(self._commands,cmd)
end,
},
new = function(self,channel)
local ret = {}
for k,v in pairs(self._mt) do
ret[k] = v
end
ret._channel = channel
ret._commands = {{command="clear"}}
return ret
end,
}
mesecon.luacontroller_libraries.TSLib = tslib
local libexpander = {
pre_hook = function()
if event.type == "program" then
mem.__libexpander_registered = {}
end
if event.type == "digiline" then
local oldevent = event
for k,v in pairs(mem.__libexpander_registered) do
if k == oldevent.channel then
if oldevent.msg.a ~= v.pinastate and v.pina then
mem.__libexpander_registered[k].pinastate = oldevent.msg.a
event = {
type = v.pinastate and "on" or "off",
pin = {
name = string.upper(v.pina),
channel = oldevent.channel,
virtual = true,
},
}
end
if oldevent.msg.b ~= v.pinbstate and v.pinb then
mem.__libexpander_registered[k].pinbstate = oldevent.msg.b
event = {
type = v.pinbstate and "on" or "off",
pin = {
name = string.upper(v.pinb),
channel = oldevent.channel,
virtual = true,
},
}
end
if oldevent.msg.c ~= v.pincstate and v.pinc then
mem.__libexpander_registered[k].pincstate = oldevent.msg.c
event = {
type = v.pincstate and "on" or "off",
pin = {
name = string.upper(v.pinc),
channel = oldevent.channel,
virtual = true,
},
}
end
if oldevent.msg.d ~= v.pindstate and v.pind then
mem.__libexpander_registered[k].pindstate = oldevent.msg.d
event = {
type = v.pindstate and "on" or "off",
pin = {
name = string.upper(v.pind),
channel = oldevent.channel,
virtual = true,
},
}
end
end
end
end
for _,v in pairs(mem.__libexpander_registered) do
if v.pina then
pin[v.pina] = v.pinastate
port[v.pina] = v.portastate
end
if v.pinb then
pin[v.pinb] = v.pinbstate
port[v.pinb] = v.portbstate
end
if v.pinc then
pin[v.pinc] = v.pincstate
port[v.pinc] = v.portcstate
end
if v.pind then
pin[v.pind] = v.pindstate
port[v.pind] = v.portdstate
end
end
end,
post_hook = function()
for k,v in pairs(mem.__libexpander_registered) do
local changed = false
if v.pina and (v.portastate ~= port[v.pina]) then
changed = true
v.portastate = port[v.pina]
end
if v.pinb and (v.portbstate ~= port[v.pinb]) then
changed = true
v.portbstate = port[v.pinb]
end
if v.pinc and (v.portcstate ~= port[v.pinc]) then
changed = true
v.portcstate = port[v.pinc]
end
if v.pind and (v.portdstate ~= port[v.pind]) then
changed = true
v.portdstate = port[v.pind]
end
if changed then
digiline_send(k,{a = v.portastate,b = v.portbstate,c = v.portcstate,d = v.portdstate})
end
end
end,
new = function(channel,pina,pinb,pinc,pind)
assert(type(channel) == "string","Invalid channel")
local def = {pinastate = false,pinbstate = false,pincstate = false,pindstate = false,portastate = false,portbstate = false,portcstate = false,portdstate = false,}
if type(pina) == "string" then def.pina = pina end
if type(pinb) == "string" then def.pinb = pinb end
if type(pinc) == "string" then def.pinc = pinc end
if type(pind) == "string" then def.pind = pind end
mem.__libexpander_registered[channel] = def
digiline_send(channel,"GET")
end,
delete = function(channel)
mem.__libexpander_registered[channel] = nil
end,
}
mesecon.luacontroller_libraries.libexpander = libexpander
local libdetector = {
pre_hook = function()
if event.type == "program" then
mem.__libdetector_registered = {}
end
if event.type == "digiline" then
local channel = event.channel
if mem.__libdetector_registered[channel] then
if not mem.__libdetector_registered[channel].active then
mem.__libdetector_registered[channel].active = true
if not port[string.lower(mem.__libdetector_registered[channel].virtualpin)] then
event = {
type = "on",
pin = {
name = string.upper(mem.__libdetector_registered[channel].virtualpin),
channel = channel,
virtual = true,
},
}
end
end
interrupt(mem.__libdetector_registered[channel].timeout,"__libdetector"..channel)
end
end
if event.type == "interrupt" and string.sub(event.iid,1,13) == "__libdetector" then
local channel = string.sub(event.iid,14,-1)
if mem.__libdetector_registered[channel] then
mem.__libdetector_registered[channel].active = false
if not port[string.lower(mem.__libdetector_registered[channel].virtualpin)] then
event = {
type = "off",
pin = {
name = string.upper(mem.__libdetector_registered[channel].virtualpin),
channel = channel,
virtual = true,
},
}
end
end
end
for k,v in pairs(mem.__libdetector_registered) do
pin[string.lower(v.virtualpin)] = v.active or port[string.lower(v.virtualpin)]
end
end,
new = function(channel,virtualpin,timeout)
if not tonumber(timeout) then timeout = 1.5 end
timeout = math.max(0.5,timeout)
assert(type(channel) == "string","channel must be a string")
assert(type(virtualpin) == "string","virtualpin must be a string")
mem.__libdetector_registered[channel] = {
virtualpin = virtualpin,
timeout = timeout,
active = false,
}
end,
delete = function(channel)
if not mem.__libdetector_registered[channel] then return false end
mem.__libdetector_registered[channel] = nil
return true
end,
}
mesecon.luacontroller_libraries.libdetector = libdetector
end
|