Advertisement
bigflipperfish

TurtleTemp

Jul 10th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.54 KB | None | 0 0
  1. -------------------------------------------------------------------------------
  2. -- Filename: Tornado.lua
  3. --
  4. -- Description:
  5. -- Displays the connection status of a turtle and allows user to back up to the
  6. -- home display menu
  7. -------------------------------------------------------------------------------
  8. local PROTOCOL = "FishNET"
  9.  
  10. local NAME = "Tornado"
  11.  
  12. -------------------------------------------------------------------------------
  13. -- Displays the text.
  14. --
  15. -- Arguments:
  16. -- monitor  - the monitor that the text is to be displayed on
  17. -- row      - the row that the text is to be displayed on
  18. -- col      - the column that the text is to start on
  19. -- text     - the text that is to be displayed
  20. --
  21. -- Returns:
  22. -- Nothing
  23. -------------------------------------------------------------------------------
  24.  
  25. local function displayText(monitor, row, col, text)
  26.   monitor.setCursorPos(col, row)
  27.   monitor.write(text)
  28. end
  29.  
  30. -------------------------------------------------------------------------------
  31. -- Displays the title of the status page.
  32. --
  33. -- Arguments:
  34. -- monitor  - the monitor that the title is to be displayed on
  35. -- row      - the row that the title is to be displayed on
  36. -- col      - the column that the title is to start on
  37. -- text     - the title that is to be displayed
  38. --
  39. -- Returns:
  40. -- Nothing
  41. -------------------------------------------------------------------------------
  42.  
  43. local function displayTitle(monitor, row, col, title)
  44.   monitor.setBackgroundColor(colors.black)
  45.   monitor.setTextColor(colors.white)
  46.   displayText(monitor, row, col, title)
  47. end
  48.  
  49. -------------------------------------------------------------------------------
  50. -- Displays the back button on the screen
  51. --
  52. -- Arguments:
  53. -- monitor  - the monitor that the title is to be displayed on
  54. -- row      - the row that the title is to be displayed on
  55. -- col      - the column that the title is to start on
  56. --
  57. -- Returns:
  58. -- Nothing
  59. -------------------------------------------------------------------------------
  60.  
  61. local function displayBackButton(monitor, row, col)
  62.   monitor.setBackgroundColor(colors.red)
  63.   monitor.setTextColor(colors.white)
  64.   displayText(monitor, row, col, "<-")
  65.   monitor.setBackgroundColor(colors.black)
  66. end
  67.  
  68. -------------------------------------------------------------------------------
  69. -- Converts screen row, col coordinates to the back button
  70. --
  71. -- Arguments:
  72. -- row  - the row the back button is on
  73. -- col  - the column the back button is on
  74. --
  75. -- Returns:
  76. -- Button name if it is selected
  77. -------------------------------------------------------------------------------
  78.  
  79. local function screenCoordToBack(row, col)
  80.   local buttonRow = 1
  81.   local buttonStart = 1
  82.   local buttonEnd = 2
  83.  
  84.   if row == buttonRow and col >= buttonStart and col <= buttonEnd then
  85.     return true
  86.   else
  87.     return false
  88.   end
  89. end
  90.  
  91. -------------------------------------------------------------------------------
  92. -- Displays a label of a status to be shown
  93. --
  94. -- Arguments:
  95. -- monitor
  96. -- row
  97. -- col
  98. -- label
  99. --
  100. -- Returns:
  101. -- Nothing
  102. -------------------------------------------------------------------------------
  103.  
  104. local function displayLabel(monitor, row, col, label)
  105.   monitor.setBackgroundColor(colors.black)
  106.   monitor.setTextColor(colors.blue)
  107.   displayText(monitor, row, col, label .. ": ")
  108. end
  109.  
  110. -------------------------------------------------------------------------------
  111. -- Broadcasts a lookup packet with dns protocol
  112. --
  113. -- Arguments:
  114. -- protocol - the specified protocol in the lookup packet
  115. -- name     - the specified name of the computer in the lookup packet
  116. --
  117. -- Returns:
  118. -- Nothing
  119. -------------------------------------------------------------------------------
  120.  
  121. local function ping(protocol, name)
  122.   rednet.broadcast(
  123.     {
  124.       sType = "lookup",
  125.       sProtocol = protocol,
  126.       sHostname = name,
  127.     }, "dns" )
  128. end
  129.  
  130. -------------------------------------------------------------------------------
  131. -- Waits for a message over rednet and checks if it is a response to the lookup
  132. -- packet
  133. --
  134. -- Arguments:
  135. -- name     - filter all responses unless it came from a computer with this
  136. --            name
  137. -- pingTimeout  - the amount of time in seconds for the computer to wait for a
  138. --            response until timing out
  139. --
  140. -- Returns:
  141. -- true   - if a response is received from "name" within a specified period of
  142. --          time
  143. -- false  - if a response is not received within a specified period of time
  144. -------------------------------------------------------------------------------
  145.  
  146. local function pingReturn(pingTimeout, name)
  147.   local pingTimer = os.startTimer(pingTimeout)
  148.  
  149.   while true do
  150.     local event, p1, p2, p3 = os.pullEvent()
  151.    
  152.     if event == "rednet_message" then
  153.       local ID, message, messageProtocol = p1, p2, p3
  154.      
  155.       if messageProtocol == "dns" and message.sType == "lookup response" then
  156.         if message.sHostname == name then
  157.           return true
  158.         end
  159.       end
  160.    
  161.     elseif event == "timer" and p1 == pingTimer then
  162.       return false
  163.     end
  164.   end
  165. end
  166.  
  167. -------------------------------------------------------------------------------
  168. -- Gets the connection status of the turtle
  169. --
  170. -- Arguments
  171. -- protocol     - the rednet protocol that the turtle should be using
  172. -- name         - the name of the turtle to ping
  173. -- pingTimeout  - a time in seconds
  174. --
  175. -- Returns:
  176. -- True if the turtle responds to the ping
  177. -- False if the turtle does not respond within 2 seconds
  178. --------------------------------------------------------------------------------
  179.  
  180. local function isConected(protocol, name, pingTimeout)
  181.   ping(protocol, name)
  182.  
  183.   if pingReturn(pingTimeout, name) then
  184.     return true
  185.   else
  186.     return false
  187.   end
  188. end
  189.  
  190. --------------------------------------------------------------------------------
  191. -- Displays the connection status on the monitor
  192. --
  193. -- Arguments
  194. -- monitor
  195. -- row
  196. -- col
  197. --
  198. -- Returns:
  199. -- Nothing
  200. -------------------------------------------------------------------------------
  201.  
  202. local function displayConectionStatus(monitor, row, col)
  203.   local protocol = PROTOCOL
  204.   local name = NAME
  205.   local timeout = 0.25
  206.  
  207.  
  208.   if isConected(protocol, name, timeout) then
  209.     monitor.setTextColor(colors.green)
  210.     displayText(monitor, row, col, "         ")
  211.     displayText(monitor, row, col, "Connected")
  212.    else
  213.     monitor.setTextColor(colors.red)
  214.     displayText(monitor, row, col, "         ")
  215.     displayText(monitor, row, col, "Lost")
  216.   end
  217. end
  218.  
  219. -------------------------------------------------------------------------------
  220. -- Displays the parts of the screen that are static
  221. --
  222. -- Arguments:
  223. -- None
  224. --
  225. -- Returns:
  226. -- Nothing
  227. -------------------------------------------------------------------------------
  228.  
  229. local function displayStaticScreen()
  230.   local monitor = peripheral.wrap("top")
  231.   local monitorWidth, monitorHight = monitor.getSize()
  232.  
  233.   local row, col
  234.  
  235.   monitor.setBackgroundColor(colors.black)
  236.   monitor.clear()
  237.  
  238.   -- back button
  239.   row = 1
  240.   col = 1
  241.   displayBackButton(monitor, row, col)
  242.  
  243.   -- title
  244.   row = 2
  245.   col = monitorWidth/2 - #NAME/2
  246.   displayTitle(monitor, row, col, NAME)
  247.  
  248.   -- "connection" label
  249.   row = 4
  250.   col = 2
  251.   label = "Connection"
  252.   displayLabel(monitor, row, col, label)
  253. end
  254.  
  255. -------------------------------------------------------------------------------
  256. -- Displays real time status of the turtle
  257. --
  258. -- Arguments:
  259. -- None
  260. --
  261. -- Returns:
  262. -- Nothing
  263. -------------------------------------------------------------------------------
  264.  
  265. local function displayStatus()
  266.   local monitor = peripheral.wrap("top")
  267.  
  268.   local row, col
  269.  
  270.   -- connection status
  271.   row = 4
  272.   col = 14
  273.  
  274.   displayConectionStatus(monitor, row, col)
  275. end
  276.  
  277. -------------------------------------------------------------------------------
  278. -- Waits for user to click on a button, if the button that is clicked is back
  279. -- then home program launches and this program will end. Otherwise it will do
  280. -- what the button is programmed to do.
  281. --
  282. -- Arguments:
  283. -- None
  284. --
  285. -- Returns:
  286. -- Nothing
  287. -------------------------------------------------------------------------------
  288.  
  289. local function checkSelectButton(row, col)
  290.   monitor.setCursorPos(math.random(1, 44), 12)
  291.   monitor.setTextColor(colors.yellow)
  292.   monitor.write("Click!")
  293. end
  294.  
  295. -------------------------------------------------------------------------------
  296. -- Waits for an event in the event queue. If the event is a timeout event then
  297. -- the program will update the monitor. If it is a monitor touch it will
  298. -- check if the touch was on a button
  299. --
  300. -- Arguments:
  301. -- updateFreq - the frequency at which displayStatus() is called
  302. --
  303. -- Returns:
  304. -- Nothing
  305. -------------------------------------------------------------------------------
  306.  
  307. local function waitForEvent(updateFreq)
  308.   local row, col
  309.   local statusUpdate = os.startTimer(updateFreq)
  310.  
  311.   while not screenCoordToBack(row, col) do
  312.     local event, par1, par2, par3 = os.pullEvent()
  313.    
  314.     if event == "timer" and par1 == statusUpdate then
  315.       displayStatus()
  316.       statusUpdate = os.startTimer(updateFreq)
  317.       print("updated screen.....")
  318.     elseif event == "monitor_touch" then
  319.       row = par3
  320.       col = par2
  321.       checkSelectButton(row, col)
  322.     end
  323.   end
  324.  
  325.   shell.run("/displayHome")
  326. end
  327.  
  328. -------------------------------------------------------------------------------
  329.  
  330. local function main()
  331.   displayStaticScreen()
  332.   displayStatus()
  333.   waitForEvent(1)
  334. end
  335.  
  336. -------------------------------------------------------------------------------
  337.  
  338. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement