diff --git a/lib/scan/scan.cpp b/lib/scan/scan.cpp index bf8b416..c1f777f 100644 --- a/lib/scan/scan.cpp +++ b/lib/scan/scan.cpp @@ -6,17 +6,17 @@ #include #include -float Scan::getRSSI() { return 0.1; } - -uint16_t Scan::rssiMethod(uint16_t *result) +uint16_t Scan::rssiMethod(size_t samples, uint16_t *result, size_t res_size) { + float scale((float)res_size / (HI_RSSI_THRESHOLD - LO_RSSI_THRESHOLD + 0.1)); + 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++) + for (int r = 0; r < samples; r++) { float rssi = getRSSI(); if (rssi < -65535) diff --git a/lib/scan/scan.h b/lib/scan/scan.h index 36f449c..1c8ca8e 100644 --- a/lib/scan/scan.h +++ b/lib/scan/scan.h @@ -30,17 +30,11 @@ constexpr float LO_RSSI_THRESHOLD = HI_RSSI_THRESHOLD - 66; struct Scan { - Scan(int sz) - : res_size(sz), scale((float)sz / (HI_RSSI_THRESHOLD - LO_RSSI_THRESHOLD + 0.1)) - { - } + virtual float getRSSI() = 0; - virtual float getRSSI(); - - uint16_t rssiMethod(uint16_t *result); - - int res_size; - float scale; + // rssiMethod gets the data similar to the scan method, + // but uses getRSSI directly. + uint16_t rssiMethod(size_t samples, uint16_t *result, size_t res_size); }; #endif diff --git a/src/main.cpp b/src/main.cpp index 47a844f..6dcb633 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -706,8 +706,6 @@ void check_ranges() struct RadioScan : Scan { - RadioScan() : Scan(RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE) {} - float getRSSI() override; }; @@ -882,7 +880,8 @@ void loop(void) // Spectrum analyzer using getRSSI { LOG("METHOD RSSI"); - uint16_t max_rssi = r.rssiMethod(result); + uint16_t max_rssi = r.rssiMethod(SAMPLES_RSSI, result, + RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE); if (max_x_rssi[display_x] > max_rssi) { max_x_rssi[display_x] = max_rssi; diff --git a/test/test_rssi.cpp b/test/test_rssi.cpp index 9c4b023..b6ed9f3 100644 --- a/test/test_rssi.cpp +++ b/test/test_rssi.cpp @@ -9,7 +9,7 @@ void tearDown(void) {} struct TestScan : Scan { - TestScan(float *ctx, int sz) : Scan(sz), ctx(ctx), sz(sz), idx(0) {} + TestScan(float *ctx, int sz) : ctx(ctx), sz(sz), idx(0) {} float getRSSI() override; @@ -29,18 +29,19 @@ float TestScan::getRSSI() } constexpr int test_sz = 13; +constexpr int inputs_sz = 13; void test_rssi(void) { uint16_t samples[test_sz]; - float inputs[] = {-40.0, -100.0, -200.0, -50.0, -400.0, -60.0, -20, - -75.5, -70, -80, -90, -55.9, -110}; + float inputs[inputs_sz] = {-40.0, -100.0, -200.0, -50.0, -400.0, -60.0, -20, + -75.5, -70, -80, -90, -55.9, -110}; - TestScan t = TestScan(inputs, test_sz); + TestScan t = TestScan(inputs, inputs_sz); - uint16_t r = t.rssiMethod(samples); + uint16_t r = t.rssiMethod(inputs_sz, samples, test_sz); - uint16_t expect[test_sz] = {20, 50, 55, 60, 0, 70, 75, 80, 0, 90, 0, 100, 200}; + uint16_t expect[test_sz] = {20, 50, 55, 60, 0, 70, 75, 80, 0, 90, 0, 100, 110}; TEST_ASSERT_EQUAL_INT16(20, r); TEST_ASSERT_EQUAL_INT16_ARRAY(expect, samples, test_sz);