Don't like ads? PRO users don't see any ads ;-)
Guest

sensors

By: a guest on Sep 21st, 2012  |  syntax: Lua  |  size: 6.55 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --[[
  2. Sensors API
  3.  
  4.  
  5. CHANGELOG
  6.  
  7. 0.2
  8. - added: comments for all functions
  9. - added:
  10.  
  11.  
  12. ]]--
  13.  
  14. -- sensorAPI version
  15. function getVersion()
  16.         return "0.2"
  17. end
  18.  
  19. -- returns a dict of attached peripherals : SensorController,monitor,modem
  20. function getPeripherals()
  21.         local dict={sensor=nil,monitor=nil,modem=nil};
  22.         local sides={"left","right","back","front","bottom","top"}
  23.         for i=1,#sides do
  24.        
  25.                 if peripheral.isPresent( sides[i] ) then
  26.                         if peripheral.getType( sides[i])  == "SensorController" then
  27.                                 dict["sensor"]=sides[i];
  28.                         elseif peripheral.getType( sides[i])  == "mon" then
  29.                                 dict["mon"]=sides[i];
  30.                                 -- add any special handling here
  31.                         elseif peripheral.getType( sides[i])  == "modem" then
  32.                                 dict["modem"]=sides[i];
  33.                                 -- add any special handling required here - like turn it on when detected
  34.                         else
  35.                                 dict[peripheral.getType( sides[i])] = sides[i];
  36.                         end
  37.                 end
  38.         end
  39.         return dict;
  40. end
  41.  
  42. -- return the side the first controller is attached at
  43. function getController()
  44.         local side="none"
  45.         local sides={"left","right","back","front","bottom","top"}
  46.         for i=1,#sides do
  47. --              print ("check side: "..sides[i])
  48.                 if peripheral.isPresent( sides[i] ) then
  49.         --              print("Found peripheral on side: "..sides[i])
  50.                         if peripheral.getType( sides[i])  == "SensorController" then
  51.                                 side=sides[i]
  52.                         end
  53.                 end
  54.         end
  55.         return side
  56. end
  57.  
  58. function rIterator(t) local i = 1 return function() i=i+2 return t[i-2], t[i-1] end end
  59. function tabtodict(t)
  60.         local r = {}
  61.         for k=1,#t,2 do
  62.                 r[t[k]]=t[k+1]
  63.         end
  64.         return r
  65. end
  66.  
  67. -- return sensor reading as a table instead of a dict
  68. function getReadingAsTable(side,sensor,...)
  69.         result = {peripheral.call( side, "getSensorReading",sensor,...)}
  70.         --for idx,val in ipairs(result) do
  71.         --      print (idx.."] "..tostring(val))
  72.         --end  
  73.         return result
  74.  
  75. end
  76.  
  77. -- set the active target for a given sensor
  78. function setTarget(side,sensor,target)
  79.         return peripheral.call(side,"setTarget",sensor,target)
  80. end
  81.  
  82. -- set the sensor range (as long is < max_sensor_range
  83. function setSensorRange(side,sensor,range)
  84.         return peripheral.call(side,"setSensorRange",sensor,range)
  85.  
  86. end
  87.  
  88. -- returns names of all readings available for a given sensor
  89. function getAvailableReadings(side,sensor)
  90.         result = {peripheral.call( side, "getAvailableReadings",sensor,"type")}
  91.         return result
  92. end
  93.  
  94. -- set the active reading to be used when getReading is called
  95. function setActiveReading(side,sensor,reading)
  96.         return {peripheral.call( side, "setReading",sensor,reading)}
  97. end
  98.  
  99. -- returns the available targets for a given sensor
  100. function getAvailableTargets(side,sensor)
  101.         result = {peripheral.call( side, "getAvailableTargets",sensor)}
  102.         return result
  103. end
  104.  
  105. function getAvailableTargetsforProbe(side,sensor,probe)
  106.         result = {peripheral.call( side, "getAvailableTargets",sensor,probe)}
  107.         --for i,v in ipairs(result) do
  108.         --print(i..","..v)
  109.         --end
  110.         return result
  111. end
  112.  
  113. function getAvailableTargetsforProbe2(side,sensor,probe)
  114.         local result2 = {peripheral.call( side, "getAvailableTargets",sensor,probe)}
  115.         local result={};
  116.         for i,v in ipairs(result2) do
  117.                
  118.                 result[i] =string.match(v,"%a+");
  119.         end
  120.         return result
  121. end
  122.  
  123.  
  124.  
  125. function getProbes(side,sensor)
  126.         local data = sensors.getSensorInfo(side,sensor,"probes");
  127.         local r={};
  128.         local i=1;
  129.         if data.probes ~= nil then
  130.                 for p in string.gmatch(data.probes,"%a+") do
  131.                         r[i] = p;
  132.                         i =i+1;
  133.                 end
  134.         end
  135.         return r;
  136. end
  137.  
  138. -- returns the following information for a given sensor:
  139. -- cardType,name,activereading,activereadingid,distance,location,methods,SensorRange
  140. function getSensorInfo(side,sensor,...)
  141.         result = {peripheral.call( side, "getSensorInfo",sensor,...)}
  142.         return tabtodict(result)
  143. end
  144. function getSensorInfoAsTable(side,sensor,...)
  145.         result = {peripheral.call( side, "getSensorInfo",sensor,...)}
  146.         return result
  147. end
  148.  
  149.  
  150. -- returns sensor reading in a dict of reading,value pairs
  151. function getReading2(side,sensor,probe,target,...)
  152.         result = {peripheral.call( side, "getSensorReading2",sensor,probe,target,...)}
  153.         return tabtodict(result)
  154. end
  155.  
  156. -- returns sensor reading in a dict of reading,value pairs
  157. function getReading(side,sensor,...)
  158.         result = {peripheral.call( side, "getSensorReading",sensor,...)}
  159.         return tabtodict(result)
  160. end
  161.  
  162. -- returns the names of all connected sensors
  163. function getSensors(side)
  164.         return {peripheral.call( side, "getSensorNames" )}
  165. end
  166.  
  167. --IC2 Reactor
  168. --"heat","output","lastOutput","addedToEnergyNet","chambers","maxheat"
  169.  
  170.  
  171. -- get a reading while setting the target and reading type for a given sensor
  172. -- return a dictionary
  173. function getSensorReadingAsDict(side,sensor,target,reading,...)
  174.         setTarget(side,sensor,target)
  175.         setActiveReading(side,sensor,reading)
  176.         return getReading(side,sensor,...)
  177. end
  178. function getSensorReadingAsDict2(side,sensor,probe,target,...)
  179.         return getReading2(side,sensor,probe,target,...)
  180. end
  181.  
  182. -- get a reading while setting the target and reading type for a given sensor
  183. -- returns a table
  184. function getSensorReadingAsTable(side,sensor,target,reading,...)
  185.         setTarget(side,sensor,target)
  186.         setActiveReading(side,sensor,reading)
  187.         result = {peripheral.call( side, "getSensorReading",sensor,...)}
  188.         return result
  189.         --return getReadingAsTable(side,sensor,...)
  190. end
  191.  
  192. -- 1xUranium Cell@15
  193. -- return item info from an inventory dictionary
  194. function getItemInfo(name,content)
  195.         local count=0;
  196.         local totalDmg=0;
  197.        
  198.         for i,v in pairs(content) do
  199.                 --print(i.." - "..v)
  200.                 q,item,dmg= string.match(v,"(%d+)\*(.*)@(%d+)")
  201.                 if item==name then
  202.                 --print(tostring(item).." vs "..tostring(name));
  203.                         totalDmg = totalDmg + dmg;
  204.                         count=count+q;
  205.                 end
  206.                
  207.         end    
  208.  
  209.         --print ("Total:"..count..", dmg:"..totalDmg);
  210.         return count,totalDmg;
  211. end
  212.  
  213.  
  214. local itemsDict={
  215. itemCellCoolant="Coolant Cell",
  216. itemReactorCooler="Integrated Heat Disperser",
  217. itemReactorPlating="Integrated Reactor Plating",
  218. itemCellUran="Uranium Cell",
  219. ice="Ice",
  220. bucketWater="Water Bucket",
  221. bucket="Bucket",
  222. };
  223.  
  224. -- returns a dict for each item in the given names table with .dmg and .qty
  225. function getItemsInfo(names,content)
  226.         local r={total=0,};
  227.         for i,v in pairs(content) do
  228.                 q,item,dmg= string.match(v,"(%d+)\*(.*)@(%d+)")
  229.                 itb = string.sub(item,6)
  230.                 item = itemsDict[itb] or item;
  231.                 r.total = r.total+1;
  232.                 for idx,name in ipairs(names) do
  233.                         if item==name then
  234.                                 local vl=0;
  235.                                 local vq=0;
  236.                                 if r[item] then vl =r[item].dmg end
  237.                                 if r[item] then vq =r[item].qty end
  238.                                 r[item] = {dmg=vl+dmg,qty=q+vq};
  239.                         end
  240.                 end
  241.                
  242.         end    
  243.         for idx,name in ipairs(names) do
  244.                 if r[name] == nil then
  245.                         r[name] = {qty=0, dmg=0}
  246.                 end
  247.         end    
  248.         return r;
  249. end