diff --git a/arduino/arduino.py b/arduino/arduino.py index 8719833..bf8fdd7 100644 --- a/arduino/arduino.py +++ b/arduino/arduino.py @@ -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 @@ -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(): @@ -132,25 +133,7 @@ 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): @@ -158,22 +141,26 @@ def start(setup=None, loop=None, cleanup = None, preload = 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) \ No newline at end of file