Added clock logic

This commit is contained in:
2025-12-15 10:23:46 +01:00
parent f3965aa2fb
commit 9e259bb9fb
5 changed files with 93 additions and 26 deletions

View File

@@ -2,18 +2,24 @@
from board_mate_client import ApiClient, Configuration
from controllers.board_mate_controller import ApiController
from models.clock import Clock
from scripts.timer.grove_rgb_lcd import setRGB, setText
if __name__ == "__main__":
config = Configuration(
host="http://192.168.15.117:8000"
)
setRGB(255, 255, 255)
controller = ApiController(config)
white_name = input("White Name: ")
black_name = input("Black Name: ")
time_value = int(input("Time value: "))
increment = int(input("Increment: "))
white_clock = Clock(time_value, increment)
black_clock = Clock(time_value, increment)
print("Creating the party...")
game_id = controller.create_party(white_name, black_name, time_value, increment)
@@ -24,16 +30,18 @@ if __name__ == "__main__":
print("Party Created!")
currentPlayer = 0
white_clock.start()
while True:
message = None
if currentPlayer == 0 :
message = "White to play"
currentPlayer = 1
white_clock.stop()
else :
message = "Black to play"
currentPlayer = 0
black_clock.start()
move = input(f"{message} : ")
controller.add_move(game_id, move)
setText(f"W ${white_clock.clock_to_str()}\n B ${black_clock.clock_to_str()}")
controller.add_move(game_id, move)

76
rpi/models/clock.py Normal file
View File

@@ -0,0 +1,76 @@
import threading
import time
from datetime import datetime
class Clock:
def __init__(self, value: int, increment: int) -> None:
self.millis = value * 1000
self.increment = increment * 1000
self._run_event = threading.Event()
self._terminate_event = threading.Event()
self._lock = threading.Lock()
self.thread = threading.Thread(target=self._update_clock, daemon=True)
# ---------- time computation ----------
def _compute_seconds(self) -> int:
return (self.millis // 1000) % 60
def _compute_minutes(self) -> int:
return self.millis // 60000
def _add_increment(self) -> None:
self.millis += self.increment
# ---------- thread logic ----------
def _update_clock(self):
while not self._terminate_event.is_set():
self._run_event.wait()
with self._lock:
if self.millis <= 0:
self.millis = 0
self._run_event.clear()
break
self.millis -= 1
time.sleep(0.001)
# ---------- public API ----------
def is_running(self) -> bool:
return self._run_event.is_set()
def start(self) -> None:
self._run_event.set()
if not self.thread.is_alive():
self.thread = threading.Thread(target=self._update_clock, daemon=True)
self.thread.start()
def stop(self) -> None:
if self._run_event.is_set():
with self._lock:
self._add_increment()
self._run_event.clear()
def terminate(self) -> None:
self._terminate_event.set()
self._run_event.set()
self.thread.join()
def clock_to_str(self) -> str:
with self._lock:
minutes = self._compute_minutes()
seconds = self._compute_seconds()
return f"{minutes:02d}:{seconds:02d}"
if __name__ == "__main__":
clock = Clock(600, 0)
clock.start()
while True:
time.sleep(1)
print(clock.clock_to_str())

5
rpi/models/player.py Normal file
View File

@@ -0,0 +1,5 @@
from clock import Clock
class Player:
def __init__(self) -> None:
pass

View File

@@ -1,22 +0,0 @@
import requests
import os
from dotenv import load_dotenv
def query_api(url, params=None, headers=None, timeout=10):
response = requests.get(
url,
params=params,
headers=headers,
timeout=timeout
)
response.raise_for_status() # Raises an error for 4xx/5xx responses
return response.json()
if __name__ == "__main__":
load_dotenv()
url = "https://api.example.com/users"
params = {"limit": 10}
data = query_api(url, params=params)
print(data)