From e61153c9787b8286d0e8f65b8a29bab68b6ad7df Mon Sep 17 00:00:00 2001 From: will Date: Mon, 19 Aug 2024 13:11:18 +0100 Subject: [PATCH] added inital clock file --- clock.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 clock.py diff --git a/clock.py b/clock.py new file mode 100644 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() + -- 2.39.2