X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/ecc0929cb0e011db2825e79d7495d7ef7b113e1e..8ad37ef82d656775eb955d19cafa736e00dacbae:/imago_pack/timer.py?ds=inline diff --git a/imago_pack/timer.py b/imago_pack/timer.py index bc8a5c3..b5ec3fb 100755 --- a/imago_pack/timer.py +++ b/imago_pack/timer.py @@ -59,78 +59,85 @@ class Timer: else: return False -parser = argparse.ArgumentParser(description=__doc__) -parser.add_argument('-m', type=int, default=10, - help="main time in minutes (default is 10)") -parser.add_argument('-b', type=int, nargs=2, default=[5, 20], - help="japanese byoyomi: number of periods, period length in" - " seconds (default is 5 periods, 20 seconds)") -parser.add_argument('-c', '--camera', dest='cam', action='store_true', - help="camera on") -parser.add_argument('-d', type=int, default=0, - help="video device id") -parser.add_argument('-r', type=int, nargs=2, default=[640, 480], - help="set camera resolution") -args = parser.parse_args() - - -pygame.init() -pygame.display.set_mode((600, 130)) -pygame.display.set_caption("Go timer") -screen = pygame.display.get_surface() - -clock = pygame.time.Clock() - -font = pygame.font.Font(pygame.font.match_font('monospace'), 80) -font2 = pygame.font.Font(None, 25) - -done = False -first = True - -main_time = args.m * 60 - -timers = (Timer(main_time, args.b[0], args.b[1]), Timer(main_time, args.b[0], args.b[1])) - -if args.cam: - capture = subprocess.Popen(['python', 'capture.py', '-c', '-d', str(args.d), '-r', - str(args.r[0]), - str(args.r[1])], stdin=subprocess.PIPE) -last = 0 - -while not done: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - done = True - elif event.type == pygame.KEYDOWN: - if time.time() - last < 0.7: - continue - last = time.time() - if first: - timers[0].start() - first = False +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('-m', type=int, default=10, + help="main time in minutes (default is 10)") + parser.add_argument('-b', type=int, nargs=2, default=[5, 20], + help="japanese byoyomi: number of periods, period length in" + " seconds (default is 5 periods, 20 seconds)") + parser.add_argument('-c', '--camera', dest='cam', action='store_true', + help="camera on") + parser.add_argument('-d', type=int, default=0, + help="video device id") + parser.add_argument('-r', type=int, nargs=2, default=[640, 480], + help="set camera resolution") + args = parser.parse_args() + + + pygame.init() + pygame.display.set_mode((600, 130)) + pygame.display.set_caption("Go timer") + screen = pygame.display.get_surface() + + clock = pygame.time.Clock() + + font = pygame.font.Font(pygame.font.match_font('monospace'), 80) + font2 = pygame.font.Font(None, 25) + + done = False + first = True + + main_time = args.m * 60 + + timers = (Timer(main_time, args.b[0], args.b[1]), Timer(main_time, args.b[0], args.b[1])) + + if args.cam: + capture = subprocess.Popen(['python', 'capture.py', '-c', '-d', str(args.d), '-r', + str(args.r[0]), + str(args.r[1])], stdin=subprocess.PIPE) + last = 0 + + while not done: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + done = True + elif event.type == pygame.KEYDOWN: + if time.time() - last < 0.7: + continue + last = time.time() + if first: + timers[0].start() + first = False + if args.cam: + print >> capture.stdin, "stop" + print >> capture.stdin, "take" + continue if args.cam: - print >> capture.stdin, "stop" print >> capture.stdin, "take" - continue - if args.cam: - print >> capture.stdin, "take" - for timer in timers: - if timer.is_running(): - timer.stop() - else: - timer.start() - - screen.fill([0, 0, 0]) - text1 = font.render(timers[0].get_time(), True, [128, 255, 128]) - screen.blit(text1, [10, 10]) - text2 = font.render(timers[1].get_time(), True, [128, 255, 128]) - screen.blit(text2, [300, 10]) - text3 = font2.render(timers[0].byost, True, [128, 255, 128]) - screen.blit(text3, [10, 90]) - text4 = font2.render(timers[1].byost, True, [128, 255, 128]) - screen.blit(text4, [300, 90]) - pygame.display.flip() - clock.tick(15) - -if args.cam: - print >> capture.stdin, "exit" + for timer in timers: + if timer.is_running(): + timer.stop() + else: + timer.start() + + screen.fill([0, 0, 0]) + text1 = font.render(timers[0].get_time(), True, [128, 255, 128]) + screen.blit(text1, [10, 10]) + text2 = font.render(timers[1].get_time(), True, [128, 255, 128]) + screen.blit(text2, [300, 10]) + text3 = font2.render(timers[0].byost, True, [128, 255, 128]) + screen.blit(text3, [10, 90]) + text4 = font2.render(timers[1].byost, True, [128, 255, 128]) + screen.blit(text4, [300, 90]) + pygame.display.flip() + clock.tick(15) + + if args.cam: + print >> capture.stdin, "exit" + +if __name__ == '__main__': + try: + main() + finally: + pygame.quit()