From 4f7a833151333807c8f5f760bde11dacd81b46e9 Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Tue, 3 Oct 2017 16:36:23 +0200 Subject: [PATCH] Add sleep mode --- photoboite.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/photoboite.py b/photoboite.py index 70b485f..b5fe37c 100755 --- a/photoboite.py +++ b/photoboite.py @@ -126,7 +126,7 @@ class CaptureButton(Button): capture = Capture(self.count) self.count += 1 - for i in range(4): + for i in range(3): wait = True start = time.time() while wait: @@ -143,6 +143,7 @@ class CaptureButton(Button): pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP)) self.capture_mode = False self.enabled = True + self.photoboite.sleep.move() class Capture: @@ -166,6 +167,32 @@ class Photo: return self +class SleepMode: + def __init__(self, seconds): + self.enabled = True + self.drawn = False + self.seconds = seconds + + def move(self): + self.last_action = time.time() + + def event(self): + for event in pygame.event.get(): + if event.type == pygame.MOUSEBUTTONUP or event.type == pygame.KEYUP: + self.move() + self.enabled = False + + def draw_on(self, screen): + if not self.drawn: + screen.fill((255,255,255)) + pygame.display.flip() + self.drawn = True + + def sleep(self): + if time.time() - self.last_action > self.seconds: + self.enabled = True + self.drawn = False + class Photoboite: def __init__(self): self.screen = self.screen() @@ -185,6 +212,8 @@ class Photoboite: self.clock = pygame.time.Clock() + self.sleep = SleepMode(30) + def screen(self): if os.getenv('DISPLAY'): @@ -220,6 +249,8 @@ class Photoboite: def event(self): for event in pygame.event.get(): + self.sleep.move() + if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q': self.quit() elif event.type == pygame.MOUSEBUTTONDOWN: @@ -234,10 +265,15 @@ class Photoboite: def run(self): pressed = False while True: - self.event() - self.draw() - - pygame.display.flip() + if self.sleep.enabled: + self.sleep.event() + self.sleep.draw_on(self.screen) + self.clock.tick(60) + else: + self.event() + self.draw() + self.sleep.sleep() + pygame.display.flip() def quit(self): self.camera.close()