Advertisement
Oeed

Clock

Dec 20th, 2014
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.49 KB | None | 0 0
  1. local _, err = pcall(function()
  2.     local timezones = {
  3.         {'America/Los_Angeles', 'Los Angeles', 0},
  4.         {'Europe/London', 'London', 0},
  5.         {'Australia/Adelaide', 'Adelaide', 0},
  6.         {'Pacific/Auckland', 'Auckland', 0}
  7.     }
  8.  
  9.     -- Thanks tobit for this
  10.     function drawCircle( centerX, centerY, radius, col, solid )
  11.         solid = solid or false
  12.         local isAdvanced = term.isColor and term.isColor()
  13.         local char = isAdvanced and " " or "|"
  14.         if isAdvanced then term.setBackgroundColor( col ) end
  15.         local radStep = 1/(1.5*radius)
  16.         for angle = 1, math.pi+radStep, radStep do
  17.             local pX = math.cos( angle ) * radius * 1.5
  18.             local pY = math.sin( angle ) * radius
  19.             if solid then
  20.                 local chord = 2*math.abs(pX)
  21.                 term.setCursorPos( centerX - chord/2, centerY - pY )
  22.                 write( char:rep( chord ) )
  23.                 term.setCursorPos( centerX - chord/2, centerY + pY )
  24.                 write( char:rep( chord ) )
  25.             else
  26.                 for i=-1,1,2 do
  27.                     for j=-1,1,2 do
  28.                         term.setCursorPos( centerX + i*pX, centerY + j*pY )
  29.                         write( char )
  30.                     end
  31.                 end
  32.             end
  33.         end
  34.     end
  35.  
  36.     local function drawClock(timezone)
  37.         local night = false
  38.         local time = timezones[timezone][3]
  39.         if time <= 6 or time >= 18 then
  40.             night = true
  41.         end
  42.  
  43.         local w, h = term.getSize()
  44.         term.setBackgroundColour(colours.black)
  45.         term.clear()
  46.         local centerX, centerY = math.ceil(w/2)+1, math.ceil(h/2)
  47.         drawCircle(centerX, centerY, math.ceil(h/2), (night and colours.grey or colours.white), true)
  48.  
  49.  
  50.         for i = 0, 11 do
  51.             local minDeg = i * 2 * math.pi / 12
  52.             hyp1 = math.ceil(h/2) - 1
  53.             _w1 = math.cos(minDeg) * hyp1 * 1.5
  54.             _h1 = math.sin(minDeg) * hyp1
  55.             hyp2 = math.ceil(h/2) - 4
  56.             _w2 = math.cos(minDeg) * hyp2 * 1.5
  57.             _h2 = math.sin(minDeg) * hyp2
  58.  
  59.             paintutils.drawLine(centerX + _w1, centerY + _h1, centerX + _w2, centerY + _h2, colours.lightGrey)
  60.         end
  61.  
  62.         local hour = time % 12
  63.         local minute = math.floor((time - math.floor(time))*60)
  64.  
  65.         local hourDeg = hour * 2 * math.pi / 12 - math.pi/2
  66.         local hyp = math.ceil(h/2) - 6
  67.         local _w = math.cos(hourDeg) * hyp * 1.5
  68.         local _h = math.sin(hourDeg) * hyp
  69.         paintutils.drawLine(centerX, centerY, centerX + _w, centerY + _h, (night and colours.white or colours.black))
  70.  
  71.         local minDeg = minute * 2 * math.pi / 60 - math.pi/2
  72.         hyp = math.ceil(h/2) - 3
  73.         _w = math.cos(minDeg) * hyp * 1.5
  74.         _h = math.sin(minDeg) * hyp
  75.         paintutils.drawLine(centerX, centerY, centerX + _w, centerY + _h, colours.red)
  76.     end
  77.  
  78.     local monitors = {}
  79.     local sides = {'front','right', 'back', 'left'}
  80.  
  81.     for i, v in ipairs(sides) do
  82.         if peripheral.getType(v) == 'modem' then
  83.             local tRemote = peripheral.call( v, "getNamesRemote" )
  84.             monitors[i] = {}
  85.             for i2, name in ipairs(tRemote) do
  86.                 if peripheral.getType(name) == 'monitor' then
  87.                     local m = peripheral.wrap(name)
  88.                     local w,h = m.getSize()
  89.                     if h == 40 then
  90.                         monitors[i][1] = m
  91.                     else
  92.                         monitors[i][2] = m
  93.                         m.setTextScale(4)
  94.                     end
  95.                 end
  96.             end
  97.         end
  98.     end
  99.  
  100.     local function drawTimezone(i)
  101.         local w, h = term.getSize()
  102.         term.setBackgroundColour(colours.black)
  103.         term.clear()
  104.         local name = timezones[i][2]
  105.         term.setCursorPos(math.ceil((w - #name)/2) + 1, 1)
  106.         term.write(name)
  107.     end
  108.  
  109.     local function updateTimes()
  110.         os.startTimer(60)
  111.         for i, v in ipairs(timezones) do
  112.             v[3] = v[3] + 1/60
  113.         end
  114.     end
  115.  
  116.     local function downloadTimes()
  117.         for i, v in ipairs(timezones) do
  118.             local h = http.get('http://api.timezonedb.com/?zone='..v[1]..'&key=ASEPIBTKXXQA')
  119.             local s = h.readAll()
  120.             local t = s:match('<timestamp>(%d+)</timestamp>')
  121.             v[3] = (t/60/60)%24
  122.             print('Downloaded: '..v[2] .. ' ('..v[3]..')')
  123.         end
  124.         os.setAlarm(0)
  125.     end
  126.  
  127.     downloadTimes()
  128.     os.startTimer(60)
  129.     os.queueEvent('draw')
  130.  
  131.     while true do
  132.         local event, detail = os.pullEvent()
  133.         if event == 'alarm' then
  134.             downloadTimes()
  135.         elseif event == 'timer' then
  136.             updateTimes()
  137.         elseif event == 'char' and detail == '\\' then
  138.             os.reboot()
  139.         end
  140.  
  141.         for i, v in ipairs(monitors) do
  142.             term.redirect(v[2])
  143.             drawTimezone(i)
  144.             term.redirect(v[1])
  145.             drawClock(i)
  146.         end
  147.         term.redirect(term.native())
  148.     end
  149. end)
  150. if err then
  151.     term.redirect(term.native())
  152.     printError(err)
  153.     os.startTimer(5)
  154.     print('Press any key to cancel reboot.')
  155.     local ev = os.pullEvent()
  156.     if ev == 'timer' then
  157.         os.reboot()
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement