-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudp_server.lua
51 lines (41 loc) · 1.01 KB
/
udp_server.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
dofile('apa102.lua')
udp_server = {
output = nil,
message = nil,
port = 65506,
brightness = 0xed,
}
udp_server.__index = udp_server
function udp_server:new()
local self = setmetatable({}, udp_server)
return self
end
function udp_server:packet_callback(data)
--print(self.message)
apa102.write(self.brightness, data)
end
function udp_server:start()
print("Starting UDP nodestar server")
apa102.init()
svr=net.createServer(net.UDP)
svr:on("receive", function(socket, data)
self:receive_data(socket, data)
end)
svr:listen(self.port)
tmr.alarm(0, 1, 1, function()
self:process_loop()
end)
end
function udp_server:receive_data(socket, data, packet_callback)
self.message = "Got "..string.len(data).." bytes"
self.output = data
socket:send(string.char(0xac))
end
function udp_server:process_loop()
out = self.output
self.output = nil
if nil ~= out then
tmr.wdclr()
self:packet_callback(out)
end
end