Skip to content

Simplifies catching keyboard interrupt #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions arduino/arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
OUTPUT = Pin.OUT
INPUT = Pin.IN

# Blocking by default to allow threads to run
# If you're not using threads, you can set this to True
NON_BLOCKING = False

HIGH = 1
LOW = 0

Expand Down Expand Up @@ -119,9 +123,6 @@ def copy_sketch(source_path = '', destination_path = '.', name = None, overwrite
# the following methods are just for testing
# will produce output when this module is run as __main__
def preload():
print()
print()
print()
print('preload test')

def setup():
Expand All @@ -132,48 +133,34 @@ def loop():
delay(1000)

def cleanup():
print()
print('cleanup test')
print()
print()


def frame_counter():
global frame_count
frame_count += 1
try:
sleep_ms(1)
return True
except (Exception, KeyboardInterrupt) as e:
if cleanup is not None:
cleanup()
if not isinstance(e, KeyboardInterrupt):
raise e
return False


# RUNTIME
def start(setup=None, loop=None, cleanup = None, preload = None):
if preload is not None:
preload()
if setup is not None:
setup()
while True:
try:
if loop is not None:
loop()
if not frame_counter():
try:
while True:
try:
if loop is not None:
loop()
if not NON_BLOCKING:
sleep_ms(1)

except (Exception, KeyboardInterrupt) as e:
if cleanup is not None:
cleanup()
if not isinstance(e, KeyboardInterrupt):
raise e
else:
break

except (Exception, KeyboardInterrupt) as e:
if cleanup is not None:
cleanup()
if not isinstance(e, KeyboardInterrupt):
raise e
else:
break
except (Exception, KeyboardInterrupt) as e:
if cleanup is not None:
cleanup()
if not isinstance(e, KeyboardInterrupt):
raise e

if __name__ == '__main__':
start(setup = setup, loop = loop, cleanup = cleanup, preload = preload)