]> OzVa Git service - doomsday-clock/commitdiff
added inital clock file
authorwill <greenwoodw50@gmail.com>
Mon, 19 Aug 2024 12:11:18 +0000 (13:11 +0100)
committerwill <greenwoodw50@gmail.com>
Mon, 19 Aug 2024 12:11:18 +0000 (13:11 +0100)
clock.py [new file with mode: 0644]

diff --git a/clock.py b/clock.py
new file mode 100644 (file)
index 0000000..2cbbc8a
--- /dev/null
+++ b/clock.py
@@ -0,0 +1,89 @@
+import requests
+import time
+import sacn
+import RPi.GPIO as GPIO
+
+class clock:
+    def __init__(self):
+        self.url = "https://data.ozva.co.uk/shopping/clock.json"
+
+        self.request_speed = 0.1
+        
+        self.light_speed = 0.5
+        self.cues = 0
+
+        self.current_position = 0
+        self.last_position = 0
+        self.movement_speed = 10
+        self.function = "linear"
+
+        get_loop = Process(target=self.get(), daemon = True)
+        move_loop = Process(target=self.move(), daemon = True)
+        light_loop = Process(target=self.light(), daemon = True)
+        get_loop.start()
+        move_loop.start()
+        light_loop.start()
+
+    def get(self):
+        try:
+            while True:
+                t = time.time()
+
+                r = requests.get(self.url)
+                data = r.json()
+
+                new_target = int(data["currentPosition"])
+                if new_target != self.target_position:
+                    self.last_position = self.current_position
+                    self.target_position = new_target
+
+                self.movement_speed = int(data["movementSpeed"])
+                self.function = data["function"]
+                self.cues = data["lightingCues"]
+
+                time.sleep(self.request_speed - (time.time() - t))
+
+        except:
+            pass
+
+    def move(self):
+        GPIO.setmode(GPIO.BOARD)
+        GPIO.setup((11, 13, 15), GPIO.OUT)
+
+        try:
+            while True:
+                t = time.time()
+
+                parked = self.current_position != self.target_position
+                forward = self.target_position >= self.current_position
+                if forward and (not parked):
+                    GPIO.output(13, GPIO.HIGH)
+                    GPIO.output(11, GPIO.HIGH)
+                elif (not forward) and (not parked):
+                    GPIO.output(11, GPIO.HIGH)
+
+                GPIO.output((11, 13, 15), GPIO.LOW)
+
+                time.sleep(self.movement_speed - (time.time() - t))
+
+        except:
+            GPIO.cleanup()
+
+    def light(self):
+        sender = sacn.sACNsender()
+        sender.start()
+        sender.activate_output(2)
+        sender[2].multicast = True
+            
+        try:
+            while True:
+                t = time.time()
+
+                data = tuple(255 if i == "true" else 0 for i in self.cues)
+                sender.dmx_data = data
+
+                time.sleep(self.light_speed - (time.time() - t))
+
+        except:
+            sender.stop()
+