Advertisement
sidekick_

CCsensors - Proximity Module

Jan 19th, 2013
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. --[[
  2.     CCsensors: Proximity Module.
  3.     Proximity requires the Proximity SensorModule to work.
  4. ]]--
  5.  
  6. os.unloadAPI("sensors")
  7. os.loadAPI("/rom/apis/sensors")
  8.  
  9. -- Variables
  10. controllerSide = sensors.getController()
  11. sizeX, sizeY = term.getSize()
  12.  
  13. -- Check to see if there is a controller connected to the computer
  14. if not controllerSide then
  15.     print("No sensor controller found!\nPlease connect a sensor controller.")
  16.     return
  17. end
  18.  
  19. --[[ Functions ]]--
  20.  
  21. -- [[ Other functions ]] --
  22.  
  23. function cp(c, x, y) if c==1 then term.clear() end term.setCursorPos(x,y) end
  24.  
  25. function select( x, y, title, tData )
  26.     local function writeAt(xPos, yPos, text)
  27.         term.setCursorPos(xPos, yPos)
  28.         write(text)
  29.     end
  30.    
  31.     writeAt( x, y, title..": "..#tData )
  32.    
  33.     for i, text in pairs(tData) do
  34.         writeAt( x + 2, y + 1 + i, text )
  35.     end
  36.    
  37.     local sel = 1
  38.     writeAt( x + 1, y + 1 + sel, "*")
  39.     while true do
  40.         _, key = os.pullEvent("key")
  41.         writeAt( x + 1, y + 1 + sel, " ")
  42.         if key == 200 then
  43.             -- Up
  44.             sel = sel - 1
  45.         elseif key == 208 then
  46.             -- Down
  47.             sel = sel + 1
  48.         elseif key == 28 then
  49.             -- Enter
  50.             return tData[sel]
  51.         end
  52.         if sel < 1 then sel = #tData
  53.         elseif sel > #tData then sel = 1 end
  54.        
  55.         writeAt( x + 1, y + 1 + sel, "*")
  56.     end
  57. end
  58. term.clear()
  59. -- Get and Select a sensor
  60. Sensors = sensors.getSensors(controllerSide)
  61. dataSensor = select( 1, 1, "Available Sensors", Sensors )
  62.  
  63. -- Get and Select a probe
  64. Probes = sensors.getProbes( controllerSide, dataSensor )
  65. dataProbe = select( 1, #Sensors + 4, "Available Probes", Probes)
  66.  
  67. while true do
  68.     cp(1, 1, 1)
  69.     Targets = sensors.getAvailableTargetsforProbe( controllerSide, dataSensor, dataProbe )
  70.     sTarget = select(1, 1, "Targets", Targets)
  71.     readings = sensors.getSensorReadingAsDict( controllerSide, dataSensor, sTarget, dataProbe)
  72.     cp(1, 1, 1)
  73.     for i, v in pairs(readings) do
  74.         print(i .. ": " .. v)
  75.     end
  76.     sleep(5)
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement