Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -------------------------------------------------------------------------------
- -- Filename: Tornado.lua
- --
- -- Description:
- -- Displays the connection status of a turtle and allows user to back up to the
- -- home display menu
- -------------------------------------------------------------------------------
- local PROTOCOL = "FishNET"
- local NAME = "Tornado"
- -------------------------------------------------------------------------------
- -- Displays the text.
- --
- -- Arguments:
- -- monitor - the monitor that the text is to be displayed on
- -- row - the row that the text is to be displayed on
- -- col - the column that the text is to start on
- -- text - the text that is to be displayed
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayText(monitor, row, col, text)
- monitor.setCursorPos(col, row)
- monitor.write(text)
- end
- -------------------------------------------------------------------------------
- -- Displays the title of the status page.
- --
- -- Arguments:
- -- monitor - the monitor that the title is to be displayed on
- -- row - the row that the title is to be displayed on
- -- col - the column that the title is to start on
- -- text - the title that is to be displayed
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayTitle(monitor, row, col, title)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- displayText(monitor, row, col, title)
- end
- -------------------------------------------------------------------------------
- -- Displays the back button on the screen
- --
- -- Arguments:
- -- monitor - the monitor that the title is to be displayed on
- -- row - the row that the title is to be displayed on
- -- col - the column that the title is to start on
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayBackButton(monitor, row, col)
- monitor.setBackgroundColor(colors.red)
- monitor.setTextColor(colors.white)
- displayText(monitor, row, col, "<-")
- monitor.setBackgroundColor(colors.black)
- end
- -------------------------------------------------------------------------------
- -- Converts screen row, col coordinates to the back button
- --
- -- Arguments:
- -- row - the row the back button is on
- -- col - the column the back button is on
- --
- -- Returns:
- -- Button name if it is selected
- -------------------------------------------------------------------------------
- local function screenCoordToBack(row, col)
- local buttonRow = 1
- local buttonStart = 1
- local buttonEnd = 2
- if row == buttonRow and col >= buttonStart and col <= buttonEnd then
- return true
- else
- return false
- end
- end
- -------------------------------------------------------------------------------
- -- Displays a label of a status to be shown
- --
- -- Arguments:
- -- monitor
- -- row
- -- col
- -- label
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayLabel(monitor, row, col, label)
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.blue)
- displayText(monitor, row, col, label .. ": ")
- end
- -------------------------------------------------------------------------------
- -- Broadcasts a lookup packet with dns protocol
- --
- -- Arguments:
- -- protocol - the specified protocol in the lookup packet
- -- name - the specified name of the computer in the lookup packet
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function ping(protocol, name)
- rednet.broadcast(
- {
- sType = "lookup",
- sProtocol = protocol,
- sHostname = name,
- }, "dns" )
- end
- -------------------------------------------------------------------------------
- -- Waits for a message over rednet and checks if it is a response to the lookup
- -- packet
- --
- -- Arguments:
- -- name - filter all responses unless it came from a computer with this
- -- name
- -- pingTimeout - the amount of time in seconds for the computer to wait for a
- -- response until timing out
- --
- -- Returns:
- -- true - if a response is received from "name" within a specified period of
- -- time
- -- false - if a response is not received within a specified period of time
- -------------------------------------------------------------------------------
- local function pingReturn(pingTimeout, name)
- local pingTimer = os.startTimer(pingTimeout)
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- if event == "rednet_message" then
- local ID, message, messageProtocol = p1, p2, p3
- if messageProtocol == "dns" and message.sType == "lookup response" then
- if message.sHostname == name then
- return true
- end
- end
- elseif event == "timer" and p1 == pingTimer then
- return false
- end
- end
- end
- -------------------------------------------------------------------------------
- -- Gets the connection status of the turtle
- --
- -- Arguments
- -- protocol - the rednet protocol that the turtle should be using
- -- name - the name of the turtle to ping
- -- pingTimeout - a time in seconds
- --
- -- Returns:
- -- True if the turtle responds to the ping
- -- False if the turtle does not respond within 2 seconds
- --------------------------------------------------------------------------------
- local function isConected(protocol, name, pingTimeout)
- ping(protocol, name)
- if pingReturn(pingTimeout, name) then
- return true
- else
- return false
- end
- end
- --------------------------------------------------------------------------------
- -- Displays the connection status on the monitor
- --
- -- Arguments
- -- monitor
- -- row
- -- col
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayConectionStatus(monitor, row, col)
- local protocol = PROTOCOL
- local name = NAME
- local timeout = 0.25
- if isConected(protocol, name, timeout) then
- monitor.setTextColor(colors.green)
- displayText(monitor, row, col, " ")
- displayText(monitor, row, col, "Connected")
- else
- monitor.setTextColor(colors.red)
- displayText(monitor, row, col, " ")
- displayText(monitor, row, col, "Lost")
- end
- end
- -------------------------------------------------------------------------------
- -- Displays the parts of the screen that are static
- --
- -- Arguments:
- -- None
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayStaticScreen()
- local monitor = peripheral.wrap("top")
- local monitorWidth, monitorHight = monitor.getSize()
- local row, col
- monitor.setBackgroundColor(colors.black)
- monitor.clear()
- -- back button
- row = 1
- col = 1
- displayBackButton(monitor, row, col)
- -- title
- row = 2
- col = monitorWidth/2 - #NAME/2
- displayTitle(monitor, row, col, NAME)
- -- "connection" label
- row = 4
- col = 2
- label = "Connection"
- displayLabel(monitor, row, col, label)
- end
- -------------------------------------------------------------------------------
- -- Displays real time status of the turtle
- --
- -- Arguments:
- -- None
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function displayStatus()
- local monitor = peripheral.wrap("top")
- local row, col
- -- connection status
- row = 4
- col = 14
- displayConectionStatus(monitor, row, col)
- end
- -------------------------------------------------------------------------------
- -- Waits for user to click on a button, if the button that is clicked is back
- -- then home program launches and this program will end. Otherwise it will do
- -- what the button is programmed to do.
- --
- -- Arguments:
- -- None
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function checkSelectButton(row, col)
- monitor.setCursorPos(math.random(1, 44), 12)
- monitor.setTextColor(colors.yellow)
- monitor.write("Click!")
- end
- -------------------------------------------------------------------------------
- -- Waits for an event in the event queue. If the event is a timeout event then
- -- the program will update the monitor. If it is a monitor touch it will
- -- check if the touch was on a button
- --
- -- Arguments:
- -- updateFreq - the frequency at which displayStatus() is called
- --
- -- Returns:
- -- Nothing
- -------------------------------------------------------------------------------
- local function waitForEvent(updateFreq)
- local row, col
- local statusUpdate = os.startTimer(updateFreq)
- while not screenCoordToBack(row, col) do
- local event, par1, par2, par3 = os.pullEvent()
- if event == "timer" and par1 == statusUpdate then
- displayStatus()
- statusUpdate = os.startTimer(updateFreq)
- print("updated screen.....")
- elseif event == "monitor_touch" then
- row = par3
- col = par2
- checkSelectButton(row, col)
- end
- end
- shell.run("/displayHome")
- end
- -------------------------------------------------------------------------------
- local function main()
- displayStaticScreen()
- displayStatus()
- waitForEvent(1)
- end
- -------------------------------------------------------------------------------
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement