· 5 years ago · Oct 15, 2020, 07:38 AM
1
2local Decoder = require("jurokunext")
3
4-- local JUROKU_HOST = "ws://me.lemmmy.pw:8089/stream"
5-- local JUROKU_HOST = "ws://switchcraft.pw:9999/api/client"
6-- local JUROKU_VIDEO = "ws://bf677dd2.eu.ngrok.io/api/ws/video"
7-- local JUROKU_CONTROL = "ws://bf677dd2.eu.ngrok.io/api/ws/control/0"
8local JUROKU_VIDEO = "ws://quincy.chuie.io:9999/api/ws/video"
9local JUROKU_CONTROL = "ws://quincy.chuie.io:9999/api/ws/control/0"
10
11if not debug then error("Missing debug API") end
12local dp
13for i = 1, 16 do
14 local name, value = debug.getupvalue(peripheral.getNames, i)
15 if name == "native" then
16 dp = value
17 break
18 end
19end
20if not dp then error("failed to get direct peripheral access") end
21
22local function wrapPeriph(side)
23 local call = dp.call
24 return function(method, ...)
25 return call(side, method, ...)
26 end
27end
28
29local monitors = { wrapPeriph("left") }
30
31-- local monitors = {
32-- wrapRemote("monitor_576", "bottom"),
33-- wrapRemote("monitor_577", "bottom"),
34-- wrapRemote("monitor_578", "bottom"),
35-- wrapRemote("monitor_579", "bottom")
36-- }
37
38local joy = {
39 ["b"] = 0,
40 ["y"] = 1,
41 ["select"] = 2,
42 ["start"] = 3,
43 ["up"] = 4,
44 ["down"] = 5,
45 ["left"] = 6,
46 ["right"] = 7,
47 ["a"] = 8,
48 ["x"] = 9,
49 ["l"] = 10,
50 ["r"] = 11,
51 ["l2"] = 12,
52 ["r2"] = 13,
53 ["l3"] = 14,
54 ["r3"] = 15
55}
56
57local keyBindings = {
58 [keys.x] = joy.a,
59 [keys.z] = joy.b,
60 [keys.a] = joy.y,
61 [keys.s] = joy.x,
62 [keys.q] = joy.l,
63 [keys.w] = joy.r,
64 [keys.up] = joy.up,
65 [keys.down] = joy.down,
66 [keys.left] = joy.left,
67 [keys.right] = joy.right,
68 [keys.enter] = joy.start,
69 [keys.rightShift] = joy.select
70}
71
72local function clearMonitors()
73 for k, m in pairs(monitors) do
74 local w, h = m("getSize")
75 m("setPaletteColour", colours.black, 0x000000)
76 m("setBackgroundColour", colours.black)
77 m("clear")
78 m("setTextScale", 0.5)
79 m("setCursorPos", 1, 1)
80 end
81end
82
83local function run()
84 clearMonitors()
85 print("Connecting")
86
87 local videoWS, err = http.websocket(JUROKU_VIDEO)
88 if not videoWS then
89 error(err)
90 end
91
92 local controlWS, err = http.websocket(JUROKU_CONTROL)
93 if not controlWS then
94 error(err)
95 end
96
97 local decoder = Decoder.new(monitors)
98
99 for k, v in pairs(decoder) do
100 print(k)
101 end
102
103 while true do
104 local e = {os.pullEvent()}
105 local event = e[1]
106
107 if event == "websocket_failure" then
108 error("Connection failed")
109 elseif event == "websocket_message" then
110 decoder:renderNextMonitor(0, e[3])
111 elseif event == "key" and not e[3] and keyBindings[e[2]] ~= nil then
112 -- key down and not being held
113 controlWS.send(keyBindings[e[2]] .. " 1")
114 elseif event == "key_up" and keyBindings[e[2]] ~= nil then
115 controlWS.send(keyBindings[e[2]] .. " 0")
116 end
117 end
118
119 if ws then ws.close() end
120end
121
122run()
123
124clearMonitors()
125
126print("Stream ended or something")