-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvibrationThread.lua
More file actions
136 lines (116 loc) · 3.76 KB
/
vibrationThread.lua
File metadata and controls
136 lines (116 loc) · 3.76 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
require "love"
require "love.filesystem"
CHANNEL_IN = love.thread.getChannel("vibration_channel_in")
CHANNEL_OUT = love.thread.getChannel("vibration_channel_out")
require('engine.object')
local json = require('Mods.BalatroBuzz.json')
local websocket = require("Mods.BalatroBuzz.websocket")
local intiface_host = CHANNEL_IN:demand()
local intiface_port = CHANNEL_IN:demand()
CHANNEL_OUT:push('using Intiface host '..intiface_host..':'..intiface_port)
local client = websocket.new(intiface_host, intiface_port, "/")
local Intiface = Object:extend()
function Intiface:init()
self.message_id = 1
self.device_index = nil
self.ready = false
self.callback_table = {}
end
function Intiface:send_json(data, callback)
local payload = json.encode({data})
CHANNEL_OUT:push('sent: '..payload)
self.callback_table[self.message_id] = callback
client:send(payload)
self.message_id = self.message_id + 1
end
function Intiface:connect(callback)
self:send_json({
RequestServerInfo = {
Id = self.message_id,
ClientName = "BalatroBuzz",
MessageVersion = 2
}
}, callback)
end
function Intiface:get_device_list(callback)
-- [{"RequestDeviceList":{"Id":2}}]
self:send_json({
RequestDeviceList = {
Id = self.message_id
}
}, callback)
end
function Intiface:scan(callback)
-- [{"StartScanning":{"Id":3}}]
self:send_json({
StartScanning = {
Id = self.message_id
}
}, callback)
end
function Intiface:vibrate(intensity)
-- v3 version - for some reason the v3 handshake is having issues
--
-- self:send_json({
-- ScalarCmd = {
-- Id = self.message_id,
-- DeviceIndex = 0,
-- Scalars = {{ Index = 0, Scalar = intensity, ActuatorType = "Vibrate" }}
-- }
--})
self:send_json({
VibrateCmd = {
Id = self.message_id,
DeviceIndex = self.device_index,
Speeds = {{ Index = 0, Speed = intensity }}
}
})
end
function Intiface:callback(message_type, message_id, message)
CHANNEL_OUT:push(message_type.." message: "..json.encode(message))
local callback = self.callback_table[message_id]
if callback then callback(message) end
self.callback_table[message_id] = nil
end
local buttplugClient = Intiface()
function client:onmessage(data)
local json_data = json.decode(data)
for _, message in ipairs(json_data) do
for message_type, message_value in pairs(message) do
local message_id = message_value.Id
buttplugClient:callback(message_type, message_id, message_value)
end
end
end
function client:onopen()
buttplugClient:connect(function ()
buttplugClient:scan(function ()
buttplugClient:get_device_list(function(device_list)
local devices = device_list.Devices
local device = devices[1]
if not device then
CHANNEL_OUT:push('Could not find any devices')
return
end
local deviceName = device.DeviceName
buttplugClient.device_index = device.DeviceIndex
CHANNEL_OUT:push('Using device: '..deviceName..' with device index '..buttplugClient.device_index)
buttplugClient.ready = true
CHANNEL_OUT:push("Intiface ready!")
end)
end)
end)
end
function client:onclose(code, reason)
CHANNEL_OUT:push("closecode: "..code..", reason: "..reason)
end
while true do
client:update()
--Monitor the channel for any new requests
if buttplugClient.ready then
local data = CHANNEL_IN:pop()
if data then
buttplugClient:vibrate(data)
end
end
end