Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --VAR
- ThisLevel = 0
- ----
- IsReady = false -- lift is ready, don't moving and await for commands
- CurrentLevel = 0
- TargetLevel = 0
- SensorSide = "bottom"
- b_start_x = 1
- b_start_y = 1
- b_width = 6
- b_height = 3
- --initialize
- monitor = peripheral.wrap("top")
- modem = peripheral.wrap("back")
- modem.open(21)
- ------------------
- ---lift client start up
- function Initialize()
- monitor.clear()
- ProcessRedstone()
- WriteButton()
- modem.transmit(21, 22, "request")
- end
- --
- function ProcessCommand(command)
- write("received=")
- print(command)
- --
- if (command == "moving 0") then
- IsReady = false
- TargetLevel = 0
- end
- if (command == "moving 1") then
- IsReady = false
- TargetLevel = 1
- end
- if (command == "contact 0") then
- CurrentLevel = 0
- end
- if (command == "contact 1") then
- CurrentLevel = 1
- end
- --
- if (IsReady) then
- if (command == "lock 0 UP") then
- --
- end
- if (command == "lock 0 DOWN") then
- --
- end
- end
- --if IsReady == false
- if (command == "ready" or command == "init") then
- IsReady = true
- end
- WriteButton()
- end
- --
- function ProcessRedstone()
- detector = rs.getInput(SensorSide)
- if (detector == true) then
- CurrentLevel = ThisLevel
- modem.transmit(21, 22, "contact "..ThisLevel)
- WriteButton()
- end
- end
- --Write On/Off button
- function WriteButton()
- if (IsReady == false) then
- monitor.clear()
- return
- end
- monitor.setTextColor(colors.white)
- if (CurrentLevel == ThisLevel) then
- monitor.setBackgroundColour((colours.lime))
- monitor.setCursorPos(b_start_x,b_start_y)
- monitor.write(" ")
- monitor.setCursorPos(b_start_x,b_start_y+1)
- monitor.write(" MOVE ")
- monitor.setCursorPos(b_start_x,b_start_y+2)
- monitor.write(" ")
- else
- monitor.setBackgroundColour((colours.red))
- monitor.setCursorPos(b_start_x,b_start_y)
- monitor.write(" ")
- monitor.setCursorPos(b_start_x,b_start_y+1)
- monitor.write(" CALL ")
- monitor.setCursorPos(b_start_x,b_start_y+2)
- monitor.write(" ")
- end
- monitor.setBackgroundColour((colours.black))
- end
- --
- function ProcessClick(mouseX, mouseY)
- if (IsReady == false) then
- return
- end
- if (mouseX >= b_start_x and mouseX < b_start_x + b_width and mouseY > b_start_y and mouseY < b_start_y + b_height) then
- if (CurrentLevel == ThisLevel) then
- TargetLevel = 1
- modem.transmit(21, 22, "call 1")
- else
- TargetLevel = ThisLevel --0
- modem.transmit(21, 22, "call "..ThisLevel )
- end
- end
- end
- --initialize
- Initialize()
- --Main loop
- repeat
- event,p1,p2,p3,p4,p5 = os.pullEvent()
- --
- if (event=="redstone") then
- ProcessRedstone()
- end
- if (event=="modem_message") then
- --event, side, channel, replyChannel, message, distance
- ProcessCommand(p4)
- end
- if event=="monitor_touch" then
- ProcessClick(p2, p3) -- this runs our function
- end
- until event=="char" and p1==("q")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement