-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
freq_counter.py
33 lines (28 loc) · 1.08 KB
/
freq_counter.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
import time
from machine import Pin
from micropython import const
import array
from uart_wrapper import DEFAULT_BAUDRATE
NUM_SAMPLES = const(8)
def _freq_counter_irq_falling(fc, pin_obj):
now_us = time.ticks_us()
fc.period_samples[fc.list_idx] = time.ticks_diff(now_us, fc.last_ticks_us)
fc.list_idx = (fc.list_idx + 1) % NUM_SAMPLES
fc.last_ticks_us = now_us
class FreqCounter():
def __init__(self, pin_number):
# super().__init__()
self.pin = Pin(pin_number, Pin.IN)
self.last_ticks_us = 0
self.period_us = int(1000000/DEFAULT_BAUDRATE)
self.period_samples = array.array('L', [self.period_us] * NUM_SAMPLES)
self.freq_hz = int(1/self.period_us)
self.list_idx = 0
self.pin.irq(
trigger=Pin.IRQ_FALLING,
handler=lambda pin_obj: _freq_counter_irq_falling(self, pin_obj)
)
def average_samples(self):
self.period_us = sum(self.period_samples) / NUM_SAMPLES
# print('{} periods {}'.format(self.pin, self.period_samples))
self.freq_hz = 1000000/self.period_us