Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Init device
- local function initDevice()
- rednet.open("back")
- return peripheral.wrap("front")
- end
- -- Init floor
- local function initFloor()
- tab = {}
- tab[0] = "B1"
- return tab
- end
- -- Get floor
- local function getFloor(mainID)
- local sender, msg, distance = rednet.receive(0.5)
- local nameFloor = "CF"
- -- Message receive ?
- if sender ~= nil and msg ~= nil and sender == mainID then
- local tbl = textutils.unserialize(msg)
- if tbl["floor"] ~= nil then
- nameFloor = tbl["floor"]
- end
- end
- return nameFloor
- end
- -- Adding a floor for main PC
- local function callLift(mon)
- local i = 0
- local j = 0
- mon.clear()
- -- Writing text in the screen
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- for i = 1, 5, 1 do
- mon.setCursorPos(1, i)
- mon.write(" ")
- end
- mon.setBackgroundColor(colors.white)
- mon.setTextColor(colors.black)
- for i = 2, 4, 1 do
- mon.setCursorPos(1, i)
- mon.write(" ")
- end
- mon.setCursorPos(1, 3)
- mon.write(" APPEL ")
- -- Test touch screen
- local e, side, x, y = os.pullEvent("monitor_touch")
- if x > 0 and x < 8 and y > 1 and y < 5 then
- return true
- else
- return false
- end
- -- Reset monitor option
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.setCursorPos(1, 1)
- end
- local function choiceFloor(mon, tab, select)
- local i = 0
- local j = 0
- local s = "N"
- mon.clear()
- -- Writing text in the screen
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.red)
- mon.setCursorPos(2, 1)
- mon.write("ETAGE")
- -- More button
- mon.setBackgroundColor(colors.red)
- mon.setTextColor(colors.black)
- mon.setCursorPos(1, 5)
- mon.write(" + ")
- -- Less button
- mon.setCursorPos(5, 5)
- mon.write(" - ")
- -- Writing floors
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.setCursorPos(3, 3)
- mon.write(">"..tab[select])
- -- Test touch screen
- local e, side, x, y = os.pullEvent("monitor_touch")
- if x > 2 and x < 7 and y > 2 and y < 4 then
- s = tab[select]
- elseif x > 0 and x < 4 and y > 4 and y < 6 then
- s = "PM"
- elseif x > 5 and x < 8 and y > 4 and y < 6 then
- s = "PL"
- end
- -- Reset monitor option
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.setCursorPos(1, 1)
- return s
- end
- -- Check select and send to main PC
- local function checkSend(test, tab, mPC)
- result = false
- for i = 0, #tab do
- if tab[i] == test then
- -- Send
- finish = false
- tab = {}
- tab["floor"] = test
- while finish == false do
- rednet.send(mainPC, textutils.serialize(tab))
- local id, msg, distance = rednet.receive(0.5)
- if msg ~= nil and id == mPC then
- if msg == "ok_call" then
- print("Call accepted")
- finish = true
- end
- end
- end
- finish = false
- result = true
- end
- end
- return result
- end
- -- Main function fields
- local present = false
- local nameFloor = "GF"
- local actualFloor = "CF"
- local floorSelect = "CF"
- local tempSelect = 0
- local mainPC = 8
- local callLaunch = false
- m = initDevice()
- f = initFloor()
- -- Main loop
- while true do
- actualFloor = getFloor(mainPC)
- print("receive")
- -- Call lift or go to next floor
- if actualFloor == nameFloor and choice ~= true then
- callLaunch = false
- floorSelect = choiceFloor(m, f, tempSelect)
- sleep(0.1)
- if floorSelect == "PM" then
- tempSelect = tempSelect + 1
- elseif floorSelect == "PL" then
- tempSelect = tempSelect - 1
- end
- if tempSelect < 0 then
- tempSelect = #f
- elseif tempSelect > #f then
- tempSelect = 0
- end
- if checkSend(floorSelect, f, mainPC) == true then
- --choice = true
- end
- elseif actualFloor ~= nameFloor then
- choice = false
- if callLaunch ~= true then
- callLaunch = callLift(m)
- sleep(0.2)
- -- Launch call to main PC
- if callLaunch == true then
- finish = false
- tab = {}
- tab["floor"] = nameFloor
- while finish == false do
- rednet.send(mainPC, textutils.serialize(tab))
- local id, msg, distance = rednet.receive(0.5)
- if msg ~= nil and id == mainPC then
- if msg == "ok_call" then
- print("Call accepted")
- finish = true
- end
- end
- end
- finish = false
- end
- else
- m.clear()
- -- Writing text in the screen
- m.setBackgroundColor(colors.black)
- m.setTextColor(colors.white)
- for i = 1, 5, 1 do
- m.setCursorPos(1, i)
- m.write(" ")
- end
- end
- end -- IF FLOOR
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement