Advertisement
Pinkishu

clock-implemented

Mar 9th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.38 KB | None | 0 0
  1. if fs.exists("dark") == false then -- load darkAPI
  2.   print("Missing DarkAPI")
  3.   sleep(2)
  4.   print("Attempting to download...")
  5.   getGit = http.get("https://github.com/darkrising/darkprograms/raw/darkprograms/darksecurity/dark")
  6.   getGit = getGit.readAll()
  7.   file = fs.open("dark", "w")
  8.   file.write(getGit)
  9.   file.close()
  10.   os.reboot()
  11. else
  12.   os.loadAPI("dark")
  13. end
  14. local mon = peripheral.wrap("back")
  15. mon.setTextScale(1)
  16. mon.setTextColor(colors.white)
  17. mon.setBackgroundColor(colors.black)
  18.  
  19. Side = "bottom"
  20. c = colors.combine()
  21. local button = {}
  22.  
  23.  
  24. local clockTimer = -1
  25. local clockOffsetX,clockOffsetY=0,0
  26. local clockAutoCenter = true
  27. local clockUpdateRate = 0.08
  28.  
  29. do
  30.     local mW,mH = mon.getSize()
  31.     clockOffsetY = mH-1
  32.     if clockAutoCenter then
  33.  
  34.     clockOffsetX = (mW/2)-5
  35.     end
  36. end
  37.  
  38.  
  39.  
  40. function saveState()
  41.   dark.db.save("State", button)
  42. end
  43. function LoadStuff()
  44.   if fs.exists("State") == true then
  45.     button = dark.db.load("State")
  46.   end
  47.   for name, data in pairs(button) do
  48.     if data["active"] == true then
  49.       c = colors.combine(c, data["color"])
  50.       rs.setBundledOutput(Side, c)
  51.     end
  52.   end
  53. end
  54.          
  55. function setTable(name, xmin, xmax, ymin, ymax, RScolor)
  56.   xmax = xmax or mon.getSize()
  57.   if not button[name] then
  58.    button[name] = {}
  59.    button[name]["active"] = false
  60.    button[name]["xmin"] = xmin
  61.    button[name]["ymin"] = ymin
  62.    button[name]["xmax"] = xmax
  63.    button[name]["ymax"] = ymax
  64.    button[name]["color"] = RScolor
  65.   end
  66. end
  67.  
  68. function fillTable()
  69.    setTable("Arrow Dispensers", 1, nil,2 ,6, colors.white)
  70.    setTable("Defense Station", 1, nil, 8, 12, colors.orange)
  71.    setTable("Lights", 1, nil, 14, 18, colors.magenta)
  72. end    
  73.  
  74. function fill(text, color, bData)
  75.    mon.setBackgroundColor(color)
  76.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  77.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  78.    for j = bData["ymin"], bData["ymax"] do
  79.       mon.setCursorPos(bData["xmin"], j)
  80.       if j == yspot then
  81.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  82.             if k == xspot then
  83.                mon.write(text)
  84.             else
  85.                mon.write(" ")
  86.             end
  87.          end
  88.       else
  89.          for i = bData["xmin"], bData["xmax"] do
  90.             mon.write(" ")
  91.          end
  92.       end
  93.    end
  94.    mon.setBackgroundColor(colors.black)
  95. end
  96.      
  97. function screen()
  98.    local currColor
  99.    for name,data in pairs(button) do
  100.       local on = data["active"]
  101.       if on == true then currColor = colors.lime else currColor = colors.red end
  102.       fill(name, currColor, data)
  103.    end
  104. end
  105.      
  106. function checkxy(x, y)
  107.    for name, data in pairs(button) do
  108.       if y>=data["ymin"] and  y <= data["ymax"] then
  109.          if x>=data["xmin"] and x<= data["xmax"] then
  110.             data["active"] = not data["active"]
  111.             if data["active"] == true then
  112.               c = colors.combine(c, data["color"])
  113.             else
  114.               c = colors.subtract(c, data["color"])
  115.             end
  116.             rs.setBundledOutput(Side, c)
  117.             print(name)
  118.          end
  119.       end
  120.    end
  121. end
  122.      
  123. function heading(text)
  124.    w, h = mon.getSize()
  125.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  126.    mon.write(text)
  127. end
  128.  
  129. function updateClock()
  130.     mon.setCursorPos(1+clockOffsetX,1+clockOffsetY)
  131.     mon.clearLine()
  132.     local time = os.time()
  133.     local hours = math.floor(time)
  134.     local minutes = math.floor( (time - hours)*60)
  135.     local ampm = "am"
  136.  
  137.     if hours > 11 then
  138.       ampm = "pm"
  139.       if hours > 12 then
  140.         hours = hours - 12
  141.       end
  142.     elseif hours == 0 then hours = 12
  143.     end
  144.     mon.write(string.format("%s:%s",(hours<10 and "0"..hours or hours),(minutes<10 and "0"..minutes or minutes)).. " " .. ampm)
  145.     clockTimer = os.startTimer(clockUpdateRate)
  146. end
  147.      
  148. fillTable()
  149. LoadStuff()
  150. updateClock()
  151. mon.clear()
  152. heading("Castle Controls V1.0")
  153. screen()
  154. while true do
  155.  
  156.    local evData = { os.pullEvent() }
  157.    if evData[1] == "monitor_touch" then
  158.       local e,side,x,y = unpack(evData)
  159.       print(x..":"..y)
  160.       checkxy(x,y)
  161.       saveState()
  162.       sleep(.1)
  163.       mon.clear()
  164.       heading("Castle Controls V1.0")
  165.       screen()
  166.       updateClock()
  167.    elseif evData[1] == "timer" then
  168.       if evData[2] == clockTimer then
  169.          updateClock()
  170.       end
  171.    end
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement