SY573M_404

Untitled

May 28th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. local FLOOR = 1
  2. local expect = dofile("rom/modules/main/cc/expect.lua").expect
  3.  
  4. function rednet_or_redstone(protocol_filter, timeout)
  5. -- The parameters used to be ( nTimeout ), detect this case for backwards compatibility
  6. if type(protocol_filter) == "number" and timeout == nil then
  7. protocol_filter, timeout = nil, protocol_filter
  8. end
  9. expect(1, protocol_filter, "string", "nil")
  10. expect(2, timeout, "number", "nil")
  11.  
  12. -- Start the timer
  13. local timer = nil
  14. if timeout then
  15. timer = os.startTimer(timeout)
  16. end
  17.  
  18. -- Wait for events
  19. while true do
  20. local event, p1, p2, p3 = os.pullEvent()
  21. if event == "rednet_message" then
  22. -- Return the first matching rednet_message
  23. local sender_id, message, protocol = p1, p2, p3
  24. if protocol_filter == nil or protocol == protocol_filter then
  25. return "rednet_message", sender_id, message, protocol
  26. end
  27. elseif event == "timer" then
  28. -- Return nil if we timeout
  29. if p1 == timer then
  30. return nil
  31. end
  32. elseif event == "redstone" then
  33. return "redstone"
  34. end
  35. end
  36. end
  37.  
  38. local monitor = peripheral.find("monitor")
  39.  
  40. monitor.setBackgroundColor(colors.white)
  41. monitor.setTextColor(colors.black)
  42. monitor.setTextScale(5)
  43. monitor.clear()
  44.  
  45. function display_floor(floor)
  46. monitor.clear()
  47. monitor.setCursorPos(1, 1)
  48. monitor.write(floor)
  49. end
  50.  
  51. peripheral.find("modem", rednet.open)
  52.  
  53. while true do
  54. local type, id, message = rednet_or_redstone()
  55.  
  56. if type == "redstone" then
  57. if redstone.getInput("back") then
  58. local data = {
  59. event = "arrived",
  60. floor = FLOOR
  61. }
  62.  
  63. rednet.broadcast(textutils.serialise(data))
  64.  
  65. display_floor(FLOOR)
  66. end
  67. elseif type == "rednet_message" then
  68. local data = textutils.unserialise(message)
  69. local event = data["event"]
  70.  
  71. if event == "arrived" then
  72. local floor = data["floor"]
  73.  
  74. display_floor(floor)
  75. end
  76. end
  77. end
  78.  
Advertisement
Add Comment
Please, Sign In to add comment