diff --git a/pocketremote.lua b/pocketremote.lua index f1aadfb..a9d8584 100644 --- a/pocketremote.lua +++ b/pocketremote.lua @@ -23,7 +23,7 @@ local w, h = term.getSize() -- Tracked turtles local turtles = {} local selectedTurtle = nil -local viewMode = "overview" -- overview, detail, manual +local viewMode = "overview" -- overview, detail, manual, modes -- Button system local buttons = {} @@ -90,6 +90,16 @@ local function sendCommand(turtleID, command, param) }) 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 local function formatFuel(fuel) if not fuel then @@ -127,7 +137,7 @@ local function drawOverview() -- Turtle info box term.setCursorPos(1, y) 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 print(string.format(" %d,%d,%d", @@ -197,7 +207,7 @@ local function drawDetail() term.setTextColor(colors.white) print("") - print("Mode: " .. (turtle.mode or "unknown")) + print("State: " .. (turtle.state or turtle.mode or "idle")) print("Fuel: " .. formatFuel(turtle.fuel)) if turtle.position then @@ -249,12 +259,16 @@ local function drawDetail() end, colors.red) btnY = h - 4 - addButton(1, btnY, 12, 2, "MANUAL", function() + addButton(1, btnY, 8, 2, "MANUAL", function() viewMode = "manual" sendCommand(turtle.turtleID, "manual") 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") end, colors.blue) @@ -369,6 +383,80 @@ local function drawManual() term.setTextColor(colors.white) 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() if viewMode == "overview" then drawOverview() @@ -376,6 +464,8 @@ local function draw() drawDetail() elseif viewMode == "manual" then drawManual() + elseif viewMode == "modes" then + drawModes() end end @@ -458,6 +548,10 @@ parallel.waitForAny( local found = false for i, t in ipairs(turtles) do 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 found = true break @@ -465,6 +559,9 @@ parallel.waitForAny( end if not found then + if not message.state then + message.state = message.mode or "idle" + end table.insert(turtles, message) if not selectedTurtle then selectedTurtle = 1 @@ -472,8 +569,16 @@ parallel.waitForAny( end draw() - elseif channel == CHANNEL_RECEIVE and type(message) == "table" and message.status then - -- Response from turtle + elseif channel == CHANNEL_RECEIVE and type(message) == "table" then + -- 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() end end