From 02df8efa1c9b67aac1bf796f9fc39217a00f447b Mon Sep 17 00:00:00 2001 From: Sassa NF Date: Sun, 29 Sep 2024 08:29:40 +0100 Subject: [PATCH] DecoratedBarChart - labels and axis with ticks --- lib/charts/BarChart.cpp | 59 +++++++++++++++++++++++++++++++++++++++++ lib/charts/charts.h | 21 +++++++++++++++ src/main.cpp | 32 +++------------------- src/ui.cpp | 12 --------- 4 files changed, 84 insertions(+), 40 deletions(-) diff --git a/lib/charts/BarChart.cpp b/lib/charts/BarChart.cpp index eacd78d..cdc04d8 100644 --- a/lib/charts/BarChart.cpp +++ b/lib/charts/BarChart.cpp @@ -90,3 +90,62 @@ int BarChart::y2pos(float y) return height - height * (y - min_y) / (max_y - min_y); } + +void DecoratedBarChart::draw() +{ + bool draw_axis = redraw_all; + + BarChart::draw(); + + display.setColor(BLACK); + display.fillRect(pos_x, text_y, width, pos_y - text_y); + + display.setColor(WHITE); + display.setTextAlignment(TEXT_ALIGN_LEFT); + + for (uint16_t x = 0; x < width; x++) + { + float y = ys[x]; + if (y >= level_y) + { + String s = String(ys[x], 0); + uint16_t w = display.getStringWidth(s); + uint16_t x1 = x; + for (; x < x1 + w; x++) + { + if (ys[x] > y) + { + y = ys[x]; + s = String(y, 0); + w = max(w, display.getStringWidth(s)); + } + } + + display.drawString(x1, text_y, s); + } + } + + if (draw_axis) + { + display.setColor(WHITE); + + uint16_t y = pos_y + height + 1; + display.fillRect(pos_x, y, width, X_AXIS_WEIGHT); + + // Start and end ticks + display.fillRect(pos_x, y + 1, 2, AXIS_HEIGHT); + display.fillRect(pos_x + width - 2, y + 1, 2, AXIS_HEIGHT); + + for (float step = 0; min_x + step * MAJOR_TICKS < max_x; step += 1) + { + int tick_pos = x2pos(min_x + step * MAJOR_TICKS); + display.drawVerticalLine(pos_x + tick_pos, y + 1, MAJOR_TICK_LENGTH); + } + + for (float step = 0; min_x + step * MINOR_TICKS < max_x; step += 1) + { + int tick_pos = x2pos(min_x + step * MINOR_TICKS); + display.drawVerticalLine(pos_x + tick_pos, y + 1, MINOR_TICK_LENGTH); + } + } +} diff --git a/lib/charts/charts.h b/lib/charts/charts.h index 5c8a4ce..83675a8 100644 --- a/lib/charts/charts.h +++ b/lib/charts/charts.h @@ -64,4 +64,25 @@ struct BarChart : Chart int x2pos(float x); int y2pos(float y); }; + +#define LABEL_HEIGHT 6 +#define X_AXIS_WEIGHT 1 +#define MAJOR_TICK_LENGTH 2 +#define MAJOR_TICKS 10 +#define MINOR_TICK_LENGTH 1 +#define MINOR_TICKS 5 +#define AXIS_HEIGHT (X_AXIS_WEIGHT + MAJOR_TICK_LENGTH + 1) +struct DecoratedBarChart : BarChart +{ + int text_y; + + DecoratedBarChart(OLEDDisplay &d, uint16_t x, uint16_t y, uint16_t w, uint16_t h, + float min_x, float max_x, float min_y, float max_y, float level_y) + : BarChart(d, x, y + LABEL_HEIGHT, w, h - LABEL_HEIGHT - AXIS_HEIGHT, min_x, + max_x, min_y, max_y, level_y), + text_y(y) {}; + + void draw() override; +}; + #endif diff --git a/src/main.cpp b/src/main.cpp index 4e232ab..939ffaa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -657,11 +657,9 @@ void setup(void) xTaskCreate(logToSerialTask, "LOG_DATA_JSON", 2048, NULL, 1, NULL); #endif - bar = new BarChart(display, 0, START_LOW, display.width(), - display.height() / 2 - START_LOW, FREQ_BEGIN, FREQ_END, - LO_RSSI_THRESHOLD, HI_RSSI_THRESHOLD, - -(float)show_db_after); // LO_RSSI_THRESHOLD + (HI_RSSI_THRESHOLD - - // LO_RSSI_THRESHOLD) * 0.3); + bar = new DecoratedBarChart( + display, 0, 0, display.width(), display.height() / 2 + AXIS_HEIGHT, FREQ_BEGIN, + FREQ_END, LO_RSSI_THRESHOLD, HI_RSSI_THRESHOLD, -(float)show_db_after); bar->reset(); } @@ -1070,18 +1068,9 @@ void loop(void) detected = detected_at < RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE; detected_y[display_x] = false; -#if FILTER_SPECTRUM_RESULTS float rr; if (detected) { - // calculating max window x RSSI after filters - x_window = (int)(display_x / WINDOW_SIZE); - int abs_result = abs(result[detected_at]); - if (max_x_window[x_window] > abs_result) - { - max_x_window[x_window] = abs_result; - LOG("MAX x window: %i %i\n", x_window, abs_result); - } rr = -(float)result[detected_at]; } else @@ -1095,7 +1084,6 @@ void loop(void) { bar->drawOne(updated); } -#endif if (detected_at <= drone_detection_level) { @@ -1258,19 +1246,7 @@ void loop(void) #endif bar->draw(); -#ifdef METHOD_RSSI - // Printing Max Window DB. - for (int x2 = 0; x2 < STEPS / WINDOW_SIZE; x2++) - { - if (max_x_window[x2] < show_db_after && max_x_window[x2] != 0) - { - display.drawString(x2 * WINDOW_SIZE + WINDOW_SIZE, 0, - "-" + String(max_x_window[x2])); - } - max_x_window[x2] = 999; - } -#endif - // Render display data here + // Render display data here display.display(); #ifdef OSD_ENABLED // Sometimes OSD prints entire screen with the digits. diff --git a/src/ui.cpp b/src/ui.cpp index ee8bc92..ea376dd 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -184,9 +184,6 @@ void UI_displayDecorate(int begin = 0, int end = 0, bool redraw = false) { if (!ui_initialized) { - // Start and end ticks - display_instance->fillRect(0, HEIGHT + X_AXIS_WEIGHT, 2, MAJOR_TICK_LENGTH + 1); - display_instance->fillRect(126, HEIGHT + X_AXIS_WEIGHT, 2, MAJOR_TICK_LENGTH + 1); // Drone detection level display_instance->setTextAlignment(TEXT_ALIGN_RIGHT); display_instance->drawString(128, 0, String(drone_detection_level)); @@ -299,14 +296,5 @@ void UI_displayDecorate(int begin = 0, int end = 0, bool redraw = false) String(SCAN_RANGES[range_item + 1] % 1000)); } } - if (ui_initialized == false) - { - // X-axis - display_instance->fillRect(0, HEIGHT, STEPS, X_AXIS_WEIGHT); -// ticks -#ifdef MAJOR_TICKS - drawTicks(MAJOR_TICKS, MAJOR_TICK_LENGTH); -#endif - } ui_initialized = true; }