From 637827c6af33adedacc56188f077419d779cff75 Mon Sep 17 00:00:00 2001 From: hkc Date: Fri, 12 Jan 2024 22:04:57 +0300 Subject: [PATCH] Move ports and update from internet --- turtos/ctrl.lua | 18 +++++++++--------- turtos/main.lua | 4 ++-- turtos/startup.lua | 29 +++++++++++++++++++++++------ turtos/update.json | 2 +- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/turtos/ctrl.lua b/turtos/ctrl.lua index e794ea0..8dc4ad4 100644 --- a/turtos/ctrl.lua +++ b/turtos/ctrl.lua @@ -4,7 +4,7 @@ end) if not wlan then error("no wireless interface") end -wlan.open(9999) +wlan.open(9998) local respondedTurtles = {} local keyStates = {} @@ -17,19 +17,19 @@ function() elseif ev == "key_up" then keyStates[keycode] = false end - if not repeating then + if ev == "key" and not repeating then if keycode == keys.up then - wlan.transmit(9999, 9999, { _ = "move", dir = "fwd", dig = keyStates[keys.leftShift] }) + wlan.transmit(9999, 9998, { _ = "move", dir = "fwd", dig = keyStates[keys.leftShift] }) elseif keycode == keys.down then - wlan.transmit(9999, 9999, { _ = "move", dir = "bck" }) + wlan.transmit(9999, 9998, { _ = "move", dir = "bck" }) elseif keycode == keys.left then - wlan.transmit(9999, 9999, { _ = "move", dir = "rotl" }) + wlan.transmit(9999, 9998, { _ = "move", dir = "rotl" }) elseif keycode == keys.right then - wlan.transmit(9999, 9999, { _ = "move", dir = "rotr" }) + wlan.transmit(9999, 9998, { _ = "move", dir = "rotr" }) elseif keycode == keys.pageUp then - wlan.transmit(9999, 9999, { _ = "move", dir = "up", dig = keyStates[keys.leftShift] }) + wlan.transmit(9999, 9998, { _ = "move", dir = "up", dig = keyStates[keys.leftShift] }) elseif keycode == keys.pageDown then - wlan.transmit(9999, 9999, { _ = "move", dir = "down", dig = keyStates[keys.leftShift] }) + wlan.transmit(9999, 9998, { _ = "move", dir = "down", dig = keyStates[keys.leftShift] }) end end end @@ -38,7 +38,7 @@ end, function() while true do local _, side, chan, rchan, data, dist = os.pullEvent("modem_message") - if chan == 9999 and rchan == chan then + if chan == 9998 and rchan == 9999 then if data._ == "WakeUp" then respondedTurtles[data.from] = { true, "Hello!" } elseif data._ == "Ack" then diff --git a/turtos/main.lua b/turtos/main.lua index a92cd05..5786cef 100644 --- a/turtos/main.lua +++ b/turtos/main.lua @@ -8,7 +8,7 @@ wlan.open(9999) local ID = os.getComputerID() -wlan.transmit(9999, 9999, { +wlan.transmit(9998, 9999, { ["_"] = "WakeUp", ["from"] = ID }) @@ -16,7 +16,7 @@ wlan.transmit(9999, 9999, { parallel.waitForAll(function() while true do local _, side, chan, rchan, data, dist = os.pullEvent("modem_message") - if chan == 9999 and chan == rchan then + if chan == 9999 and rchan == 9998 then if data._ == "move" then wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID }) if data.tgt == nil or data.tgt == ID then diff --git a/turtos/startup.lua b/turtos/startup.lua index dd4a281..d028d0f 100644 --- a/turtos/startup.lua +++ b/turtos/startup.lua @@ -3,6 +3,8 @@ if not turtle then return end +local base_path = "https://git.salushnes.solutions/hkc/cc-stuff/raw/branch/master/turtos" + _G._TOS_VER = "N/A" local tos_ver_fp = io.open("/.tos-ver", "r") if tos_ver_fp then @@ -10,12 +12,28 @@ if tos_ver_fp then tos_ver_fp:close() end -if fs.exists("/disk/update.json") then - local info_fp = io.open("/disk/update.json", "r") - local info = textutils.unserializeJSON(info_fp:read("a")) - info_fp:close() +local function getFile(url, path) + local r, err = http.get(url) + io.write("GET " .. path .. " ... ") + if not r then + print("FAIL: " .. err) + return false, err + end + io.open(path, "w"):write(r.readAll()):close() + io.write("OK\n") +end + + +local req, err = http.get(base_path .. "/update.json") +if not req then + printError("Failed to get update info:", err) +else + local info = textutils.unserializeJSON(req.readAll()) + req.close() + print("OTP version: " .. info.ver) print("H/W version: " .. _TOS_VER) + if info.ver == _TOS_VER then print("Running on latest firmware") else @@ -27,8 +45,7 @@ if fs.exists("/disk/update.json") then term.write(" [DEL] ...") fs.delete(file.dst) end - fs.copy("/disk" .. file.src, file.dst) - print(" OK, copied from", file.src) + getFile(base_path .. file.src, file.dst) end print("Writing new version info") io.open("/.tos-ver", "w"):write(info.ver):close() diff --git a/turtos/update.json b/turtos/update.json index 3baafbb..dfdfd27 100644 --- a/turtos/update.json +++ b/turtos/update.json @@ -1,5 +1,5 @@ { - "ver": "0.0.2", + "ver": "0.0.3", "files": [ { "src": "/startup.lua", "dst": "/startup.lua" }, { "src": "/main.lua", "dst": "/main.lua" }