Advertisement
Sedrowow

FancyReciever_Pocket

May 1st, 2025 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. -- Program to monitor turtle status
  2.  
  3. os.loadAPI("flex.lua") -- Assuming flex.lua is on the pocket computer
  4.  
  5. local status_listen_channel = 6465 -- Channel to listen on for status updates from the turtle
  6.  
  7. print("DEBUG: Starting receive_pocket.lua (Status Monitor)") -- Debug print
  8.  
  9. local modem
  10. print("DEBUG: Looking for modem peripheral.") -- Debug print
  11. local p = flex.getPeripheral("modem")
  12. if #p > 0 then
  13.     print("DEBUG: Modem peripheral found: " .. tostring(p[1])) -- Debug print
  14.     modem = peripheral.wrap(p[1])
  15.     modem.open(status_listen_channel) -- Open the modem on the status listen channel
  16.     print("DEBUG: Modem opened on channel " .. tostring(status_listen_channel)) -- Debug print
  17. else
  18.     print("DEBUG: No modem peripheral found.") -- Debug print
  19.     flex.printColors("Please attach a wireless or ender modem\n", colors.red)
  20.     sleep(2)
  21.     return
  22. end
  23.  
  24. local last_status = nil -- Variable to store the last received status
  25.  
  26. local function displayStatus()
  27.     term.clear()
  28.     term.setCursorPos(1, 1)
  29.  
  30.     print("--- Turtle Status ---")
  31.     if last_status == nil then
  32.         print("Waiting for status update...")
  33.     else
  34.         print("Turtle ID: " .. tostring(last_status.id))
  35.         if last_status.label and last_status.label ~= "" then
  36.             print("Turtle Label: " .. tostring(last_status.label))
  37.         end
  38.         print("Fuel: " .. tostring(last_status.fuel))
  39.         print("Position: X=" .. tostring(last_status.position.x) .. ", Y=" .. tostring(last_status.position.y) .. ", Z=" .. tostring(last_status.position.z))
  40.         print("Mining: " .. tostring(last_status.is_mining))
  41.         print("Estimated Time: " .. tostring(last_status.estimated_time))
  42.  
  43.         print("\nInventory Summary:")
  44.         if last_status.inventory_summary and #last_status.inventory_summary > 0 then
  45.             for _, item in ipairs(last_status.inventory_summary) do
  46.                 print("  " .. item.name .. " (" .. tostring(item.count) .. ")")
  47.             end
  48.         else
  49.             print("  Inventory empty or not detailed.")
  50.         end
  51.     end
  52.  
  53.     print("\nWaiting for next update...")
  54.     -- print("DEBUG: Display updated.") -- Optional debug
  55. end
  56.  
  57. print("Waiting for status message on channel " .. tostring(status_listen_channel) .. "...")
  58.  
  59. while true do
  60.     displayStatus() -- Update display
  61.     -- Use os.pullEvent with a timeout to allow display updates even without messages
  62.     local event, modemSide, senderChannel, replyChannel, message, senderDistance =
  63.         os.pullEvent("modem_message", 0.5) -- Add a small timeout
  64.  
  65.     if event == "modem_message" then
  66.         -- print("DEBUG: Received modem message event on channel " .. tostring(senderChannel)) -- Optional debug
  67.         -- Check if the message is a status update and from the expected channel
  68.         if senderChannel == status_listen_channel and type(message) == "table" and message.type == "status_update" then
  69.             -- print("DEBUG: Received valid status update.") -- Optional debug
  70.             last_status = message -- Store the latest status
  71.         -- else
  72.             -- print("DEBUG: Received unexpected message format or channel.") -- Optional debug
  73.             -- print("DEBUG: Sender Channel: "..tostring(senderChannel)..", Message Type: "..(type(message) == "table" and tostring(message.type) or type(message)))
  74.         end
  75.     end
  76.     -- The loop will naturally call displayStatus() again after the event or timeout
  77. end
  78.  
  79. -- No cleanup needed as the script runs in a loop until stopped
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement