/*
 * toothedwheel.h
 *
 *  Created on: Aug 4, 2025
 *      Author: herli
 */

#ifndef INC_TOOTHED_WHEEL_H_
#define INC_TOOTHED_WHEEL_H_

#include <id.h>
#include "stdint.h"

extern volatile uint32_t IC_RPM_Val1;
extern volatile uint32_t IC_RPM_Val2;
extern volatile uint32_t IC_EOI;
extern volatile uint8_t currentTooth;
extern volatile uint32_t IC_INJ;
extern float refClock;
extern float toothAlpha;

extern volatile float TEETH_RPM;
extern volatile float MT_RPM;
extern volatile float last_MT_RPM;

extern volatile float RPM;

extern uint8_t blankInj;
extern volatile uint8_t TW_RPM_SENSOR_STATE;

/*typedef struct {
    uint32_t ic;      // TIM2->CNT capture
    float    rpm;     // instantaneous rpm estimated for this edge's interval
} EdgeSample;
extern volatile EdgeSample edgeBuf[TW_PERCYL_TEETH+1];*/

extern float correction_beta;
extern float correction_eoi;
extern float nominal_boi_angle;

extern void TW_RESET_SENSOR();
extern void TW_EVAL_MAX_TEETH();
extern void TW_DEFER_BOI_EVAL(uint32_t ic);
extern void TW_DEFER_EOI_EVAL(uint32_t ic);
extern void TW_Service();

float fclamp(float value, float min, float max);

#endif /* INC_TOOTHED_WHEEL_H_ */
