diff options
| author | hmj6502 <hashim@hmj6502.com> | 2025-12-19 09:32:26 +0000 |
|---|---|---|
| committer | hmj6502 <hashim@hmj6502.com> | 2025-12-19 09:32:26 +0000 |
| commit | 392ed614a8f6d10d31812150cffd20f39e91509a (patch) | |
| tree | 1050f2edbead3006a75e9f90765bd56305039890 /inputs.py | |
| parent | a80904fed39038d7a5d5dc0ef6b960bfbf35d492 (diff) | |
| download | lock-n-log-392ed614a8f6d10d31812150cffd20f39e91509a.tar.gz lock-n-log-392ed614a8f6d10d31812150cffd20f39e91509a.tar.bz2 lock-n-log-392ed614a8f6d10d31812150cffd20f39e91509a.zip | |
make timer accurate; add non blocking input
- timer accurate and quits when 'q' is pressed, in a non blocking
manner, polling at 120Hz
- some minor refactoring in inputs.py too
Diffstat (limited to 'inputs.py')
| -rw-r--r-- | inputs.py | 34 |
1 files changed, 21 insertions, 13 deletions
@@ -1,32 +1,35 @@ -import sys, tty, termios +import sys +import tty +import termios +import select +import datetime def getch(): - import sys, tty, termios - old_settings = termios.tcgetattr(0) + """get next character without needing to press enter""" + old_settings = termios.tcgetattr(sys.stdin) new_settings = old_settings[:] new_settings[3] &= ~termios.ICANON try: termios.tcsetattr(0, termios.TCSANOW, new_settings) ch = sys.stdin.read(1) -# except BlockingIOError: -# pass finally: termios.tcsetattr(0, termios.TCSANOW, old_settings) return ch +def getch_nb(timeout): + """gets next character in cbreak; returns false if none; cbreak must be set before""" + ch = False + if select.select([sys.stdin], [], [], timeout) == ([sys.stdin], [], []): + ch = sys.stdin.read(1) + return ch + def get_valid_char(char_list): """returns input if it matches a list of valid chars, else retruns False""" - valid = False - in_char = getch() for char in char_list: if in_char == char: - valid = True - break - - if not valid: - return False - return in_char + return in_char + return False def get_pos_int(): """returns input as int; returns False if not a positive integer""" @@ -38,3 +41,8 @@ def get_pos_int(): if in_int < 1: return False return in_int + +def delta_to_HM(delta): + """returns a %H:%M string from a timedelta object""" + mins, secs = divmod(int(delta.total_seconds()), 60) + return str(mins).zfill(2) + ":" + str(secs).zfill(2) |
