mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-08-07 09:22:46 +02:00
Refactor RSSI Scan
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#ifndef LORASA_CORE_CPP
|
||||
#define LORASA_CORE_CPP
|
||||
|
||||
#include "scan.h"
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <stdlib.h>
|
||||
|
||||
float Scan::getRSSI() { return 0.1; }
|
||||
|
||||
uint16_t Scan::rssiMethod(uint16_t *result)
|
||||
{
|
||||
memset(result, 0, res_size * sizeof(uint16_t));
|
||||
int result_index = 0;
|
||||
|
||||
//
|
||||
uint16_t max_signal = 65535;
|
||||
// N of samples
|
||||
for (int r = 0; r < SAMPLES_RSSI; r++)
|
||||
{
|
||||
float rssi = getRSSI();
|
||||
if (rssi < -65535)
|
||||
rssi = -65535;
|
||||
|
||||
uint16_t abs_rssi = abs(rssi);
|
||||
if (abs_rssi < max_signal)
|
||||
{
|
||||
max_signal = abs_rssi;
|
||||
}
|
||||
// ToDO: check if 4 is correct value for 33 power bins
|
||||
// Now we have more space because we are ignoring low dB values
|
||||
// we can / 3 default 4
|
||||
if (RSSI_OUTPUT_FORMULA == 1)
|
||||
{
|
||||
result_index =
|
||||
/// still not clear formula but it works
|
||||
uint8_t(abs(rssi) / 4);
|
||||
}
|
||||
else if (RSSI_OUTPUT_FORMULA == 2)
|
||||
{
|
||||
if (rssi > HI_RSSI_THRESHOLD)
|
||||
{
|
||||
rssi = HI_RSSI_THRESHOLD;
|
||||
}
|
||||
else if (rssi < LO_RSSI_THRESHOLD)
|
||||
{
|
||||
rssi = LO_RSSI_THRESHOLD;
|
||||
}
|
||||
|
||||
result_index = uint8_t((HI_RSSI_THRESHOLD - rssi) * scale);
|
||||
}
|
||||
|
||||
if (result_index >= res_size)
|
||||
{
|
||||
// Maximum index possible
|
||||
result_index = res_size - 1;
|
||||
}
|
||||
|
||||
LOG("RSSI: %f IDX: %d\n", rssi, result_index);
|
||||
if (result[result_index] == 0 || result[result_index] > abs_rssi)
|
||||
{
|
||||
result[result_index] = abs_rssi;
|
||||
}
|
||||
}
|
||||
|
||||
return max_signal;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <cstdint>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef LORASA_CORE_H
|
||||
|
||||
#define LORASA_CORE_H
|
||||
|
||||
#ifdef PRINT_DEBUG
|
||||
#define LOG(args...) Serial.printf(args...)
|
||||
#define LOG_IF(cond, args...) \
|
||||
if (cond) \
|
||||
LOG(args...)
|
||||
#elif !defined(LOG)
|
||||
#define LOG(args...)
|
||||
#define LOG_IF(cond, args...)
|
||||
#endif
|
||||
|
||||
// Output Pixel Formula
|
||||
// 1 = rssi / 4, 2 = (rssi / 2) - 22 or 20
|
||||
constexpr int RSSI_OUTPUT_FORMULA = 2;
|
||||
|
||||
// based on the formula for RSSI_OUTPUT_FORMULA == 2
|
||||
// -2 * (22 + RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE) < rssi =< -44
|
||||
// practice may require a better pair of thresholds
|
||||
constexpr float HI_RSSI_THRESHOLD = -44.0;
|
||||
constexpr float LO_RSSI_THRESHOLD = HI_RSSI_THRESHOLD - 66;
|
||||
|
||||
// number of samples for RSSI method
|
||||
#define SAMPLES_RSSI 12 // 21 //
|
||||
|
||||
struct Scan
|
||||
{
|
||||
Scan(int sz)
|
||||
: res_size(sz), scale((float)sz / (HI_RSSI_THRESHOLD - LO_RSSI_THRESHOLD + 0.1))
|
||||
{
|
||||
}
|
||||
|
||||
virtual float getRSSI();
|
||||
|
||||
uint16_t rssiMethod(uint16_t *result);
|
||||
|
||||
int res_size;
|
||||
float scale;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user