Commit 9898d72b authored by Tom Pillot's avatar Tom Pillot
Browse files

Start all Thymios at the same time

parent 43f0078a
game/icons/start.png

5.92 KB | W: | H:

game/icons/start.png

3.73 KB | W: | H:

game/icons/start.png
game/icons/start.png
game/icons/start.png
game/icons/start.png
  • 2-up
  • Swipe
  • Onion skin
import cv2 import cv2
from socket import *
from PyQt5 import QtWidgets as qtw from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg from PyQt5 import QtGui as qtg
...@@ -7,7 +8,9 @@ from game.detector import Detector ...@@ -7,7 +8,9 @@ from game.detector import Detector
class ScoreWindow(ScoreWindowUi): class ScoreWindow(ScoreWindowUi):
SEND_TIME = 500
WEBCAM_REFRESH_TIME = 50 WEBCAM_REFRESH_TIME = 50
PORT = 42563
def __init__(self, teams): def __init__(self, teams):
super().__init__() super().__init__()
...@@ -15,22 +18,31 @@ class ScoreWindow(ScoreWindowUi): ...@@ -15,22 +18,31 @@ class ScoreWindow(ScoreWindowUi):
self.teams = teams self.teams = teams
self.initial_time = 0 self.initial_time = 0
self.time_is_red = False self.time_is_red = False
self.is_started = False self.join_started = False
self.detector = Detector() self.detector = Detector()
self.start_stop_button.clicked.connect(self.start_stop_activity) self.socket = socket(AF_INET, SOCK_DGRAM)
self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.socket.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
self.socket_dest = ('255.255.255.255', self.PORT)
self.scoreboard_timer = qtc.QTimer()
self.webcam_timer = qtc.QTimer()
self.send_timer = qtc.QTimer()
self.start_join_button.clicked.connect(self.start_stop_activity)
def start_scoreboard(self, time): def start_scoreboard(self, time):
# Start and display the clock # Start and display the clock
self.time_edit.setTime(time) self.time_edit.setTime(time)
self.initial_time = - time.secsTo(qtc.QTime(0, 0, 0, 0)) # Time set initially in seconds self.initial_time = - time.secsTo(qtc.QTime(0, 0, 0, 0)) # Time set initially in seconds
self.timer.timeout.connect(self.timer_timeout) self.scoreboard_timer.timeout.connect(self.timer_timeout)
# Show the webcam # Show the webcam
self.webcam_timer.timeout.connect(self.refresh_webcam) self.webcam_timer.timeout.connect(self.refresh_webcam_no_detect)
self.refresh_webcam(detect_tags=False) self.webcam_timer.start(self.WEBCAM_REFRESH_TIME)
def timer_timeout(self): def timer_timeout(self):
self.timer.start(1000) self.scoreboard_timer.start(1000)
if self.time_edit.time() == qtc.QTime(0, 0, 0, 0): if self.time_edit.time() == qtc.QTime(0, 0, 0, 0):
if self.time_is_red: if self.time_is_red:
self.time_edit.setStyleSheet("color: rgba(255, 255, 255, 222)") self.time_edit.setStyleSheet("color: rgba(255, 255, 255, 222)")
...@@ -75,6 +87,9 @@ class ScoreWindow(ScoreWindowUi): ...@@ -75,6 +87,9 @@ class ScoreWindow(ScoreWindowUi):
# Keep the points item so the points can be modified later easily # Keep the points item so the points can be modified later easily
team.points_item = points_item team.points_item = points_item
def refresh_webcam_no_detect(self):
self.refresh_webcam(detect_tags=False)
def refresh_webcam(self, detect_tags=True): def refresh_webcam(self, detect_tags=True):
# Read an image from the webcam and detect the tags it contains # Read an image from the webcam and detect the tags it contains
ret, frame = self.detector.cam.read() ret, frame = self.detector.cam.read()
...@@ -112,9 +127,7 @@ class ScoreWindow(ScoreWindowUi): ...@@ -112,9 +127,7 @@ class ScoreWindow(ScoreWindowUi):
item.setText("#" + str(row + 1)) item.setText("#" + str(row + 1))
def add_points(self, tag_id): def add_points(self, tag_id):
""" """ Add points to the team with the corresponding tag id """
Add points to the team with the corresponding tag id
"""
team = self.teams[tag_id] team = self.teams[tag_id]
remaining_time = - self.time_edit.time().secsTo(qtc.QTime(0, 0, 0, 0)) remaining_time = - self.time_edit.time().secsTo(qtc.QTime(0, 0, 0, 0))
if remaining_time > 0: # Teams can't win any points after the timer has ended if remaining_time > 0: # Teams can't win any points after the timer has ended
...@@ -130,16 +143,34 @@ class ScoreWindow(ScoreWindowUi): ...@@ -130,16 +143,34 @@ class ScoreWindow(ScoreWindowUi):
self.list_widget.insertItem(0, message) self.list_widget.insertItem(0, message)
def start_stop_activity(self): def start_stop_activity(self):
""" """ Start or stop the activity depending on the last button state """
Start or stop the activity depending on the last button state if self.join_started:
""" # Start scoreboard and tag detection
if self.is_started: self.webcam_timer.timeout.connect(self.refresh_webcam)
self.timer.stop() self.scoreboard_timer.start(1000)
self.webcam_timer.stop()
self.start_stop_button.setIcon(qtg.QIcon('game/icons/start.png'))
self.is_started = False
else:
self.timer.start(1000)
self.webcam_timer.start(self.WEBCAM_REFRESH_TIME) self.webcam_timer.start(self.WEBCAM_REFRESH_TIME)
self.start_stop_button.setIcon(qtg.QIcon('game/icons/stop.png'))
self.is_started = True self.start_join_button.setIcon(qtg.QIcon('game/icons/start.png'))
self.join_started = False
# Run all Thymios at the same time
self.send_timer.timeout.connect(self.send_start_broadcast)
self.send_timer.start(self.SEND_TIME)
else:
self.start_join_button.setIcon(qtg.QIcon('game/icons/join.png'))
self.join_started = True
# Send a message to all Thymios so they can join the activity
self.send_timer.timeout.connect(self.send_join_broadcast)
self.send_timer.start(self.SEND_TIME)
def send_join_broadcast(self):
""" Send a message to all Thymios so they can join the activity """
self.socket.sendto("join".encode(), self.socket_dest)
self.send_timer.start(self.SEND_TIME)
def send_start_broadcast(self):
""" Send a message to all Thymios to run at the same time """
self.socket.sendto("start".encode(), self.socket_dest)
self.send_timer.start(self.SEND_TIME)
...@@ -18,9 +18,9 @@ class ScoreWindowUi(qtw.QMainWindow): ...@@ -18,9 +18,9 @@ class ScoreWindowUi(qtw.QMainWindow):
palette.setColor(qtg.QPalette.Background, qtg.QColor("#E0E1E0")) palette.setColor(qtg.QPalette.Background, qtg.QColor("#E0E1E0"))
self.setPalette(palette) self.setPalette(palette)
self.start_stop_button = qtw.QPushButton() self.start_join_button = qtw.QPushButton()
self.start_stop_button.setIcon(qtg.QIcon('game/icons/start.png')) self.start_join_button.setIcon(qtg.QIcon('game/icons/start.png'))
self.start_stop_button.setIconSize(qtc.QSize(50, 50)) self.start_join_button.setIconSize(qtc.QSize(50, 50))
self.time_edit = qtw.QTimeEdit() self.time_edit = qtw.QTimeEdit()
self.time_edit.setDisplayFormat("mm:ss") self.time_edit.setDisplayFormat("mm:ss")
...@@ -28,12 +28,15 @@ class ScoreWindowUi(qtw.QMainWindow): ...@@ -28,12 +28,15 @@ class ScoreWindowUi(qtw.QMainWindow):
self.time_edit.setFixedHeight(200) self.time_edit.setFixedHeight(200)
self.time_edit.setDisabled(True) # The time can't be modified or selected self.time_edit.setDisabled(True) # The time can't be modified or selected
horizontal_spacer = qtw.QSpacerItem(40, 20, qtw.QSizePolicy.Minimum)
# The time edit needs to be inside a widget so that we can set the style of the widget and # The time edit needs to be inside a widget so that we can set the style of the widget and
# later when the font color of the time edit is changed, the widget style remains the same # later when the font color of the time edit is changed, the widget style remains the same
self.time_widget = qtw.QWidget() self.time_widget = qtw.QWidget()
hlayout1 = qtw.QHBoxLayout() hlayout1 = qtw.QHBoxLayout()
hlayout1.addItem(horizontal_spacer)
hlayout1.addWidget(self.time_edit) hlayout1.addWidget(self.time_edit)
hlayout1.addWidget(self.start_stop_button) hlayout1.addWidget(self.start_join_button)
self.time_widget.setLayout(hlayout1) self.time_widget.setLayout(hlayout1)
with open("game/qss/time_widget.qss", "r") as f: with open("game/qss/time_widget.qss", "r") as f:
...@@ -46,7 +49,6 @@ class ScoreWindowUi(qtw.QMainWindow): ...@@ -46,7 +49,6 @@ class ScoreWindowUi(qtw.QMainWindow):
self.list_widget.setStyleSheet(f.read()) self.list_widget.setStyleSheet(f.read())
self.webcam_label = qtw.QLabel() self.webcam_label = qtw.QLabel()
self.webcam_timer = qtc.QTimer()
self.table_widget = qtw.QTableWidget(0, 3, self) self.table_widget = qtw.QTableWidget(0, 3, self)
self.table_widget.setHorizontalHeaderItem(self.RANK, qtw.QTableWidgetItem("Classement")) self.table_widget.setHorizontalHeaderItem(self.RANK, qtw.QTableWidgetItem("Classement"))
...@@ -66,8 +68,6 @@ class ScoreWindowUi(qtw.QMainWindow): ...@@ -66,8 +68,6 @@ class ScoreWindowUi(qtw.QMainWindow):
with open("game/qss/table_widget.qss", "r") as f: with open("game/qss/table_widget.qss", "r") as f:
self.setStyleSheet(f.read()) self.setStyleSheet(f.read())
self.timer = qtc.QTimer(self)
vlayout1 = qtw.QVBoxLayout() vlayout1 = qtw.QVBoxLayout()
vlayout1.addWidget(self.list_widget) vlayout1.addWidget(self.list_widget)
vlayout1.addWidget(self.webcam_label) vlayout1.addWidget(self.webcam_label)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment