Advertisement
redeye83

RedBIT Train Network Platform Screen

Jan 25th, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.00 KB | None | 0 0
  1. local function findPeripheral( kind )
  2.     for _,v in pairs( rs.getSides() ) do
  3.         if peripheral.getType( v ) == kind then
  4.             return v
  5.         end
  6.     end
  7.  
  8.     return nil
  9. end
  10.  
  11. local monitor  -- empty variable waiting for peripheral.wrap
  12. local monitorSide = findPeripheral( "monitor" )
  13. local sizeX, sizeY = term.getSize() -- get the pixel size of the screen ( currently the computers screen )
  14.  
  15. if not monitorSide then
  16.     print( "No monitor found! :(")
  17.     error() -- exit the program here
  18. else
  19.     monitor = peripheral.wrap(monitorSide)
  20. end
  21.  
  22. local function cwrite( msg, y )
  23.     local w, h = term.getSize() -- get the pixel size of the current screen
  24.     term.setCursorPos( w/2-#msg/2+1, y or h/2 ) -- set the cursor position so the supplied text will be in the middle of the screen
  25.     write( msg ) -- draw the message
  26. end
  27.  
  28. local function cline( y, col )
  29.     term.setCursorPos(1,y)
  30.     term.setBackgroundColor( col )
  31.     term.clearLine()
  32.     term.setBackgroundColor( colors.black )
  33. end
  34.  
  35. local activeLines = {
  36.     approch = "Train Approching",
  37.     leaving = "Train Leaving",
  38.     here = "Train Arrived",
  39. }
  40.  
  41. local function drawScreen()
  42.     term.clear()
  43.     term.setCursorPos(1,1)
  44.     term.setTextColor( colors.white )
  45.     local w, h = term.getSize()
  46.  
  47.     cline( 1, colors.gray )
  48.     cwrite("\[Pitsea Station\]",1)
  49.  
  50.     local rsInput = rs.getBundledInput("bottom")
  51.  
  52.     term.setTextColor( colors.white )
  53.     cline( 3, colors.red )
  54.     cwrite("Platform 1",3)
  55.     cwrite("No Trains",5)
  56.     if colors.test( rsInput, colors.blue ) then
  57.         term.setTextColor( colors.lime )
  58.         cwrite( activeLines.here, 5)
  59.     end
  60.     if colors.test( rsInput, colors.lime ) then
  61.         term.setTextColor( colors.blue )
  62.         cwrite( activeLines.approch, 5)
  63.     end
  64.     if colors.test( rsInput, colors.orange ) then
  65.         term.setTextColor( colors.red )
  66.         cwrite( activeLines.leaving, 5)
  67.     end
  68.  
  69.     term.setTextColor( colors.white )
  70.     cline( h/2+2, colors.blue )
  71.     cwrite("Platform 2",h/2+2)
  72.     cwrite("No Trains", h/2+4)
  73.     if colors.test( rsInput, colors.red ) then
  74.         term.setTextColor( colors.lime )
  75.         cwrite( activeLines.here, h/2+4)
  76.     end
  77.     if colors.test( rsInput, colors.black ) then
  78.         term.setTextColor( colors.blue )
  79.         cwrite( activeLines.approch, h/2+4)
  80.     end
  81.     if colors.test( rsInput, colors.white ) then
  82.         term.setTextColor( colors.red )
  83.         cwrite( activeLines.leaving, h/2+4)
  84.     end
  85. end
  86.  
  87. term.clear() -- clear the terminal screen
  88. cwrite( "Monitor has focus" ) -- print in the middle of the screen that the monitor has control
  89.  
  90. term.redirect( monitor ) -- give control of term functions to the monitor object meaning term. print and write will all show on the monitor
  91.  
  92. local refresh = os.startTimer( 1 ) -- this starts a timer that takes 1 second to complete, used for refreshing ( its better than sleep but requires event loops )
  93.  
  94. while true do
  95.     drawScreen() -- call the function that will draw the screen
  96.  
  97.     os.pullEvent( "timer" ) -- the program will wait here until the timer finishes
  98.    
  99.     refresh = os.startTimer( 1 ) -- restart the refresh timer
  100. end
  101.  
  102. term.restore() -- give term control back to the computer screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement