13
13
OUTPUT = Pin .OUT
14
14
INPUT = Pin .IN
15
15
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
+
16
20
HIGH = 1
17
21
LOW = 0
18
22
@@ -119,9 +123,6 @@ def copy_sketch(source_path = '', destination_path = '.', name = None, overwrite
119
123
# the following methods are just for testing
120
124
# will produce output when this module is run as __main__
121
125
def preload ():
122
- print ()
123
- print ()
124
- print ()
125
126
print ('preload test' )
126
127
127
128
def setup ():
@@ -132,48 +133,34 @@ def loop():
132
133
delay (1000 )
133
134
134
135
def cleanup ():
135
- print ()
136
136
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
-
154
137
155
138
# RUNTIME
156
139
def start (setup = None , loop = None , cleanup = None , preload = None ):
157
140
if preload is not None :
158
141
preload ()
159
142
if setup is not None :
160
143
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 :
166
153
if cleanup is not None :
167
154
cleanup ()
155
+ if not isinstance (e , KeyboardInterrupt ):
156
+ raise e
157
+ else :
168
158
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
177
164
178
165
if __name__ == '__main__' :
179
166
start (setup = setup , loop = loop , cleanup = cleanup , preload = preload )
0 commit comments