-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathinit.lua
2103 lines (1943 loc) · 71.3 KB
/
init.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
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
--- === hs.loupedeck ===
---
--- Control surface support for the Loupedeck CT, Loupedeck Live, Loupedeck Live-S and Razer Stream Controller.
---
--- Special thanks to William Viker & Håkon Nessjøen for their [NodeJS experiments](https://github.com/bitfocus/loupedeck-ct).
---
--- Special thanks to [Max Maischein](https://github.com/Corion) for his [HID-LoupedeckCT](https://github.com/Corion/HID-LoupedeckCT) experiments.
local log = require "hs.logger".new("loupedeck")
local bytes = require "hs.bytes"
local drawing = require "hs.drawing"
local inspect = require "hs.inspect"
local network = require "hs.network"
local serial = require "hs.serial"
local timer = require "hs.timer"
local utf8 = require "hs.utf8"
local just = require "cp.just"
local tools = require "cp.tools"
local wshttp = require "cp.websocket.http"
local wsserial = require "cp.websocket.serial"
local semver = require "semver"
local concat = table.concat
local doAfter = timer.doAfter
local floor = math.floor
local format = string.format
local hexDump = utf8.hexDump
local tableContains = tools.tableContains
local tableMatch = tools.tableMatch
local bytesToHex = bytes.bytesToHex
local hexToBytes = bytes.hexToBytes
local int16be = bytes.int16be
local int8 = bytes.int8
local remainder = bytes.remainder
local uint16be = bytes.uint16be
local uint16le = bytes.uint16le
local uint24be = bytes.uint24be
local uint32be = bytes.uint32be
local uint8 = bytes.uint8
-- LOUPEDECK_VENDOR_ID -> number
-- Constant
-- Loupedeck's USB Vendor ID
local LOUPEDECK_VENDOR_ID = 11970
-- RAZER_VENDOR_ID -> number
-- Constant
-- Razer's USB Vendor ID
local RAZER_VENDOR_ID = 0x1532
-- LOUPEDECK_CT_ID -> number
-- Constant
-- Loupedeck CT USB Product ID
local LOUPEDECK_CT_ID = 3
-- LOUPEDECK_CT_V2_ID -> number
-- Constant
-- Loupedeck CT USB Product ID
local LOUPEDECK_CT_V2_ID = 7
-- LOUPEDECK_LIVE_ID -> number
-- Constant
-- Loupedeck LIVE USB Product ID
local LOUPEDECK_LIVE_ID = 4
-- LOUPEDECK_LIVE_S_ID -> number
-- Constant
-- Loupedeck Live-S USB Product ID
local LOUPEDECK_LIVE_S_ID = 0x0006
-- RAZER_STREAM_CONTROLLER_ID -> number
-- Constant
-- Razer Stream Controller USB Product ID
local RAZER_STREAM_CONTROLLER_ID = 0x0d06
local mod = {}
mod.mt = {}
mod.mt.__index = mod.mt
--- hs.loupedeck.deviceTypes -> table
--- Constant
--- A table containing the device types.
mod.deviceTypes = {
CT = "Loupedeck CT",
LIVE = "Loupedeck Live",
LIVE_S = "Loupedeck Live-S",
RAZER_STREAM_CONTROLLER = "Razer Stream Controller",
}
--- hs.loupedeck:registerCallback([callbackFn]) -> number
--- Method
--- Registers a callback.
---
--- Parameters:
--- * [callbackFn] - The optional callback function
---
--- Returns:
--- * If `callbackFn` is supplied a unique callback id between 2 and 255, otherwise 1.
function mod.mt:registerCallback(callbackFn)
if type(callbackFn) == "function" then
local id
for i=2, 255 do
if not self.callbackRegister[i] then
id = i
break
end
end
if not id then
log.ef("Unexpected error: All 256 callback IDs are already allocated, so ignoring this callback registration.")
return 1
end
self.callbackRegister[id] = callbackFn
return id
else
return 1
end
end
--- hs.loupedeck.getCallback(id[, preserve]) -> function | nil
--- Function
--- Retrieves the callback function at the specified id.
---
--- Parameters:
--- * id - the callback ID to retrieve
--- * preserve - (optional) if `true`, the callback will not be cleared from the register. defaults to `false`.
---
--- Returns:
--- * The callback `function`, or `nil` if not available.
function mod.mt:getCallback(id, preserve)
local callback = self.callbackRegister[id]
if not preserve then
self.callbackRegister[id] = nil
end
return callback
end
-- rgbToInt16(r, g, b) -> number
-- Function
-- Converts 8-bit Red/Green/Blue values into a 16-bit integer.
--
-- Parameters:
-- * r - Red component
-- * g - Green component
-- * b - Blue component
--
-- Returns:
-- * The 16-bit integer for the colour value.
local function rgbToInt16(r, g, b)
return (((r >> 3) & 0x1F) << 11) | (((g >> 2) & 0x3F) << 5) | ((b >> 3) & 0x1F)
end
-- colorToInt16(colorTable) -> number
-- Function
-- Converts an `hs.drawing.color` value into a 16-bit integer.
--
-- Parameters:
-- * colorTable - A `table` containing a color that matches requirements for `hs.drawing.color`.
--
-- Returns:
-- * The 16-bit integer for the colour value.
local function colorToInt16(colorTable)
local rgb = drawing.color.asRGB(colorTable)
local r = floor(rgb.red * 255)
local g = floor(rgb.green * 255)
local b = floor(rgb.blue * 255)
return rgbToInt16(r, g, b)
end
-- toInt16Color(color) -> number
-- Function
-- Attempts to convert different colour definitions to a 16-bit color value.
--
-- Parameters:
-- * color - Either a `hs.drawing.color` table, a 16-bit color value, or 24-bit hex strings (e.g. "FFFFFF")
local function toInt16Color(color)
local colorType = type(color)
if colorType == "string" then
return colorToInt16({hex=color, alpha=1.0})
elseif colorType == "table" then
return colorToInt16(color)
elseif colorType == "number" then
return color
else
error(format("Unexpected color value: ", inspect(color)))
end
end
-- rgbToInt24(r, g, b) -> number
-- Function
-- Converts the individual 8-bit RGB integers into a single 24-bit integer value, ordered as R,G,B.
--
-- Parameters:
-- * r - The Red component.
-- * g - The Green component.
-- * b - The Blue component.
--
-- Returns:
-- * The 24-bit integer for the color value.
local function rgbToInt24(r, g, b)
return ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF))
end
-- colorToInt24(colorTable) -> n umber
-- Function
-- Converts an `hs.drawing.color` value into a 16-bit integer.
--
-- Parameters:
-- * colorTable - A `table` containing a color that matches requirements for `hs.drawing.color`.
--
-- Returns:
-- * The 24-bit integer for the colour value.
local function colorToInt24(colorTable)
local rgb = drawing.color.asRGB(colorTable)
local r = floor(rgb.red * 255)
local g = floor(rgb.green * 255)
local b = floor(rgb.blue * 255)
return rgbToInt24(r, g, b)
end
-- toInt24Color(color) -> number
-- Function
-- Attempts to convert different colour definitions to a 24-bit color value (RGB * 8-bits).
--
-- Parameters:
-- * color - Either a `hs.drawing.color` table, a 24-bit color value, or 24-bit hex strings (e.g. "FFFFFF")
local function toInt24Color(color)
local colorType = type(color)
if colorType == "string" then
return colorToInt24({hex=color, alpha=1.0})
elseif colorType == "table" then
return colorToInt24(color)
elseif colorType == "number" then
return color
else
error(format("Unexpected color value: ", inspect(color)))
end
end
-- findLast(haystack, needle) -> number
-- Function
-- Finds the position of the last occurance of a character in a string.
--
-- Parameters:
-- * haystack - What to search
-- * needle - What to search for
--
-- Returns:
-- * A number with the position otherwise `nil`
local function findLast(haystack, needle)
local i=haystack:match(".*"..needle.."()")
if i==nil then return nil else return i-1 end
end
--- hs.loupedeck.setLogLevel(loglevel) -> none
--- Function
--- Sets the Log Level.
---
--- Parameters:
--- * loglevel - can be 'nothing', 'error', 'warning', 'info', 'debug', or 'verbose'; or a corresponding number between 0 and 5
---
--- Returns:
--- * None
function mod.setLogLevel(loglevel)
log:setLogLevel(loglevel)
end
--- hs.loupedeck:connected() -> boolean
--- Method
--- Checks if the websocket is connected or not
---
--- Parameters:
--- * None
---
--- Returns:
--- * `true` if connected otherwise `false`
function mod.mt:connected()
return self.websocket and self.websocket:isOpen()
end
--- hs.loupedeck:send() -> boolean
--- Method
--- Sends a message via the websocket if connected
---
--- Parameters:
--- * message - The message to send.
---
--- Returns:
--- * `true` if sent.
function mod.mt:send(message)
if self:connected() then
local data = type(message) == "table" and concat(message) or tostring(message)
self.websocket:send(data)
return true
end
return false
end
-- hs.loupedeck:sendBytes(...) -> boolean
-- Method
-- Sends a series of `string`, `hs.bytes`, or `hs.bytes` functions to the websocket if connected.
--
-- Parameter:
-- * ... - The list of `string`, `hs.bytes` or `hs.bytes` functions to send.
--
-- Returns:
-- * `true` if sent.
function mod.mt:sendBytes(...)
return self:send(bytes(...):bytes())
end
--- hs.loupedeck:sendCommand(commandID[, callbackFn[, ...]]) -> boolean
--- Method
--- Sends the specified command, with the provided callback function, along with any additional binary string blocks.
---
--- Parameters:
--- * commandID - An 16-bit integer with the command ID.
--- * callbackFn - A `function` that will be called with the `data` from the response. (optional)
--- * ... - a variable number of byte string values, which will be concatenated together with the command and callback ID when being sent.
---
--- Returns:
--- * `true` if sent.
function mod.mt:sendCommand(commandID, callbackFn, ...)
return self:sendBytes(uint16be(commandID), uint8(self:registerCallback(callbackFn)), ...)
end
--- hs.loupedeck:initaliseDevice() -> None
--- Method
--- Starts the background loop, performs self-test and resets screens and buttons.
---
--- Parameters:
--- * None
---
--- Returns:
--- * None
function mod.mt:initaliseDevice()
-- This must be executed before writing to the main Touch Screen:
self:resetDevice()
-- Reset all the buttons to black:
local black = 0x000000
for _,id in pairs(mod.buttonID) do
self:buttonColor(id, black)
end
-- Reset all the screens to black:
local b = drawing.color.hammerspoon.black
if self.deviceType == mod.deviceTypes.LIVE_S then
self:updateScreenColor(mod.screens.live_s, b)
else
self:updateScreenColor(mod.screens.left, b)
self:updateScreenColor(mod.screens.middle, b)
self:updateScreenColor(mod.screens.right, b)
end
if self.deviceType == mod.deviceTypes.CT then
self:updateScreenColor(mod.screens.wheel, b)
end
--------------------------------------------------------------------------------
-- Check the Loupedeck Firmware:
--------------------------------------------------------------------------------
if not self.loupedeckDeviceIsUsingRazerFirmwareCheckDone and (self.deviceType == mod.deviceTypes.LIVE or self.deviceType == mod.deviceTypes.CT) then
log.df("Loupedeck CT or Live connected, so lets check the firmware version...")
self:requestFirmwareVersion(function(data)
local firmwareVersion = data and data.b and semver(data.b)
if firmwareVersion then
self.loupedeckDeviceIsUsingRazerFirmwareCheckDone = true
log.df("Loupedeck device is using firmware version: v%s", firmwareVersion)
if firmwareVersion > semver("0.2.5") then
log.df("Loupedeck device is running firmware greater than v0.2.5, so using Razer screen format.")
self.loupedeckDeviceIsUsingRazerFirmware = true
--------------------------------------------------------------------------------
-- Refresh the screen:
--------------------------------------------------------------------------------
self:initaliseDevice()
else
--log.df("Loupedeck Live is running firmware lower than v0.2.5")
self.loupedeckDeviceIsUsingRazerFirmware = false
end
else
log.df("Failed to get the Loupedeck Live firmware version.")
end
end)
end
end
--- hs.loupedeck:callback([callbackFn]) -> boolean
--- Method
--- Sets a callback when new messages are received.
---
--- Parameters:
--- * callbackFn - a function to set as the callback for `hs.loupedeck`. If the value provided is `nil`, any currently existing callback function is removed.
---
--- Returns:
--- * `true` if successful otherwise `false`
function mod.mt:callback(callbackFn)
if type(callbackFn) == "function" then
self._callback = callbackFn
return true
elseif type(callbackFn) == "nil" then
self._callback = nil
return true
else
log.ef("Callback received an invalid type: %s", type(callbackFn))
return false
end
end
--- hs.loupedeck:triggerCallback -> none
--- Method
--- Triggers a callback function
---
--- Parameters:
--- * data - Any data to pass along to the callback function as a table
---
--- Returns:
--- * None
function mod.mt:triggerCallback(data)
--------------------------------------------------------------------------------
-- Trigger the callback:
--------------------------------------------------------------------------------
if self._callback then
local success, result = xpcall(function() self._callback(data, self.deviceNumber) end, debug.traceback)
if not success then
log.ef("Error in Loupedeck Callback: %s", result)
end
end
end
-- events -> table
-- Constant
-- A table containing functions triggered by websocket events.
local events = {
--------------------------------------------------------------------------------
-- WEBSOCKET OPENING:
--------------------------------------------------------------------------------
opening = function(obj)
obj:triggerCallback {
action = "websocket_opening",
}
end,
--------------------------------------------------------------------------------
-- WEBSOCKET OPENED:
--------------------------------------------------------------------------------
opened = function(obj)
--log.df("Initalising device...")
obj:initaliseDevice()
obj:triggerCallback {
action = "websocket_opened",
}
end,
--------------------------------------------------------------------------------
-- WEBSOCKET CLOSING:
--------------------------------------------------------------------------------
closing = function(obj)
obj:triggerCallback {
action = "websocket_closing",
}
end,
--------------------------------------------------------------------------------
-- WEBSOCKET CLOSED:
--------------------------------------------------------------------------------
closed = function(obj)
obj:triggerCallback {
action = "websocket_closed",
}
end,
--------------------------------------------------------------------------------
-- WEBSOCKET FAILED:
--------------------------------------------------------------------------------
error = function(obj, message)
obj:triggerCallback({
action = "websocket_error",
error = message,
})
end,
--------------------------------------------------------------------------------
-- WEBSOCKET RECEIVED MESSAGE:
--------------------------------------------------------------------------------
message = function(obj, message)
-- read the command ID, callback ID and the remainder of the message...
local id, callbackID, data = bytes.read(message,
uint16be, uint8, remainder
)
--------------------------------------------------------------------------------
-- NOTE: Starting with the 0.1.79 firmware update on both the Loupedeck Live
-- and Loupedeck CT, the hardware seems to constantly send a 1024
-- message. It doesn't seem to be useful (i.e. it appears to just be
-- websocket spam) - so we're ignoring it early.
--------------------------------------------------------------------------------
if id == 0x0400 then return end
local response = {
id = id,
data = data,
}
-- first, check if we have a callback...
local callback = obj:getCallback(callbackID)
if callback then
local ok, res = xpcall(function() return callback(response) end, debug.traceback)
if not ok then
log.ef("Error executing callback for %04x: %s", response.id, res)
return
end
else
-- if not, see if there is a handler.
local handler = mod.responseHandler[id]
if handler then
local ok, res = xpcall(function() return handler(obj, response) end, debug.traceback)
if not ok then
log.ef("Error executing callback for %04x: %s", response.id, res)
return
end
elseif not mod.ignoreResponses[id] then
log.ef("Unexpected command: id: %04x; callback: %02x; message:\n%s", id, callbackID, hexDump(message))
return
end
end
end
}
--- hs.loupedeck.event -> table
--- Constant
--- The set of events sent from the Loupedeck device.
---
--- Notes:
--- * Includes:
--- * BUTTON_PRESS - occurs when a button is pressed or released.
--- * ENCODER_MOVE - occurs when the wheel/encoder moves left or right
--- * WHEEL_PRESSED - occurs when the wheel is pressed.
--- * WHEEL_RELEASED - occurs when the wheel is released.
--- * SCREEN_PRESSED - occurs when the main screen is pressed.
--- * SCREEN_RELEASED - occurs when the main screen is released.
mod.event = {
BUTTON_PRESS = 0x0500, -- 1280
ENCODER_MOVE = 0x0501, -- 1281
WHEEL_PRESSED = 0x0952, -- 2386
WHEEL_RELEASED = 0x0972, -- 2418
SCREEN_PRESSED = 0x094D, -- 2381
SCREEN_RELEASED = 0x096D, -- 2413
}
--- hs.loupedeck.ignoreResponses -> table
--- Constant
--- A table of responses to ignore.
mod.ignoreResponses = {
[0x0302] = true, -- Button Color confirmation (770)
[0x040F] = true, -- Screen Image Update confirmation (1039)
[0x041B] = true, -- Vibration confirmation (1051)
[0x0409] = true, -- Reset Device (1033)
[0x0819] = true, -- Flash Drive confirmation (2073)
[0x041e] = true, -- Wheel Sensitivity confirmation (1054)
[0x1c73] = true, -- This is triggered when the Loupedeck Live first connects
[0x1573] = true, -- This is triggered when the Loupedeck Live first connects
[0x1f73] = true, -- This is triggered when the Loupedeck Live first connects
[0x0410] = true, -- Not sure... seems to happen during restarts.
-- These trigger when you start a Loupedeck Live with Serial Firmware,
-- and I have no idea why:
[0x1273] = true,
[0x2573] = true,
[0x1773] = true,
[0x1873] = true,
[0x3773] = true,
[0x0573] = true,
[0x1d73] = true,
[0x3073] = true,
}
-- convertWheelXandYtoButtonID(x, y) -> number
-- Function
-- Converts X and Y coordinates into a button ID for the wheel touch screen.
--
-- Parameters:
-- * x - The x-axis as a number
-- * y - The y-axis as a number
--
-- Returns:
-- * A button ID as a number. Left to right, top to bottom.
local function convertWheelXandYtoButtonID(x, y)
local button = 0
-- Left Column:
if x >= 0 and x <= 79 then
if y >= 0 and y <= 119 then
button = 1
else
button = 4
end
end
-- Middle Column:
if x >= 80 and x <= 159 then
if y >= 0 and y <= 119 then
button = 2
else
button = 5
end
end
-- Middle Column:
if x >= 160 and x <= 240 then
if y >= 0 and y <= 119 then
button = 3
else
button = 6
end
end
return button
end
-- convertXandYtoButtonID(x, y, deviceType) -> number
-- Function
-- Converts X and Y coordinates into a button ID for the middle touch screen.
--
-- Parameters:
-- * x - The x-axis as a number
-- * y - The y-axis as a number
-- * deviceType - The device type.
--
-- Returns:
-- * A button ID as a number. Left to right, top to bottom.
local function convertXandYtoButtonID(x, y, deviceType)
local button = 0
if deviceType == mod.deviceTypes.LIVE_S then
-- Add in the x offset:
x = x + (30/2)
-- First Row:
if x >= 60 and x <= 140 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 1
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 6
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 11
end
end
-- Second Row:
if x >= 160 and x <= 230 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 2
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 7
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 12
end
end
-- Third Row:
if x >= 240 and x <= 320 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 3
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 8
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 13
end
end
-- Fourth Row:
if x >= 340 and x <= 390 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 4
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 9
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 14
end
end
-- Five Row:
if x >= 410 and x <= 460 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 5
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 10
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 15
end
end
else
-- First Row:
if x >= 60 and x <= 140 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 1
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 5
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 9
end
end
-- Second Row:
if x >= 160 and x <= 230 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 2
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 6
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 10
end
end
-- Third Row:
if x >= 240 and x <= 320 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 3
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 7
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 11
end
end
-- Fourth Row:
if x >= 340 and x <= 390 then
-- Top Row:
if y >= 0 and y <= 100 then
button = 4
end
-- Middle Row:
if y >= 110 and y <= 170 then
button = 8
end
-- Bottom Row:
if y >= 200 and y <= 260 then
button = 12
end
end
end
return button
end
--- hs.loupedeck.responseHandler -> table
--- Constant
--- Set of response handlers for device-generated events.
mod.responseHandler = {
--------------------------------------------------------------------------------
-- BUTTON PRESS/RELEASE:
--
-- Examples:
-- 07 00 Down
-- 07 01 Up
--------------------------------------------------------------------------------
[mod.event.BUTTON_PRESS] = function(obj, response)
local id, dirByte = bytes(response.data):read(int8, int8)
if dirByte == 0x00 then
response.direction = "down"
elseif dirByte == 0x01 then
response.direction = "up"
else
log.ef("Invalid Button Direction: %02x", dirByte)
return
end
response.buttonID = id
obj:triggerCallback(response)
end,
--------------------------------------------------------------------------------
-- ENCODER ROTATION:
--
-- Examples:
-- 01 01 Right
-- 01 FF Left
--------------------------------------------------------------------------------
[mod.event.ENCODER_MOVE] = function(obj, response)
local id, dirByte = bytes.read(response.data, uint8, int8)
if dirByte == -1 then
response.direction = "left"
elseif dirByte == 1 then
response.direction = "right"
else
log.ef("Invalid Encoder Direction: %02x", dirByte)
return
end
response.buttonID = id
obj:triggerCallback(response)
end,
--------------------------------------------------------------------------------
-- WHEEL PRESSED:
--
-- Example:
-- 00 00 7E 00 76 00
--------------------------------------------------------------------------------
[mod.event.WHEEL_PRESSED] = function(obj, response)
response.multitouch, response.x, response.y, response.unknown = bytes.read(response.data, uint8, int16be, int16be, uint8)
response.multitouch = response.multitouch == 0x01
-- Get button ID:
local buttonID = convertWheelXandYtoButtonID(response.x, response.y)
if buttonID > 0 then
response.buttonID = buttonID
end
obj:triggerCallback(response)
end,
--------------------------------------------------------------------------------
-- WHEEL RELEASED:
--
-- Example:
-- 00 00 7B 00 94 00
--------------------------------------------------------------------------------
[mod.event.WHEEL_RELEASED] = function(obj, response)
response.multitouch, response.x, response.y, response.unknown = bytes.read(response.data, uint8, int16be, int16be, uint8)
response.multitouch = response.multitouch == 0x01
-- Get button ID:
local buttonID = convertWheelXandYtoButtonID(response.x, response.y)
if buttonID > 0 then
response.buttonID = buttonID
end
obj:triggerCallback(response)
end,
--------------------------------------------------------------------------------
-- SCREEN PRESSED:
--
-- Example:
-- 00 01 C9 00 9A 27
--------------------------------------------------------------------------------
[mod.event.SCREEN_PRESSED] = function(obj, response)
response.multitouch, response.x, response.y, response.pressure = bytes.read(response.data, uint8, int16be, int16be, uint8)
-- Get button ID:
local buttonID = convertXandYtoButtonID(response.x, response.y, obj.deviceType)
if buttonID > 0 then
response.buttonID = buttonID
end
-- Get screen ID:
if response.x <= mod.screens.left.width then
response.screenID = mod.screens.left.id
elseif response.x >= (mod.screens.left.width + mod.screens.middle.width) then
response.screenID = mod.screens.right.id
else
response.screenID = mod.screens.middle.id
end
obj:triggerCallback(response)
end,
--------------------------------------------------------------------------------
-- SCREEN RELEASED:
--
-- Example:
-- 00 01 BC 00 BE 25
--------------------------------------------------------------------------------
[mod.event.SCREEN_RELEASED] = function(obj, response)
response.multitouch, response.x, response.y, response.pressure = bytes.read(response.data, uint8, int16be, int16be, uint8)
-- Get button ID:
local buttonID = convertXandYtoButtonID(response.x, response.y, obj.deviceType)
if buttonID > 0 then
response.buttonID = buttonID
end
obj:triggerCallback(response)
end,
}
--- hs.loupedeck:websocketCallback(event, message) -> none
--- Method
--- The websocket callback function.
---
--- Parameters:
--- * event - A string containing the type of event (i.e. "open" or "closed")
--- * message - The message from the websocket
---
--- Returns:
--- * None
function mod.mt:createWebsocketCallback()
return function(event, message)
local handler = events[event]
if handler then
handler(self, message)
else
log.wf("Unexpected websocket event '%s':\n%s", event, hexDump(message))
end
end
end
--- hs.loupedeck:requestDeviceInfo([callbackFn]) -> boolean
--- Method
--- Sends a request to the Loupedeck asking for device information.
---
--- Parameters:
--- * callbackFn - (optional) Function called with a `response` table as the first parameter
---
--- Returns:
--- * `true` if the device is connected and the message was sent.
---
--- Notes:
--- * the `response` contains the `id`, `data`,
function mod.mt:requestDeviceInfo(callbackFn)
--------------------------------------------------------------------------------
-- DEVICE INFORMATION:
--
-- Sending message (19): (19) 13-1C-02-7E-1E-38-CF-55-8B-2C-13-AB-14-64-71-1C ...
-- Message sent (19): (19) 13-1C-02-7E-1E-38-CF-55-8B-2C-13-AB-14-64-71-1C-ED-B0-8A
-- Message received (19): (19) 13-1C-02-64-42-AA-22-DC-81-7A-80-DB-E9-E7-31-03 ...
-- Get device information
--------------------------------------------------------------------------------
-- TODO: figure out what these bytes mean...
return self:sendCommand(0x131C, callbackFn, hexToBytes("61f1392a8e936ba66e992daedb40f65f"))
end
--- hs.loupedeck:requestFirmwareVersion([callbackFn]) -> boolean
--- Method
--- Sends a request to the Loupedeck asking for its firmware version.
---
--- Parameters:
--- * callbackFn - (optional) Function called with a `response` table as the first parameter
---
--- Returns:
--- * `true` if the device is connected and the message was sent.
---
--- Notes:
--- * the `response` contains the `id`, `data`,
function mod.mt:requestFirmwareVersion(callbackFn)
--------------------------------------------------------------------------------
-- FIRMWARE VERSION: