forked from jromang/picochess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgtiface.py
195 lines (173 loc) · 7.96 KB
/
dgtiface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (C) 2013-2016 Jean-Francois Romang ([email protected])
# Shivkumar Shivaji ()
# Jürgen Précour ([email protected])
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from utilities import *
from threading import Timer, Thread, Lock
class DgtIface(DisplayDgt, Thread):
def __init__(self, dgtserial, dgttranslate):
super(DgtIface, self).__init__()
self.dgtserial = dgtserial
self.dgttranslate = dgttranslate
self.enable_dgt_3000 = False
self.enable_dgt_pi = self.dgtserial.is_pi
self.clock_found = False
self.time_left = None
self.time_right = None
self.maxtimer = None
self.maxtimer_running = False
self.clock_running = False
self.time_factor = 1 # This is for testing the duration - remove it lateron!
# delayed task array
self.tasks = []
self.do_process = True
self.msg_lock = Lock()
self.display_hash = None # Hash value of clock's display
def display_text_on_clock(self, text, beep=False, left_dots=0, right_dots=0):
raise NotImplementedError()
def display_move_on_clock(self, move, fen, side, beep=False, left_dots=0, right_dots=0):
raise NotImplementedError()
def display_time_on_clock(self, force=False):
raise NotImplementedError()
def light_squares_revelation_board(self, squares):
raise NotImplementedError()
def clear_light_revelation_board(self):
raise NotImplementedError()
def stop_clock(self):
raise NotImplementedError()
def resume_clock(self, side):
raise NotImplementedError()
def start_clock(self, time_left, time_right, side):
raise NotImplementedError()
def stopped_maxtimer(self):
self.maxtimer_running = False
if self.clock_running:
logging.debug('showing the running clock again')
# self.display_time_on_clock(force=False)
self.show(Dgt.DISPLAY_TIME(force=False, wait=True))
else:
logging.debug('clock not running - ignored maxtime')
if self.tasks:
logging.debug('processing delayed tasks: {}'.format(self.tasks))
while self.tasks:
message = self.tasks.pop(0)
self.process_message(message)
if self.maxtimer_running: # run over the task list until a maxtime command was processed
break
def handle_message(self, message):
for case in switch(message):
if case(DgtApi.DISPLAY_MOVE):
ld = message.ld if hasattr(message, 'ld') else 0
rd = message.rd if hasattr(message, 'rd') else 0
self.display_move_on_clock(message.move, message.fen, message.side, message.beep, ld, rd)
break
if case(DgtApi.DISPLAY_TEXT):
if self.enable_dgt_pi:
text = message.l
else:
text = message.m if self.enable_dgt_3000 else message.s
if text is None:
text = message.m
ld = message.ld if hasattr(message, 'ld') else 0
rd = message.rd if hasattr(message, 'rd') else 0
self.display_text_on_clock(text, message.beep, ld, rd)
break
if case(DgtApi.DISPLAY_TIME):
self.display_time_on_clock(message.force)
break
if case(DgtApi.LIGHT_CLEAR):
self.clear_light_revelation_board()
break
if case(DgtApi.LIGHT_SQUARES):
self.light_squares_revelation_board(message.squares)
break
if case(DgtApi.CLOCK_STOP):
self.clock_running = False
self.stop_clock()
break
if case(DgtApi.CLOCK_START):
self.clock_running = (message.side != ClockSide.NONE)
# log times
l_hms = hours_minutes_seconds(message.time_left)
r_hms = hours_minutes_seconds(message.time_right)
logging.debug('last time received from clock l:{} r:{}'.format(self.time_left, self.time_right))
logging.debug('sending time to clock l:{} r:{}'.format(l_hms, r_hms))
self.start_clock(message.time_left, message.time_right, message.side)
break
if case(DgtApi.CLOCK_VERSION):
if not self.clock_found:
self.show(self.dgttranslate.text('Y20_picochess'))
self.clock_found = True
if message.main == 2:
self.enable_dgt_3000 = True
break
if case(DgtApi.CLOCK_TIME):
self.time_left = message.time_left
self.time_right = message.time_right
break
if case(): # Default
pass
def process_message(self, message):
do_handle = True
if repr(message) in (DgtApi.CLOCK_START, DgtApi.CLOCK_STOP):
self.display_hash = None # Cant know the clock display if command changing the running status
else:
if repr(message) in (DgtApi.DISPLAY_MOVE, DgtApi.DISPLAY_TEXT):
if self.display_hash == hash(message) and not message.beep:
do_handle = False
else:
self.display_hash = hash(message)
if do_handle:
logging.debug("handle DgtApi: %s", message)
if hasattr(message, 'maxtime') and message.maxtime > 0:
self.maxtimer = Timer(message.maxtime * self.time_factor, self.stopped_maxtimer)
self.maxtimer.start()
logging.debug('showing {} for {} secs'.format(message, message.maxtime * self.time_factor))
self.maxtimer_running = True
with self.msg_lock:
self.handle_message(message)
else:
logging.debug("ignore DgtApi: %s", message)
def run(self):
logging.info('dgt_queue ready')
while True:
# Check if we have something to display
try:
message = self.dgt_queue.get()
logging.debug("received command from dgt_queue: %s", message)
self.do_process = True
if self.maxtimer_running:
if hasattr(message, 'wait'):
if message.wait:
self.tasks.append(message)
logging.debug('tasks delayed: {}'.format(self.tasks))
self.do_process = False
else:
logging.debug('ignore former maxtime')
self.maxtimer.cancel()
self.maxtimer_running = False
if self.tasks:
logging.debug('delete following tasks: {}'.format(self.tasks))
self.tasks = []
else:
logging.debug('command doesnt change the clock display => no need to interrupt max timer')
else:
logging.debug('max timer not running')
if self.do_process:
self.process_message(message)
else:
logging.debug('task delayed: {}'.format(message))
except queue.Empty:
pass