Advertisement
xKevinn

Untitled

Oct 3rd, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local bShowHourChar = false
  2. local bShowMinuteChar = false
  3. local bFaceFilled = true
  4.  
  5. -- coloured clocks anyone?
  6. local nBackColour = colors.black
  7. local nFaceColour = colors.white
  8. local nHandColour = colors.black
  9. local nTickColour = colors.gray
  10.  
  11. -- find the monitor and set it up
  12. local monitor = nil
  13. local i,side
  14. for i, side in pairs(rs.getSides()) do
  15.   if peripheral.getType(side) == "monitor" then
  16.     monitor = peripheral.wrap(side)
  17.     break
  18.   end
  19. end
  20. if monitor==nil then
  21.   print("NO MONITOR FOUND")
  22.   print("Please attach a monitor and try again.")
  23.   return
  24. end
  25.  
  26. -- calculate scaling
  27. monitor.setTextScale(0.5)
  28.  
  29. -- hackish fix to enable large monitor arrays enough time to initialise?
  30. os.sleep(1)
  31.  
  32. local mw,mh = monitor.getSize()
  33. mw = mw / (15/10) -- 15x10 is w,h of 0.5 scale single monitor
  34. local minSize = (math.min(mw,mh) / 2) - 2
  35.  
  36. -- clock radius in character. 18 is good for a 6x6 advanced monitor
  37. -- or you can use minSize which attempts to fit
  38. -- to monitor size automatically
  39. local nClockRad = minSize
  40.  
  41. -- set up the screens
  42. term.clear()
  43. term.setCursorPos(1,1)
  44. term.redirect(monitor)
  45. term.setBackgroundColor(nBackColour)
  46. term.clear()
  47.  
  48. -- some variables
  49. local h = 0 -- parsed hour
  50. local m = 0 -- parsed minute
  51. local nOldHX = -1
  52. local nOldHY = -1
  53. local nOldMX = -1
  54. local nOldMY = -1
  55.  
  56. local function plot(s,x,y)
  57.   term.setCursorPos(x,y)
  58.   term.write(s)
  59. end
  60.  
  61. -- the clock face ticks
  62. local function ticks(nW,nH)
  63.   local nIndex
  64.   local x
  65.   local y
  66.   local xi
  67.   local yi
  68.   local nInRadius = nClockRad-(nClockRad/4)
  69.   local nOutRadius = nClockRad-2
  70.  
  71.   local nxOutRad = nOutRadius + (nOutRadius/2)
  72.   local nyOutRad = nOutRadius
  73.   local nxInRad = nInRadius + (nInRadius/2)
  74.   local nyInRad = nInRadius
  75.  
  76.   for nIndex=0,359,(360/12) do
  77.     x = nW + math.sin(math.rad(nIndex)) * nxOutRad
  78.     y = nH + math.cos(math.rad(nIndex)) * nyOutRad
  79.  
  80.     xi = nW + math.sin(math.rad(nIndex)) * nxInRad
  81.     yi = nH + math.cos(math.rad(nIndex)) * nyInRad
  82.    
  83.     paintutils.drawLine(xi,yi,x,y,nTickColour)
  84.   end
  85. end
  86.  
  87. -- draw the face (circle)
  88. -- by working out a quarter of the points
  89. -- then drawing lines from oposite quarter
  90. -- results in much faster circle code
  91. local function face(nW,nH,nRadius)
  92.   local nIndex
  93.   local x
  94.   local y
  95.   local nxRad = nRadius + (nRadius/2)
  96.   local nyRad = nRadius
  97.   local nOldX = -1
  98.   local nOldY = -1
  99.  
  100.   local points = {}
  101.   local xy
  102.   local n = 1
  103.  
  104.   for nIndex=0,89 do
  105.     x = math.sin(math.rad(nIndex)) * nxRad
  106.     y = math.cos(math.rad(nIndex)) * nyRad
  107.     x = math.floor(x)
  108.     y = math.floor(y)
  109.  
  110.     -- only store positions if they are different from last time  
  111.     if x~=nOldX or y~=nOldY then
  112.       xy = {}
  113.       xy[1] = x
  114.       xy[2] = y
  115.       points[n] = xy
  116.      
  117.       if y~=nOldY or not bFaceFilled then
  118.         n=n+1
  119.       end
  120.  
  121.       nOldX = x
  122.       nOldY = y
  123.     end
  124.   end
  125.  
  126.   -- plot the circle
  127.   for nIndex=1,#points do
  128.     x = points[nIndex][1]
  129.     y = points[nIndex][2]
  130.  
  131.     if bFaceFilled then
  132.       paintutils.drawLine(nW-x,nH+y,nW+x,nH+y,nFaceColour)
  133.       paintutils.drawLine(nW-x,nH-y,nW+x,nH-y,nFaceColour)
  134.     else
  135.       paintutils.drawPixel(nW-x,nH+y,nFaceColour)
  136.       paintutils.drawPixel(nW+x,nH+y,nFaceColour)
  137.       paintutils.drawPixel(nW-x,nH-y,nFaceColour)
  138.       paintutils.drawPixel(nW+x,nH-y,nFaceColour)
  139.     end
  140.   end
  141. end
  142.  
  143. -- parse the time into H and M
  144. -- these are stored globally in h and m
  145. local function parseTime()
  146.   local t = os.time()
  147.   local sT = textutils.formatTime(t, true)
  148.  
  149.   local n = string.find(sT, ":")
  150.  
  151.   if(n>0) then
  152.     h = tonumber(string.sub(sT,1,n-1))
  153.     m = tonumber(string.sub(sT,n+1))
  154.   end
  155. end
  156.  
  157. -- plot minute hand
  158. -- it removes the previous minute hand
  159. -- then draws the new one
  160. local function plotM(nW,nH,nX,nY)
  161.   local nTmpColour
  162.  
  163.   if(nOldMX==nX and nOldMY==nY) then
  164.     return
  165.   end
  166.  
  167.   -- remove previous by drawing it again
  168.   -- in the face colour.  Also remove the
  169.   -- hand tag
  170.   if(nOldMX~=-1) then
  171.     if bFaceFilled then
  172.       nTmpColour = nFaceColour
  173.     else
  174.       nTmpColour = nBackColour
  175.     end
  176.     paintutils.drawLine(nW,nH,nOldMX,nOldMY,nTmpColour)
  177.      
  178.     if(bShowMinuteChar) then
  179.       plot(".",nOldMX,nOldMY)
  180.     end
  181.   end
  182.  
  183.   nOldMX = nX
  184.   nOldMY = nY
  185.  
  186.   paintutils.drawLine(nW,nH,nX,nY,nHandColour)
  187.  
  188.   -- draw the centre O
  189.   term.setTextColor(colors.black)
  190.   term.setBackgroundColor(colors.lightGray)
  191.   plot("O",nW,nH)
  192.  
  193.   if(bShowMinuteChar) then
  194.     -- draw the minute hand tag
  195.     plot("M",nX,nY)
  196.   end
  197. end
  198.  
  199. -- plot hour hand
  200. -- it removes the previous hour hand
  201. -- then draws the new one
  202. local function plotH(nW,nH,nX,nY)
  203.   local nTmpColour
  204.   if(nOldHX==nX and nOldHY==nY) then
  205.     return
  206.   end
  207.  
  208.   -- remove previous by drawing it again
  209.   -- in the face colour.  Also remove the
  210.   -- hand tag
  211.   if(nOldHX~=-1) then
  212.     if bFaceFilled then
  213.       nTmpColour = nFaceColour
  214.     else
  215.       nTmpColour = nBackColour
  216.     end
  217.     paintutils.drawLine(nW,nH,nOldHX,nOldHY,nTmpColour)
  218.  
  219.     if(bShowHourChar) then
  220.       plot(".",nOldHX,nOldHY)
  221.     end
  222.   end
  223.  
  224.   nOldHX = nX
  225.   nOldHY = nY
  226.  
  227.   paintutils.drawLine(nW,nH,nX,nY,nHandColour)
  228.  
  229.   if(bShowHourChar) then
  230.     -- draw the hour hand tag
  231.     term.setTextColor(colors.black)
  232.     term.setBackgroundColor(colors.lightGray)
  233.     plot("H",nX,nY)
  234.   end
  235. end
  236.  
  237. local fH
  238. local fM
  239. local fXH
  240. local fYH
  241. local fXM
  242. local fYM
  243. local nyMRadius = nClockRad - (nClockRad / 4)
  244. local nyHRadius = nClockRad - (nClockRad / 2)
  245.  
  246. local nxMRadius = nyMRadius + (nyMRadius / 2)
  247. local nxHRadius = nyHRadius + (nyHRadius / 2)
  248. local nW, nH = term.getSize()
  249.  
  250. nW = (nW / 2)+1
  251. nH = (nH / 2)+1
  252.  
  253. -- plot the circle
  254. face(nW,nH,nClockRad + 1)
  255.  
  256. local bRunning = true
  257. local nTimer
  258. nTimer = os.startTimer(0.25) -- the timer interval to update the clock
  259.  
  260. while(bRunning) do
  261.   parseTime()
  262.  
  263.   fH = (((h % 12)/12) + (m/60/12)) * 360
  264.   fM = (m/60) * 360
  265.  
  266.   -- shift by half a circle
  267.   fH = fH + 180
  268.   fM = fM + 180
  269.  
  270.   fXH = math.sin(math.rad(-fH))
  271.   fYH = math.cos(math.rad(fH))
  272.   fXM = math.sin(math.rad(-fM))
  273.   fYM = math.cos(math.rad(fM))
  274.  
  275.   plotH(nW,nH,  nW + fXH * nxHRadius, nH + fYH * nyHRadius)
  276.   plotM(nW,nH, nW + fXM * nxMRadius, nH + fYM * nyMRadius)
  277.  
  278.   -- draw the ticks on
  279.   ticks(nW,nH)
  280.  
  281.   local event, param1 = os.pullEvent()
  282.  
  283.   while (not (event=="timer" and param1==nTimer) and event~="char") do
  284.     event, param1 = os.pullEvent()
  285.   end
  286.  
  287.   if(event=="char") then
  288.      if(param1=="q" or param1=="Q") then
  289.        bRunning=false
  290.      end
  291.   elseif(event=="timer" and param1==nTimer) then
  292.      -- timer timed out, lets start again
  293.      nTimer = os.startTimer(0.25)
  294.   end
  295. end
  296.  
  297. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement