hevohevo

ComputerCraft Tutorial: radio_ctrl(turtle)0_2

Jan 22nd, 2014
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. -- ############################################
  2. -- radio_ctrl  (turtle side)
  3. -- version 0.2
  4. -- http://hevohevo.hatenablog.com/
  5. --   1. Refer to controller side program (http://pastebin.com/h93ywUvS).
  6. --   2. Input to the following and reboot TurtleOS
  7. --     > pastebin get z6QVrviC startup
  8.  
  9. -- ################## config ##################
  10. ModemSide = "right"
  11. Logfile = "Logfile"
  12. ControllerID = nil -- set ID, if you need to restrict controller ID.
  13.  
  14. -- ################## functions ###############
  15.  
  16. function writeLogfile(filename, str, err)
  17.   local fh = fs.open(filename,"a")
  18.   if not err then
  19.     fh.writeLine(str)
  20.   else
  21.     fh.writeLine("--[[ "..err)
  22.     fh.writeLine(str)
  23.     fh.writeLine("--]]")
  24.   end
  25.   fh.close()
  26. end
  27.  
  28. function eval(str)
  29.   local status, err = pcall(loadstring(str))
  30.   if status then  -- success
  31.     if string.find(str, "os.run") then
  32.       writeLogfile(Logfile, str, "os.run: Logfile")
  33.     elseif string.find(str, "clearLogfile") then
  34.       -- do nothing
  35.     else
  36.       writeLogfile(Logfile, str)
  37.     end
  38.   else  -- error
  39.     writeLogfile(Logfile, str, err)
  40.     print('Error: ',err)
  41.   end
  42. end
  43.  
  44. function clearLogfile()
  45.   if fs.exists(Logfile) then fs.delete(Logfile) end
  46.   local fh = fs.open(Logfile,"w")
  47.   fh.writeLine("-- received messages log")
  48.   fh.close()
  49. end
  50.  
  51. -- ###############################
  52. -- Define usable functions in received messages
  53. _G["clearLogfile"]  = clearLogfile
  54.  
  55.  
  56. -- ###############################
  57. -- Main
  58. rednet.open(ModemSide)
  59. clearLogfile()
  60.  
  61. repeat
  62.   local sender_id, message, distance = rednet.receive()
  63.   print(message)
  64.   if not ControllerID or (sender_id == ControllerID) then eval(message) end
  65. until message == "exit()"
Advertisement
Add Comment
Please, Sign In to add comment