From cd243353a99f49a26797654a2c611e875a9a30b9 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Sun, 11 Dec 2022 11:19:23 +0000 Subject: [PATCH] switch from pygame event loop to glib --- csc.py | 47 ++++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/csc.py b/csc.py index 07c311b..aaa4aa7 100644 --- a/csc.py +++ b/csc.py @@ -1,5 +1,6 @@ import BLE_GATT from gi.repository import GLib + import pdb import struct @@ -121,31 +122,35 @@ csc_measurement = '00002a5b-0000-1000-8000-00805F9B34FB' bike.on_value_change(csc_measurement, app.listener.process) running = True + +def handle_pygame_event(event): + if event.type == pygame.QUIT: + running = False + if event.type == pygame.KEYUP and event.key == pygame.K_q: + running = False + if event.type == pygame.KEYUP and event.key == pygame.K_SPACE: + if app.listener.startTime == None: + app.listener.startTime = datetime.now() + else: + app.listener.startTime = None + if event.type == pygame.VIDEORESIZE: + app.width = event.w + app.height = event.h + app.font = None + +def on_timer(): + handle_pygame_event(pygame.event.poll()) + app.on_update() + return True + + +GLib.timeout_add(100, on_timer) + try: # Time to go live - context = bike.mainloop.get_context() - print("Listening for events...") - while running: - time.sleep(0.1) - context.iteration(False) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False - if event.type == pygame.KEYUP and event.key == pygame.K_q: - running = False - if event.type == pygame.KEYUP and event.key == pygame.K_SPACE: - if app.listener.startTime == None: - app.listener.startTime = datetime.now() - else: - app.listener.startTime = None - if event.type == pygame.VIDEORESIZE: - app.width = event.w - app.height = event.h - app.font = None + bike.mainloop.run() - app.on_update() - finally: pygame.quit()