Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Program to monitor turtle status
- -- <[email protected]>
- os.loadAPI("flex.lua") -- Assuming flex.lua is on the pocket computer
- local status_listen_channel = 6465 -- Channel to listen on for status updates from the turtle
- print("DEBUG: Starting receive_pocket.lua (Status Monitor)") -- Debug print
- local modem
- print("DEBUG: Looking for modem peripheral.") -- Debug print
- local p = flex.getPeripheral("modem")
- if #p > 0 then
- print("DEBUG: Modem peripheral found: " .. tostring(p[1])) -- Debug print
- modem = peripheral.wrap(p[1])
- modem.open(status_listen_channel) -- Open the modem on the status listen channel
- print("DEBUG: Modem opened on channel " .. tostring(status_listen_channel)) -- Debug print
- else
- print("DEBUG: No modem peripheral found.") -- Debug print
- flex.printColors("Please attach a wireless or ender modem\n", colors.red)
- sleep(2)
- return
- end
- local last_status = nil -- Variable to store the last received status
- local function displayStatus()
- term.clear()
- term.setCursorPos(1, 1)
- print("--- Turtle Status ---")
- if last_status == nil then
- print("Waiting for status update...")
- else
- print("Turtle ID: " .. tostring(last_status.id))
- if last_status.label and last_status.label ~= "" then
- print("Turtle Label: " .. tostring(last_status.label))
- end
- print("Fuel: " .. tostring(last_status.fuel))
- print("Position: X=" .. tostring(last_status.position.x) .. ", Y=" .. tostring(last_status.position.y) .. ", Z=" .. tostring(last_status.position.z))
- print("Mining: " .. tostring(last_status.is_mining))
- print("Estimated Time: " .. tostring(last_status.estimated_time))
- print("\nInventory Summary:")
- if last_status.inventory_summary and #last_status.inventory_summary > 0 then
- for _, item in ipairs(last_status.inventory_summary) do
- print(" " .. item.name .. " (" .. tostring(item.count) .. ")")
- end
- else
- print(" Inventory empty or not detailed.")
- end
- end
- print("\nWaiting for next update...")
- -- print("DEBUG: Display updated.") -- Optional debug
- end
- print("Waiting for status message on channel " .. tostring(status_listen_channel) .. "...")
- while true do
- displayStatus() -- Update display
- -- Use os.pullEvent with a timeout to allow display updates even without messages
- local event, modemSide, senderChannel, replyChannel, message, senderDistance =
- os.pullEvent("modem_message", 0.5) -- Add a small timeout
- if event == "modem_message" then
- -- print("DEBUG: Received modem message event on channel " .. tostring(senderChannel)) -- Optional debug
- -- Check if the message is a status update and from the expected channel
- if senderChannel == status_listen_channel and type(message) == "table" and message.type == "status_update" then
- -- print("DEBUG: Received valid status update.") -- Optional debug
- last_status = message -- Store the latest status
- -- else
- -- print("DEBUG: Received unexpected message format or channel.") -- Optional debug
- -- print("DEBUG: Sender Channel: "..tostring(senderChannel)..", Message Type: "..(type(message) == "table" and tostring(message.type) or type(message)))
- end
- end
- -- The loop will naturally call displayStatus() again after the event or timeout
- end
- -- No cleanup needed as the script runs in a loop until stopped
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement