1313OUTPUT = Pin .OUT
1414INPUT = Pin .IN
1515
16+ # Blocking by default to allow threads to run
17+ # If you're not using threads, you can set this to True
18+ NON_BLOCKING = False
19+
1620HIGH = 1
1721LOW = 0
1822
@@ -119,9 +123,6 @@ def copy_sketch(source_path = '', destination_path = '.', name = None, overwrite
119123# the following methods are just for testing
120124# will produce output when this module is run as __main__
121125def preload ():
122- print ()
123- print ()
124- print ()
125126 print ('preload test' )
126127
127128def setup ():
@@ -132,48 +133,34 @@ def loop():
132133 delay (1000 )
133134
134135def cleanup ():
135- print ()
136136 print ('cleanup test' )
137- print ()
138- print ()
139-
140-
141- def frame_counter ():
142- global frame_count
143- frame_count += 1
144- try :
145- sleep_ms (1 )
146- return True
147- except (Exception , KeyboardInterrupt ) as e :
148- if cleanup is not None :
149- cleanup ()
150- if not isinstance (e , KeyboardInterrupt ):
151- raise e
152- return False
153-
154137
155138# RUNTIME
156139def start (setup = None , loop = None , cleanup = None , preload = None ):
157140 if preload is not None :
158141 preload ()
159142 if setup is not None :
160143 setup ()
161- while True :
162- try :
163- if loop is not None :
164- loop ()
165- if not frame_counter ():
144+ try :
145+ while True :
146+ try :
147+ if loop is not None :
148+ loop ()
149+ if not NON_BLOCKING :
150+ sleep_ms (1 )
151+
152+ except (Exception , KeyboardInterrupt ) as e :
166153 if cleanup is not None :
167154 cleanup ()
155+ if not isinstance (e , KeyboardInterrupt ):
156+ raise e
157+ else :
168158 break
169-
170- except (Exception , KeyboardInterrupt ) as e :
171- if cleanup is not None :
172- cleanup ()
173- if not isinstance (e , KeyboardInterrupt ):
174- raise e
175- else :
176- break
159+ except (Exception , KeyboardInterrupt ) as e :
160+ if cleanup is not None :
161+ cleanup ()
162+ if not isinstance (e , KeyboardInterrupt ):
163+ raise e
177164
178165if __name__ == '__main__' :
179166 start (setup = setup , loop = loop , cleanup = cleanup , preload = preload )
0 commit comments