Advertisement
Micrafast

Drone client

Sep 8th, 2018
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. local useport = 1351 -- make sure this number is the same as the drone program
  2.  
  3. local component = require("component")
  4. if not component.isAvailable("modem") then
  5.     io.stderr:write("This program needs a modem to run.\n")
  6.     return
  7. end
  8.  
  9. local event = require("event")
  10. local seri = require("serialization")
  11. local modem = component.modem
  12.  
  13. local RED, YELLOW, GREEN, BLUE, CLEAR = "\27[31m", "\27[33m", "\27[32m", "\27[36m", "\27[0m"
  14.  
  15. modem.open(useport)
  16. modem.broadcast(useport, "_BROADCAST_TARGETS_PS_BOOT")
  17. print(YELLOW.."Waiting for drones booting..."..CLEAR)
  18. os.sleep(1)
  19. local droneaddresses = {}
  20. modem.broadcast(useport, "_BROADCAST_TARGETS_PS_K")
  21. while true do
  22.     local name, _, sender = event.pull(2, "modem_message")
  23.     if name == "modem_message" then
  24.         table.insert(droneaddresses, sender)
  25.     else
  26.         break
  27.     end
  28. end
  29. print(BLUE.."Select a drone:"..CLEAR)
  30. for i, v in ipairs(droneaddresses) do
  31.     print(tostring(i)..". "..v)
  32. end
  33. io.write(BLUE.."Input number:"..CLEAR)
  34. local drone = droneaddresses[tonumber(io.read())]
  35.  
  36. local function invoke(command)
  37.     modem.send(drone, useport, command)
  38.     local name, _, _, _, _, ret = event.pull(20, "modem_message")
  39.     if name == "modem_message" then
  40.         return ret
  41.     else
  42.         return RED.."Response timed out."..CLEAR
  43.     end
  44. end
  45.  
  46. print(GREEN.."Drone remote controller"..CLEAR)
  47. print(GREEN.."Programmed by Micrafast"..CLEAR)
  48. print(BLUE..'Type "exit" to exit.'..CLEAR)
  49. while true do
  50.     io.write(BLUE..drone.."> "..CLEAR)
  51.     local cmd = io.read()
  52.     if cmd == "exit" then
  53.         print(YELLOW.."Bye!"..CLEAR)
  54.         return
  55.     end
  56.     print(invoke(cmd))
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement