Advertisement
Te-ki

CCClock

Feb 18th, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. --CCClock by Teki
  2. local tArgs = { ... }
  3. local isMon = false
  4. local isNote = false
  5. math.randomseed(os.time())
  6.  
  7. local peripherals
  8. local monitors = {}
  9. local sizeX = {}
  10. local sizeY = {}
  11. local scales = {}
  12. local noteBlocks = {}
  13. local pitchs = {}
  14.  
  15. local rings = 0
  16. local lastRing = 0
  17. local curTime
  18. local hours
  19. local minutes
  20. local finalTime
  21.  
  22. function formatTime()
  23.     curTime = os.time()
  24.     hours = math.floor(curTime)
  25.     minutes = math.floor((curTime - hours)*60)
  26.     return string.format("%02d:%02d", hours, minutes)
  27. end
  28.  
  29. function findScale(side)
  30.     local monitor = peripheral.wrap(side)
  31.     local sizeX2,sizeY2 = monitor.getSize()
  32.     if sizeX2 ~= sizeX[side] or scales[side] == 0 then
  33.         for i=1, 5 do
  34.             monitor.setTextScale(6-i)
  35.             sizeX[side],sizeY[side] = monitor.getSize()
  36.             if sizeX[side] >= 5 then
  37.                 scales[side] = 6-i
  38.                 break
  39.             end
  40.         end
  41.         monitor.clear()
  42.     end
  43. end
  44.  
  45. local function resizeEvent()
  46.     local event, side = os.pullEvent("monitor_resize")
  47.     if event == "monitor_resize" then
  48.         findScale(side)
  49.     end
  50. end
  51.  
  52. function ring()
  53.     for i = 1, #noteBlocks do
  54.         pitchs[i] = pitchs[i] - (math.random(-1, 1)*3)
  55.         if pitchs[i] < 0 then pitchs[i] = pitchs[i] + 25 end
  56.         if pitchs[i] > 24 then pitchs[i] = pitchs[i] - 25 end
  57.         noteBlocks[i].setPitch(pitchs[i])
  58.         noteBlocks[i].triggerNote()
  59.     end
  60.     rings = rings - 1
  61. end
  62.  
  63. local function drawTime()
  64.     finalTime = formatTime()
  65.     for i = 1, #monitors do
  66.         monitors[i].setCursorPos(1,1)
  67.         monitors[i].write(finalTime)
  68.     end
  69. end
  70.  
  71. local function ringTime()
  72.     if isNote then
  73.         -- if lastRing ~= hours then
  74.             -- lastRing = hours
  75.             -- if hours == 0 then
  76.                 -- rings = 12
  77.                 -- rings = 1
  78.             -- elseif hours > 12 then
  79.                 -- rings = hours - 12
  80.                 -- rings = 1
  81.             -- else
  82.                 -- rings = hours
  83.                 -- rings = 1
  84.             -- end
  85.         -- end
  86.         if hours == 18 and minutes >= 30 and minutes < 35 and rings == 0 then
  87.             rings = 6
  88.         end
  89.        
  90.         if rings > 0 then
  91.             ring()
  92.         end
  93.     end
  94. end
  95.  
  96. local function mainLoop()
  97.     while true do
  98.         parallel.waitForAll(drawTime, ringTime)
  99.         sleep(1)
  100.     end
  101. end
  102.  
  103. peripherals = peripheral.getNames()
  104.  
  105. for i=1, #peripherals do
  106.     if peripheral.getType(peripherals[i]) == "monitor" then
  107.         monitors[#monitors+1] = peripheral.wrap(peripherals[i])
  108.         sizeX[peripherals[i]],sizeY[peripherals[i]] = monitors[#monitors].getSize()
  109.         scales[peripherals[i]] = 0
  110.         findScale(peripherals[i])
  111.         isMon = true
  112.     elseif peripheral.getType(peripherals[i]) == "note_block" then
  113.         noteBlocks[#noteBlocks+1] = peripheral.wrap(peripherals[i])
  114.         -- pitchs[#noteBlocks] = noteBlocks[#noteBlocks].getNote()
  115.         pitchs[#noteBlocks] = 0
  116.         isNote = true
  117.     end
  118. end
  119.  
  120. while true do
  121.     if isMon then
  122.         parallel.waitForAny(resizeEvent, mainLoop)
  123.     else
  124.         mainLoop()
  125.     end
  126.     sleep(0)
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement