Advertisement
xuma202

Analog Clock v 1.0.3

Apr 2nd, 2012
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.24 KB | None | 0 0
  1. function HSVtoRGB(hue, sat, val)
  2.  
  3.   local h = round(hue / 60)
  4.   local f = hue / 60 - h
  5.   local p = val * (1 - sat)
  6.   local q = val * (1 - sat * f)
  7.   local t = val * (1- sat * (1 - f))
  8.   local r, g, b = 0
  9.  
  10.      
  11.   if h == 1 then
  12.     r = q
  13.     g = 1
  14.     b = p
  15.   elseif h == 2 then
  16.     r = p
  17.     g = 1
  18.     b = t      
  19.   elseif h == 3 then
  20.     r = p
  21.     g = q
  22.     b = 1    
  23.   elseif h == 4 then
  24.     r = t
  25.     g = p
  26.     b = 1      
  27.   elseif h == 5 then
  28.     r = 1
  29.     g = p
  30.     b = q      
  31.   else
  32.     r = 1
  33.     g = t
  34.     b = p      
  35.   end  
  36.  
  37.   return r, g, b
  38. end
  39.  
  40. function round(num, idp)
  41.   local mult = 10^(idp or 0)
  42.   return math.floor(num * mult + 0.5) / mult
  43. end
  44.  
  45.  
  46. print("       Analog Clock      ")
  47. sleep(3.0)
  48.  
  49. gpuCount = 0
  50. gpus = {}
  51. gpuSide = ""
  52.  
  53. --detecting gpus
  54.  
  55. sides = {"back","front","left","right","top"}
  56. for i = 1, 5, 1 do
  57.   if peripheral.isPresent(sides[i]) then
  58.     if peripheral.getType(sides[i]) == "gpu" then
  59.       gpuCount = gpuCount + 1                             --increasing gpu count
  60.       table.insert(gpus, (#gpus + 1), sides[i]) --adding gpu to list
  61.     end
  62.   end
  63. end
  64.  
  65. --tell User
  66. local exit = false
  67.  
  68. if gpuCount == 0 then
  69.   print("No gpu found!")
  70.   exit = true
  71. elseif (gpuCount > 1) then
  72.   print("More than 1 gpu found!")
  73.   print()
  74.   local reading = true
  75.   while reading do
  76.     print("Please choose:")
  77.     for i = 1, #gpus, 1 do
  78.       print("GPU at side: " .. gpus[i])
  79.     end
  80.     print()
  81.     gpuSide = read()
  82.    
  83.     for i = 1, #gpus, 1 do
  84.  
  85.       if gpuSide == gpus[i] then
  86.         reading = false
  87.       end
  88.     end
  89.    
  90.     if reading == true then
  91.       print("Invaild input!! Please repeat")
  92.       sleep(1.5)
  93.     end
  94.   end
  95.  
  96. elseif gpuCount == 1 then
  97.   gpuSide = gpus[1]
  98. end
  99.  
  100. if not exit then
  101.   print("GPU at side " .. gpuSide .. " selected")
  102.   print("")
  103.   print("Do you want the outer ring to crossfade colors? <y/n>")
  104.   local fade = read()
  105.  
  106.   if fade == "y" or fade == "Y" then
  107.     print("")
  108.     print("")
  109.     repeat
  110.       print("")
  111.       print("How many seconds should it take until the crossfading restart?")
  112.       fadeTime = tonumber(read())
  113.       if  type(fadeTime) ~= "number" or fadeTime <= 0 then print ("Illeagal Input (min = 1)") end
  114.     until type(fadeTime) == "number" and fadeTime > 0
  115.   end
  116.  
  117.   print("")
  118.   print("")
  119.   print("")
  120.   print("Please enter the frequency of frame updates.")
  121.   print("  smale value (2-10) => more MC FPS, maybe less accurate Time")
  122.   print("")
  123.   print("  high value (>10) => less MC FPS, high accurate Time")
  124.   print("A alue of > 25 is a waste of CPU usage")
  125.   print("")
  126.   print("Recommended: 6.5")
  127.  
  128.   repeat
  129.     frq = tonumber(read())
  130.     illegalInput = type(frq) ~= "number" or frq < 1 or frq > 35
  131.     if illegalInput then print("Illegal Input (min=1; max=100)") end
  132.   until  not illegalInput
  133.   print("")
  134.   print("")
  135.   print("")
  136.   print("")
  137.   print("")
  138.   print("Hold [Ctrl] + [T] to terminate!")
  139.   sleep(2)
  140.   print("Starting Clock")
  141.  
  142.   color = 0
  143.   my_r = 0
  144.   my_g = 0.3
  145.   my_b = 1
  146.   lasttick = 0
  147.  
  148.   --Generate Circle
  149.  
  150.   clockCode = " glBegin(GL_POINTS)"
  151.   for i = 0,360,1 do
  152.     clockCode = clockCode .. " glVertex2f(".. round(math.cos( math.rad(i) )*63.5 + 63) .. ",".. round(math.sin( math.rad(i) )*63.5 + 64)..")"    
  153.   end
  154.   clockCode = clockCode .. " glEnd()"
  155.   --EOF Generate Circle
  156.  
  157.   while true do
  158.     time = os.time()
  159.        
  160.     hours = math.floor(time)
  161.     minutes = math.floor((time-hours)*60)
  162.    
  163.     if fade == "y" or fade == "Y" then
  164.       my_r, my_g, my_b = HSVtoRGB(color, 1, 1)    
  165.       color = color + (( os.clock() - lasttick) / fadeTime  * 360)
  166.       lasttick = os.clock()
  167.       if color > 359 then color = 0 end
  168.     end
  169.      
  170.    
  171.     clockColor = "glColor3f(" .. my_r .. "," .. my_g .."," .. my_b .. ")"
  172.      
  173.    
  174.     -- hourHand
  175.    
  176.       hourHand = " glColor3f(1,1,1) glBegin(GL_TRIANGLES)"
  177.      
  178.       hourHand = hourHand .. "  glVertex2f(".. round(math.sin( math.rad((hours % 12) * 30 - 2) )*63.5 + 63) .. ",".. round(math.cos( math.rad((hours % 12) * 30 - 2) )*-63.5 + 64) .. ")"
  179.       hourHand = hourHand .. "  glVertex2f(".. round(math.sin( math.rad((hours % 12) * 30 + 2) )*63.5 + 63) .. ",".. round(math.cos( math.rad((hours % 12) * 30 + 2) )*-63.5 + 64) ..")"
  180.       hourHand = hourHand .. "  glVertex2f(63,64)"
  181.  
  182.      
  183.       hourHand = hourHand .. "  glEnd()"
  184.      
  185.     -- minuteHand
  186.    
  187.       minuteHand = " glColor3f(0.8,0,0.1) glBegin(GL_TRIANGLES)"
  188.            
  189.       minuteHand = minuteHand .. "  glVertex2f(".. round(math.sin( math.rad((minutes) * 6 - 1) )*63.5 + 63) .. ",".. round(math.cos( math.rad((minutes) * 6 - 1) )*-63.5 + 64) .. ")"
  190.       minuteHand = minuteHand .. "  glVertex2f(".. round(math.sin( math.rad((minutes) * 6 + 1) )*63.5 + 63) .. ",".. round(math.cos( math.rad((minutes) * 6 + 1) )*-63.5 + 64) ..")"
  191.       minuteHand = minuteHand .. "  glVertex2f(63,64)"
  192.            
  193.       minuteHand = minuteHand .. "  glEnd()"
  194.        
  195.    
  196.     --EOF creating OPENGL code
  197.    
  198.     gpu.setRenderCode(gpuSide, [[
  199.    
  200.       gpuBindOutputBuffer(1)
  201.       gpuClearBuffer()
  202.       ]] .. clockColor .. clockCode .. " " .. hourHand .. " " .. minuteHand)
  203.      
  204.     sleep(1 / frq)
  205.   end
  206.  
  207. end
  208.  
  209. print("Going down..")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement