minor cleanup

This commit is contained in:
Matthew Kennedy 2022-12-01 22:55:55 -08:00
parent 4223319657
commit 0870981763
1 changed files with 19 additions and 8 deletions

View File

@ -62,10 +62,15 @@ static float AverageSamples(adcsample_t* buffer, size_t idx)
static float GetMaxSample(adcsample_t* buffer, size_t idx) static float GetMaxSample(adcsample_t* buffer, size_t idx)
{ {
uint32_t max = 0; adcsample_t max = 0;
for (size_t i = 0; i < ADC_OVERSAMPLE; i++) {
for (size_t i = 0; i < ADC_OVERSAMPLE; i++)
{
if (buffer[idx] > max) if (buffer[idx] > max)
{
max = buffer[idx]; max = buffer[idx];
}
idx += ADC_CHANNEL_COUNT; idx += ADC_CHANNEL_COUNT;
} }
@ -74,7 +79,8 @@ static float GetMaxSample(adcsample_t* buffer, size_t idx)
return (float)max * scale; return (float)max * scale;
} }
static float l_vbatt = 0.0, r_vbatt = 0.0; static float l_vbatt = 0;
static float r_vbatt = 0;
AnalogResult AnalogSample() AnalogResult AnalogSample()
{ {
@ -84,11 +90,16 @@ AnalogResult AnalogSample()
adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE); adcConvert(&ADCD1, &convGroup, adcBuffer, ADC_OVERSAMPLE);
if ((l_heater) && (!palReadPad(L_HEATER_PORT, L_HEATER_PIN))) if ((l_heater) && (!palReadPad(L_HEATER_PORT, L_HEATER_PIN)))
l_vbatt = BATTERY_FILTER_ALPHA * GetMaxSample(adcBuffer, 6) / BATTERY_INPUT_DIVIDER {
+ (1.0 - BATTERY_FILTER_ALPHA) * l_vbatt; float vbatt_raw = GetMaxSample(adcBuffer, 6) / BATTERY_INPUT_DIVIDER;
l_vbatt = BATTERY_FILTER_ALPHA * vbatt_raw + (1.0 - BATTERY_FILTER_ALPHA) * l_vbatt;
}
if ((r_heater) && (!palReadPad(R_HEATER_PORT, R_HEATER_PIN))) if ((r_heater) && (!palReadPad(R_HEATER_PORT, R_HEATER_PIN)))
r_vbatt = BATTERY_FILTER_ALPHA * GetMaxSample(adcBuffer, 7) / BATTERY_INPUT_DIVIDER {
+ (1.0 - BATTERY_FILTER_ALPHA) * r_vbatt; float vbatt_raw = GetMaxSample(adcBuffer, 7) / BATTERY_INPUT_DIVIDER;
r_vbatt = BATTERY_FILTER_ALPHA * vbatt_raw + (1.0 - BATTERY_FILTER_ALPHA) * r_vbatt;
}
return return
{ {