Guest User

r_mst3

a guest
May 24th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. --vars
  2. master_rx_port=314
  3. master_tx_port=315
  4. modem_side = "right"
  5. data = {}
  6. number_o_slaves = 7
  7. wait_time = 8
  8. --Functions:
  9.  
  10. --Transmit
  11. --Asking for data
  12. function inforeq()
  13.  modem.transmit(master_tx_port,master_rx_port,"SendInfo")
  14.  logging("transmit")
  15. end
  16. --/Transmit
  17.  
  18. --Recieve
  19. --The slaves have 8 seconds to send data. If not
  20. --all data are recieved in that time, then the
  21. --error flag is set. Incoming date is stored in
  22. --datain table varriable.
  23. function recieve_info(exp_msg,time_out)
  24.  local cnt=0 --counter for the recieved messages
  25.  local datain = {}
  26.  local timerID=os.startTimer(time_out) --starting timer
  27.  print(timerID)
  28.  while(cnt<=exp_msg) do --loop for recieving all messages
  29.   local event,side,rxCH,txCH,msg = os.pullEvent()
  30.   if event == "modem_message" and rxCH == master_rx_port then --data recieved
  31.    datain[cnt]=msg --storing data
  32.    cnt=cnt+1
  33.   end
  34.   if event == "timer" then --not all messages have arrived
  35.    erc = 01 --setting the error flag
  36.    logging("timeout",cnt)
  37.    break --if timer went off, then no more looping
  38.   end
  39.  end
  40.  if cnt == exp_msg then
  41.   os.cancelTimer(timerID)
  42.   logging("info_req_ok")
  43.   return datain
  44.  else
  45.   local retrycount = retrycount+1
  46.   term.setCursorPos(1,2)
  47.   if retrycount == 1 then
  48.    print("Timed out, retrying for the 1st time...")
  49.   elseif retrycount == 2 then
  50.    print("Timed out, retrying for the 2nd time...")
  51.   elseif retrycount == 3 then
  52.    print("Timed out, retrying for the 3rd time...")
  53.   else
  54.    print("Timed out, retrying for the "..retrycount.."th time")
  55.   end
  56.  end
  57. end
  58. --/Recieve
  59.  
  60. --Counter
  61. --Counts the number of full, empty and half-empty
  62. --RECs, then calls the evaluator.
  63. function counter (data)
  64.  local full = 0
  65.  local some = 0
  66.  local empty = 0
  67.  local i = 1
  68.  local v = 1
  69.  for i,v in pairs (data) do
  70.   if v == "Full" then full = full+1
  71.   elseif v == "Some" then some = some+1
  72.   else empty = empty+1
  73.   end
  74.  end
  75.  logging("counter",full,some,empty)
  76.  evaluator(full, some, empty)
  77. end
  78. --/Counter
  79.  
  80. --Evaluator
  81. --Turning on and off redstone signals (engines)
  82. --according to the set rules. Lazy to type them...
  83. function evaluator(full, some, empty)
  84.  if some < 1 then
  85.   if full <= 3 and empty >= 5 then
  86.    rs.setOutput("top",true)
  87.    rs.setOutput("bottom",true)
  88.    logging("evaluator",2)
  89.   elseif empty <= 4 and empty >= 1 then
  90.    rs.setOutput("top",true)
  91.    rs.setOutput("bottom",false)
  92.    logging("evaluator",1)
  93.   else
  94.    rs.setOutput("top",false)
  95.    rs.setOutput("bottom",false)
  96.    logging("evaluator",0)
  97.   end
  98.  else
  99.   if full <= 2 and empty >=5 then
  100.    rs.setOutput("top",true)
  101.    rs.setOutput("bottom",true)
  102.    logging("evaluator",2)
  103.   elseif empty <=4 and empty >= 2 then
  104.    rs.setOutput("top",false)
  105.    rs.setOutput("bottom",true)
  106.    logging("evaluator",1)
  107.   else
  108.    rs.setOutput("top",false)
  109.    rs.setOutput("bottom",false)
  110.    logging("evaluator",0)
  111.   end
  112.  end
  113. end
  114. --/Evaluator
  115.  
  116. --Logging
  117. --Creates a r_mst.log file and then writes notes
  118. --to it as the program runs.
  119. function logging(specifier, p1,p2,p3)
  120. local time = textutils.formatTime(os.time(),true)
  121. local day = os.day()
  122. local activity
  123. local f
  124.  if not fs.exists("r_mst.log") then
  125.   f=fs.open("r_mst.log","w")
  126.  else
  127.   f=fs.open("r_mst.log","a")
  128.  end
  129.  if specifier == "transmit" then
  130.   activity = "Transmitting request..."
  131.  elseif specifier == "info_req_ok" then
  132.   activity = "All answers recieved!"
  133.  elseif specifier == "timeout" then
  134.   activity = "Timed out. Recieved "..p1.." answers"
  135.  elseif specifier == "counter" then
  136.   activity = "Counted "..p1.." full, "..p2.." half-full and "..p3.." empty"
  137.  elseif specifier == "evaluator" then
  138.   activity = p1.."engines operating"
  139.  end
  140.  output=day.." @ "..time..": "..activity
  141.  
  142.  f.writeLine(output)
  143.  f.close()
  144. end
  145.  
  146. --Main
  147. --The program is intended to request information
  148. --from the slaves about energy cells. Then recieveis
  149. --the requested data. The recieved data contains
  150. --how many REC are full empty or has some energy in
  151. --them. After this the program counts how many full
  152. --or empty and then turns the engines (redstone outputs)
  153. --ON or OFF according to the values.
  154.  
  155. modem=peripheral.wrap(modem_side)
  156. modem.open(master_rx_port)
  157.  
  158. while true do
  159. print("Refinery program is Running")
  160.  erc = 00 --Flag to see if all data has arrived
  161.  retrycount = 1
  162.  while true do --Loop for re-requesting data in case of error
  163.   inforeq( ) --Information request
  164.   data = recieve_info(number_o_slaves,wait_time) -- data table
  165.   if erc == 00 then break end --error check
  166.  end
  167.  term.clear()
  168.  counter(data) --counting and evaluating data
  169.  
  170. end
Advertisement
Add Comment
Please, Sign In to add comment