Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- gps_status_gui_error_in_dashboard.lua
- -- Function to check GPS status by attempting to get a location
- local function checkGPS()
- local success, latitude, longitude = gps.locate(5) -- 5 second timeout
- if success then
- return true
- else
- return false, "Failed to retrieve GPS data"
- end
- end
- -- Function to find a connected monitor
- local function findMonitor()
- for _, side in ipairs(peripheral.getNames()) do
- if peripheral.getType(side) == "monitor" then
- return peripheral.wrap(side)
- end
- end
- return nil
- end
- -- Function to create a dashboard UI window
- local function createWindow(monitor)
- local width, height = monitor.getSize()
- monitor.clear()
- monitor.setBackgroundColor(colors.black)
- monitor.setTextColor(colors.white)
- -- Title bar with border
- monitor.setBackgroundColor(colors.blue)
- monitor.setCursorPos(1, 1)
- for x = 1, width do
- monitor.write(" ")
- end
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(math.floor((width - 20) / 2), 1)
- monitor.write("GPS Status Dashboard")
- return width, height
- end
- -- Function to update the GPS status on the monitor
- local function updateStatus(monitor, width, height, status, errorMessage)
- -- GPS Status Section
- monitor.setCursorPos(2, 3)
- monitor.setTextColor(colors.lightGray)
- monitor.write("GPS Status: ")
- if status then
- monitor.setTextColor(colors.green)
- monitor.write("Active")
- else
- monitor.setTextColor(colors.red)
- monitor.write("ERROR: " .. errorMessage)
- end
- -- Info Section with GPS Data Display
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(2, 5)
- monitor.write("Last checked: " .. textutils.formatTime(os.time(), true))
- -- Additional Info
- monitor.setCursorPos(2, 7)
- monitor.write("GPS Accuracy: High")
- end
- -- Function to flash the error message status text between red and white
- local function flashErrorStatus(monitor, width, height)
- local flashes = 6
- local flashDelay = 0.5
- local flashColors = {colors.red, colors.white} -- Alternate between red and white
- -- Create a flashing effect for the "ERROR" status text
- for i = 1, flashes do
- -- Set the text color to the next color in the list
- monitor.setTextColor(flashColors[i % #flashColors + 1])
- -- Set the "ERROR" status text
- monitor.setCursorPos(2, 3)
- monitor.write("GPS Status: ERROR")
- -- Pause for flashing effect
- sleep(flashDelay)
- -- Reset the text color to the opposite color for the next flash
- monitor.setTextColor(flashColors[(i + 1) % #flashColors + 1])
- monitor.setCursorPos(2, 3)
- monitor.write("GPS Status: ERROR")
- -- Pause before the next cycle
- sleep(flashDelay)
- end
- end
- -- Main loop
- local monitor = findMonitor()
- if not monitor then
- print("No monitor found. Please connect a monitor.")
- return
- end
- -- Create the window for displaying status on the monitor (full screen)
- local width, height = createWindow(monitor)
- while true do
- -- Check GPS status and retrieve data
- local isAvailable, errorMessage = checkGPS()
- -- Update monitor with current GPS status
- if isAvailable then
- updateStatus(monitor, width, height, isAvailable, "")
- else
- updateStatus(monitor, width, height, isAvailable, errorMessage)
- flashErrorStatus(monitor, width, height) -- Flash the "ERROR" status text
- end
- -- Wait before checking again (0.5 seconds delay)
- sleep(0.5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement