@@ -210,12 +210,14 @@ class Counter(DigitalBase):
210
210
ONE_EDGE = 1
211
211
BOTH_EDGES = 2
212
212
213
- def __init__ (self , pin_name , stable_read_count = 4 , edges = ONE_EDGE , reset_on_read = False , ** kwargs ):
213
+ def __init__ (self , pin_name , stable_read_count = 4 , edges = ONE_EDGE , reset_on_read = False ,
214
+ rollover = 2 ** 32 , ** kwargs ):
214
215
'''Arguments not in the inheritance chain:
215
216
edges: Either ONE_EDGE or BOTH_EDGES indicating which transitions of the pulse to
216
217
count.
217
218
reset_on_read: if True, the counter value will be reset to 0 after the count
218
219
is read via a call to the value() method.
220
+ rollover: count will roll to 0 when it hits this value.
219
221
Note that the default 'stable_read_count' value is set to 4 reads. With the
220
222
default 2.1 ms read interval, this requires stability for 8.4 ms. Counters are
221
223
often reed switches or electronic pulse trains, both of which have little to no
@@ -225,10 +227,15 @@ def __init__(self, pin_name, stable_read_count=4, edges=ONE_EDGE, reset_on_read=
225
227
DigitalBase .__init__ (self , pin_name , stable_read_count = stable_read_count , ** kwargs )
226
228
self .edges = edges
227
229
self .reset_on_read = reset_on_read
228
- self ._low_to_high_count = 0
229
- self ._high_to_low_count = 0
230
+ self ._rollover = rollover
231
+ self ._count = 0
230
232
self ._new_val = None # need to allocate memory here, not in interrupt routine
231
233
234
+ def incr_counter (self ):
235
+ '''Increments the count accounting for rollover.
236
+ '''
237
+ self ._count = (self ._count + 1 ) % self ._rollover
238
+
232
239
def service_input (self ):
233
240
# shift the prior readings over one bit, and put the new reading
234
241
# in the LSb position.
@@ -241,29 +248,28 @@ def service_input(self):
241
248
if self ._new_val == self ._cur_val :
242
249
# no change in value
243
250
return
244
- if self ._new_val > self ._cur_val :
245
- # transition from low to high occurred.
246
- self ._low_to_high_count += 1
247
- else :
248
- # transition from high to low occurred.
249
- self ._high_to_low_count += 1
251
+ if self ._new_val < self ._cur_val :
252
+ # transition from high to low occurred. Always count these
253
+ # transitions.
254
+ self .incr_counter ()
255
+ elif self .edges == Counter .BOTH_EDGES :
256
+ # A low to high transition occurred and counting both edges
257
+ # was requested, so increment counter.
258
+ self .incr_counter ()
250
259
self ._cur_val = self ._new_val
251
260
252
261
def _compute_value (self ):
253
- if self .edges == Counter .ONE_EDGE :
254
- ct = self ._high_to_low_count
255
- else :
256
- ct = self ._high_to_low_count + self ._low_to_high_count
262
+ ct = self ._count
257
263
if self .reset_on_read :
258
- self ._high_to_low_count = self . _low_to_high_count = 0
264
+ self ._count = 0
259
265
return ct
260
266
261
267
def reset_count (self ):
262
268
'''Resets the counts and protects the processs form being
263
269
interrupted.
264
270
'''
265
271
irq_state = pyb .disable_irq ()
266
- self ._high_to_low_count = self . _low_to_high_count = 0
272
+ self ._count = 0
267
273
pyb .enable_irq (irq_state )
268
274
269
275
0 commit comments