Advertisement
balloonanimal

LiftControlL1

Apr 28th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.97 KB | None | 0 0
  1. --Pastebin code: Eps1arN4
  2.  
  3. --local bundleSide = "right"
  4. local modem = peripheral.wrap("top")
  5. local mon = peripheral.wrap("bottom")
  6. local mX, mY = mon.getSize()
  7. mon.setTextScale(1)
  8. mon.setTextColor(colors.white)
  9. local button={}
  10. mon.setBackgroundColor(colors.black)
  11. liftControl = 32
  12.  
  13.  
  14. function MoveUp( dist )
  15.   rednet.send(liftControl, "up")
  16.   local id, message, distance = rednet.receive(5)
  17.   if id == liftControl then
  18.     if message == "ok" then
  19.       rednet.send(liftControl, tostring(dist))
  20.       id, message, distance = rednet.receive()  
  21.     end
  22.   end
  23. end
  24.  
  25. function MoveDown()
  26.   rednet.send(liftControl, "down")
  27.   local id, message, distance = rednet.receive(5)
  28.   if id == liftControl then
  29.     if message == "ok" then
  30.       rednet.send(liftControl, tostring(dist))
  31.       id, message, distance = rednet.receive()  
  32.     end
  33.   end
  34. end
  35.  
  36. function checkxy(x, y)
  37.    local activeFunc = "none"
  38.    print(x.."  "..y)
  39.    for name, data in pairs(button) do
  40.       if y>=data["ymin"] and  y <= data["ymax"] then
  41.          if x>=data["xmin"] and x<= data["xmax"] then
  42.             data["active"] = not data["active"]
  43.             activeFunc = name
  44.             print(name)
  45.          end
  46.       end
  47.    end
  48.    if (activeFunc ~= "none") then
  49.         button[activeFunc]["func"]()
  50.    end
  51. end
  52.  
  53. function fill(text, color, bData)
  54.    mon.setBackgroundColor(color)
  55.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  56.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  57.    for j = bData["ymin"], bData["ymax"] do
  58.       mon.setCursorPos(bData["xmin"], j)
  59.       if j == yspot then
  60.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  61.             if k == xspot then
  62.                mon.write(text)
  63.             else
  64.                mon.write(" ")
  65.             end
  66.          end
  67.       else
  68.          for i = bData["xmin"], bData["xmax"] do
  69.             mon.write(" ")
  70.          end
  71.       end
  72.    end
  73.    mon.setBackgroundColor(colors.black)
  74. end
  75.  
  76. function CreateButton(name, func, minX, minY, maxX, maxY)
  77.         button[name] = {}
  78.         button[name]["func"] = func
  79.         button[name]["active"] = false
  80.         button[name]["xmin"] = minX
  81.         button[name]["ymin"] = minY
  82.         button[name]["xmax"] = maxX
  83.         button[name]["ymax"] = maxY
  84. end
  85.  
  86. function SetUpButtons()
  87.         CreateButton("Sec", Top, 3, 2, 5, 3)
  88.         CreateButton("B", Basement, 2, 11, 3, 12)
  89.         CreateButton("LB", LowerBasement, 5, 11, 6, 12)
  90.         CreateButton("G", Ground, 2, 9, 3, 10)
  91.         CreateButton("1", FirstFloor, 5, 9, 6, 10)
  92.  
  93. end
  94.  
  95. function MoveLift(level)
  96.     local x, z, y = gps.locate()
  97.     print("destination.."..level)
  98.     print("start.."..y)
  99.     if y == level then
  100.         --do nothing as on right floor 
  101.     elseif y < level then
  102.         --while y ~= level do
  103.             MoveUp(level - y)
  104.             x, z, y = gps.locate()
  105.             print("up.."..y)
  106.         --end  
  107.     elseif y > level then
  108.         --while y ~= level do
  109.             MoveDown(y - level)
  110.             x, z, y = gps.locate()
  111.             print("down.."..y)
  112.         --end
  113.     end
  114. end
  115.  
  116. function Top()
  117.     print("Top")
  118.     local level = 152
  119.     MoveLift(level)
  120. end
  121.  
  122. function Basement()
  123.     print("B")
  124.     local level = 66
  125.     MoveLift(level)
  126. end
  127.  
  128. function LowerBasement()
  129.     print("LB")
  130.     local level = 51
  131.     MoveLift(level)
  132. end
  133.  
  134. function Ground()
  135.     print("G")
  136.     local level = 77
  137.     MoveLift(level)
  138. end
  139.  
  140. function FirstFloor()
  141.     print("1")
  142.     local level = 83
  143.     MoveLift(level)
  144. end
  145.  
  146. function Screen()
  147.     local currColor
  148.         for name,data in pairs(button) do
  149.                 local on = data["active"]
  150.                 if on == true then
  151.                         currColor = colors.lime
  152.                 else currColor = colors.red
  153.         end
  154.        
  155.         fill(name, currColor, data)
  156.         end
  157. end
  158.  
  159. SetUpButtons()
  160. while true do
  161.     mon.clear()
  162.     Screen()
  163.     local event, side, x, y = os.pullEvent()
  164.         if event == "monitor_touch" then
  165.         print("event: "..event)
  166.                 checkxy(x, y)
  167.         end
  168.         sleep(0.1)      
  169. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement