2021-08-01 18:52:58 -07:00
|
|
|
/**
|
|
|
|
* @file frequency_sensor.cpp
|
|
|
|
*/
|
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include "frequency_sensor.h"
|
|
|
|
|
|
|
|
#include "digital_input_exti.h"
|
|
|
|
|
|
|
|
// Callback adapter since we can't pass a member function to a callback
|
|
|
|
static void freqSensorExtiCallback(void* arg) {
|
|
|
|
auto inst = reinterpret_cast<FrequencySensor*>(arg);
|
|
|
|
inst->onEdge(getTimeNowNt());
|
|
|
|
}
|
|
|
|
|
2021-08-23 21:55:41 -07:00
|
|
|
void FrequencySensor::init(brain_pin_e pin, const char* const msg) {
|
2021-08-20 23:02:05 -07:00
|
|
|
m_pin = pin;
|
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
#if EFI_PROD_CODE
|
|
|
|
// todo: refactor https://github.com/rusefi/rusefi/issues/2123
|
2021-08-23 21:55:41 -07:00
|
|
|
efiExtiEnablePin(msg, pin,
|
2021-08-01 12:58:39 -07:00
|
|
|
PAL_EVENT_MODE_FALLING_EDGE,
|
|
|
|
freqSensorExtiCallback, reinterpret_cast<void*>(this));
|
|
|
|
#endif // EFI_PROD_CODE
|
|
|
|
}
|
|
|
|
|
2021-08-20 23:02:05 -07:00
|
|
|
void FrequencySensor::deInit() {
|
|
|
|
if (!isBrainPinValid(m_pin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
|
|
|
efiExtiDisablePin(m_pin);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_pin = GPIO_UNASSIGNED;
|
|
|
|
}
|
|
|
|
|
2021-08-01 12:58:39 -07:00
|
|
|
void FrequencySensor::onEdge(efitick_t nowNt) {
|
|
|
|
float frequency = 1 / m_edgeTimer.getElapsedSecondsAndReset(nowNt);
|
|
|
|
|
|
|
|
postRawValue(frequency, nowNt);
|
|
|
|
}
|