forked from jromang/picochess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgthw.py
123 lines (108 loc) · 4.9 KB
/
dgthw.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
# Copyright (C) 2013-2016 Jean-Francois Romang (jromang@posteo.de)
# Shivkumar Shivaji ()
# Jürgen Précour (LocutusOfPenguin@posteo.de)
#
# 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 chess import Board
from dgtiface import *
from dgtlib import *
from utilities import *
from threading import Lock
class DgtHw(DgtIface):
def __init__(self, dgtserial, dgttranslate):
super(DgtHw, self).__init__(dgtserial, dgttranslate)
self.lib_lock = Lock()
self.lib = DgtLib(self.dgtserial)
self.dgtserial.run()
def _display_on_dgt_xl(self, text, beep=False, left_dots=0, right_dots=0):
if not self.clock_found: # This can only happen on the XL function
logging.debug('DGT clock (still) not found. Ignore [%s]', text)
self.dgtserial.startup_serial_clock()
return
text = text.ljust(6)
if len(text) > 6:
logging.warning('DGT XL clock message too long [%s]', text)
logging.debug(text)
with self.lib_lock:
res = self.lib.set_text_xl(text, 0x03 if beep else 0x00, left_dots, right_dots)
if not res:
logging.warning('Finally failed %i', res)
def _display_on_dgt_3000(self, text, beep=False, left_dots=0, right_dots=0):
text = text.ljust(8)
if len(text) > 8:
logging.warning('DGT 3000 clock message too long [%s]', text)
logging.debug(text)
text = bytes(text, 'utf-8')
with self.lib_lock:
res = self.lib.set_text_3k(text, 0x03 if beep else 0x00, left_dots, right_dots)
if not res:
logging.warning('Finally failed %i', res)
def display_text_on_clock(self, text, beep=False, left_dots=0, right_dots=0):
if self.enable_dgt_3000:
self._display_on_dgt_3000(text, beep, left_dots, right_dots)
else:
self._display_on_dgt_xl(text, beep, left_dots, right_dots)
def display_move_on_clock(self, move, fen, side, beep=False, left_dots=0, right_dots=0):
if self.enable_dgt_3000:
bit_board = Board(fen)
move_text = bit_board.san(move)
if side == ClockSide.RIGHT:
move_text = move_text.rjust(8)
text = self.dgttranslate.move(move_text)
self._display_on_dgt_3000(text, beep, left_dots, right_dots)
else:
move_text = move.uci()
if side == ClockSide.RIGHT:
move_text = move_text.rjust(6)
self._display_on_dgt_xl(move_text, beep, left_dots, right_dots)
def display_time_on_clock(self, force=False):
if self.clock_running or force:
self.lib.end_text()
else:
logging.debug('DGT clock isnt running - no need for endClock')
def light_squares_revelation_board(self, squares):
if self.dgtserial.enable_revelation_leds:
for sq in squares:
dgt_square = (8 - int(sq[1])) * 8 + ord(sq[0]) - ord('a')
logging.debug("REV2 light on square %s", sq)
self.lib.write([DgtCmd.DGT_SET_LEDS, 0x04, 0x01, dgt_square, dgt_square])
def clear_light_revelation_board(self):
if self.dgtserial.enable_revelation_leds:
logging.debug('REV2 lights turned off')
self.lib.write([DgtCmd.DGT_SET_LEDS, 0x04, 0x00, 0, 63])
def stop_clock(self):
self.resume_clock(ClockSide.NONE)
def resume_clock(self, side):
l_hms = self.time_left
r_hms = self.time_right
if l_hms is None or r_hms is None:
logging.debug('time values not set - abort function')
return
lr = rr = 0
if side == ClockSide.LEFT:
lr = 1
if side == ClockSide.RIGHT:
rr = 1
with self.lib_lock:
res = self.lib.set_and_run(lr, l_hms[0], l_hms[1], l_hms[2], rr, r_hms[0], r_hms[1], r_hms[2])
if not res:
logging.warning('Finally failed %i', res)
else:
self.clock_running = (side != ClockSide.NONE)
# this is needed for some(!) clocks
self.lib.end_text()
def start_clock(self, time_left, time_right, side):
self.time_left = hours_minutes_seconds(time_left)
self.time_right = hours_minutes_seconds(time_right)
self.resume_clock(side)