From 009a4973f1330ab5a81d8f54cc2e05f402a2dda7 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 15:44:29 +0100 Subject: [PATCH 1/9] Enhanced exiting behaviour --- gamemenu.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/gamemenu.py b/gamemenu.py index b9d7c9c..7e32f8a 100755 --- a/gamemenu.py +++ b/gamemenu.py @@ -325,7 +325,12 @@ if __name__ == '__main__': inp1 = BalanceInput((host, int(port)), 0) inp2 = BalanceInput((host, int(port)), 1) - while inp1.get_event() != InputEvent.STEP_ON: + while True: + evt = inp1.get_event() + if evt == InputEvent.STEP_ON: + break + elif evt == InputEvent.QUIT: + sys.exit() time.sleep(0.05) init_frame = create_init_screen(dim) @@ -363,11 +368,15 @@ if __name__ == '__main__': wait_time = 3 start = time.time() while time.time() < start+wait_time: - if inp2.get_event() == InputEvent.STEP_ON: - inp1.running = False - inp2.running = False - start_game(game, 2) - time.sleep(0.1) + while True: + evt = inp2.get_event() + if evt == InputEvent.STEP_ON: + inp1.running = False + inp2.running = False + start_game(game, 2) + elif evt == InputEvent.QUIT: + sys.exit() + time.sleep(0.1) inp1.running = False inp2.running = False From 8fe38ceb5815174785fbf2c06c11fbb17004c18b Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 15:48:42 +0100 Subject: [PATCH 2/9] Possibly fixed detection of second player --- gamemenu.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gamemenu.py b/gamemenu.py index 7e32f8a..f5f3b34 100755 --- a/gamemenu.py +++ b/gamemenu.py @@ -177,6 +177,7 @@ InputEvent = enum.Enum('InputEvent', ['UP', 'DOWN', 'LEFT', class AbstractInput(): def __init__(self): + self.player_present = False pass def get_event(self): @@ -203,6 +204,8 @@ class TestInput(AbstractInput): pygame = self.__pygame for event in pygame.event.get(): if event.type == pygame.KEYDOWN: + if self.control.get(event.key, None) == InputEvent.STEP_ON: + self.player_present = True return self.controls.get(event.key, None) return None @@ -370,7 +373,7 @@ if __name__ == '__main__': while time.time() < start+wait_time: while True: evt = inp2.get_event() - if evt == InputEvent.STEP_ON: + if inp2.player_present: inp1.running = False inp2.running = False start_game(game, 2) From 1e9df425790b89fe109b959ad013d43cd1dd94e3 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 15:55:16 +0100 Subject: [PATCH 3/9] Removed a bit of debug output --- gamemenu.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gamemenu.py b/gamemenu.py index f5f3b34..5137d60 100755 --- a/gamemenu.py +++ b/gamemenu.py @@ -241,7 +241,6 @@ class BalanceInput(AbstractInput, threading.Thread): elif self.player_id == 1: xbal, ybal = p1x, p1y - print('player_id=%d xbal=%d ybal=%d' % (self.player_id, xbal, ybal)) THRESHOLD = 40 MIN_TIMES = { InputEvent.LEFT: 0.05, From b0efc71e2219483c7a520ada70269c7fe68a202f Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 16:16:52 +0100 Subject: [PATCH 4/9] Maybe fixed one player game --- gamemenu.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/gamemenu.py b/gamemenu.py index 5137d60..b4ce70b 100755 --- a/gamemenu.py +++ b/gamemenu.py @@ -367,18 +367,17 @@ if __name__ == '__main__': p2_frame = create_2player_screen(dim) out.sendFrame(p2_frame) - wait_time = 3 + wait_time = 2 start = time.time() while time.time() < start+wait_time: - while True: - evt = inp2.get_event() - if inp2.player_present: - inp1.running = False - inp2.running = False - start_game(game, 2) - elif evt == InputEvent.QUIT: - sys.exit() - time.sleep(0.1) + evt = inp2.get_event() + if inp2.player_present: + inp1.running = False + inp2.running = False + start_game(game, 2) + elif evt == InputEvent.QUIT: + sys.exit() + time.sleep(0.1) inp1.running = False inp2.running = False From 46c8208e2623c6e6e80c4a652f525cd3036ce97e Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 17:37:59 +0100 Subject: [PATCH 5/9] Battery voltages and weights can now be retrieved on the network port --- wii-pair/daemon.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index 990e275..7557a67 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -190,16 +190,30 @@ try: r = b'' for w in wiis: - if w.pos_x is None: - v = -128 + if m[0] == ord('w'): + # Return weight + v = 0 + if w.weight is not None: + v = round(w.weight) + r += struct.pack('b', v) + elif m[0] == ord('b'): + # Return battery + v = 0 + if w.board.battery is not None: + v = round(w.board.battery*100) + r += struct.pack('b', v) else: - v = int(round(w.pos_x*100)) - r += struct.pack('b', v) - if w.pos_y is None: - v = -128 - else: - v = int(round(w.pos_y*100)) - r += struct.pack('b', v) + # Return balance + if w.pos_x is None: + v = -128 + else: + v = int(round(w.pos_x*100)) + r += struct.pack('b', v) + if w.pos_y is None: + v = -128 + else: + v = int(round(w.pos_y*100)) + r += struct.pack('b', v) try: sock.send(r) From 4b97c34092280308b44aaab14508d6638ab8e9c2 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 19:31:25 +0100 Subject: [PATCH 6/9] Added 5ms sleep in gamemenu thread --- gamemenu.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gamemenu.py b/gamemenu.py index b4ce70b..5b8a4c6 100755 --- a/gamemenu.py +++ b/gamemenu.py @@ -285,6 +285,7 @@ class BalanceInput(AbstractInput, threading.Thread): self.evt = InputEvent.STEP_ON self.player_present = True continue + time.sleep(0.005) def get_event(self): evt = self.evt From fb31db8377e574cae7b4801601dcce155edbf19e Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 19:35:45 +0100 Subject: [PATCH 7/9] Sleep times in read thread depend on player presence --- gamemenu.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gamemenu.py b/gamemenu.py index 5b8a4c6..d1b5be7 100755 --- a/gamemenu.py +++ b/gamemenu.py @@ -279,13 +279,13 @@ class BalanceInput(AbstractInput, threading.Thread): self.evt = evt self.lastevt = evt print('player_id=%d event=%s' % (self.player_id, self.evt)) - + time.sleep(0.005) else: if xbal != -128 and ybal != -128: self.evt = InputEvent.STEP_ON self.player_present = True continue - time.sleep(0.005) + time.sleep(0.05) def get_event(self): evt = self.evt From 287d2cd7b1cb09756d655795d498b65a493c471e Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 19:45:05 +0100 Subject: [PATCH 8/9] Added stdout flushing --- wii-pair/daemon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index 7557a67..eef1002 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -73,6 +73,7 @@ class StatusThread(Thread): print(b1.ljust(colw) + b2) print(w1.ljust(colw) + w2) print(p1.ljust(colw) + p2) + sys.stdout.flush() time.sleep(1) From 64a53b69a2be800efd440600fac419c919007379 Mon Sep 17 00:00:00 2001 From: klonfish Date: Tue, 27 Dec 2016 19:51:13 +0100 Subject: [PATCH 9/9] Catch if nothing has been read from the socket --- wii-pair/daemon.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/wii-pair/daemon.py b/wii-pair/daemon.py index eef1002..34cd010 100755 --- a/wii-pair/daemon.py +++ b/wii-pair/daemon.py @@ -189,6 +189,10 @@ try: conns.remove(sock) continue + if len(m) == 0: + conns.remove(sock) + continue + r = b'' for w in wiis: if m[0] == ord('w'):