Advertisement
DOGGYWOOF

TEST APPS

Jan 24th, 2025 (edited)
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. -- gps_status_gui_error_in_dashboard.lua
  2.  
  3. -- Function to check GPS status by attempting to get a location
  4. local function checkGPS()
  5. local success, latitude, longitude = gps.locate(5) -- 5 second timeout
  6. if success then
  7. return true
  8. else
  9. return false, "Failed to retrieve GPS data"
  10. end
  11. end
  12.  
  13. -- Function to find a connected monitor
  14. local function findMonitor()
  15. for _, side in ipairs(peripheral.getNames()) do
  16. if peripheral.getType(side) == "monitor" then
  17. return peripheral.wrap(side)
  18. end
  19. end
  20. return nil
  21. end
  22.  
  23. -- Function to create a dashboard UI window
  24. local function createWindow(monitor)
  25. local width, height = monitor.getSize()
  26. monitor.clear()
  27. monitor.setBackgroundColor(colors.black)
  28. monitor.setTextColor(colors.white)
  29.  
  30. -- Title bar with border
  31. monitor.setBackgroundColor(colors.blue)
  32. monitor.setCursorPos(1, 1)
  33. for x = 1, width do
  34. monitor.write(" ")
  35. end
  36. monitor.setTextColor(colors.white)
  37. monitor.setCursorPos(math.floor((width - 20) / 2), 1)
  38. monitor.write("GPS Status Dashboard")
  39.  
  40. return width, height
  41. end
  42.  
  43. -- Function to update the GPS status on the monitor
  44. local function updateStatus(monitor, width, height, status, errorMessage)
  45. -- GPS Status Section
  46. monitor.setCursorPos(2, 3)
  47. monitor.setTextColor(colors.lightGray)
  48. monitor.write("GPS Status: ")
  49.  
  50. if status then
  51. monitor.setTextColor(colors.green)
  52. monitor.write("Active")
  53. else
  54. monitor.setTextColor(colors.red)
  55. monitor.write("ERROR: " .. errorMessage)
  56. end
  57.  
  58. -- Info Section with GPS Data Display
  59. monitor.setTextColor(colors.white)
  60. monitor.setCursorPos(2, 5)
  61. monitor.write("Last checked: " .. textutils.formatTime(os.time(), true))
  62.  
  63. -- Additional Info
  64. monitor.setCursorPos(2, 7)
  65. monitor.write("GPS Accuracy: High")
  66. end
  67.  
  68. -- Function to flash the error message status text between red and white
  69. local function flashErrorStatus(monitor, width, height)
  70. local flashes = 6
  71. local flashDelay = 0.5
  72. local flashColors = {colors.red, colors.white} -- Alternate between red and white
  73.  
  74. -- Create a flashing effect for the "ERROR" status text
  75. for i = 1, flashes do
  76. -- Set the text color to the next color in the list
  77. monitor.setTextColor(flashColors[i % #flashColors + 1])
  78.  
  79. -- Set the "ERROR" status text
  80. monitor.setCursorPos(2, 3)
  81. monitor.write("GPS Status: ERROR")
  82.  
  83. -- Pause for flashing effect
  84. sleep(flashDelay)
  85.  
  86. -- Reset the text color to the opposite color for the next flash
  87. monitor.setTextColor(flashColors[(i + 1) % #flashColors + 1])
  88. monitor.setCursorPos(2, 3)
  89. monitor.write("GPS Status: ERROR")
  90.  
  91. -- Pause before the next cycle
  92. sleep(flashDelay)
  93. end
  94. end
  95.  
  96. -- Main loop
  97. local monitor = findMonitor()
  98.  
  99. if not monitor then
  100. print("No monitor found. Please connect a monitor.")
  101. return
  102. end
  103.  
  104. -- Create the window for displaying status on the monitor (full screen)
  105. local width, height = createWindow(monitor)
  106.  
  107. while true do
  108. -- Check GPS status and retrieve data
  109. local isAvailable, errorMessage = checkGPS()
  110.  
  111. -- Update monitor with current GPS status
  112. if isAvailable then
  113. updateStatus(monitor, width, height, isAvailable, "")
  114. else
  115. updateStatus(monitor, width, height, isAvailable, errorMessage)
  116. flashErrorStatus(monitor, width, height) -- Flash the "ERROR" status text
  117. end
  118.  
  119. -- Wait before checking again (0.5 seconds delay)
  120. sleep(0.5)
  121. end
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement