Mralko99

Orologio Analogico 6x6

Feb 2nd, 2013
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- side of the computer that the monitor is attached to
  2. local monitor = peripheral.wrap("back")
  3. -- clock radius in character. 18 is good for a 6x6 advanced monitor
  4. local nClockRad = 18
  5. -- these two settings will hide/show
  6. -- the M and H that is on the end of the hand
  7. local bShowHourChar = false
  8. local bShowMinuteChar = false
  9.  
  10. -- set up the screens
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. print("Orologio fatto da Mr.Alko99 2013")
  14. print("Premi 'Q' per spegnere.")
  15. term.redirect(monitor)
  16. term.setBackgroundColor(colors.black)
  17. term.clear()
  18.  
  19. -- some variables
  20. local h = 0 -- parsed hour
  21. local m = 0 -- parsed minute
  22. local nOldHX = -1
  23. local nOldHY = -1
  24. local nOldMX = -1
  25. local nOldMY = -1
  26.  
  27. local function plot(s,x,y)
  28.   term.setCursorPos(x,y)
  29.   term.write(s)
  30. end
  31.  
  32. local function approx(v)
  33.   if(v>=0) then
  34.     return math.floor(v+0.5)
  35.   else
  36.     return math.ceil(v+0.5)
  37.   end
  38. end
  39.  
  40. local function line(x0, y0, x1, y1)
  41.   if x0==x1 and y0==y1 then
  42.     return false
  43.   end
  44.  
  45.   if x0==x1 then
  46.     if y0>y1 then
  47.       inc=-1
  48.     else
  49.       inc=1
  50.     end
  51.    
  52.     for y=y0,y1,inc do
  53.       plot(".",x0,y)
  54.     end
  55.    
  56.     return true
  57.   end
  58.  
  59.   if y0==y1 then
  60.     if x0>x1 then
  61.       inc=-1
  62.     else
  63.       inc=1
  64.     end
  65.  
  66.     for x=x0,x1,inc do
  67.       plot(".",x,y0)
  68.     end
  69.  
  70.     return true
  71.   end
  72.  
  73.   if x0~=x1 and y0~=y1 then
  74.     m=(y1-y0)/(x1-x0)
  75.    
  76.     if m<1 and m>-1 then
  77.       n=y0-m*x0
  78.       if x0>x1 then
  79.         inc=-1
  80.       else
  81.         inc=1
  82.       end
  83.      
  84.       for x=x0,x1,inc do
  85.         y=m*x+n
  86.         y=approx(y)
  87.         plot(".",x,y)
  88.       end
  89.       return true
  90.     else
  91.       m=(x1-x0)/(y1-y0)
  92.       n=x0-m*y0
  93.       if y0>y1 then
  94.         inc=-1
  95.       else
  96.         inc=1
  97.       end
  98.      
  99.       for y=y0,y1,inc do
  100.         x=m*y+n
  101.         x=approx(x)
  102.         plot(".",x,y)
  103.       end
  104.       return true
  105.     end
  106.   end
  107. end
  108.  
  109. -- the clock face ticks
  110. local function ticks(nW,nH)
  111.   local nIndex
  112.   local x
  113.   local y
  114.   local xi
  115.   local yi
  116.   local nInRadius = nClockRad-(nClockRad/4)
  117.   local nOutRadius = nClockRad-1
  118.  
  119.   local nxOutRad = nOutRadius + (nOutRadius/2)
  120.   local nyOutRad = nOutRadius
  121.   local nxInRad = nInRadius + (nInRadius/2)
  122.   local nyInRad = nInRadius
  123.  
  124.   for nIndex=0,359,(360/12) do
  125.     term.setTextColor(colors.black)
  126.     term.setBackgroundColor(colors.black)
  127.  
  128.     x = nW + math.sin(math.rad(nIndex)) * nxOutRad
  129.     y = nH + math.cos(math.rad(nIndex)) * nyOutRad
  130.  
  131.     xi = nW + math.sin(math.rad(nIndex)) * nxInRad
  132.     yi = nH + math.cos(math.rad(nIndex)) * nyInRad
  133.  
  134.     line(xi,yi,x,y)
  135.   end
  136. end
  137.  
  138. -- draw the face (circle)
  139. -- which is essentially a line from
  140. -- centre to all points on the circle
  141. -- in character terms.  Who cares about
  142. -- overlap ;)
  143. local function face(nW,nH,nRadius)
  144.   local nIndex
  145.   local x
  146.   local y
  147.   local nxRad = nRadius + (nRadius/2)
  148.   local nyRad = nRadius
  149.   local nOldX = -1
  150.   local nOldY = -1
  151.  
  152.   for nIndex=0,359 do
  153.     x = nW + math.sin(math.rad(nIndex)) * nxRad
  154.     y = nH + math.cos(math.rad(nIndex)) * nyRad
  155.  
  156.     -- the circle
  157.     -- only do it if the positions are different from last time
  158.     -- speeds things up a bit    
  159.     if x~=nOldX or y~=nOldY then
  160.       term.setTextColor(colors.white)
  161.       term.setBackgroundColor(colors.white)
  162.  
  163.       line(nW,nH,x,y)
  164.  
  165.       nOldX = x
  166.       nOldY = y
  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.   if(nOldMX==nX and nOldMY==nY) then
  190.     return
  191.   end
  192.  
  193.   -- remove previous by drawing it again
  194.   -- in the face colour.  Also remove the
  195.   -- hand tag
  196.   if(nOldMX~=-1) then
  197.     term.setTextColor(colors.white)
  198.     term.setBackgroundColor(colors.white)
  199.     line(nW,nH,nOldMX,nOldMY)
  200.  
  201.     if(bShowMinuteChar) then
  202.       plot(".",nOldMX,nOldMY)
  203.     end
  204.   end
  205.  
  206.   nOldMX = nX
  207.   nOldMY = nY
  208.  
  209.   term.setTextColor(colors.black)
  210.   term.setBackgroundColor(colors.black)
  211.   line(nW,nH,nX,nY)
  212.  
  213.   -- draw the centre O
  214.   term.setTextColor(colors.black)
  215.   term.setBackgroundColor(colors.lightGray)
  216.   plot("O",nW,nH)
  217.  
  218.   if(bShowMinuteChar) then
  219.     -- draw the minute hand tag
  220.     term.setTextColor(colors.black)
  221.     term.setBackgroundColor(colors.lightGray)
  222.     plot("M",nX,nY)
  223.   end
  224. end
  225.  
  226. -- plot hour hand
  227. -- it removes the previous hour hand
  228. -- then draws the new one
  229. local function plotH(nW,nH,nX,nY)
  230.   if(nOldHX==nX and nOldHY==nY) then
  231.     return
  232.   end
  233.  
  234.   -- remove previous by drawing it again
  235.   -- in the face colour.  Also remove the
  236.   -- hand tag
  237.   if(nOldHX~=-1) then
  238.     term.setTextColor(colors.white)
  239.     term.setBackgroundColor(colors.white)
  240.     line(nW,nH,nOldHX,nOldHY)
  241.  
  242.     if(bShowHourChar) then
  243.       plot(".",nOldHX,nOldHY)
  244.     end
  245.   end
  246.  
  247.   nOldHX = nX
  248.   nOldHY = nY
  249.  
  250.   term.setTextColor(colors.black)
  251.   term.setBackgroundColor(colors.black)
  252.   line(nW,nH,nX,nY)
  253.  
  254.   if(bShowHourChar) then
  255.     -- draw the hour hand tag
  256.     term.setTextColor(colors.black)
  257.     term.setBackgroundColor(colors.lightGray)
  258.     plot("H",nX,nY)
  259.   end
  260. end
  261.  
  262. local fH
  263. local fM
  264. local fXH
  265. local fYH
  266. local fXM
  267. local fYM
  268. local nyMRadius = nClockRad - (nClockRad / 4)
  269. local nyHRadius = nClockRad - (nClockRad / 2)
  270.  
  271. local nxMRadius = nyMRadius + (nyMRadius / 2)
  272. local nxHRadius = nyHRadius + (nyHRadius / 2)
  273. local nW, nH = term.getSize()
  274.  
  275. nW = (nW / 2)+1
  276. nH = (nH / 2)+1
  277.  
  278. -- plot the circle
  279. face(nW,nH,nClockRad + 1)
  280.  
  281. local bRunning = true
  282. local nTimer
  283. nTimer = os.startTimer(0.25) -- the timer interval to update the clock
  284.  
  285. while(bRunning) do
  286.   parseTime()
  287.  
  288.   fH = (((h % 12)/12) + (m/60/12)) * 360
  289.   fM = (m/60) * 360
  290.  
  291.   -- shift by half a circle
  292.   fH = fH + 180
  293.   fM = fM + 180
  294.  
  295.   fXH = math.sin(math.rad(-fH))
  296.   fYH = math.cos(math.rad(fH))
  297.   fXM = math.sin(math.rad(-fM))
  298.   fYM = math.cos(math.rad(fM))
  299.  
  300.   plotH(nW,nH,  nW + fXH * nxHRadius, nH + fYH * nyHRadius)
  301.   plotM(nW,nH, nW + fXM * nxMRadius, nH + fYM * nyMRadius)
  302.  
  303.   -- draw the ticks on
  304.   ticks(nW,nH)
  305.  
  306.   local event, param1 = os.pullEvent()
  307.  
  308.   while (not (event=="timer" and param1==nTimer) and event~="char") do
  309.     event, param1 = os.pullEvent()
  310.   end
  311.  
  312.   if(event=="char") then
  313.      if(param1=="q" or param1=="Q") then
  314.        bRunning=false
  315.      end
  316.   elseif(event=="timer" and param1==nTimer) then
  317.      -- timer timed out, lets start again
  318.      nTimer = os.startTimer(0.25)
  319.   end
  320. end
  321.  
  322. term.restore()
  323. print("Ended.")
Advertisement
Add Comment
Please, Sign In to add comment