Advertisement
Guest User

startup

a guest
Oct 19th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.19 KB | None | 0 0
  1. -- Analogue Clock
  2. -- (c) Ramdor 2013
  3. -- v1.6
  4. --
  5. -- changes
  6. -- v1.6 - added non filled face option (bFaceFilled = true  ... is the default)
  7. -- v1.5 - now uses computercraft paintutils line, re-worked circle code, auto find monitor
  8. -- v1.4 - colours added and some loop derps fixed
  9. -- v1.3 - added resize/auto scale and 0.5 textsize
  10. --        to double the effective resolution
  11. -- v1.2 - changed timer code
  12. -- v1.1 - tidy up
  13. -- v1.0 - initial release
  14. --
  15. -- You may edit/copy/redistribute/whatever this
  16. -- code in any way.  Just a credit listing this
  17. -- pastebin link (pastebin.com/LGNUpDzz) will be fine :)
  18. -- Have fun, I did :)
  19.  
  20. -- see  http://www.youtube.com/watch?v=RrgOIGW0C-s   for some more info
  21.  
  22. -- these two settings will hide/show
  23. -- the M and H that is on the end of the hand
  24. local bShowHourChar = false
  25. local bShowMinuteChar = false
  26. local bFaceFilled = true
  27.  
  28. -- coloured clocks anyone?
  29. local nBackColour = colors.black
  30. local nFaceColour = colors.white
  31. local nHandColour = colors.black
  32. local nTickColour = colors.red
  33.  
  34. -- find the monitor and set it up
  35. local monitor = nil
  36. local i,side
  37. for i, side in pairs(rs.getSides()) do
  38.   if peripheral.getType(side) == "monitor" then
  39.     monitor = peripheral.wrap(side)
  40.     break
  41.   end
  42. end
  43. if monitor==nil then
  44.   print("NO MONITOR FOUND")
  45.   print("Please attach a monitor and try again.")
  46.   return
  47. end
  48.  
  49. -- calculate scaling
  50. monitor.setTextScale(0.5)
  51.  
  52. -- hackish fix to enable large monitor arrays enough time to initialise?
  53. os.sleep(1)
  54.  
  55. local mw,mh = monitor.getSize()
  56. mw = mw / (15/10) -- 15x10 is w,h of 0.5 scale single monitor
  57. local minSize = (math.min(mw,mh) / 2) - 2
  58.  
  59. -- clock radius in character. 18 is good for a 6x6 advanced monitor
  60. -- or you can use minSize which attempts to fit
  61. -- to monitor size automatically
  62. local nClockRad = minSize
  63.  
  64. -- set up the screens
  65. term.clear()
  66. term.setCursorPos(1,1)
  67. print("Clock by Ramdor (c) 2013")
  68. print("Press 'Q' to terminate.")
  69. term.redirect(monitor)
  70. term.setBackgroundColor(nBackColour)
  71. term.clear()
  72.  
  73. -- some variables
  74. local h = 0 -- parsed hour
  75. local m = 0 -- parsed minute
  76. local nOldHX = -1
  77. local nOldHY = -1
  78. local nOldMX = -1
  79. local nOldMY = -1
  80.  
  81. --Hold time string from TimeAPI.org
  82. local time = ""
  83.  
  84. local function plot(s,x,y)
  85.   term.setCursorPos(x,y)
  86.   term.write(s)
  87. end
  88.  
  89. -- the clock face ticks
  90. local function ticks(nW,nH)
  91.   local nIndex
  92.   local x
  93.   local y
  94.   local xi
  95.   local yi
  96.   local nInRadius = nClockRad-(nClockRad/4)
  97.   local nOutRadius = nClockRad-2
  98.  
  99.   local nxOutRad = nOutRadius + (nOutRadius/2)
  100.   local nyOutRad = nOutRadius
  101.   local nxInRad = nInRadius + (nInRadius/2)
  102.   local nyInRad = nInRadius
  103.  
  104.   for nIndex=0,359,(360/12) do
  105.     x = nW + math.sin(math.rad(nIndex)) * nxOutRad
  106.     y = nH + math.cos(math.rad(nIndex)) * nyOutRad
  107.  
  108.     xi = nW + math.sin(math.rad(nIndex)) * nxInRad
  109.     yi = nH + math.cos(math.rad(nIndex)) * nyInRad
  110.    
  111.     paintutils.drawLine(xi,yi,x,y,nTickColour)
  112.   end
  113. end
  114.  
  115. -- draw the face (circle)
  116. -- by working out a quarter of the points
  117. -- then drawing lines from oposite quarter
  118. -- results in much faster circle code
  119. local function face(nW,nH,nRadius)
  120.   local nIndex
  121.   local x
  122.   local y
  123.   local nxRad = nRadius + (nRadius/2)
  124.   local nyRad = nRadius
  125.   local nOldX = -1
  126.   local nOldY = -1
  127.  
  128.   local points = {}
  129.   local xy
  130.   local n = 1
  131.  
  132.   for nIndex=0,89 do
  133.     x = math.sin(math.rad(nIndex)) * nxRad
  134.     y = math.cos(math.rad(nIndex)) * nyRad
  135.     x = math.floor(x)
  136.     y = math.floor(y)
  137.  
  138.     -- only store positions if they are different from last time  
  139.     if x~=nOldX or y~=nOldY then
  140.       xy = {}
  141.       xy[1] = x
  142.       xy[2] = y
  143.       points[n] = xy
  144.      
  145.       if y~=nOldY or not bFaceFilled then
  146.         n=n+1
  147.       end
  148.  
  149.       nOldX = x
  150.       nOldY = y
  151.     end
  152.   end
  153.  
  154.   -- plot the circle
  155.   for nIndex=1,#points do
  156.     x = points[nIndex][1]
  157.     y = points[nIndex][2]
  158.  
  159.     if bFaceFilled then
  160.       paintutils.drawLine(nW-x,nH+y,nW+x,nH+y,nFaceColour)
  161.       paintutils.drawLine(nW-x,nH-y,nW+x,nH-y,nFaceColour)
  162.     else
  163.       paintutils.drawPixel(nW-x,nH+y,nFaceColour)
  164.       paintutils.drawPixel(nW+x,nH+y,nFaceColour)
  165.       paintutils.drawPixel(nW-x,nH-y,nFaceColour)
  166.       paintutils.drawPixel(nW+x,nH-y,nFaceColour)
  167.     end
  168.   end
  169. end
  170.  
  171. -- parse the time into H and M
  172. -- these are stored globally in h and m
  173. local function parseTime()
  174.   local t = os.time()
  175.   local sT = textutils.formatTime(t, true)
  176.  
  177.   local n = string.find(sT, ":")
  178.  
  179.   if(n>0) then
  180.     h = tonumber(string.sub(sT,1,n-1))
  181.     m = tonumber(string.sub(sT,n+1))
  182.   end
  183. end
  184.  
  185. -- plot minute hand
  186. -- it removes the previous minute hand
  187. -- then draws the new one
  188. local function plotM(nW,nH,nX,nY)
  189.   local nTmpColour
  190.  
  191.   if(nOldMX==nX and nOldMY==nY) then
  192.     return
  193.   end
  194.  
  195.   -- remove previous by drawing it again
  196.   -- in the face colour.  Also remove the
  197.   -- hand tag
  198.   if(nOldMX~=-1) then
  199.     if bFaceFilled then
  200.       nTmpColour = nFaceColour
  201.     else
  202.       nTmpColour = nBackColour
  203.     end
  204.     paintutils.drawLine(nW,nH,nOldMX,nOldMY,nTmpColour)
  205.      
  206.     if(bShowMinuteChar) then
  207.       plot(".",nOldMX,nOldMY)
  208.     end
  209.   end
  210.  
  211.   nOldMX = nX
  212.   nOldMY = nY
  213.  
  214.   paintutils.drawLine(nW,nH,nX,nY,nHandColour)
  215.  
  216.   -- draw the centre O
  217.   term.setTextColor(colors.black)
  218.   term.setBackgroundColor(colors.lightGray)
  219.   plot("O",nW,nH)
  220.  
  221.   if(bShowMinuteChar) then
  222.     -- draw the minute hand tag
  223.     plot("M",nX,nY)
  224.   end
  225. end
  226.  
  227. -- plot hour hand
  228. -- it removes the previous hour hand
  229. -- then draws the new one
  230. local function plotH(nW,nH,nX,nY)
  231.   local nTmpColour
  232.   if(nOldHX==nX and nOldHY==nY) then
  233.     return
  234.   end
  235.  
  236.   -- remove previous by drawing it again
  237.   -- in the face colour.  Also remove the
  238.   -- hand tag
  239.   if(nOldHX~=-1) then
  240.     if bFaceFilled then
  241.       nTmpColour = nFaceColour
  242.     else
  243.       nTmpColour = nBackColour
  244.     end
  245.     paintutils.drawLine(nW,nH,nOldHX,nOldHY,nTmpColour)
  246.  
  247.     if(bShowHourChar) then
  248.       plot(".",nOldHX,nOldHY)
  249.     end
  250.   end
  251.  
  252.   nOldHX = nX
  253.   nOldHY = nY
  254.  
  255.   paintutils.drawLine(nW,nH,nX,nY,nHandColour)
  256.  
  257.   if(bShowHourChar) then
  258.     -- draw the hour hand tag
  259.     term.setTextColor(colors.black)
  260.     term.setBackgroundColor(colors.lightGray)
  261.     plot("H",nX,nY)
  262.   end
  263. end
  264.  
  265. local function updateTime ()
  266.   local timeHandle = http.get("http://www.timeapi.org/utc/in+two+hours")
  267.  
  268.   time = timeHandle.readLine()
  269.   timeHandle.close()
  270. end
  271.  
  272. local function getHours ()
  273.   return tonumber(time:sub(12, 13))
  274. end
  275.  
  276. local function getMinutes ()
  277.   return tonumber(time:sub(15, 16))
  278. end
  279.  
  280. local fH
  281. local fM
  282. local fXH
  283. local fYH
  284. local fXM
  285. local fYM
  286. local nyMRadius = nClockRad - (nClockRad / 4)
  287. local nyHRadius = nClockRad - (nClockRad / 2)
  288.  
  289. local nxMRadius = nyMRadius + (nyMRadius / 2)
  290. local nxHRadius = nyHRadius + (nyHRadius / 2)
  291. local nW, nH = term.getSize()
  292.  
  293. nW = (nW / 2)+1
  294. nH = (nH / 2)+1
  295.  
  296. -- plot the circle
  297. face(nW,nH,nClockRad + 1)
  298.  
  299. local bRunning = true
  300. local nTimer
  301. nTimer = os.startTimer(0.25) -- the timer interval to update the clock
  302.  
  303. function clockLoop ()
  304.   while (bRunning) do
  305.     --parseTime()
  306.     h = getHours()
  307.     m = getMinutes()
  308.  
  309.     fH = (((h % 12)/12) + (m/60/12)) * 360
  310.     fM = (m/60) * 360
  311.  
  312.     -- shift by half a circle
  313.     fH = fH + 180
  314.     fM = fM + 180
  315.  
  316.     fXH = math.sin(math.rad(-fH))
  317.     fYH = math.cos(math.rad(fH))
  318.     fXM = math.sin(math.rad(-fM))
  319.     fYM = math.cos(math.rad(fM))
  320.  
  321.     plotH(nW,nH,  nW + fXH * nxHRadius, nH + fYH * nyHRadius)
  322.     plotM(nW,nH, nW + fXM * nxMRadius, nH + fYM * nyMRadius)
  323.    
  324.     -- draw the ticks on
  325.     ticks(nW,nH)
  326.  
  327.     local event, param1 = os.pullEvent()
  328.  
  329.     while (not (event=="timer" and param1==nTimer) and event~="char") do
  330.       event, param1 = os.pullEvent()
  331.     end
  332.  
  333.     if(event=="char") then
  334.        if(param1=="q" or param1=="Q") then
  335.          bRunning=false
  336.        end
  337.     elseif(event=="timer" and param1==nTimer) then
  338.       -- timer timed out, lets start again
  339.       nTimer = os.startTimer(0.25)
  340.     end
  341.   end
  342. end
  343.  
  344. function timeLoop ()
  345.   while true do
  346.     sleep(55)
  347.  
  348.     updateTime()
  349.   end
  350. end
  351.  
  352. updateTime()
  353.  
  354. parallel.waitForAny(clockLoop, timeLoop)
  355.  
  356. term.restore()
  357. print("Ended.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement