feat: Add state management and modes for turtle commands in Pocket Computer
This commit is contained in:
119
pocketremote.lua
119
pocketremote.lua
@@ -23,7 +23,7 @@ local w, h = term.getSize()
|
|||||||
-- Tracked turtles
|
-- Tracked turtles
|
||||||
local turtles = {}
|
local turtles = {}
|
||||||
local selectedTurtle = nil
|
local selectedTurtle = nil
|
||||||
local viewMode = "overview" -- overview, detail, manual
|
local viewMode = "overview" -- overview, detail, manual, modes
|
||||||
|
|
||||||
-- Button system
|
-- Button system
|
||||||
local buttons = {}
|
local buttons = {}
|
||||||
@@ -90,6 +90,16 @@ local function sendCommand(turtleID, command, param)
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Send state change command to turtle (new protocol)
|
||||||
|
local function sendStateCommand(turtleID, stateName, stateData)
|
||||||
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
|
type = "state_change",
|
||||||
|
state = stateName,
|
||||||
|
data = stateData or {},
|
||||||
|
target = turtleID
|
||||||
|
})
|
||||||
|
end
|
||||||
|
|
||||||
-- Helper function to format fuel display
|
-- Helper function to format fuel display
|
||||||
local function formatFuel(fuel)
|
local function formatFuel(fuel)
|
||||||
if not fuel then
|
if not fuel then
|
||||||
@@ -127,7 +137,7 @@ local function drawOverview()
|
|||||||
-- Turtle info box
|
-- Turtle info box
|
||||||
term.setCursorPos(1, y)
|
term.setCursorPos(1, y)
|
||||||
term.setTextColor(selected and colors.lime or colors.white)
|
term.setTextColor(selected and colors.lime or colors.white)
|
||||||
print(string.format("T-%d [%s]", turtle.turtleID or 0, turtle.mode or "unknown"))
|
print(string.format("T-%d [%s]", turtle.turtleID or 0, turtle.state or turtle.mode or "idle"))
|
||||||
|
|
||||||
if turtle.position then
|
if turtle.position then
|
||||||
print(string.format(" %d,%d,%d",
|
print(string.format(" %d,%d,%d",
|
||||||
@@ -197,7 +207,7 @@ local function drawDetail()
|
|||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
|
|
||||||
print("")
|
print("")
|
||||||
print("Mode: " .. (turtle.mode or "unknown"))
|
print("State: " .. (turtle.state or turtle.mode or "idle"))
|
||||||
print("Fuel: " .. formatFuel(turtle.fuel))
|
print("Fuel: " .. formatFuel(turtle.fuel))
|
||||||
|
|
||||||
if turtle.position then
|
if turtle.position then
|
||||||
@@ -249,12 +259,16 @@ local function drawDetail()
|
|||||||
end, colors.red)
|
end, colors.red)
|
||||||
|
|
||||||
btnY = h - 4
|
btnY = h - 4
|
||||||
addButton(1, btnY, 12, 2, "MANUAL", function()
|
addButton(1, btnY, 8, 2, "MANUAL", function()
|
||||||
viewMode = "manual"
|
viewMode = "manual"
|
||||||
sendCommand(turtle.turtleID, "manual")
|
sendCommand(turtle.turtleID, "manual")
|
||||||
end, colors.purple)
|
end, colors.purple)
|
||||||
|
|
||||||
addButton(14, btnY, 12, 2, "SET HOME", function()
|
addButton(10, btnY, 8, 2, "MODES", function()
|
||||||
|
viewMode = "modes"
|
||||||
|
end, colors.cyan)
|
||||||
|
|
||||||
|
addButton(19, btnY, 7, 2, "HOME*", function()
|
||||||
sendCommand(turtle.turtleID, "setHome")
|
sendCommand(turtle.turtleID, "setHome")
|
||||||
end, colors.blue)
|
end, colors.blue)
|
||||||
|
|
||||||
@@ -369,6 +383,80 @@ local function drawManual()
|
|||||||
term.setTextColor(colors.white)
|
term.setTextColor(colors.white)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function drawModes()
|
||||||
|
if not selectedTurtle or not turtles[selectedTurtle] then
|
||||||
|
viewMode = "overview"
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
clearButtons()
|
||||||
|
local turtle = turtles[selectedTurtle]
|
||||||
|
|
||||||
|
term.setBackgroundColor(colors.black)
|
||||||
|
term.clear()
|
||||||
|
term.setCursorPos(1, 1)
|
||||||
|
term.setTextColor(colors.cyan)
|
||||||
|
print("=STATE MACHINE=")
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
print(string.format("T-%d [%s]", turtle.turtleID or 0, turtle.state or turtle.mode or "idle"))
|
||||||
|
|
||||||
|
-- State buttons
|
||||||
|
local btnY = 4
|
||||||
|
local btnW = 12
|
||||||
|
|
||||||
|
addButton(1, btnY, btnW, 2, "IDLE", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "idle")
|
||||||
|
sendCommand(turtle.turtleID, "stop")
|
||||||
|
end, colors.gray)
|
||||||
|
|
||||||
|
addButton(14, btnY, btnW, 2, "EXPLORE", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "exploring")
|
||||||
|
sendCommand(turtle.turtleID, "explore")
|
||||||
|
end, colors.green)
|
||||||
|
|
||||||
|
btnY = btnY + 3
|
||||||
|
addButton(1, btnY, btnW, 2, "MINE", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "mining")
|
||||||
|
sendCommand(turtle.turtleID, "explore")
|
||||||
|
end, colors.orange)
|
||||||
|
|
||||||
|
addButton(14, btnY, btnW, 2, "FARM", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "farming")
|
||||||
|
end, colors.lime)
|
||||||
|
|
||||||
|
btnY = btnY + 3
|
||||||
|
addButton(1, btnY, btnW, 2, "GO HOME", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "goHome")
|
||||||
|
sendCommand(turtle.turtleID, "returnHome")
|
||||||
|
end, colors.yellow)
|
||||||
|
|
||||||
|
addButton(14, btnY, btnW, 2, "REFUEL", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "refueling")
|
||||||
|
sendCommand(turtle.turtleID, "refuel")
|
||||||
|
end, colors.red)
|
||||||
|
|
||||||
|
btnY = btnY + 3
|
||||||
|
addButton(1, btnY, btnW, 2, "DUMP INV", function()
|
||||||
|
sendStateCommand(turtle.turtleID, "dumpInventory")
|
||||||
|
end, colors.brown)
|
||||||
|
|
||||||
|
addButton(14, btnY, btnW, 2, "MOVE TO", function()
|
||||||
|
-- Could prompt for coordinates, for now just sends moving state
|
||||||
|
sendStateCommand(turtle.turtleID, "moving")
|
||||||
|
end, colors.lightBlue)
|
||||||
|
|
||||||
|
-- Back button
|
||||||
|
addButton(1, h - 1, 12, 2, "< BACK", function()
|
||||||
|
viewMode = "detail"
|
||||||
|
end, colors.gray)
|
||||||
|
|
||||||
|
for _, btn in ipairs(buttons) do
|
||||||
|
drawButton(btn)
|
||||||
|
end
|
||||||
|
|
||||||
|
term.setTextColor(colors.white)
|
||||||
|
end
|
||||||
|
|
||||||
local function draw()
|
local function draw()
|
||||||
if viewMode == "overview" then
|
if viewMode == "overview" then
|
||||||
drawOverview()
|
drawOverview()
|
||||||
@@ -376,6 +464,8 @@ local function draw()
|
|||||||
drawDetail()
|
drawDetail()
|
||||||
elseif viewMode == "manual" then
|
elseif viewMode == "manual" then
|
||||||
drawManual()
|
drawManual()
|
||||||
|
elseif viewMode == "modes" then
|
||||||
|
drawModes()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -458,6 +548,10 @@ parallel.waitForAny(
|
|||||||
local found = false
|
local found = false
|
||||||
for i, t in ipairs(turtles) do
|
for i, t in ipairs(turtles) do
|
||||||
if t.turtleID == message.turtleID then
|
if t.turtleID == message.turtleID then
|
||||||
|
-- Preserve state if not in message
|
||||||
|
if not message.state then
|
||||||
|
message.state = t.state or message.mode or "idle"
|
||||||
|
end
|
||||||
turtles[i] = message
|
turtles[i] = message
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
@@ -465,6 +559,9 @@ parallel.waitForAny(
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not found then
|
if not found then
|
||||||
|
if not message.state then
|
||||||
|
message.state = message.mode or "idle"
|
||||||
|
end
|
||||||
table.insert(turtles, message)
|
table.insert(turtles, message)
|
||||||
if not selectedTurtle then
|
if not selectedTurtle then
|
||||||
selectedTurtle = 1
|
selectedTurtle = 1
|
||||||
@@ -472,8 +569,16 @@ parallel.waitForAny(
|
|||||||
end
|
end
|
||||||
|
|
||||||
draw()
|
draw()
|
||||||
elseif channel == CHANNEL_RECEIVE and type(message) == "table" and message.status then
|
elseif channel == CHANNEL_RECEIVE and type(message) == "table" then
|
||||||
-- Response from turtle
|
-- State change confirmation or other response
|
||||||
|
if message.type == "state_changed" and message.turtleID then
|
||||||
|
for i, t in ipairs(turtles) do
|
||||||
|
if t.turtleID == message.turtleID then
|
||||||
|
turtles[i].state = message.state
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
draw()
|
draw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user