Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Self clearing multi-monitor graffiti board
- --# Connect to one or more 2+ tall monitor arrays
- --# pastebin: eq9ht0wJ
- --# Don't edit anything below this line
- local tArgs = { ... }
- local timeOutMinutes = (tArgs[1] and tonumber(tArgs[1])) and tonumber(tArgs[1]) or 15 --# how often the monitor(s) should be cleared (in minutes)
- local TIMEOUT = math.ceil(timeOutMinutes * 60) --# the value used by the countdown timer
- local countdown = TIMEOUT --# the value displayed onscreen
- local clearTimer = os.startTimer(1) --# start the clear monitor timer
- local validColors = { [1] = true; [2] = true; [4] = true; [8] = true; [16] = true; [32] = true; [64] = true; [128] = true; [256] = true; [512] = true; [1024] = true; [2048] = true; [4096] = true; [8192] = true; [16384] = true, [32768] = true; } --# This is a reference table of the 16 valid single color values
- local mon, monList, monNames, monX, monY, monCount = { }, { }, { }, { }, { }, 0 --# tables for monitors, monitor number by name, monitor name by number, monitor size (x & y), and a tracking variable for building the monitor name/number lookup lists
- local function initMonitors(clearMon) --# function to find and wrap all color monitors that are 2 tall and 1 wide or bigger
- local bgColor, yPos --# declare bgColor and yPos variables for use in monitor cataloging (so they aren't declared over and over for each monitor)
- mon = { --# build the monitor table
- peripheral.find("monitor", --# search for monitors
- function(name, object)
- if object.isColor() then --# if the monitor is color then...
- object.setTextScale(0.5) --# set the text scale to 0.5
- else --# ...otherwise...
- return false --# don't add the monitor to the monitor table
- end
- monX[name], monY[name] = object.getSize() --# get each monitor's size and save the values in the monX and monY tables
- if monY[name] > 15 then --# if the monitor is 16 'pixels' tall or taller then...
- object.setTextColor(colors.white)
- bgColor = object.getBackgroundColor() --# save the current background color
- if clearMon then --# if clearMon is true then clear the monitor
- object.setBackgroundColor(colors.black)
- object.clear()
- end
- yPos = math.floor(monY[name] / 2) - 8 --# initialize the y position variable for drawing the color pips
- for color in pairs(validColors) do --# cycle through the valid colors list
- yPos = yPos + 1 --# increment the y position
- object.setBackgroundColor(color) --# set the pip color
- object.setCursorPos(monX[name], yPos) --# place the cursor at the far right and the y position
- object.write(" ") --# draw the color pip
- end
- object.setBackgroundColor(bgColor) --# set the background color back to the color it was before drawing the color pips
- monCount = monCount + 1 --# increment the monitor number tracking variable
- monList[name] = monCount --# add entry to the monitor name to number lookup table
- monNames[monCount] = name --# add entry to the monitor number to name lookup table
- return true --# tell peripheral.find() to wrap the monitor in a new entry to the monitor table
- else --# ...otherwise...
- return false --# don't add the monitor to the monitor table
- end
- end
- )
- }
- end
- local function drawColorPips(i) --# function to clear a single monitor array and redraw the color pips
- local bgColor = mon[i].getBackgroundColor() --# save the current background color
- mon[i].setBackgroundColor(colors.black) --# set the monitor's background color to black
- mon[i].clear() --# clear the monitor
- local yPos = math.floor(monY[monNames[i]] / 2) - 8 --# initialize the y position variable for drawing the color pips
- for color in pairs(validColors) do --# cycle through the valid colors table
- yPos = yPos + 1 --# increment the y position variable
- mon[i].setBackgroundColor(color) --# set the pip color
- mon[i].setCursorPos(monX[monNames[i]], yPos) --# place the cursor at the far right and the y position
- mon[i].write(" ") --# draw the color pip
- end
- mon[i].setBackgroundColor(bgColor) --# set the monitor's background color back to the color it was before clearing and drawing the color pips
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.clear()
- initMonitors(true) --# initialize the monitors (clearing them as they're initialized)
- if not mon[1] then error("No valid monitor arrays found. Attach one or more arrays that are at least 1 wide and 2 tall.\n", 0) end
- print("Go draw!\n")
- print("Time before clear: " .. tostring(countdown) .. "\n")
- print("Press 'q' to quit")
- while true do --# start an infinite loop
- local event, data, x, y = os.pullEvent() --# capture all events on the system for processing
- if event == "char" and data:lower() == "q" then --# if a valid character was pressed on the keyboard and it was 'q' then...
- for i = 1, monCount do --# cycle through the monitor table (the table of wrapped monitors)
- mon[i].setBackgroundColor(colors.black)
- mon[i].clear()
- end
- term.clear()
- term.setCursorPos(1, 1)
- return --# return from the loop (to nowhere) and thus exit the program
- elseif event == "monitor_touch" then --# if a monitor was touched then...
- if x == monX[data] then --# if the right margin of the monitor was touched then...
- local yPos = math.floor(monY[data] / 2) - 8 --# initialize the y position variable for tracking touches on the color pips
- for color in pairs(validColors) do --# cycle through the valid colors table
- yPos = yPos + 1 --# increment the y position variable
- if y == yPos then --# if the touch y position == yPos then...
- mon[monList[data]].setBackgroundColor(color) --# change the background color on the monitor
- break --# stop the loop
- end
- end
- elseif x < monX[data] - 1 then --# if some part of the monitor at least one column to the left of the right hand margin is touched then...
- mon[monList[data]].setCursorPos(x, y) --# set the monitor's cursor position to the touch position
- mon[monList[data]].write(" ") --# draw the 'pixel'
- end
- elseif event == "monitor_resize" then --# if a monitor is resized then...
- monX[data], monY[data] = mon[monList[data]].getSize() --# get the new size
- drawColorPips(monList[data]) --# clear the monitor and redraw the color pips
- elseif event == "peripheral" or event == "peripheral_detach" then --# if a peripheral is attached or removed then...
- for i = monCount, 1, -1 do --# cycle through the monitor list (backwards)
- if monList[monNames[i]] then monList[monNames[i]] = nil end --# clear the monList table entry
- if monX[monNames[i]] then monX[monNames[i]] = nil end --# clear the monX table entry
- if monY[monNames[i]] then monY[monNames[i]] = nil end --# clear the monY table entry
- monNames[i] = nil --# clear the monNames table entry
- mon[i] = nil --# clear the monitor table entry
- end
- monCount = 0 --# reset the monitor count
- initMonitors() --# re-initialize the monitors (don't clear the monitors)
- elseif event == "timer" and data == clearTimer then --# if a timer event is captured and it is the monitor clear timer then...
- if countdown == 0 then --# if the countdown == 0 then...
- for i = 1, monCount do --# cycle through the monitor table (the table of wrapped monitors)
- drawColorPips(i) --# clear the monitor and redraw the color pips
- end
- end
- countdown = countdown ~= 0 and countdown - 1 or TIMEOUT --# decrement or reset countdown
- term.setCursorPos(20, 3)
- term.write(tostring(countdown) .. " ")
- clearTimer = os.startTimer(1) --# restart the monitor clear timer
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement