Advertisement
HydrantHunter

Graffiti Board

Jun 14th, 2017
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.36 KB | None | 0 0
  1. --# Self clearing multi-monitor graffiti board
  2. --# Connect to one or more 2+ tall monitor arrays
  3. --# pastebin: eq9ht0wJ
  4. --# Don't edit anything below this line
  5. local tArgs = { ... }
  6. local timeOutMinutes = (tArgs[1] and tonumber(tArgs[1])) and tonumber(tArgs[1]) or 15 --# how often the monitor(s) should be cleared (in minutes)
  7. local TIMEOUT = math.ceil(timeOutMinutes * 60)          --# the value used by the countdown timer
  8. local countdown = TIMEOUT                               --# the value displayed onscreen
  9. local clearTimer = os.startTimer(1)                     --# start the clear monitor timer
  10. 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
  11. 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
  12.  
  13. local function initMonitors(clearMon)                   --# function to find and wrap all color monitors that are 2 tall and 1 wide or bigger
  14.   local bgColor, yPos                                   --# declare bgColor and yPos variables for use in monitor cataloging (so they aren't declared over and over for each monitor)
  15.   mon = {                                               --# build the monitor table
  16.     peripheral.find("monitor",                          --# search for monitors
  17.       function(name, object)
  18.         if object.isColor() then                        --# if the monitor is color then...
  19.           object.setTextScale(0.5)                      --# set the text scale to 0.5
  20.         else                                            --# ...otherwise...
  21.           return false                                  --# don't add the monitor to the monitor table
  22.         end
  23.         monX[name], monY[name] = object.getSize()       --# get each monitor's size and save the values in the monX and monY tables
  24.         if monY[name] > 15 then                         --# if the monitor is 16 'pixels' tall or taller then...
  25.           object.setTextColor(colors.white)
  26.           bgColor = object.getBackgroundColor()         --# save the current background color
  27.           if clearMon then                              --# if clearMon is true then clear the monitor
  28.             object.setBackgroundColor(colors.black)
  29.             object.clear()
  30.           end
  31.           yPos = math.floor(monY[name] / 2) - 8         --# initialize the y position variable for drawing the color pips
  32.           for color in pairs(validColors) do            --# cycle through the valid colors list
  33.             yPos = yPos + 1                             --# increment the y position
  34.             object.setBackgroundColor(color)            --# set the pip color
  35.             object.setCursorPos(monX[name], yPos)       --# place the cursor at the far right and the y position
  36.             object.write(" ")                           --# draw the color pip
  37.           end
  38.           object.setBackgroundColor(bgColor)            --# set the background color back to the color it was before drawing the color pips
  39.           monCount = monCount + 1                       --# increment the monitor number tracking variable
  40.           monList[name] = monCount                      --# add entry to the monitor name to number lookup table
  41.           monNames[monCount] = name                     --# add entry to the monitor number to name lookup table
  42.           return true                                   --# tell peripheral.find() to wrap the monitor in a new entry to the monitor table
  43.         else                                            --# ...otherwise...
  44.           return false                                  --# don't add the monitor to the monitor table
  45.         end
  46.       end
  47.     )
  48.   }
  49. end
  50.  
  51. local function drawColorPips(i)                         --# function to clear a single monitor array and redraw the color pips
  52.   local bgColor = mon[i].getBackgroundColor()           --# save the current background color
  53.   mon[i].setBackgroundColor(colors.black)               --# set the monitor's background color to black
  54.   mon[i].clear()                                        --# clear the monitor
  55.   local yPos = math.floor(monY[monNames[i]] / 2) - 8    --# initialize the y position variable for drawing the color pips
  56.   for color in pairs(validColors) do                    --# cycle through the valid colors table
  57.     yPos = yPos + 1                                     --# increment the y position variable
  58.     mon[i].setBackgroundColor(color)                    --# set the pip color
  59.     mon[i].setCursorPos(monX[monNames[i]], yPos)        --# place the cursor at the far right and the y position
  60.     mon[i].write(" ")                                   --# draw the color pip
  61.   end
  62.   mon[i].setBackgroundColor(bgColor)                    --# set the monitor's background color back to the color it was before clearing and drawing the color pips
  63. end
  64.  
  65. term.setBackgroundColor(colors.black)
  66. term.setTextColor(colors.white)
  67. term.setCursorPos(1, 1)
  68. term.clear()
  69.  
  70. initMonitors(true)                                      --# initialize the monitors (clearing them as they're initialized)
  71. 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
  72.  
  73. print("Go draw!\n")
  74. print("Time before clear: " .. tostring(countdown) .. "\n")
  75. print("Press 'q' to quit")
  76.  
  77. while true do                                           --# start an infinite loop
  78.   local event, data, x, y = os.pullEvent()              --# capture all events on the system for processing
  79.   if event == "char" and data:lower() == "q" then       --# if a valid character was pressed on the keyboard and it was 'q' then...
  80.     for i = 1, monCount do                              --# cycle through the monitor table (the table of wrapped monitors)
  81.       mon[i].setBackgroundColor(colors.black)
  82.       mon[i].clear()
  83.     end
  84.     term.clear()
  85.     term.setCursorPos(1, 1)
  86.     return                                              --# return from the loop (to nowhere) and thus exit the program
  87.   elseif event == "monitor_touch" then                  --# if a monitor was touched then...
  88.     if x == monX[data] then                             --# if the right margin of the monitor was touched then...
  89.       local yPos = math.floor(monY[data] / 2) - 8       --# initialize the y position variable for tracking touches on the color pips
  90.       for color in pairs(validColors) do                --# cycle through the valid colors table
  91.         yPos = yPos + 1                                 --# increment the y position variable
  92.         if y == yPos then                               --# if the touch y position == yPos then...
  93.           mon[monList[data]].setBackgroundColor(color)  --# change the background color on the monitor
  94.           break                                         --# stop the loop
  95.         end
  96.       end
  97.     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...
  98.       mon[monList[data]].setCursorPos(x, y)             --# set the monitor's cursor position to the touch position
  99.       mon[monList[data]].write(" ")                     --# draw the 'pixel'
  100.     end
  101.   elseif event == "monitor_resize" then                 --# if a monitor is resized then...
  102.     monX[data], monY[data] = mon[monList[data]].getSize() --# get the new size
  103.     drawColorPips(monList[data])                        --# clear the monitor and redraw the color pips
  104.   elseif event == "peripheral" or event == "peripheral_detach" then --# if a peripheral is attached or removed then...
  105.     for i = monCount, 1, -1 do                          --# cycle through the monitor list (backwards)
  106.       if monList[monNames[i]] then monList[monNames[i]] = nil end --# clear the monList table entry
  107.       if monX[monNames[i]] then monX[monNames[i]] = nil end --# clear the monX table entry
  108.       if monY[monNames[i]] then monY[monNames[i]] = nil end --# clear the monY table entry
  109.       monNames[i] = nil                                 --# clear the monNames table entry
  110.       mon[i] = nil                                      --# clear the monitor table entry
  111.     end
  112.     monCount = 0                                        --# reset the monitor count
  113.     initMonitors()                                      --# re-initialize the monitors (don't clear the monitors)
  114.   elseif event == "timer" and data == clearTimer then   --# if a timer event is captured and it is the monitor clear timer then...
  115.     if countdown == 0 then                              --# if the countdown == 0 then...
  116.       for i = 1, monCount do                            --# cycle through the monitor table (the table of wrapped monitors)
  117.         drawColorPips(i)                                --# clear the monitor and redraw the color pips
  118.       end
  119.     end
  120.     countdown = countdown ~= 0 and countdown - 1 or TIMEOUT --# decrement or reset countdown
  121.     term.setCursorPos(20, 3)
  122.     term.write(tostring(countdown) .. " ")
  123.     clearTimer = os.startTimer(1)                       --# restart the monitor clear timer
  124.   end
  125. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement