From 51d1d0545889ef3e4fd2d1331b506cf1961b4923 Mon Sep 17 00:00:00 2001 From: "ionsurdu@github.com" Date: Wed, 7 Aug 2024 21:06:21 +0300 Subject: [PATCH] WIP ... minor changes --- README.md | 2 +- include/ui.h | 5 ++- src/main.cpp | 89 ++++++++++++++++++++++++---------------------------- src/ui.cpp | 66 +++++++++++++++++++++++++------------- 4 files changed, 88 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 767c204..d0a843e 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ to disable - uncomment this line #define WATERFALL_ENABLED true ``` -Waterfall shows the last **N** = SCREEN_HEIGHT (64) - WATERFALL_START(37) - 8 (part of the STATUS_TEXT_TOP) = **19** signal detection that excited set signal level +Waterfall shows the last **N** = SCREEN_HEIGHT (64) - WATERFALL_START(37) - 8 (part of the ROW_STATUS_TEXT) = **19** signal detection that excited set signal level ### RSSI Method of scan diff --git a/include/ui.h b/include/ui.h index 5fb9ca7..fe7bff6 100644 --- a/include/ui.h +++ b/include/ui.h @@ -15,16 +15,15 @@ #define ONE_MILLISEC 1 // Prints debug information and the scan measurement bins from the SX1262 in hex -// #define PRINT_DEBUG // Change spectrum plot values at once or by line -#define ANIMATED_RELOAD true +#define ANIMATED_RELOAD false #define MAJOR_TICK_LENGTH 2 #define MINOR_TICK_LENGTH 1 // WEIGHT of the x-axis line #define X_AXIS_WEIGHT 1 -#define STATUS_TEXT_TOP (64 - 10) +#define ROW_STATUS_TEXT (64 - 10) // The number of the spectrum screen lines = width of screen // Resolution of the scan is limited by 128-pixel screen diff --git a/src/main.cpp b/src/main.cpp index 870eea5..0a935dc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,8 +44,8 @@ typedef enum #define SCAN_METHOD METHOD_SPECTRAL // Feature to scan diapazones. Other frequency settings will be ignored. -int SCAN_RANGES[] = {}; // int SCAN_RANGES[] = {850890, 920950}; +int SCAN_RANGES[] = {}; // MHZ per page // to put everething into one page set RANGE_PER_PAGE = FREQ_END - 800 @@ -116,7 +116,7 @@ uint64_t scan_time = 0; uint64_t scan_start_time = 0; #endif -uint64_t x, y, scan_iteration, w = 0; +uint64_t x, y, range_item, w = 0; uint64_t ranges_count = 0; float freq = 0; @@ -124,12 +124,15 @@ int rssi = 0; int state = 0; int result_index = 0; -uint64_t button_pressed_counter = 0; +uint8_t button_pressed_counter = 0; + +uint64_t loop_cnt = 0; void setup(void) { float vbat; float resolution; + loop_cnt = 0; pinMode(LED, OUTPUT); pinMode(BUZZER_PIN, OUTPUT); @@ -177,19 +180,17 @@ void setup(void) display.clear(); resolution = RANGE / STEPS; - if (RANGE_PER_PAGE == range) - { - single_page_scan = true; - } - else - { - single_page_scan = false; - } + + single_page_scan = (RANGE_PER_PAGE == range); + +#ifdef DISABLED_CODE // Adjust range if it is not even to RANGE_PER_PAGE if (!single_page_scan && range % RANGE_PER_PAGE != 0) { // range = range + range % RANGE_PER_PAGE; } +#endif + if (single_page_scan) { both.println("Single Page Screen MODE"); @@ -254,6 +255,9 @@ void loop(void) //reset scan time scan_time = 0; + // general purpose loop conter + loop_cnt++; + #ifdef PRINT_PROFILE_TIME loop_start = millis(); #endif @@ -307,18 +311,18 @@ void loop(void) } // Iterating by small ranges by 50 Mhz each pixel is 0.4 Mhz - for (scan_iteration = 0; scan_iteration < iterations; scan_iteration++) + for (range_item = 0; range_item < iterations; range_item++) { range = RANGE_PER_PAGE; if (ranges_count == 0) { - fr_begin = (scan_iteration == 0) ? fr_begin : fr_begin += range; + fr_begin = (range_item == 0) ? fr_begin : fr_begin += range; fr_end = fr_begin + RANGE_PER_PAGE; } else { - fr_begin = SCAN_RANGES[scan_iteration] / 1000; - fr_end = SCAN_RANGES[scan_iteration] % 1000; + fr_begin = SCAN_RANGES[range_item] / 1000; + fr_end = SCAN_RANGES[range_item] % 1000; range = fr_end - fr_begin; } @@ -347,30 +351,11 @@ void loop(void) scan_start_time = millis(); #endif - waterfall[scan_iteration][x][w] = false; + waterfall[range_item][x][w] = false; freq = fr_begin + (range * ((float)x / STEPS)); - radio.setFrequency(freq,false); + radio.setFrequency(freq,false); // false = no calibration need here - // TODO: RSSI METHOD - // Gets RSSI (Recorded Signal Strength Indicator) - // Restart continuous receive mode on the new frequency - // state = radio.startReceive(); - // if (state == RADIOLIB_ERR_NONE) { - // Serial.println(F("Started continuous receive mode")); - //} else { - // Serial.print(F("Failed to start receive mode, error code: ")); - // Serial.println(state); - //} - // rssi = radio.getRSSI(false); - // Serial.println(String(rssi) + "db"); - // delay(25); - // This code will iterate over the specified frequencies, - // changing the frequency every - // second and printing the RSSI value for each frequency to the serial monitor. Adjust the frequencies array - // to include the specific frequencies you're interested in monitoring. - // A short delay after changing the frequency - // ensures the module has time to stabilize and get an accurate RSSI reading. #ifdef PRINT_DEBUG Serial.printf("Step:%d\n",x); // Serial.printf("Step:%d Freq: %f\n",x,freq); @@ -410,10 +395,8 @@ void loop(void) Serial.println(state); } - for (int r = 1; r < RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE; r++) - { - result[r] = 0; // ????? - } + // memset + memset(result,0,RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE); result_index = 0; // N of samples for (int r = 1; r < SAMPLES_RSSI; r++) @@ -421,7 +404,7 @@ void loop(void) rssi = radio.getRSSI(false); // delay(ONE_MILLISEC); // ToDO: check if 4 is correct value for 33 power bins - result_index = (abs(rssi) / 4); /// WTF ??? + result_index = (abs(rssi) / 4); /// still not clear formula #ifdef PRINT_DEBUG Serial.printf("Freq: %.2f RSSI: %d \n",freq,rssi); @@ -479,7 +462,7 @@ void loop(void) if (single_page_scan) { // Drone detection true for waterfall - waterfall[scan_iteration][x][w] = true; + waterfall[range_item][x][w] = true; display.setColor(WHITE); display.setPixel(x, w); } @@ -520,17 +503,20 @@ void loop(void) // display.setPixel(x, 2); // display.setPixel(x, 3); // display.setPixel(x, 4); + + // draw vertical line on top of display for "drone detected" frequencies + display.drawLine(x , 1, x, 6 ); } #if ( WATERFALL_ENABLED == true ) if ((filtered_result[y] == 1) && ( y > drone_detection_level) && ( single_page_scan ) - && ( waterfall[scan_iteration][x][w] != true) ) + && ( waterfall[range_item][x][w] != true) ) { // If drone not found set dark pixel on the waterfall // TODO: make something like scrolling up if possible - waterfall[scan_iteration][x][w] = false; + waterfall[range_item][x][w] = false; display.setColor(BLACK); display.setPixel(x, w); display.setColor(WHITE); @@ -635,7 +621,7 @@ void loop(void) heltec_loop(); } w++; - if (w > STATUS_TEXT_TOP + 1) + if (w > ROW_STATUS_TEXT + 1) { w = WATERFALL_START; } @@ -648,15 +634,22 @@ void loop(void) display.setColor(WHITE); } #endif - display.display(); + + // Render display data here + display.display(); } + #ifdef PRINT_DEBUG Serial.println("----"); #endif -// display.display(); -#ifdef PRINT_PROFILE_TIME + loop_time = millis() - loop_start; + + +#ifdef PRINT_PROFILE_TIME +#ifdef PRINT_DEBUG Serial.printf("LOOP: %lld ms; SCAN: %lld ms;\n ", loop_time,scan_time); #endif +#endif } diff --git a/src/ui.cpp b/src/ui.cpp index 7b1fd81..6359786 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -34,7 +34,10 @@ extern unsigned int ranges_count; extern int SCAN_RANGES[]; extern unsigned int ranges_count; extern unsigned int iterations; -extern unsigned int scan_iteration; +extern unsigned int range_item; + +extern uint64_t loop_time; + void UI_Init(SSD1306Wire *display_ptr) { @@ -56,13 +59,13 @@ void clearStatus(void) { // clear status line display_instance->setColor(BLACK); - display_instance->fillRect(0, STATUS_TEXT_TOP + 2, 128, 13); + display_instance->fillRect(0, ROW_STATUS_TEXT + 2, 128, 13); display_instance->setColor(WHITE); } void UI_clearPlotter(void) { - // clear the scan plot rectangle + // clear the scan plot rectangle (top part) display_instance->setColor(BLACK); display_instance->fillRect(0, 0, STEPS, HEIGHT); display_instance->setColor(WHITE); @@ -171,21 +174,25 @@ void UI_displayDecorate(int begin = 0, int end = 0, bool redraw = false) display_instance->setColor(BLACK); display_instance->fillRect(0, SCALE_TEXT_TOP + 1, 128, 12); display_instance->setColor(WHITE); + // Drone detection level display_instance->setTextAlignment(TEXT_ALIGN_RIGHT); display_instance->drawString(128, 0, String(drone_detection_level)); // Frequency start display_instance->setTextAlignment(TEXT_ALIGN_LEFT); - display_instance->drawString(0, SCALE_TEXT_TOP, + display_instance->drawString(0, ROW_STATUS_TEXT, (begin == 0) ? String(FREQ_BEGIN) : String(begin)); + + // Frequency detected display_instance->setTextAlignment(TEXT_ALIGN_CENTER); - display_instance->drawString(128 / 2, SCALE_TEXT_TOP, + display_instance->drawString(128 / 2, ROW_STATUS_TEXT, (begin == 0) ? String(median_frequency) : String(begin + ((end - begin) / 2))); // Frequency end display_instance->setTextAlignment(TEXT_ALIGN_RIGHT); - display_instance->drawString(128, SCALE_TEXT_TOP, + display_instance->drawString(128, ROW_STATUS_TEXT, (end == 0) ? String(FREQ_END) : String(end)); } + if (led_flag == true && detection_count >= 5) { digitalWrite(LED, HIGH); @@ -200,6 +207,7 @@ void UI_displayDecorate(int begin = 0, int end = 0, bool redraw = false) { digitalWrite(LED, LOW); } + // Status text block if (!drone_detected) { @@ -209,21 +217,26 @@ void UI_displayDecorate(int begin = 0, int end = 0, bool redraw = false) clearStatus(); if (scan_progress_count == 0) { - display_instance->drawString(start_scan_text, STATUS_TEXT_TOP, - "Scan. "); + display_instance->drawString(start_scan_text, ROW_STATUS_TEXT, + "Scan \\"); } else if (scan_progress_count == 1) { - display_instance->drawString(start_scan_text, STATUS_TEXT_TOP, - "Scan.. "); + display_instance->drawString(start_scan_text, ROW_STATUS_TEXT, + "Scan |"); } else if (scan_progress_count == 2) { - display_instance->drawString(start_scan_text, STATUS_TEXT_TOP, - "Scan..."); + display_instance->drawString(start_scan_text, ROW_STATUS_TEXT, + "Scan /"); + } + else if (scan_progress_count == 3) + { + display_instance->drawString(start_scan_text, ROW_STATUS_TEXT, + "Scan -"); } scan_progress_count++; - if (scan_progress_count == 3) + if (scan_progress_count >= 4) { scan_progress_count = 0; } @@ -233,29 +246,38 @@ void UI_displayDecorate(int begin = 0, int end = 0, bool redraw = false) display_instance->setTextAlignment(TEXT_ALIGN_CENTER); // clear status line clearStatus(); - display_instance->drawString(start_scan_text, STATUS_TEXT_TOP, + display_instance->drawString(start_scan_text, ROW_STATUS_TEXT, String(drone_detected_frequency_start) + ">RF<" + String(drone_detected_frequency_end)); + + } if (ranges_count == 0) { +#ifdef DEBUG display_instance->setTextAlignment(TEXT_ALIGN_LEFT); - display_instance->drawString(0, STATUS_TEXT_TOP, String(FREQ_BEGIN)); + display_instance->drawString(0, ROW_STATUS_TEXT,String(loop_time)); +#else + display_instance->setTextAlignment(TEXT_ALIGN_LEFT); + display_instance->drawString(0, ROW_STATUS_TEXT, String(FREQ_BEGIN)); + +#endif display_instance->setTextAlignment(TEXT_ALIGN_RIGHT); - display_instance->drawString(128, STATUS_TEXT_TOP, String(FREQ_END)); + display_instance->drawString(128, ROW_STATUS_TEXT, String(FREQ_END)); + } else if (ranges_count > 0) { display_instance->setTextAlignment(TEXT_ALIGN_LEFT); - display_instance->drawString(0, STATUS_TEXT_TOP, - String(SCAN_RANGES[scan_iteration] / 1000) + "-" + String(SCAN_RANGES[scan_iteration] % 1000)); - if (scan_iteration + 1 < iterations) + display_instance->drawString(0, ROW_STATUS_TEXT, + String(SCAN_RANGES[range_item] / 1000) + "-" + String(SCAN_RANGES[range_item] % 1000)); + if (range_item + 1 < iterations) { display_instance->setTextAlignment(TEXT_ALIGN_RIGHT); - display_instance->drawString(128, STATUS_TEXT_TOP, - String(SCAN_RANGES[scan_iteration + 1] / 1000) + "-" + String(SCAN_RANGES[scan_iteration + 1] % 1000)); + display_instance->drawString(128, ROW_STATUS_TEXT, + String(SCAN_RANGES[range_item + 1] / 1000) + "-" + String(SCAN_RANGES[range_item + 1] % 1000)); } } - if (!ui_initialized) + if (ui_initialized == false) { // X-axis display_instance->fillRect(0, HEIGHT, STEPS, X_AXIS_WEIGHT);