Parse MSP messages from the backpack while TX is in mavlink mode (#2883)
[ExpressLRS.git] / src / include / LowPassFilter.h
blob5d767524cafae8d36dc176df1d2fa37565af67a9
1 #pragma once
2 #include "stdint.h"
4 /////////// Super Simple Fixed Point Lowpass ////////////////
6 class LPF
8 public:
9 int32_t SmoothDataINT;
10 int32_t SmoothDataFP;
11 int32_t Beta = 3; // Length = 16
12 int32_t FP_Shift = 5; //Number of fractional bits
13 bool NeedReset = true; // wait for the first data to upcoming.
15 LPF(int Beta_, int FP_Shift_)
17 Beta = Beta_;
18 FP_Shift = FP_Shift_;
21 LPF(int Beta_)
23 Beta = Beta_;
26 LPF()
28 Beta = 3;
29 FP_Shift = 5;
32 int32_t ICACHE_RAM_ATTR update(int32_t Indata)
34 if (NeedReset)
36 init(Indata);
37 return SmoothDataINT;
40 int RawData;
41 RawData = Indata;
42 RawData <<= FP_Shift; // Shift to fixed point
43 SmoothDataFP = (SmoothDataFP << Beta) - SmoothDataFP;
44 SmoothDataFP += RawData;
45 SmoothDataFP >>= Beta;
46 // Don't do the following shift if you want to do further
47 // calculations in fixed-point using SmoothData
48 SmoothDataINT = SmoothDataFP >> FP_Shift;
49 return SmoothDataINT;
52 void ICACHE_RAM_ATTR reset()
54 NeedReset = true;
58 void ICACHE_RAM_ATTR init(int32_t Indata)
60 NeedReset = false;
62 SmoothDataINT = Indata;
63 SmoothDataFP = SmoothDataINT << FP_Shift;
66 int32_t value() const { return SmoothDataINT; }