Experimental low power mode

This commit is contained in:
2026-11-12 21:53:43 +01:00
parent 2d6864b1c7
commit 95724fd4d3
4 changed files with 124 additions and 12 deletions

No files matched your search

+120 -8
View File
@@ -29,6 +29,7 @@
#include "esp_err.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_sleep.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
@@ -571,10 +572,12 @@ static void history_add(uint32_t ric, unsigned baud, unsigned function,
history_cursor = 0;
history_storage_save();
ui_note_activity();
ESP_LOGI(TAG, "PAGER RIC=%lu function=%u baud=%u: %s",
(unsigned long)ric, function, baud, text);
ui_render();
if (oled_is_on) {
ui_note_activity();
ui_render();
}
}
#define POCSAG_SYNC 0x7CD215D8UL
@@ -589,6 +592,7 @@ typedef enum {
} baud_t;
static baud_t current_baud = RATE_UNKNOWN;
static TickType_t baud_detected_at;
static uint32_t bit_period_q16;
static uint32_t phase_q16;
@@ -847,7 +851,7 @@ static void ui_draw_tx(void)
static void ui_render(void)
{
if (!oled_is_on)
oled_power_on();
return;
oled_clear();
if (ui_screen == UI_MENU)
ui_draw_menu();
@@ -870,7 +874,8 @@ static void ui_render(void)
static void oled_draw_history(void)
{
ui_render();
if (oled_is_on)
ui_render();
}
static void ui_move_menu(int direction)
@@ -1120,6 +1125,8 @@ static void ui_update_display_flash(void)
oled_set_inverted(!oled_inverted);
rx_flash_toggles--;
if (!rx_flash_toggles)
oled_set_inverted(false);
rx_flash_next_toggle = now + pdMS_TO_TICKS(RX_DISPLAY_FLASH_MS);
}
@@ -1407,7 +1414,7 @@ static void decode_codeword(uint32_t raw, unsigned slot)
decode_numeric(payload);
else
decode_alpha(payload);
if (live_page && ui_screen == UI_LIVE) {
if (live_page && ui_screen == UI_LIVE && oled_is_on) {
ui_note_activity();
ui_render();
}
@@ -1466,6 +1473,7 @@ static void set_detected_baud(baud_t rate)
return;
current_baud = rate;
baud_detected_at = xTaskGetTickCount();
bit_period_q16 = (uint32_t)(((uint64_t)SAMPLE_RATE_HZ << 16) /
(unsigned)rate);
phase_q16 = bit_period_q16 / 2U;
@@ -1495,11 +1503,11 @@ static void process_sample(uint8_t sample)
edge_period_max = edge_samples;
}
if (edge_samples >= 69 && edge_samples <= 81) {
if (edge_samples >= 34 && edge_samples <= 42) {
hit512++; hit1200 = hit2400 = 0;
} else if (edge_samples >= 28 && edge_samples <= 36) {
} else if (edge_samples >= 14 && edge_samples <= 18) {
hit1200++; hit512 = hit2400 = 0;
} else if (edge_samples >= 13 && edge_samples <= 19) {
} else if (edge_samples >= 7 && edge_samples <= 10) {
hit2400++; hit512 = hit1200 = 0;
} else {
hit512 = hit1200 = hit2400 = 0;
@@ -1575,6 +1583,108 @@ static void start_sampler(void)
(unsigned long)SAMPLE_RATE_HZ);
}
static bool ui_allows_light_sleep(TickType_t now)
{
if (oled_is_on || rx_flash_toggles || hold_cue_visible ||
message_active || batch_locked ||
(int32_t)(now - page_alert_until) < 0 ||
now - ui_last_activity < pdMS_TO_TICKS(OLED_IDLE_OFF_MS))
return false;
for (unsigned i = 0; i < BUTTON_COUNT; i++) {
if (ui_buttons[i].down || gpio_get_level(ui_buttons[i].pin) == 0)
return false;
}
return true;
}
static void reset_receiver_after_wake(void)
{
sample_wr = 0;
sample_rd = 0;
current_baud = RATE_UNKNOWN;
baud_detected_at = 0;
bit_period_q16 = 0;
phase_q16 = 0;
edge_samples = 0;
hit512 = 0;
hit1200 = 0;
hit2400 = 0;
last_sample = gpio_get_level(PIN_CC1101_GDO0) ? 1U : 0U;
sync_shift = 0;
sync_bits = 0;
batch_locked = false;
invert_bits = false;
codeword_shift = 0;
codeword_bits = 0;
batch_word_index = 0;
message_abort();
}
static void reset_sync_after_carrier_probe(void)
{
sample_wr = 0;
sample_rd = 0;
edge_samples = 0;
hit512 = 0;
hit1200 = 0;
hit2400 = 0;
last_sample = gpio_get_level(PIN_CC1101_GDO0) ? 1U : 0U;
if (current_baud != RATE_UNKNOWN) {
phase_q16 = bit_period_q16 / 2U;
sync_shift = 0;
sync_bits = 0;
codeword_shift = 0;
codeword_bits = 0;
batch_word_index = 0;
}
}
static void receiver_light_sleep_check(void)
{
static TickType_t last_check;
TickType_t now = xTaskGetTickCount();
if (now - last_check < pdMS_TO_TICKS(100) ||
!ui_allows_light_sleep(now))
return;
if (current_baud != RATE_UNKNOWN) {
TickType_t acquisition_guard = pdMS_TO_TICKS(
1152000U / (unsigned)current_baud + 50U);
if (now - baud_detected_at < acquisition_guard)
return;
}
last_check = now;
ESP_ERROR_CHECK(gptimer_stop(sample_timer));
ESP_ERROR_CHECK(gptimer_disable(sample_timer));
cc_write(0x02, 0x0E); /* GDO0: carrier sense while the MCU sleeps */
vTaskDelay(pdMS_TO_TICKS(1));
bool carrier_detected = gpio_get_level(PIN_CC1101_GDO0) != 0;
if (carrier_detected || !ui_allows_light_sleep(xTaskGetTickCount())) {
cc_write(0x02, 0x0D); /* GDO0: asynchronous data */
reset_sync_after_carrier_probe();
ESP_ERROR_CHECK(gptimer_enable(sample_timer));
ESP_ERROR_CHECK(gptimer_start(sample_timer));
return;
}
ESP_ERROR_CHECK(esp_light_sleep_start());
cc_write(0x02, 0x0D); /* Restore data output before sampling */
reset_receiver_after_wake();
ESP_ERROR_CHECK(gptimer_enable(sample_timer));
ESP_ERROR_CHECK(gptimer_start(sample_timer));
}
static void configure_light_sleep_wakeup(void)
{
ESP_ERROR_CHECK(gpio_wakeup_enable(PIN_CC1101_GDO0, GPIO_INTR_HIGH_LEVEL));
for (unsigned i = 0; i < BUTTON_COUNT; i++)
ESP_ERROR_CHECK(gpio_wakeup_enable(ui_buttons[i].pin, GPIO_INTR_LOW_LEVEL));
ESP_ERROR_CHECK(esp_sleep_enable_gpio_wakeup());
}
/* Send is explicit from the TX screen; pause sampling so our own RF cannot enter RX decode. */
static void tx_send_selected(void)
{
@@ -1664,6 +1774,7 @@ void app_main(void)
setup_spi();
oled_init();
ui_hardware_init();
configure_light_sleep_wakeup();
battery_adc_init();
ui_screen = UI_LIVE;
ui_render();
@@ -1689,6 +1800,7 @@ void app_main(void)
ui_update_alert();
ui_update_display_flash();
ui_idle_power_down();
receiver_light_sleep_check();
if (xTaskGetTickCount() - last_battery_read >= pdMS_TO_TICKS(5000)) {
last_battery_read = xTaskGetTickCount();