X-Git-Url: http://git.tomasm.cz/imago.git/blobdiff_plain/702d0af1ff46776307fd8792e39bdd7c9f8273a9..2b2ac5c384ccf1294b13f4477aa3d5bb60945cfb:/timer.py diff --git a/timer.py b/timer.py index e1e247b..587b436 100755 --- a/timer.py +++ b/timer.py @@ -6,19 +6,29 @@ import os import sys import time import argparse +import subprocess import pygame class Timer: - def __init__(self): + def __init__(self, main_time): self._last = 0 self._elapsed = 0. + self._main_t = main_time def get_time(self): if self._last > 0: - return time.time() - self._last + self._elapsed + r_time = main_time - (time.time() - self._last + self._elapsed) + if (r_time - int(r_time)) < 0.75: + sep = ":" + else: + sep = " " else: - return self._elapsed + r_time = main_time - self._elapsed + sep = ":" + if r_time < 0: + r_time = 0 + return "{0:0>2}".format(int(r_time / 60)) + sep + "{0:0>2}".format(int(r_time % 60)) def start(self): self._last = time.time() @@ -33,19 +43,38 @@ class Timer: else: return False +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument('-m', type=int, default=45, + help="main time in minutes") +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((400, 100)) +pygame.display.set_mode((600, 100)) pygame.display.set_caption("Go timer") screen = pygame.display.get_surface() clock = pygame.time.Clock() -font = pygame.font.Font(None, 50) +font = pygame.font.Font(pygame.font.match_font('monospace'), 80) done = False first = True -timers = (Timer(), Timer()) +main_time = args.m * 60 + +timers = (Timer(main_time), Timer(main_time)) + +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) while not done: @@ -56,7 +85,12 @@ while not done: 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, "take" for timer in timers: if timer.is_running(): timer.stop() @@ -64,9 +98,12 @@ while not done: timer.start() screen.fill([0, 0, 0]) - text1 = font.render("{0:>6.2f}".format(timers[0].get_time()), True, [128, 255, 128]) + text1 = font.render(timers[0].get_time(), True, [128, 255, 128]) screen.blit(text1, [10, 10]) - text2 = font.render("{0:>6.2f}".format(timers[1].get_time()), True, [128, 255, 128]) - screen.blit(text2, [200, 10]) + text2 = font.render(timers[1].get_time(), True, [128, 255, 128]) + screen.blit(text2, [300, 10]) pygame.display.flip() clock.tick(15) + + +print >> capture.stdin, "exit"