-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneotimer.py
251 lines (232 loc) · 8.55 KB
/
neotimer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# Copyright [2022] [Jose Rullan]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__repo__ = "https://github.com/jrullan/micropython_neotimer.git"
##########################################################################
# Raspberry Pi Pico - Non Blocking Timer (Neotimer)
#
# Library footprint: approx 3kB
# Instance footprint: 64-112 bytes
#
# This program shows how to implement a non-blocking delay function
# to use in your program. It is based on the neotimer library I developed
# for Arduino and Propeller 2 in Spin 2.
#
# When you use a time.sleep() function in a program,
# the processor stops everything it is doing until this delay is completed.
# That is called a blocking delay, because it blocks the processor until it finishes.
#
# There are many times when we don't want this to happen.
# This timer provides a way to use time delays without
# blocking the processor, so it can do other things while the timer ends up.
# This is called a non-blocking delay timer.
#
# The timer provides basic functionality to implement different ways of timing in a program.
# You can use the timer in the following ways:
#
# A) Start-Stop-Restart Timer - You can start, stop and restart the timer until done.
# ------------------------------------------------------------------------------------
#
# start() will reset the time (counting time) and set started and waiting true.
# stop() will set started and waiting false.
# It will also return the elapsed milliseconds since it was started
# restart() will set the timer to started and waiting but will not reset the time.
#
#
# note_timer = Neotimer(200) <-------- Initializes a 200ms timer
#
# if collision_detected:
# note_timer.start() <--------- Starts timer
# explorer.set_tone(beep_tone)
# if note_timer.finished():
# explorer.set_tone(-1) <--------- Called after 200ms
#
#
# B) Periodic trigger - The following example will toggle pin 56 every 500ms
# ------------------------------------------------------------------------------------
#
# led_pin = Pin(25,Pin.OUT)
# myTimer = Neotimer(500)<---------------- Initializes a 500ms timer
#
# while True:
# if(myTimer.repeat_execution())
# led_pin.toggle() <---------------- Called every 500ms
#
#
# C) Periodic trigger with count - The following example will toggle pin 56 every 500ms,
# only 3 times. To reset the repetitions use reset_repetitions().
# ------------------------------------------------------------------------------------
#
# led_pin = Pin(25,Pin.OUT)
# button = Pin(2, Pin.IN)
#
# myTimer = Neotimer(500)<---------------- Initializes a 500ms timer
#
# while True:
# if(myTimer.repeat_execution(3)) <--- Only repeat 3 times
# led_pin.toggle() <---------------- Called every 500ms
#
# if(button.value())
# myTimer.reset_repetitions() <----- Reset repetitions
#
#
# D) Debouncer for signals - You can debounce a signal using debouce_signal.
# The debouncing period will be duration.
# ------------------------------------------------------------------------------------
# In this example, the button pin value signal will
# be debounced for 250 milliseconds:
#
# button = Pin(2, Pin.IN)
# presses = 0
# myTimer = Neotimer(250) <--------------- Initializes a 250ms timer
#
# while True:
# if myTimer.debounce_signal(button.value()): <----- button pressed signal debounced for 250ms
# presses += 1
# print(presses)
#
#
# E) Waiting - The following example will turn on the led for 1000ms each time the button is pressed
# ------------------------------------------------------------------------------------
#
# from machine import Pin
# from neotimer import *
#
# button = Pin(2, Pin.IN)
# led = Pin(25,Pin.OUT)
# led.off()
#
# myTimer = Neotimer(1000)
# debouncer = Neotimer(200)
#
# while True:
#
# if debouncer.debounce_signal(button.value()):
# myTimer.start()
#
# if myTimer.waiting():
# led.on()
# else:
# led.off()
#
#
# F) Hold Signal - If button is pressed for 1 second turn on the LED
# ------------------------------------------------------------------------------------
#
# from neotimer import *
# from machine import Pin
#
# BUTTON_A = Pin(20,Pin.IN)
#
# led = Pin(25,Pin.OUT)
#
# myTimer = Neotimer(1000)
#
# while True:
# if myTimer.hold_signal(BUTTON_A.value()):
# led.on()
# else:
# led.off()
#
#
# Author: Jose Rullan
# Date: January 24, 2022
##########################################################################
#import time
from time import ticks_ms, ticks_diff
# Neotimer Class
class Neotimer:
def __init__(self,duration):
self.duration = duration
self.last = ticks_ms()
self.started = False
self.done = False
self.repetitions = -1 #Unlimited
# Starts the timer
def start(self):
self.reset()
self.started = True
# Stops the timer
def stop(self):
self.started = False
return self.get_elapsed()
# Resets the timer
def reset(self):
self.stop()
self.last = ticks_ms()
self.done = False
# Restarts the timer
def restart(self):
if not self.done:
self.started = True
# Returns True if the timer has finished
def finished(self):
if not self.started:
return False
if self.get_elapsed() >= self.duration:
self.done = True
return True
else:
return False
# Returns elapsed time
def get_elapsed(self):
return ticks_diff(ticks_ms(),self.last)
# Debounces a signal with duration
def debounce_signal(self,signal):
if not self.started:
self.start()
if signal and self.finished():
self.start()
return True
else:
return False
# Returns true if a signal is on for duration
def hold_signal(self,signal):
if signal:
if not self.started:
self.start()
return True if self.finished() else False
self.reset() #<--- Stops and resets the timer.
return False
# Returns true when timer is done and resets it
def repeat_execution(self):
if self.finished():
self.reset()
return True
if not self.started:
self.started = True
self.last = ticks_ms()
return False
# Executes repeat_execution count times
def repeat_execution_times(self,count):
if count != -1:
if self.repetitions == -1: #<---- Initial state is -1
self.repetitions = count
if self.repetitions == 0: #<---- When finished return False
return False
if self.repeat_execution(): #<---- Otherwise call repeat_execution()
self.repetitions -= 1
return True
else:
return False
else:
return self.repeat_execution() #<---- if repetitions is -1, just call repeat_execution()
# Resets repetitions
def reset_repetitions(self):
self.repetitions = -1
# Returns True for the duration of the timer
def waiting(self):
if self.started and not self.finished():
return True
else:
return False