Advertisement
Guest User

startup

a guest
Mar 28th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. local m = peripheral.wrap("top")
  2. local states = {}
  3.  
  4. rednet.open("back")
  5.  
  6. m.clear()
  7. m.setBackgroundColor(colors.gray)
  8. m.setTextColor(colors.black)
  9. m.setCursorPos(1,1)
  10. m.setBackgroundColor(colors.orange)
  11. m.write("Automated Farm")
  12. m.setBackgroundColor(colors.gray)
  13. m.setCursorPos(1,3)
  14. m.setBackgroundColor(colors.cyan)
  15. m.write("Seed States:")
  16. m.setBackgroundColor(colors.gray)
  17. m.setTextColor(colors.white)
  18.  
  19. function string.starts(String,Start)
  20.   return string.sub(String,1,string.len(Start))==Start
  21. end
  22.  
  23. function formatCmd(cmd)
  24.   local formatted = {}
  25.   local y = 0
  26.  
  27.   for x in string.gmatch(cmd, "%S+") do
  28.     y = y+1
  29.     formatted[y] = x
  30.   end
  31.  
  32.   return formatted
  33. end
  34.  
  35. function validCmd(cmd)
  36.   if string.starts(cmd,"STATE") == true then
  37.     return true
  38.   else
  39.     return false
  40.   end
  41. end
  42.  
  43. function populateStates(fill)
  44.   for i = 1, 18, 1 do
  45.     if i % 2 == 0 then
  46.       m.setCursorPos(4,4+i/2-1)
  47.       if fill[i] == "X" then
  48.         m.setBackgroundColor(colors.red)
  49.       else
  50.         m.setBackgroundColor(colors.green)
  51.       end
  52.       m.write(fill[i])
  53.       m.setBackgroundColor(colors.gray)
  54.     else
  55.       m.setCursorPos(2,4+i/2)
  56.       if fill[i] == "X" then
  57.         m.setBackgroundColor(colors.red)
  58.       else
  59.         m.setBackgroundColor(colors.green)
  60.       end
  61.       m.write(fill[i])
  62.       m.setBackgroundColor(colors.gray)
  63.     end
  64.   end
  65. end
  66.  
  67. function init()
  68.   for j = 1, 18, 1 do
  69.     states[j] = "X"
  70.   end
  71. end
  72.  
  73. function listen(fromid, debug)
  74.   local sid, msg, protocol = rednet.receive(fromid)
  75.   if debug == true then
  76.     print("Message received!")
  77.     print("SID: " .. tostring(sid))
  78.     print("MSG: " .. tostring(msg))
  79.     print("PRT: " .. tostring(protocol))
  80.     print("=================")
  81.     for k,v in pairs(formatCmd(msg)) do
  82.       print(k .. " - " .. v)
  83.     end
  84.   end --debug
  85.  
  86.   if validCmd(msg) == true then
  87.     local a = formatCmd(msg)[2]
  88.     local b = formatCmd(msg)[3]
  89.     if b == "0" then
  90.       states[tonumber(a)] = "X"
  91.     else
  92.       states[tonumber(a)] = "v"
  93.     end
  94.     populateStates(states)
  95.     listen(15,false)
  96.   else
  97.     print("No valid command received")
  98.     shell.run("reboot")
  99.   end
  100. end
  101.  
  102. init()
  103. populateStates(states)
  104. listen(15,false)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement