change wss to moving sum

This commit is contained in:
Leonard Gies 2025-06-20 00:04:48 +02:00
parent 2afdfa8293
commit 75950a226d
Signed by: l.gies
GPG Key ID: 6F6FB9338EE44F71

View File

@ -40,6 +40,7 @@
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define TIM_BASE_FREQ 96000000UL
#define WSS_HISTORY_SIZE 10
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
@ -66,6 +67,9 @@ TIM_HandleTypeDef* PWM_TIM_MAP[3] = {&htim1, &htim4, &htim3};
static uint8_t pwm_ch_active[8];
static uint16_t wss_flanks[2];
static uint16_t wss_flanks_avg[2];
static uint8_t wss_flanks_history[2][WSS_HISTORY_SIZE];
static uint8_t wss_flanks_history_idx[2]; // index of the oldest entry (next to be overwritten)
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
@ -119,7 +123,11 @@ void loop_1kHz() {
break;
case FIN:
value = wss_flanks[signal->channel];
uint8_t oldest_hist_idx = wss_flanks_history_idx[signal->channel];
// subtract oldest entry from avg
wss_flanks_avg[signal->channel] -= wss_flanks_history[signal->channel][oldest_hist_idx];
// overwrite oldest history entry with new value and reset counter
wss_flanks_history[signal->channel][oldest_hist_idx] = wss_flanks[signal->channel];
wss_flanks[signal->channel] = 0;
break;
@ -247,6 +255,10 @@ int main(void)
mscounter = 0;
setup_complete = 1;
memset(wss_flanks, 0, sizeof(wss_flanks));
memset(wss_flanks_avg, 0, sizeof(wss_flanks));
memset(wss_flanks_history, 0, sizeof(wss_flanks_history));
memset(wss_flanks_history_idx, 0, sizeof(wss_flanks_history_idx));
/* USER CODE END 2 */
/* Infinite loop */