Browse Source

Add analogclock demo

Francois Deppierraz 9 months ago
parent
commit
8a3f8c017f
4 changed files with 86 additions and 1 deletions
  1. 11 0
      analogclock/clock.py
  2. BIN
      analogclock/dot.png
  3. 62 0
      analogclock/node.lua
  4. 13 1
      screend.py

+ 11 - 0
analogclock/clock.py

@@ -0,0 +1,11 @@
+import socket, datetime
+
+now = datetime.datetime.now()
+
+since_midnight = (
+    now - 
+    now.replace(hour=0, minute=0, second=0)
+).seconds
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.sendto('analogclock/clock/set:%d' % since_midnight, ('127.0.0.1', 4444))

BIN
analogclock/dot.png


+ 62 - 0
analogclock/node.lua

@@ -0,0 +1,62 @@
+gl.setup(1024, 1024)
+
+local dot = resource.load_image "dot.png"
+local white = resource.create_colored_texture(1,1,1,1)
+
+local base_time = N.base_time or 0
+
+util.data_mapper{
+    ["clock/set"] = function(time)
+        base_time = tonumber(time) - sys.now()
+        N.base_time = base_time
+    end;
+}
+
+function hand(size, strength, angle, r,g,b,a)
+    gl.pushMatrix()
+    gl.translate(WIDTH/2, HEIGHT/2) 
+    gl.rotate(angle, 0, 0, 1)
+    white:draw(0, -strength, size, strength)
+    gl.popMatrix()
+end
+
+local bg
+
+function node.render()
+    if not bg then
+        gl.pushMatrix()
+        gl.translate(WIDTH/2, HEIGHT/2) 
+        for i = 0, 59 do
+            gl.pushMatrix()
+            gl.rotate(360/60*i, 0, 0, 1)
+            if i % 15 == 0 then
+                white:draw(WIDTH/2.1-80, -10, WIDTH/2.1, 10, 0.8)
+            elseif i % 5 == 0 then
+                white:draw(WIDTH/2.1-50, -10, WIDTH/2.1, 10, 0.5)
+            else
+                white:draw(WIDTH/2.1-5, -5, WIDTH/2.1, 5, 0.5)
+            end
+            gl.popMatrix()
+        end
+        gl.popMatrix()
+        bg = resource.create_snapshot()
+    else
+        bg:draw(0,0,WIDTH,HEIGHT)
+    end
+
+    local time = base_time + sys.now()
+
+    local hour = (time / 3600) % 12
+    local minute = time % 3600 / 60
+    local second = time % 60
+
+    local fake_second = second * 1.05
+    if fake_second >= 60 then
+        fake_second = 60
+    end
+
+    hand(WIDTH/4,   10, 360/12 * hour - 90)
+    hand(WIDTH/2.5, 5, 360/60 * minute - 90)
+    hand(WIDTH/2.1,  2, 360/60 * (((math.sin((fake_second-0.4) * math.pi*2)+1)/8) + fake_second) - 90)
+    dot:draw(WIDTH/2-30, HEIGHT/2-30, WIDTH/2+30, HEIGHT/2+30)
+end

+ 13 - 1
screend.py

@@ -8,6 +8,7 @@ from multiprocessing import Process
 import os
 import time, arrow
 import requests
+import socket, datetime
 
 PATH = os.path.dirname(os.path.realpath(__file__))
 
@@ -72,10 +73,21 @@ def bus():
         f.close()
 
         time.sleep(30)
-        
+
+def clock():
+    now = datetime.datetime.now()
+
+    since_midnight = (
+        now - 
+        now.replace(hour=0, minute=0, second=0)
+    ).seconds
+
+    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+    sock.sendto('analogclock/clock/set:%d' % since_midnight, ('127.0.0.1', 4444))
 
 if __name__ == '__main__':
     Process(target=bus).start()
     Process(target=webhook).start()
     Process(target=calendar).start()
     Process(target=power).start()
+    Process(target=clock).start()