This commit is contained in:
Laurie Fisher 2025-06-27 15:29:05 +01:00
parent 53f96bf5db
commit 890bfad815
Signed by: PinkEyedOrphan
GPG Key ID: 7F827B68147AEE76
2 changed files with 34 additions and 3 deletions

View File

@ -4,6 +4,7 @@ local logger = require("libs.logger")
local tutil = require("libs.turtleutils") local tutil = require("libs.turtleutils")
local currentStatus = "idle" local currentStatus = "idle"
local statusCallback = nil
function M.getStatus() function M.getStatus()
return currentStatus return currentStatus
@ -13,10 +14,35 @@ function M.setStatus(value)
currentStatus = value currentStatus = value
end end
function M.setStatusCallback(fn)
statusCallback = fn
end
-- Position and status reporting coroutine
local function reportStatusLoop()
while true do
local x, y, z, gpsActive = tutil.getPosition()
local posText = gpsActive and
string.format("GPS (%d,%d,%d)", x, y, z) or
string.format("Manual (%d,%d,%d)", x, y, z)
if statusCallback then
statusCallback(currentStatus .. " @ " .. posText)
end
logger.log("Status: " .. currentStatus .. " | " .. posText)
sleep(5)
end
end
function M.run() function M.run()
-- Register the status setter for turtleutils to call -- Register the status setter so turtleutils can call M.setStatus
tutil.setStatusCallback(M.setStatus) tutil.setStatusCallback(M.setStatus)
-- Start reporting coroutine
local reporter = coroutine.create(reportStatusLoop)
coroutine.resume(reporter)
while true do while true do
currentStatus = "moving backward" currentStatus = "moving backward"
logger.log("Started moving backward") logger.log("Started moving backward")
@ -31,6 +57,11 @@ function M.run()
tutil.forward() tutil.forward()
sleep(0.5) sleep(0.5)
end end
-- Allow reporter coroutine to yield and resume
if coroutine.status(reporter) == "suspended" then
coroutine.resume(reporter)
end
end end
end end

View File

@ -99,12 +99,12 @@ local function refuelIfNeeded()
local x, y, z, gpsActive = M.getPosition() local x, y, z, gpsActive = M.getPosition()
local posText = gpsActive and local posText = gpsActive and
string.format("GPS (%d,%d,%d)", x, y, z) or string.format("GPS (%d,%d,%d)", x, y, z) or
string.format("Manual (%d,%d,%d)", x, y, z) string.format("Travelled: (%d,%d,%d)", x, y, z)
if statusCallback then if statusCallback then
statusCallback("out_of_fuel " .. posText) statusCallback("out_of_fuel " .. posText)
end end
logger.log("No fuel. Waiting for fuel... Location: " .. posText) logger.log("No fuel. Waiting for fuel... Location: " .. posText)
sleep(5) sleep(5)