2020-10-31 21:25:49 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include <main.h>
|
|
|
|
#include <adc.h>
|
|
|
|
|
|
|
|
#include <frontend.h>
|
|
|
|
#include <logger.h>
|
2020-11-03 10:05:45 +01:00
|
|
|
#include <show.h>
|
2020-11-02 14:48:13 +01:00
|
|
|
|
2020-10-31 21:25:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-01 22:15:04 +01:00
|
|
|
static volatile int32_t frontendAdcThreshold = 0;
|
2020-10-31 21:25:49 +01:00
|
|
|
static volatile bool frontendEnabled = false;
|
|
|
|
|
|
|
|
|
|
|
|
void frontendInit() {
|
|
|
|
HAL_ADCEx_Calibration_Start(&frontendAdc);
|
|
|
|
logMsg("frontendInit, calibration done");
|
|
|
|
HAL_ADC_Start_IT(&frontendAdc);
|
|
|
|
logMsg("frontendInit, adc started");
|
|
|
|
}
|
|
|
|
|
2020-11-01 22:15:04 +01:00
|
|
|
void frontendSetThreshold(int32_t threshold) {
|
2020-10-31 21:25:49 +01:00
|
|
|
frontendAdcThreshold = threshold;
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendEnable() {
|
|
|
|
frontendEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendDisable() {
|
|
|
|
frontendEnabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void frontendAdcCallback(ADC_HandleTypeDef* hadc) {
|
2020-11-01 18:48:39 +01:00
|
|
|
static int32_t holdValue = 0;
|
|
|
|
|
2020-11-22 21:23:52 +01:00
|
|
|
// show(DEBUG_2, TOGGLE);
|
2020-11-01 18:48:39 +01:00
|
|
|
|
|
|
|
if (frontendEnabled) {
|
|
|
|
int32_t currentValue = (int32_t) HAL_ADC_GetValue(hadc);
|
|
|
|
|
|
|
|
if (holdValue == 0) {
|
|
|
|
holdValue = currentValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentValue - holdValue > frontendAdcThreshold) {
|
|
|
|
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_RESET);
|
2020-11-01 18:57:25 +01:00
|
|
|
} else {
|
|
|
|
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_SET);
|
2020-11-01 18:48:39 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (holdValue != 0) {
|
|
|
|
holdValue = 0;
|
|
|
|
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_SET);
|
|
|
|
}
|
2020-11-01 13:22:57 +01:00
|
|
|
}
|
2020-10-31 21:25:49 +01:00
|
|
|
|
|
|
|
}
|