Advertisement
MigasRocha

RealTimeClock with Display /Computer ID:6

Dec 26th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | Gaming | 0 0
  1. -- Step 1: Wrap the monitor peripheral
  2. local side = "left"  -- Replace with the side the monitor is attached to
  3. local monitor = peripheral.wrap(side)
  4.  
  5. -- Check if the monitor is wrapped successfully
  6. if not monitor then
  7.     print("Error: No monitor found on the specified side: " .. side)
  8.     return
  9. end
  10.  
  11. -- Set text scale and redirection
  12. local clockTextScale = 2  -- Adjusted text scale for clock screen
  13. local titleTextScale = 2  -- Text scale for "Real Time" title
  14. local songSelectorTextScale = 2  -- Adjust text scale for song selector
  15. monitor.setTextScale(clockTextScale)
  16. term.redirect(monitor)
  17.  
  18. -- Function to get the current time as a table
  19. local function getRealTime()
  20.     local time = os.date("*t")
  21.     return {hour = time.hour, min = time.min}
  22. end
  23.  
  24. -- Function to display the time centered on the monitor
  25. local function displayTime()
  26.     local currentTime = getRealTime()
  27.     local monitorWidth, monitorHeight = monitor.getSize()
  28.    
  29.     -- Calculate text position for centering
  30.     local text = string.format("%02d:%02d", currentTime.hour, currentTime.min)
  31.     local textWidth = #text * clockTextScale
  32.     local xPos = math.floor((monitorWidth - textWidth) / 2) + 4
  33.     local yPos = math.floor((monitorHeight - 1) / 2)  -- Center vertically
  34.    
  35.     -- Clear screen and display time
  36.     monitor.clear()
  37.     monitor.setCursorPos(xPos, yPos)
  38.     monitor.setTextColor(colors.orange)
  39.     monitor.write(text)
  40.    
  41.     -- Display "Real Time" title
  42.     local title = "Real Time"
  43.     local titleWidth = #title * titleTextScale
  44.     local titleXPos = math.floor((monitorWidth - titleWidth) / 2) + 6
  45.     monitor.setCursorPos(titleXPos, 1)
  46.     monitor.setTextColor(colors.red)
  47.     monitor.setTextScale(titleTextScale)
  48.     monitor.write(title)
  49. end
  50.  
  51. -- Function to display the song selector on the monitor
  52. local function displaySongSelector()
  53.     monitor.setTextScale(songSelectorTextScale)
  54.     monitor.clear()
  55.     monitor.setCursorPos(1, 1)
  56.     monitor.setTextColor(colors.white)
  57.     monitor.write("Select a song:")
  58.    
  59.     -- Display touch indicator to switch back to clock
  60.     monitor.setCursorPos(monitor.getWidth() - 2, monitor.getHeight() - 1)
  61.     monitor.setTextColor(colors.yellow)
  62.     monitor.write(">>")  -- Double angle bracket pointing right
  63.  
  64.     local yOffset = 2
  65.     for i, song in ipairs(songs) do
  66.         monitor.setCursorPos(1, i + yOffset)
  67.         monitor.write(i .. ". " .. song)
  68.     end
  69. end
  70.  
  71. -- List of songs
  72. local songs = {
  73.     "Anarchy",
  74.     "Daisuki",
  75.     "FriendlyCreatures",
  76.     "PsychoHead",
  77.     "Psychosocial",
  78.     "seila",
  79.     "SunnyDay",
  80.     "WhiteNight"
  81. }
  82.  
  83. -- Function to get user input
  84. local function getUserSelection()
  85.     print("Enter the number of the song to play:")
  86.     local choice = tonumber(read())
  87.     if choice and choice >= 1 and choice <= #songs then
  88.         return songs[choice]
  89.     else
  90.         print("Invalid selection. Please try again.")
  91.         return getUserSelection()
  92.     end
  93. end
  94.  
  95. -- Main loop to continuously update and display the time, unless screen is touched
  96. while true do
  97.     displayTime()
  98.     local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  99.    
  100.     if xPos >= monitor.getWidth() - 2 and yPos == monitor.getHeight() - 1 then
  101.         -- Switch to song selector screen
  102.         displaySongSelector()
  103.         local selectedSong = getUserSelection()
  104.         print("You selected: " .. selectedSong)
  105.         -- Send the selection to the network
  106.         rednet.open("back")  -- Adjust the side as necessary
  107.         rednet.broadcast(selectedSong)
  108.         print("Song selection sent to network.")
  109.         sleep(2)  -- Delay to see the selection before returning to clock display
  110.     end
  111. end
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement