Skip to content
Snippets Groups Projects
Commit fc16dce8 authored by Kurt Kiefer's avatar Kurt Kiefer Committed by Lorenz Meier
Browse files

spektrum_rssi: initialize rssi lookup as constexpr

Rather than initializing the rssi percentage lookup table at runtime
on the heap, we would like it to be stored in flash.

This change pre-computes the rssi lookup table.
parent 597372be
No related branches found
No related tags found
No related merge requests found
......@@ -79,8 +79,6 @@ static unsigned dsm_channel_shift = 0; /**< Channel resolution, 0=unknown, 1=1
static unsigned dsm_frame_drops = 0; /**< Count of incomplete DSM frames */
static uint16_t dsm_chan_count = 0; /**< DSM channel count */
static const SpektrumRssi spektrum_rssi;
static bool
dsm_decode(hrt_abstime frame_time, uint16_t *values, uint16_t *num_values, bool *dsm_11_bit, unsigned max_values,
int8_t *rssi_percent);
......@@ -429,7 +427,7 @@ dsm_decode(hrt_abstime frame_time, uint16_t *values, uint16_t *num_values, bool
*rssi_percent = 0;
} else {
*rssi_percent = spektrum_rssi.dbm_to_percent(dbm);
*rssi_percent = spek_dbm_to_percent(dbm);
}
} else {
......
......@@ -41,38 +41,48 @@
#pragma once
#include <math.h>
#include <climits>
#define MIN_RSSI_DBM (-92.0f)
#define MAX_RSSI_DBM (-42.0f)
/*
min_rssi_dbm = -92.0
max_rssi_dbm = -42.0
class SpektrumRssi
{
int8_t lu_dbm_percent[129];
public:
SpektrumRssi()
{
for (int i = 0; i <= 128; i++) {
float rssi_dbm = -1.0f * (float)i;
float percent;
if (rssi_dbm > MAX_RSSI_DBM) {
percent = 100.0f;
def dbm_to_percent(abs_dbm):
if -abs_dbm < min_rssi_dbm:
return 0.0
elif -abs_dbm > max_rssi_dbm:
return 100.0
else:
return 100.0 * math.log10(1.0 + (float)(-abs_dbm - min_rssi_dbm) * (9.0 / (max_rssi_dbm - min_rssi_dbm)))
*/
constexpr int8_t lu_dbm_percent[] = {
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 99, 98, 98, 97, 96,
95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 79,
78, 77, 75, 74, 73, 71, 70, 68, 66, 65, 63, 61, 59, 57, 55, 52,
50, 47, 45, 42, 39, 35, 32, 28, 24, 19, 13, 7, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
};
} else if (rssi_dbm < MIN_RSSI_DBM) {
percent = 0.0f;
#define MASK(n) (n >> (sizeof(n) * CHAR_BIT - 1))
} else {
percent = 100.0f * log10f(1 + (rssi_dbm - MIN_RSSI_DBM) * (9.0f / (MAX_RSSI_DBM - MIN_RSSI_DBM)));
}
/* constexpr-compatible version of abs() */
constexpr unsigned c_abs(int n)
{
return (n + MASK(n)) ^ MASK(n);
}
lu_dbm_percent[i] = (int8_t)roundf(percent);
}
}
/* convert signed spektrum dbm (-92 to -42) to percentage */
constexpr int8_t spek_dbm_to_percent(int8_t dbm)
{
return lu_dbm_percent[c_abs(dbm)];
}
int8_t dbm_to_percent(int8_t dbm) const
{
return lu_dbm_percent[abs(dbm)];
}
};
static_assert(spek_dbm_to_percent(0) == 100, "0 dbm should be 100%");
static_assert(spek_dbm_to_percent(-42) == 100, "-42 dbm should be 100%");
static_assert(spek_dbm_to_percent(-80) == 50, "-80 dbm should be 50%");
static_assert(spek_dbm_to_percent(-92) == 0, "-92 dbm should be 0%");
static_assert(spek_dbm_to_percent(-128) == 0, "-128 dbm should be 0%");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment