Advertisement
quenti77

demande étage

Jan 20th, 2013
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.52 KB | None | 0 0
  1. -- Init device
  2. local function initDevice()
  3.     rednet.open("back")
  4.     return peripheral.wrap("front")
  5. end
  6.  
  7. -- Init floor
  8. local function initFloor()
  9.     tab = {}
  10.     tab[0] = "B1"
  11.    
  12.     return tab
  13. end
  14.  
  15. -- Get floor
  16. local function getFloor(mainID)
  17.     local sender, msg, distance = rednet.receive(0.5)
  18.     local nameFloor = "CF"
  19.    
  20.     -- Message receive ?
  21.     if sender ~= nil and msg ~= nil and sender == mainID then
  22.         local tbl = textutils.unserialize(msg)
  23.        
  24.         if tbl["floor"] ~= nil then
  25.             nameFloor = tbl["floor"]
  26.         end
  27.     end
  28.     return nameFloor
  29. end
  30.  
  31. -- Adding a floor for main PC
  32. local function callLift(mon)
  33.     local i = 0
  34.     local j = 0
  35.    
  36.     mon.clear()
  37.    
  38.     -- Writing text in the screen
  39.     mon.setBackgroundColor(colors.black)
  40.     mon.setTextColor(colors.white)
  41.     for i = 1, 5, 1 do
  42.         mon.setCursorPos(1, i)
  43.         mon.write("       ")
  44.     end
  45.    
  46.     mon.setBackgroundColor(colors.white)
  47.     mon.setTextColor(colors.black)
  48.     for i = 2, 4, 1 do
  49.         mon.setCursorPos(1, i)
  50.         mon.write("       ")
  51.     end
  52.     mon.setCursorPos(1, 3)
  53.     mon.write(" APPEL ")
  54.    
  55.     -- Test touch screen
  56.     local e, side, x, y = os.pullEvent("monitor_touch")
  57.    
  58.     if x > 0 and x < 8 and y > 1 and y < 5 then
  59.         return true
  60.     else
  61.         return false
  62.     end
  63.    
  64.     -- Reset monitor option
  65.     mon.setBackgroundColor(colors.black)
  66.     mon.setTextColor(colors.white)
  67.     mon.setCursorPos(1, 1)
  68. end
  69.  
  70. local function choiceFloor(mon, tab, select)
  71.     local i = 0
  72.     local j = 0
  73.     local s = "N"
  74.    
  75.     mon.clear()
  76.    
  77.     -- Writing text in the screen
  78.     mon.setBackgroundColor(colors.black)
  79.     mon.setTextColor(colors.red)
  80.     mon.setCursorPos(2, 1)
  81.     mon.write("ETAGE")
  82.    
  83.     -- More button
  84.     mon.setBackgroundColor(colors.red)
  85.     mon.setTextColor(colors.black)
  86.     mon.setCursorPos(1, 5)
  87.     mon.write(" + ")
  88.    
  89.     -- Less button
  90.     mon.setCursorPos(5, 5)
  91.     mon.write(" - ")
  92.    
  93.     -- Writing floors
  94.     mon.setBackgroundColor(colors.black)
  95.     mon.setTextColor(colors.white)
  96.     mon.setCursorPos(3, 3)
  97.     mon.write(">"..tab[select])
  98.    
  99.     -- Test touch screen
  100.     local e, side, x, y = os.pullEvent("monitor_touch")
  101.    
  102.     if x > 2 and x < 7 and y > 2 and y < 4 then
  103.         s = tab[select]
  104.     elseif x > 0 and x < 4 and y > 4 and y < 6 then
  105.         s = "PM"
  106.     elseif x > 5 and x < 8 and y > 4 and y < 6 then
  107.         s = "PL"
  108.     end
  109.    
  110.     -- Reset monitor option
  111.     mon.setBackgroundColor(colors.black)
  112.     mon.setTextColor(colors.white)
  113.     mon.setCursorPos(1, 1)
  114.    
  115.     return s
  116. end
  117.  
  118. -- Check select and send to main PC
  119. local function checkSend(test, tab, mPC)
  120.     result = false
  121.    
  122.     for i = 0, #tab do
  123.         if tab[i] == test then
  124.             -- Send
  125.             finish = false
  126.             tab = {}
  127.             tab["floor"] = test
  128.            
  129.             while finish == false do
  130.                 rednet.send(mainPC, textutils.serialize(tab))
  131.                
  132.                 local id, msg, distance = rednet.receive(0.5)
  133.                
  134.                 if msg ~= nil and id == mPC then
  135.                     if msg == "ok_call" then
  136.                         print("Call accepted")
  137.                         finish = true
  138.                     end
  139.                 end
  140.             end
  141.             finish = false
  142.             result = true
  143.         end
  144.     end
  145.    
  146.     return result
  147. end
  148.  
  149. -- Main function fields
  150. local present = false
  151. local nameFloor = "GF"
  152. local actualFloor = "CF"
  153. local floorSelect = "CF"
  154. local tempSelect = 0
  155. local mainPC = 8
  156. local callLaunch = false
  157.  
  158. m = initDevice()
  159. f = initFloor()
  160.  
  161. -- Main loop
  162. while true do
  163.     actualFloor = getFloor(mainPC)
  164.     print("receive")
  165.    
  166.     -- Call lift or go to next floor
  167.     if actualFloor == nameFloor and choice ~= true then
  168.         callLaunch = false
  169.         floorSelect = choiceFloor(m, f, tempSelect)
  170.         sleep(0.1)
  171.        
  172.         if floorSelect == "PM" then
  173.             tempSelect = tempSelect + 1
  174.         elseif floorSelect == "PL" then
  175.             tempSelect = tempSelect - 1
  176.         end
  177.        
  178.         if tempSelect < 0 then
  179.             tempSelect = #f
  180.         elseif tempSelect > #f then
  181.             tempSelect = 0
  182.         end
  183.        
  184.         if checkSend(floorSelect, f, mainPC) == true then
  185.             --choice = true
  186.         end
  187.     elseif actualFloor ~= nameFloor then
  188.         choice = false
  189.         if callLaunch ~= true then
  190.             callLaunch = callLift(m)
  191.             sleep(0.2)
  192.            
  193.             -- Launch call to main PC
  194.             if callLaunch == true then
  195.                 finish = false
  196.                 tab = {}
  197.                 tab["floor"] = nameFloor
  198.                
  199.                 while finish == false do
  200.                     rednet.send(mainPC, textutils.serialize(tab))
  201.                    
  202.                     local id, msg, distance = rednet.receive(0.5)
  203.                    
  204.                     if msg ~= nil and id == mainPC then
  205.                         if msg == "ok_call" then
  206.                             print("Call accepted")
  207.                             finish = true
  208.                         end
  209.                     end
  210.                 end
  211.                 finish = false
  212.             end
  213.         else
  214.             m.clear()
  215.    
  216.             -- Writing text in the screen
  217.             m.setBackgroundColor(colors.black)
  218.             m.setTextColor(colors.white)
  219.             for i = 1, 5, 1 do
  220.                 m.setCursorPos(1, i)
  221.                 m.write("       ")
  222.             end
  223.         end
  224.     end -- IF FLOOR
  225. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement