Files
velometer/ss49e.c
T

39 lines
1.5 KiB
C

#include "ss49e.h"
void ss49e_init(SS49E_State *self, enum MagnetPolarityType magnetPolarityType, ss49e_tick_callback_t tickCallback) {
self->workingPhase = SS49E_WORKING_PHASE_NORMAL;
self->tickCallback = tickCallback;
self->magnetPolType = magnetPolarityType;
const float level0 = 2445.0f / 4095.0f;
const float deviation = 380.0f / 4095.0f;
if (magnetPolarityType == MAGNET_POLTYPE_BIPOLAR) {
// self->threshold01 = level0 + deviation * 0.75;
// self->threshold10 = level0 - deviation * 0.75;
self->threshold01 = (2447.0 + 520.0 * 2.0 / 3.0) / 4095.0;
self->threshold10 = (2447.0 - 800.0 * 2.0 / 3.0) / 4095.0;
} else {
float activeLevel = level0 + (magnetPolarityType == MAGNET_POLTYPE_UNIPOLAR_POSITIVE ? deviation : -deviation);
self->threshold01 = level0 * 1 / 3 + activeLevel * 2 / 3;
self->threshold10 = activeLevel * 1 / 3 + level0 * 2 / 3;
}
self->currentLevel = 0;
}
void ss49e_process_sample(SS49E_State *self, uint16_t sample) {
float normalized = (float)sample / 4095.0f;
if (self->currentLevel == 0) {
if (normalized >= self->threshold01) {
self->currentLevel = 1;
if (self->magnetPolType == MAGNET_POLTYPE_BIPOLAR || self->magnetPolType == MAGNET_POLTYPE_UNIPOLAR_POSITIVE)
self->tickCallback();
}
} else {
if (normalized <= self->threshold10) {
self->currentLevel = 0;
if (self->magnetPolType == MAGNET_POLTYPE_BIPOLAR || self->magnetPolType == MAGNET_POLTYPE_UNIPOLAR_NEGATIVE)
self->tickCallback();
}
}
}