test
This commit is contained in:
parent
11ecdb960f
commit
353d13fee8
25
behavior.lua
Normal file
25
behavior.lua
Normal file
@ -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
|
||||
@ -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
|
||||
)
|
||||
40
status.lua
Normal file
40
status.lua
Normal file
@ -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
|
||||
@ -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
|
||||
Loading…
Reference in New Issue
Block a user