-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtacho.cpp
37 lines (32 loc) · 1.05 KB
/
tacho.cpp
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
#include "tacho.h"
static gpio_num_t _pin;
static int _pulsesPerRev;
static unsigned long _pulseIntervalMax;
static volatile unsigned long _pulseTime;
static volatile unsigned long _pulseInterval;
static void IRAM_ATTR tachoISR();
void tachoInit(int pin, int pulsesPerRev) {
_pin = (gpio_num_t)pin;
_pulsesPerRev = pulsesPerRev;
_pulseTime = 0;
_pulseInterval = 0;
_pulseIntervalMax = 3E6 / _pulsesPerRev; // 20 RPM
gpio_pad_select_gpio(_pin);
gpio_set_direction(_pin, GPIO_MODE_INPUT);
gpio_pullup_en(_pin);
attachInterrupt(_pin, tachoISR, RISING);
}
float tachoGetRPM(void) {
if ((micros() - _pulseTime) > _pulseIntervalMax) return 0.;
if (_pulseInterval == 0) return 0.;
return 60.E6 / (_pulseInterval * _pulsesPerRev);
}
void IRAM_ATTR tachoISR() {
unsigned long now = micros();
unsigned long interval = now - _pulseTime;
/* debounce; intervals less than 1ms are not believeable */
if (interval > 1000) {
_pulseInterval = (15 * _pulseInterval + interval) / 16;
_pulseTime = now;
}
}