Fix G3 USB sleep and two-minute button wake
This commit is contained in:
1 parent
27616e2e5b
commit
fc3298c84d
19 files changed
+527
-21
No files matched your search
+41
-10
@@ -16,6 +16,7 @@
|
||||
#include "esp_system.h"
|
||||
#include <driver/rtc_io.h>
|
||||
#include "ESP32TrueRandom.h"
|
||||
#include "UsbLogging.h"
|
||||
|
||||
#if defined(ARDUINO_USB_CDC_ON_BOOT) && ARDUINO_USB_CDC_ON_BOOT && \
|
||||
(!defined(ARDUINO_USB_MODE) || !ARDUINO_USB_MODE)
|
||||
@@ -99,9 +100,17 @@ public:
|
||||
}
|
||||
|
||||
void sleep(uint32_t secs) override {
|
||||
// Skip if not allow to sleep
|
||||
if (inhibit_sleep) {
|
||||
delay(1); // Give MCU to OTA to run
|
||||
// Native USB loses its connection in light sleep. An enumerated host
|
||||
// still needs USB serviced when no terminal asserts CDC DTR (for example,
|
||||
// after a Pi closes its serial port). Guard every caller here.
|
||||
bool keep_awake = inhibit_sleep || isUsbHostConnected();
|
||||
#if MESH_USB_LOGGING_AVAILABLE
|
||||
// A live logging stream must also remain available before a host opens
|
||||
// it and across host disconnects. Compiled-out logging is not a blocker.
|
||||
keep_awake = keep_awake || mesh::isUsbLoggingEnabled();
|
||||
#endif
|
||||
if (keep_awake) {
|
||||
delay(1); // Keep USB and OTA tasks running.
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -110,8 +119,15 @@ public:
|
||||
esp_sleep_enable_timer_wakeup(secs * 1000000ULL); // Wake up periodically to do scheduled jobs
|
||||
}
|
||||
|
||||
uint32_t irqGpio = getIRQGpio();
|
||||
if (irqGpio == static_cast<uint32_t>(-1)) {
|
||||
const uint32_t irqGpio = getIRQGpio();
|
||||
const bool radio_wakeup = irqGpio != static_cast<uint32_t>(-1);
|
||||
#if defined(MOMENTARY_BUTTON_WAKE_FROM_SLEEP) \
|
||||
&& MOMENTARY_BUTTON_WAKE_FROM_SLEEP && defined(PIN_USER_BTN)
|
||||
const int button_gpio = PIN_USER_BTN;
|
||||
#else
|
||||
const int button_gpio = -1;
|
||||
#endif
|
||||
if (!radio_wakeup && button_gpio < 0) {
|
||||
if (secs > 0) {
|
||||
esp_light_sleep_start();
|
||||
}
|
||||
@@ -124,8 +140,10 @@ public:
|
||||
// Disable CPU interrupt servicing
|
||||
portENTER_CRITICAL(&sleepMux);
|
||||
|
||||
// Skip sleep if there is a LoRa packet
|
||||
if (gpio_get_level(wakeupPin) == HIGH) {
|
||||
// Do not sleep over a pending packet or a press that needs polling.
|
||||
if ((radio_wakeup && gpio_get_level(wakeupPin) == HIGH)
|
||||
|| (button_gpio >= 0
|
||||
&& gpio_get_level((gpio_num_t)button_gpio) == USER_BTN_PRESSED)) {
|
||||
portEXIT_CRITICAL(&sleepMux);
|
||||
delay(1);
|
||||
return;
|
||||
@@ -133,14 +151,27 @@ public:
|
||||
|
||||
// Configure GPIO wakeup
|
||||
esp_sleep_enable_gpio_wakeup();
|
||||
gpio_wakeup_enable((gpio_num_t)wakeupPin, GPIO_INTR_HIGH_LEVEL); // Wake up when receiving a LoRa packet
|
||||
if (radio_wakeup) {
|
||||
gpio_wakeup_enable(wakeupPin, GPIO_INTR_HIGH_LEVEL);
|
||||
}
|
||||
if (button_gpio >= 0) {
|
||||
gpio_wakeup_enable((gpio_num_t)button_gpio,
|
||||
USER_BTN_PRESSED == LOW ? GPIO_INTR_LOW_LEVEL : GPIO_INTR_HIGH_LEVEL);
|
||||
}
|
||||
|
||||
// MCU enters light sleep
|
||||
esp_light_sleep_start();
|
||||
|
||||
// Avoid ISR flood during wakeup due to HIGH LEVEL interrupt
|
||||
gpio_wakeup_disable(wakeupPin);
|
||||
gpio_set_intr_type(wakeupPin, GPIO_INTR_POSEDGE);
|
||||
if (radio_wakeup) {
|
||||
gpio_wakeup_disable(wakeupPin);
|
||||
gpio_set_intr_type(wakeupPin, GPIO_INTR_POSEDGE);
|
||||
}
|
||||
if (button_gpio >= 0) {
|
||||
gpio_wakeup_disable((gpio_num_t)button_gpio);
|
||||
// ESP32 MomentaryButton polls; only its sleep wake source is temporary.
|
||||
gpio_set_intr_type((gpio_num_t)button_gpio, GPIO_INTR_DISABLE);
|
||||
}
|
||||
|
||||
// Enable CPU interrupt servicing
|
||||
portEXIT_CRITICAL(&sleepMux);
|
||||
|
||||
@@ -103,6 +103,17 @@ int MomentaryButton::check(bool repeat_click) {
|
||||
const int raw_btn =
|
||||
_threshold > 0 ? (analogRead(_pin) < _threshold) : digitalRead(_pin);
|
||||
|
||||
#if MOMENTARY_BUTTON_WAKE_HOLD_MS > 0
|
||||
// Keep the main loop awake after button activity, including when a press
|
||||
// starts at millis zero or spans rollover. Holding/repressing extends it.
|
||||
if (isPressed(raw_btn)) {
|
||||
_last_pressed_at = now;
|
||||
_wake_hold_active = true;
|
||||
} else if (_wake_hold_active && !isWakeHoldActive()) {
|
||||
_wake_hold_active = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Treat prev as the stable electrical level. A raw transition must remain
|
||||
// unchanged for a short interval before it can alter gesture state. Without
|
||||
// this, one physical release can be counted two or three times and a short
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
#define BUTTON_EVENT_DOUBLE_CLICK 3
|
||||
#define BUTTON_EVENT_TRIPLE_CLICK 4
|
||||
|
||||
#ifndef MOMENTARY_BUTTON_WAKE_HOLD_MS
|
||||
#define MOMENTARY_BUTTON_WAKE_HOLD_MS 0
|
||||
#endif
|
||||
|
||||
class MomentaryButton {
|
||||
int8_t _pin;
|
||||
int8_t prev, cancel;
|
||||
@@ -23,6 +27,10 @@ class MomentaryButton {
|
||||
int8_t _candidate_level;
|
||||
uint32_t _candidate_since;
|
||||
bool _debouncing;
|
||||
#if MOMENTARY_BUTTON_WAKE_HOLD_MS > 0
|
||||
uint32_t _last_pressed_at = 0;
|
||||
bool _wake_hold_active = false;
|
||||
#endif
|
||||
|
||||
bool isPressed(int level) const;
|
||||
|
||||
@@ -34,7 +42,15 @@ public:
|
||||
void cancelClick(); // suppress next BUTTON_EVENT_CLICK (if already in DOWN state)
|
||||
uint8_t getPin() { return _pin; }
|
||||
bool isPressed() const;
|
||||
bool isWakeHoldActive() const {
|
||||
#if MOMENTARY_BUTTON_WAKE_HOLD_MS > 0
|
||||
return _wake_hold_active
|
||||
&& (uint32_t)(millis() - _last_pressed_at) < MOMENTARY_BUTTON_WAKE_HOLD_MS;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
bool needsPolling() const {
|
||||
return _debouncing || _press_active || _pending_click;
|
||||
return _debouncing || _press_active || _pending_click || isWakeHoldActive();
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user