Remove the resetting of the TelemetryRcvPhase flag
[ExpressLRS.git] / src / lib / LQCALC / LQCALC.h
blobc859ff31f96d4adb7c854fcd72e839b7b687a957
1 #pragma once
3 #include <stdint.h>
5 template <uint8_t N>
6 class LQCALC
8 public:
9 LQCALC(void)
11 reset100();
14 /* Set the bit for the current period to true and update the running LQ */
15 void add()
17 if (currentIsSet())
18 return;
19 LQArray[index] |= LQmask;
20 LQ += 1;
23 /* Start a new period */
24 void ICACHE_RAM_ATTR inc()
26 // Increment the counter by shifting one bit higher
27 // If we've shifted out all the bits, move to next idx
28 LQmask = LQmask << 1;
29 if (LQmask == 0)
31 LQmask = (1 << 0);
32 index += 1;
35 // At idx N / 32 and bit N % 32, wrap back to idx=0, bit=0
36 if ((index == (N / 32)) && (LQmask & (1 << (N % 32))))
38 index = 0;
39 LQmask = (1 << 0);
42 if ((LQArray[index] & LQmask) != 0)
44 LQArray[index] &= ~LQmask;
45 LQ -= 1;
48 if (count < N)
49 ++count;
52 /* Return the current running total of bits set, in percent */
53 uint8_t ICACHE_RAM_ATTR getLQ() const
55 return (uint32_t)LQ * 100U / count;
58 /* Return the current running total of bits set, up to N */
59 uint8_t getLQRaw() const
61 return LQ;
64 /* Return the number of periods recorded so far, up to N */
65 uint8_t getCount() const
67 return count;
70 /* Return N, the size of the LQ history */
71 uint8_t getSize() const
73 return N;
76 /* Initialize and zero the history */
77 void reset()
79 // count is intentonally not zeroed here to start LQ counting up from 0
80 // after a failsafe, instead of down from 100. Use reset100() to start from 100
81 LQ = 0;
82 index = 0;
83 LQmask = (1 << 0);
84 for (uint8_t i = 0; i < (sizeof(LQArray)/sizeof(LQArray[0])); i++)
85 LQArray[i] = 0;
88 /* Reset and start at 100% */
89 void reset100()
91 reset();
92 count = 1;
95 /* Return true if the current period was add()ed */
96 bool ICACHE_RAM_ATTR currentIsSet() const
98 return LQArray[index] & LQmask;
101 private:
102 uint8_t LQ;
103 uint8_t index; // current position in LQArray
104 uint8_t count;
105 uint32_t LQmask;
106 uint32_t LQArray[(N + 31)/32];