-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change to use asyncio rather than threading
This allows event-driven things to be far more easily driven (ie. pulsectl) Also, Rather than defining a source / sync for the mute buttons, use the current default one - how often do you need to immediately mute a device that's not in use?
- Loading branch information
Showing
17 changed files
with
262 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
__pycache__/ | ||
htmlcov/ | ||
venv/ | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,80 @@ | ||
import datetime | ||
from asyncio.events import get_event_loop | ||
from datetime import datetime | ||
import os | ||
import threading | ||
import enum | ||
import asyncio | ||
from time import sleep | ||
|
||
from devdeck_core.controls.deck_control import DeckControl | ||
|
||
|
||
class TimerState(enum.Enum): | ||
RUNNING = 1 | ||
STOPPED = 2 | ||
RESET = 3 | ||
|
||
|
||
class TimerControl(DeckControl): | ||
|
||
def __init__(self, key_no, **kwargs): | ||
self.start_time = None | ||
self.end_time = None | ||
self.thread = None | ||
super().__init__(key_no, **kwargs) | ||
self.loop = get_event_loop() | ||
self.start_time: datetime = None | ||
self.end_time: datetime = None | ||
self.state = TimerState.RESET | ||
super().__init__(key_no, ** kwargs) | ||
|
||
def initialize(self): | ||
self.loop.create_task(self._update_display()) | ||
with self.deck_context() as context: | ||
with context.renderer() as r: | ||
r.image(os.path.join(os.path.dirname(__file__), "../assets/font-awesome", 'stopwatch.png')).end() | ||
r.image(os.path.join(os.path.dirname(__file__), | ||
"../assets/font-awesome", 'stopwatch.png')).end() | ||
|
||
def pressed(self): | ||
if self.start_time is None: | ||
self.start_time = datetime.datetime.now() | ||
self.thread = threading.Thread(target=self._update_display) | ||
self.thread.start() | ||
elif self.end_time is None: | ||
self.end_time = datetime.datetime.now() | ||
self.thread.join() | ||
with self.deck_context() as context: | ||
with context.renderer() as r: | ||
r.text(TimerControl.time_diff_to_str(self.end_time - self.start_time))\ | ||
.font_size(120)\ | ||
.color('red')\ | ||
.center_vertically().center_horizontally().end() | ||
else: | ||
self.start_time = None | ||
self.end_time = None | ||
with self.deck_context() as context: | ||
with context.renderer() as r: | ||
r.image(os.path.join( | ||
os.path.join(os.path.dirname(__file__), "../assets/font-awesome", 'stopwatch.png'))).end() | ||
|
||
def _update_display(self): | ||
while self.end_time is None: | ||
if self.start_time is None: | ||
sleep(1) | ||
continue | ||
cutoff = datetime.datetime.now() if self.end_time is None else self.end_time | ||
match self.state: | ||
case TimerState.RESET: | ||
self.start_time = datetime.now() | ||
self.end_time = None | ||
self.state = TimerState.RUNNING | ||
case TimerState.RUNNING: | ||
if not self.start_time: | ||
raise Exception("how did you get here?") | ||
self.end_time = datetime.now() | ||
self.state = TimerState.STOPPED | ||
case TimerState.STOPPED: | ||
self.start_time = self.end_time = None | ||
self.state = TimerState.RESET | ||
|
||
async def _update_display(self, repeat=True): | ||
while True: | ||
with self.deck_context() as context: | ||
with context.renderer() as r: | ||
r.text(TimerControl.time_diff_to_str(cutoff - self.start_time)) \ | ||
.font_size(120) \ | ||
.center_vertically().center_horizontally().end() | ||
sleep(1) | ||
match self.state: | ||
case TimerState.RUNNING: | ||
r.text(TimerControl.time_diff_to_str(datetime.now() - self.start_time))\ | ||
.font_size(120)\ | ||
.color('red')\ | ||
.center_vertically().center_horizontally().end() | ||
case TimerState.STOPPED: | ||
r.text(TimerControl.time_diff_to_str(self.end_time - self.start_time))\ | ||
.font_size(120)\ | ||
.color('yellow')\ | ||
.center_vertically().center_horizontally().end() | ||
case _: | ||
r.image(os.path.join( | ||
os.path.join(os.path.dirname(__file__), "../assets/font-awesome", 'stopwatch.png'))).end() | ||
if repeat: | ||
await asyncio.sleep(0.1) | ||
else: | ||
return | ||
|
||
@staticmethod | ||
def time_diff_to_str(diff): | ||
seconds = diff.total_seconds() | ||
minutes, seconds = divmod(seconds, 60) | ||
total_seconds = diff.total_seconds() | ||
minutes, seconds = divmod(total_seconds, 60) | ||
hours, minutes = divmod(minutes, 60) | ||
if total_seconds < 60: | ||
return f'{int(seconds):02d}' | ||
elif total_seconds < 3600: | ||
return f'{int(minutes):02d}:{int(seconds):02d}' | ||
return f'{int(hours):02d}:{int(minutes):02d}:{int(seconds):02d}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.