diff --git a/lib/charts/BarChart.cpp b/lib/charts/BarChart.cpp index f92c63a..305cdbd 100644 --- a/lib/charts/BarChart.cpp +++ b/lib/charts/BarChart.cpp @@ -109,6 +109,8 @@ void BarChart::onEvent(Event &e) return; } + level_y = e.emitter.trigger_level; + int u = updatePoint(e.emitter.current_frequency, e.detected.rssi); if (e.emitter.animated) { @@ -134,6 +136,8 @@ void DecoratedBarChart::draw() display.setColor(WHITE); display.setTextAlignment(TEXT_ALIGN_LEFT); + uint16_t first_untouched = 0; + for (uint16_t x = 0; x < width; x++) { float y = bar.ys[x]; @@ -142,7 +146,7 @@ void DecoratedBarChart::draw() String s = String(bar.ys[x], 0); uint16_t w = display.getStringWidth(s); uint16_t x1 = x; - for (; x < x1 + w; x++) + for (; x < x1 + w && x < width; x++) { if (bar.ys[x] > y) { @@ -152,7 +156,15 @@ void DecoratedBarChart::draw() } } - display.drawString(x1, pos_y, s); + if (x > width && first_untouched <= width - w) + { + x1 = width - w; + } + + first_untouched = x; + + if (x1 + w <= width) + display.drawString(pos_x + x1, pos_y, s); } } diff --git a/lib/charts/WaterfallChart.cpp b/lib/charts/WaterfallChart.cpp index 9828794..f3b6553 100644 --- a/lib/charts/WaterfallChart.cpp +++ b/lib/charts/WaterfallChart.cpp @@ -63,5 +63,7 @@ void WaterfallChart::onEvent(Event &e) return; } + level_y = e.emitter.trigger_level; + updatePoint(e.time_ms, e.detected.freq, e.detected.rssi); } diff --git a/src/main.cpp b/src/main.cpp index 9ddba7a..bda4cde 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -725,10 +725,9 @@ int binToRSSI(int bin) return 11 + (bin * 4); } -// return true if continue the code is false break the loop -bool buttonPressHandler(float freq) +// is there an input using Hot Button or joystick +bool buttonInputRequested() { - // Detection level button short press if (button.pressedFor(100) #ifdef JOYSTICK_ENABLED || joy_btn_click() @@ -736,68 +735,63 @@ bool buttonPressHandler(float freq) ) { button.update(); - button_pressed_counter = 0; - // if long press stop - while (button.pressedNow() + if (button.pressedNow() #ifdef JOYSTICK_ENABLED - || joy_btn_click() + || joy_btn_click() #endif ) { - // Print Curent frequency once - if (button_pressed_counter == 0) - { - display.setTextAlignment(TEXT_ALIGN_CENTER); - display.drawString(128 / 2, 0, String(freq)); - display.display(); - } - delay(10); - button_pressed_counter++; - if (button_pressed_counter > 150) - { - digitalWrite(LED, HIGH); - delay(150); - digitalWrite(LED, LOW); - } - } - if (button_pressed_counter > 150) - { - // Remove Curent Frequency Text - display.setTextAlignment(TEXT_ALIGN_CENTER); - display.setColor(BLACK); - display.drawString(128 / 2, 0, String(freq)); - display.setColor(WHITE); - display.display(); - return false; - } - if (button_pressed_counter > 50 && button_pressed_counter < 150) - { - if (!joy_btn_clicked) - { - // Visually confirm it's off so user releases button - display.displayOff(); - // Deep sleep (has wait for release so we don't wake up - // immediately) - heltec_deep_sleep(); - } - return false; - } - button.update(); - display.setTextAlignment(TEXT_ALIGN_RIGHT); - // erase old drone detection level value - display.setColor(BLACK); - display.fillRect(128 - 13, 0, 13, 13); - display.setColor(WHITE); - drone_detection_level++; - // print new value - display.drawString(128, 0, String(drone_detection_level)); - tone(BUZZER_PIN, 104, 150); - if (drone_detection_level > 30) - { - drone_detection_level = 1; + return true; } } - return true; + + return false; +} + +enum ButtonEvent +{ + NONE = 0, + LONG_PRESS, + SHORT_PRESS, + TOO_SHORT, + SUSPEND +}; + +ButtonEvent buttonPressEvent() +{ + button_pressed_counter = 0; + // if long press stop + while (button.pressedNow() +#ifdef JOYSTICK_ENABLED + || joy_btn_click() +#endif + ) + { + delay(10); + button_pressed_counter++; + if (button_pressed_counter > 150) + { + digitalWrite(LED, HIGH); + delay(150); + digitalWrite(LED, LOW); + } + } + if (button_pressed_counter > 150) + { + return LONG_PRESS; + } + + if (button_pressed_counter > 50) + { + if (!joy_btn_clicked) + { + return SUSPEND; + } + return SHORT_PRESS; + } + button.update(); + + return TOO_SHORT; } void drone_sound_alarm(void *arg, Event &e) @@ -1175,8 +1169,68 @@ void loop(void) display.display(); } - if (buttonPressHandler(r.current_frequency) == false) - break; + if (buttonInputRequested()) + { + display.setTextAlignment(TEXT_ALIGN_CENTER); + display.drawString(display.width() / 2, 0, String(r.current_frequency)); + display.display(); + + ButtonEvent e = buttonPressEvent(); + + if (e == LONG_PRESS) + { + // Remove Curent Frequency Text + display.setTextAlignment(TEXT_ALIGN_CENTER); + display.setColor(BLACK); + display.drawString(display.width() / 2, 0, + String(r.current_frequency)); + display.setColor(WHITE); + display.display(); + + break; + } + + if (e == SUSPEND) + { + // Visually confirm it's off so user releases button + display.displayOff(); + // Deep sleep (has wait for release so we don't wake up + // immediately) + heltec_deep_sleep(); + break; + } + + if (e == SHORT_PRESS) + break; + + if (e == TOO_SHORT) + { + String v = String(r.trigger_level) + " dB"; + uint16_t w = display.getStringWidth(v); + display.setTextAlignment(TEXT_ALIGN_RIGHT); + // erase old drone detection level value + display.setColor(BLACK); + display.fillRect(display.width() - w, 0, 13, w); + display.setColor(WHITE); + + // dt is roughly single-pixel increment + float dt = + bar->bar.height == 0 + ? 0.0 + : (LO_RSSI_THRESHOLD - HI_RSSI_THRESHOLD) / bar->bar.height; + r.trigger_level += dt; + if (r.trigger_level <= LO_RSSI_THRESHOLD) + { + r.trigger_level = HI_RSSI_THRESHOLD; + } + + // print new value + display.drawString(display.width(), 0, v); + tone(BUZZER_PIN, 104, 150); + + bar->bar.redraw_all = true; + } + } // wait a little bit before the next scan, // otherwise the SX1262 hangs