58 constexpr int8_t lu_dbm_percent[] = {
59 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
60 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
61 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 99, 98, 98, 97, 96,
62 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 79,
63 78, 77, 75, 74, 73, 71, 70, 68, 66, 65, 63, 61, 59, 57, 55, 52,
64 50, 47, 45, 42, 39, 35, 32, 28, 24, 19, 13, 7, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 #define MASK(n) (n >> (sizeof(n) * CHAR_BIT - 1)) 73 constexpr
unsigned c_abs(
int n)
75 return (n + MASK(n)) ^ MASK(n);
79 constexpr int8_t spek_dbm_to_percent(int8_t dbm)
81 return lu_dbm_percent[c_abs(dbm)];
84 static_assert(spek_dbm_to_percent(0) == 100,
"0 dbm should be 100%");
85 static_assert(spek_dbm_to_percent(-42) == 100,
"-42 dbm should be 100%");
86 static_assert(spek_dbm_to_percent(-80) == 50,
"-80 dbm should be 50%");
87 static_assert(spek_dbm_to_percent(-92) == 0,
"-92 dbm should be 0%");
88 static_assert(spek_dbm_to_percent(-128) == 0,
"-128 dbm should be 0%");