From 353d13fee87f84a7124a201930dc4fe6f8275d81 Mon Sep 17 00:00:00 2001 From: Laurie Fisher Date: Fri, 27 Jun 2025 14:17:07 +0100 Subject: [PATCH] test --- behavior.lua | 25 +++++++++++++++++++++++++ startup.lua | 8 ++++++++ status.lua | 40 ++++++++++++++++++++++++++++++++++++++++ status_broadcast.lua | 43 ------------------------------------------- 4 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 behavior.lua create mode 100644 status.lua delete mode 100644 status_broadcast.lua diff --git a/behavior.lua b/behavior.lua new file mode 100644 index 0000000..9ae285f --- /dev/null +++ b/behavior.lua @@ -0,0 +1,25 @@ +local M = {} + +local currentStatus = "idle" + +function M.getStatus() + return currentStatus +end + +function M.run() + while true do + currentStatus = "moving backward" + for i = 1, 10 do + turtle.back() + sleep(0.5) + end + + currentStatus = "moving forward" + for i = 1, 10 do + turtle.forward() + sleep(0.5) + end + end +end + +return M \ No newline at end of file diff --git a/startup.lua b/startup.lua index e69de29..98cb25b 100644 --- a/startup.lua +++ b/startup.lua @@ -0,0 +1,8 @@ +local behavior = require("behavior") +local status = require("status") + +-- Run behavior and status reporting in parallel +parallel.waitForAny( + behavior.run, + function() status.reportStatus(behavior.getStatus) end +) \ No newline at end of file diff --git a/status.lua b/status.lua new file mode 100644 index 0000000..7b3af0f --- /dev/null +++ b/status.lua @@ -0,0 +1,40 @@ +local M = {} + +local modemOpened = false +for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do + if peripheral.getType(side) == "modem" then + rednet.open(side) + modemOpened = true + break + end +end + +if not modemOpened then + print("No modem found!") + +end + +local id = os.getComputerID() +local label = os.getComputerLabel() or ("Turtle_" .. id) + +function M.reportStatus(getStatusFn) + while true do + local status = getStatusFn() + local msg = { + id = id, + label = label, + status = status, + time = os.time() + } + rednet.broadcast(textutils.serialize(msg), "turtle_status") + + -- Optional: print for debug + term.setCursorPos(1, 5) + term.clearLine() + print("Broadcasted: " .. status .. " @ " .. textutils.formatTime(os.time(), true)) + + sleep(2) + end +end + +return M diff --git a/status_broadcast.lua b/status_broadcast.lua deleted file mode 100644 index 196cdac..0000000 --- a/status_broadcast.lua +++ /dev/null @@ -1,43 +0,0 @@ -local id = os.getComputerID() -local label = os.getComputerLabel() or ("Turtle_" .. id) -local status = "idle" -- Can be changed based on the turtle's job - --- Try to open modem -local modemOpened = false -for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do - if peripheral.getType(side) == "modem" then - rednet.open(side) - modemOpened = true - break - end -end - -if not modemOpened then - print("No modem found. Aborting.") - return -end - --- Display header -term.clear() -term.setCursorPos(1, 1) -print("Status Broadcaster") -print("------------------") - -while true do - local msg = { - id = id, - label = label, - status = status, - time = os.time() - } - - local serialized = textutils.serialize(msg) - rednet.broadcast(serialized, "turtle_status") - - -- Print to screen - term.setCursorPos(1, 5) - term.clearLine() - print("Sent at: " .. os.date("%H:%M:%S")) - - sleep(5) -end