This commit is contained in:
2026-11-12 19:27:41 +01:00
parent 4ba076d333
commit 5352b76bc4
3 changed files with 461 additions and 38 deletions

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
# ESP32-H2 POCSAG Receiver
An experimental ESP-IDF 6.0 receiver for 1200-baud POCSAG using an ESP32-H2, CC1101, and 128x64 SPI SSD1306 OLED. The CC1101 is configured for a 26 MHz crystal and 435 MHz by default. Reception is decoded in firmware from the CC1101 asynchronous GDO0 output.
This project is receive-only. It does not implement POCSAG transmission.
## Hardware Pins
All button inputs are active-low and use the ESP32-H2 internal pull-ups. Connect external pushbuttons between their GPIO and GND. BOOT remains available as a complete single-button fallback.
| ESP32-H2 GPIO | Function |
| --- | --- |
| GPIO0 | CC1101 MISO |
| GPIO1 | CC1101 CSN |
| GPIO2 | Battery-divider ADC input when enabled; ADC1 channel 1 |
| GPIO3 | CC1101 GDO0 asynchronous data |
| GPIO4 | Shared SPI SCLK for CC1101 and OLED |
| GPIO5 | Shared SPI MOSI for CC1101 and OLED |
| GPIO6 | Menu Up button |
| GPIO7 | Menu Down button |
| GPIO8 | On-board NeoPixel data |
| GPIO9 | BOOT button |
| GPIO10 | OLED CS |
| GPIO11 | OLED DC |
| GPIO12 | OLED reset |
| GPIO13 | Normal status LED |
| GPIO14 | Menu Select button |
The shared SPI bus runs CC1101 and the OLED on separate chip-select pins. Keep the listed SPI, RF, display, and LED pins unchanged unless you also update the firmware and wiring.
**Button wiring change:** GPIO2 is now reserved for the optional battery ADC. The optional Select button is GPIO14; if it is still wired to GPIO2 from an earlier firmware version, move it to GPIO14 before enabling battery monitoring.
## Battery Measurement
The board's battery pads are power connections, not a voltage-sense output. Do not connect a Li-ion battery directly to an ESP32 GPIO: a charged cell can reach 4.2 V and exceed the ADC input range.
To measure battery voltage, install an external resistor divider:
```text
BAT+ ---- Rtop ----+---- GPIO2 (ADC1_CH1)
|
Rbottom
|
GND ---------------+---- ESP32-H2 GND
```
The defaults in `main/main.c` are `Rtop = 100 kOhm` and `Rbottom = 100 kOhm`, so 4.2 V at the battery produces about 2.1 V at the ADC pin. Use the actual installed resistor values in `BATTERY_DIVIDER_TOP_OHMS` and `BATTERY_DIVIDER_BOTTOM_OHMS`; the firmware calculates:
```text
battery_mV = adc_pin_mV * (Rtop + Rbottom) / Rbottom
```
After wiring and checking that the ADC pin voltage stays below the ESP32-H2 ADC limit, set `BATTERY_MONITOR_ENABLE` to `1`. The firmware uses ADC one-shot mode with IDF calibration, samples every five seconds, logs millivolts, and shows the voltage on the live screen. It is disabled by default because this board revision has no built-in voltage-sense circuit.
## Controls and Menu
With the added buttons:
- **Up / Down:** move through menu items; browse older/newer history; decrease/increase the tuning candidate.
- **Select:** activate the selected menu item, apply a tuned frequency, enter history from Live, or return to Live from History.
- **BOOT fallback:** short press advances through menu items, message pages, and history; long press opens the menu or selects/applies the current item.
The menu contains:
- **Filter:** Private mode is the default and stores/displays only `MY_RIC`. Monitor mode stores/displays all decoded RICs. Switching modes clears the in-memory history.
- **Frequency:** opens the receive-only tuner.
- **Live display:** returns to the main screen.
- **Message history:** shows the eight most recent saved messages.
- **Return:** goes back to the previous screen.
The tuner steps in 25 kHz increments across 433.000 to 435.500 MHz. Up/Down choose either direction; Select applies the frequency, recalibrates the CC1101, and resumes reception. With BOOT only, short presses step upward and wrap at the end; a long press applies the candidate.
The live screen updates while a message is decoded. Messages up to 159 characters are displayed over multiple pages. In Live, BOOT short-press advances message pages and then opens history. The OLED turns off after 60 seconds without UI activity; button presses and displayed pages wake it.
Pages addressed to `MY_RIC` (default `1234567`) flash the normal LED on GPIO13 and NeoPixel on GPIO8 for approximately three seconds.
## Persistent Message History
The firmware stores the eight-entry message ring in the default NVS partition. Saved messages are restored after reboot or power loss. Private mode saves only `MY_RIC`; Monitor mode saves all decoded RICs. Changing the filter mode clears the stored history. NVS is initialized automatically on startup; if its metadata is incompatible or out of free pages, ESP-IDF recovery may erase the NVS partition and the saved messages.
## Flash Layout
The ESP32-H2 has 4 MB of flash, and `sdkconfig` now sets the ESP image header flash size to 4 MB. The existing single-app partition layout is retained: it provides a 1 MB factory application partition plus the standard NVS and PHY partitions. The remaining flash is currently unused; the firmware does not resize partitions automatically.
## Configuration
Important definitions are near the top of `main/main.c`:
- `RF_FREQ_HZ`: startup receive frequency.
- `TUNE_MIN_HZ`, `TUNE_MAX_HZ`, `TUNE_STEP_HZ`: menu tuner range and step.
- `MY_RIC`: private-mode address and own-page alert address.
- `BATTERY_MONITOR_ENABLE`: battery voltage reading, disabled by default.
- `BATTERY_DIVIDER_TOP_OHMS`, `BATTERY_DIVIDER_BOTTOM_OHMS`: installed divider resistor values.
- `OLED_IDLE_OFF_MS`: OLED timeout.
The firmware assumes a 26 MHz CC1101 crystal. Confirm the crystal and signal frequency for your hardware. The frequency tuner only changes the receiver frequency.
## Build and Flash
Open the project in ESP-IDF 6.0 with target `esp32h2`, then run:
```sh
idf.py build
idf.py -p PORT flash
idf.py -p PORT monitor
```
The component dependencies include the ESP-IDF GPIO, SPI, GPTimer, ADC, NVS, and Espressif LED-strip drivers.
## Limitations
This is a proof of concept, not a certified pager or radio product. Reception quality depends on CC1101 configuration, antenna, RF environment, and signal timing. The firmware does not transmit pages or save menu settings across reboot.
+1 -1
View File
@@ -1,3 +1,3 @@
idf_component_register(SRCS "main.c" idf_component_register(SRCS "main.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES esp_driver_gpio esp_driver_spi esp_driver_gptimer led_strip) REQUIRES esp_driver_gpio esp_driver_spi esp_driver_gptimer esp_adc nvs_flash led_strip)
+348 -37
View File
@@ -4,8 +4,8 @@
* *
* Hardware: * Hardware:
* CC1101: SCLK=GPIO4, MOSI=GPIO5, MISO=GPIO0, CSN=GPIO1, GDO0=GPIO3 * CC1101: SCLK=GPIO4, MOSI=GPIO5, MISO=GPIO0, CSN=GPIO1, GDO0=GPIO3
* SPI SSD1306 128x64: SCLK=GPIO4, MOSI=GPIO5, CS=GPIO13, * SPI SSD1306 128x64: SCLK=GPIO4, MOSI=GPIO5, CS=GPIO10,
* DC=GPIO14, RESET=GPIO12 * DC=GPIO11, RESET=GPIO12
* *
* Assumptions: * Assumptions:
* - CC1101 module has a 26 MHz crystal. * - CC1101 module has a 26 MHz crystal.
@@ -34,6 +34,10 @@
#include "driver/spi_master.h" #include "driver/spi_master.h"
#include "driver/gptimer.h" #include "driver/gptimer.h"
#include "led_strip.h" #include "led_strip.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali_scheme.h"
#include "nvs.h"
#include "nvs_flash.h"
/* ---------------------------- Pins/config ---------------------------- */ /* ---------------------------- Pins/config ---------------------------- */
@@ -49,16 +53,28 @@
#define PIN_OLED_DC GPIO_NUM_11 #define PIN_OLED_DC GPIO_NUM_11
#define PIN_OLED_RESET GPIO_NUM_12 #define PIN_OLED_RESET GPIO_NUM_12
#define PIN_BUTTON_BOOT GPIO_NUM_9 #define PIN_BUTTON_BOOT GPIO_NUM_9
#define PIN_BUTTON_UP GPIO_NUM_6
#define PIN_BUTTON_DOWN GPIO_NUM_7
#define PIN_BUTTON_SELECT GPIO_NUM_14
#define PIN_BATTERY_ADC GPIO_NUM_2
#define PIN_STATUS_LED GPIO_NUM_13 #define PIN_STATUS_LED GPIO_NUM_13
#define PIN_NEOPIXEL GPIO_NUM_8 #define PIN_NEOPIXEL GPIO_NUM_8
/* Change this one value to tune the receiver. Rebuild and flash afterwards. */ /* Change this one value to tune the receiver. Rebuild and flash afterwards. */
#define RF_FREQ_HZ 435000000UL #define RF_FREQ_HZ 435000000UL
#define CC1101_XTAL_HZ 26000000UL #define CC1101_XTAL_HZ 26000000UL
#define TUNE_MIN_HZ 433000000UL
#define TUNE_MAX_HZ 435500000UL
#define TUNE_STEP_HZ 25000UL
#define MY_RIC 1234567UL #define MY_RIC 1234567UL
#define OLED_IDLE_OFF_MS 60000U #define OLED_IDLE_OFF_MS 60000U
#define OWN_PAGE_ALERT_MS 3000U #define OWN_PAGE_ALERT_MS 3000U
/* Enable after wiring BAT+ through a divider to GPIO2 and BAT- to GND. */
#define BATTERY_MONITOR_ENABLE 0
#define BATTERY_DIVIDER_TOP_OHMS 100000U
#define BATTERY_DIVIDER_BOTTOM_OHMS 100000U
#define SAMPLE_TIMER_HZ 1000000UL #define SAMPLE_TIMER_HZ 1000000UL
#define SAMPLE_PERIOD_TICKS 26UL #define SAMPLE_PERIOD_TICKS 26UL
#define SAMPLE_RATE_HZ (SAMPLE_TIMER_HZ / SAMPLE_PERIOD_TICKS) #define SAMPLE_RATE_HZ (SAMPLE_TIMER_HZ / SAMPLE_PERIOD_TICKS)
@@ -79,9 +95,19 @@ static spi_device_handle_t oled_dev;
static gptimer_handle_t sample_timer; static gptimer_handle_t sample_timer;
static led_strip_handle_t neopixel; static led_strip_handle_t neopixel;
static unsigned display_baud; static unsigned display_baud;
static uint32_t radio_frequency_hz = RF_FREQ_HZ;
static bool oled_is_on = true; static bool oled_is_on = true;
static TickType_t ui_last_activity; static TickType_t ui_last_activity;
#if BATTERY_MONITOR_ENABLE
static bool battery_voltage_valid;
static uint32_t battery_voltage_mv;
static adc_oneshot_unit_handle_t battery_adc_unit;
static adc_cali_handle_t battery_adc_calibration;
static adc_channel_t battery_adc_channel;
static bool battery_adc_ready;
#endif
static esp_err_t spi_tx(spi_device_handle_t dev, static esp_err_t spi_tx(spi_device_handle_t dev,
const uint8_t *data, size_t len) const uint8_t *data, size_t len)
{ {
@@ -204,9 +230,9 @@ static void cc1101_init(void)
for (size_t i = 0; i < sizeof(regs) / sizeof(regs[0]); i++) for (size_t i = 0; i < sizeof(regs) / sizeof(regs[0]); i++)
cc_write(regs[i][0], regs[i][1]); cc_write(regs[i][0], regs[i][1]);
uint32_t actual_hz = cc1101_set_frequency(RF_FREQ_HZ); uint32_t actual_hz = cc1101_set_frequency(radio_frequency_hz);
ESP_LOGI(TAG, "RF target=%lu Hz, actual=%lu Hz, crystal=%lu Hz", ESP_LOGI(TAG, "RF target=%lu Hz, actual=%lu Hz, crystal=%lu Hz",
(unsigned long)RF_FREQ_HZ, (unsigned long)radio_frequency_hz,
(unsigned long)actual_hz, (unsigned long)actual_hz,
(unsigned long)CC1101_XTAL_HZ); (unsigned long)CC1101_XTAL_HZ);
@@ -219,6 +245,18 @@ static void cc1101_init(void)
cc_read_status(0x30), cc_read_status(0x31)); cc_read_status(0x30), cc_read_status(0x31));
} }
static void cc1101_retune(uint32_t frequency_hz)
{
cc_strobe(0x36); /* SIDLE */
cc_strobe(0x3A); /* SFRX */
radio_frequency_hz = cc1101_set_frequency(frequency_hz);
cc_strobe(0x33); /* SCAL */
vTaskDelay(pdMS_TO_TICKS(5));
cc_strobe(0x34); /* SRX */
ESP_LOGI(TAG, "Receiver tuned to %lu Hz",
(unsigned long)radio_frequency_hz);
}
/* ------------------------------ SSD1306 ------------------------------ */ /* ------------------------------ SSD1306 ------------------------------ */
static uint8_t fb[1024]; static uint8_t fb[1024];
@@ -440,15 +478,82 @@ static pager_message_t history[HISTORY_COUNT];
static unsigned history_count = 0; static unsigned history_count = 0;
static unsigned history_head = 0; static unsigned history_head = 0;
static unsigned history_cursor; static unsigned history_cursor;
static bool monitor_mode; static bool monitor_mode = true;
static unsigned ui_screen; static unsigned ui_screen;
static unsigned menu_cursor; static unsigned menu_cursor;
static bool live_page; static bool live_page;
static unsigned text_page; static unsigned text_page;
static nvs_handle_t history_nvs;
static bool history_nvs_ready;
#define HISTORY_STORAGE_VERSION 1U
#define HISTORY_NVS_NAMESPACE "pager"
#define HISTORY_NVS_KEY "history"
typedef struct {
uint32_t version;
uint32_t count;
uint32_t head;
pager_message_t entries[HISTORY_COUNT];
} history_storage_t;
static void ui_render(void); static void ui_render(void);
static void ui_note_activity(void); static void ui_note_activity(void);
static void history_storage_save(void)
{
if (!history_nvs_ready)
return;
history_storage_t storage = {
.version = HISTORY_STORAGE_VERSION,
.count = history_count,
.head = history_head,
};
memcpy(storage.entries, history, sizeof(history));
esp_err_t err = nvs_set_blob(history_nvs, HISTORY_NVS_KEY,
&storage, sizeof(storage));
if (err == ESP_OK)
err = nvs_commit(history_nvs);
if (err != ESP_OK)
ESP_LOGE(TAG, "Could not save message history: %s",
esp_err_to_name(err));
}
static void history_storage_init(void)
{
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES ||
err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
ESP_ERROR_CHECK(err);
ESP_ERROR_CHECK(nvs_open(HISTORY_NVS_NAMESPACE, NVS_READWRITE,
&history_nvs));
history_nvs_ready = true;
history_storage_t storage;
size_t size = sizeof(storage);
err = nvs_get_blob(history_nvs, HISTORY_NVS_KEY, &storage, &size);
if (err == ESP_ERR_NVS_NOT_FOUND)
return;
if (err != ESP_OK || size != sizeof(storage) ||
storage.version != HISTORY_STORAGE_VERSION ||
storage.count > HISTORY_COUNT || storage.head >= HISTORY_COUNT) {
ESP_LOGW(TAG, "Saved history is invalid; starting with empty history");
return;
}
memcpy(history, storage.entries, sizeof(history));
history_count = storage.count;
history_head = storage.head;
for (unsigned i = 0; i < HISTORY_COUNT; i++)
history[i].text[MESSAGE_SIZE - 1U] = '\0';
ESP_LOGI(TAG, "Restored %u saved messages", history_count);
}
static void history_add(uint32_t ric, unsigned baud, unsigned function, static void history_add(uint32_t ric, unsigned baud, unsigned function,
const char *text) const char *text)
{ {
@@ -468,6 +573,7 @@ static void history_add(uint32_t ric, unsigned baud, unsigned function,
history_count++; history_count++;
history_cursor = 0; history_cursor = 0;
history_storage_save();
ui_note_activity(); ui_note_activity();
ESP_LOGI(TAG, "PAGER RIC=%lu function=%u baud=%u: %s", ESP_LOGI(TAG, "PAGER RIC=%lu function=%u baud=%u: %s",
(unsigned long)ric, function, baud, text); (unsigned long)ric, function, baud, text);
@@ -584,19 +690,40 @@ static const char numeric_chars[16] = {
enum { enum {
UI_LIVE, UI_LIVE,
UI_HISTORY, UI_HISTORY,
UI_MENU UI_MENU,
UI_TUNE
}; };
static TickType_t button_debounce_at;
static TickType_t button_pressed_at;
static TickType_t page_alert_until; static TickType_t page_alert_until;
static TickType_t alert_toggle_at; static TickType_t alert_toggle_at;
static int button_last_level = 1;
static bool button_down;
static bool alert_led_on; static bool alert_led_on;
static bool alert_output_state; static bool alert_output_state;
static bool alert_output_initialized; static bool alert_output_initialized;
static unsigned menu_return_screen; static unsigned menu_return_screen;
static uint32_t tune_candidate_hz;
typedef enum {
BUTTON_BOOT,
BUTTON_UP,
BUTTON_DOWN,
BUTTON_SELECT,
BUTTON_COUNT
} ui_button_id_t;
typedef struct {
gpio_num_t pin;
int last_level;
bool down;
TickType_t debounce_at;
TickType_t pressed_at;
} ui_button_state_t;
static ui_button_state_t ui_buttons[BUTTON_COUNT] = {
[BUTTON_BOOT] = {.pin = PIN_BUTTON_BOOT, .last_level = 1},
[BUTTON_UP] = {.pin = PIN_BUTTON_UP, .last_level = 1},
[BUTTON_DOWN] = {.pin = PIN_BUTTON_DOWN, .last_level = 1},
[BUTTON_SELECT] = {.pin = PIN_BUTTON_SELECT, .last_level = 1},
};
static pager_message_t *history_at_age(unsigned age) static pager_message_t *history_at_age(unsigned age)
{ {
@@ -638,9 +765,21 @@ static void oled_draw_text_page(int y, const char *text, unsigned page)
static void ui_draw_live(void) static void ui_draw_live(void)
{ {
char line[24]; char line[32];
oled_text(0, 0, monitor_mode ? "POCSAG MONITOR" : "MY PAGER"); oled_text(0, 0, monitor_mode ? "POCSAG MONITOR" : "MY PAGER");
#if BATTERY_MONITOR_ENABLE
if (battery_voltage_valid) {
snprintf(line, sizeof(line), "RIC %lu BAT %u.%02uV",
(unsigned long)MY_RIC,
(unsigned)(battery_voltage_mv / 1000U),
(unsigned)((battery_voltage_mv % 1000U) / 10U));
} else {
snprintf(line, sizeof(line), "RIC %lu BAT --",
(unsigned long)MY_RIC);
}
#else
snprintf(line, sizeof(line), "RIC %lu", (unsigned long)MY_RIC); snprintf(line, sizeof(line), "RIC %lu", (unsigned long)MY_RIC);
#endif
oled_text(0, 9, line); oled_text(0, 9, line);
if (live_page && message_active) { if (live_page && message_active) {
@@ -700,15 +839,20 @@ static void ui_draw_menu(void)
static const uint8_t cursor_rows[7] = { static const uint8_t cursor_rows[7] = {
0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01 0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01
}; };
char frequency_item[24];
snprintf(frequency_item, sizeof(frequency_item), "FREQ %lu.%03lu MHZ",
(unsigned long)(radio_frequency_hz / 1000000UL),
(unsigned long)((radio_frequency_hz / 1000UL) % 1000UL));
const char *items[] = { const char *items[] = {
monitor_mode ? "FILTER: ALL RIC" : "FILTER: MY RIC", monitor_mode ? "FILTER: ALL RIC" : "FILTER: MY RIC",
frequency_item,
"LIVE DISPLAY", "LIVE DISPLAY",
"MESSAGE HISTORY", "MESSAGE HISTORY",
"RETURN" "RETURN"
}; };
oled_text(0, 0, "PAGER MENU"); oled_text(0, 0, "PAGER MENU");
for (unsigned i = 0; i < 4; i++) { for (unsigned i = 0; i < 5; i++) {
int y = 11 + (int)i * 10; int y = 8 + (int)i * 8;
if (i == menu_cursor) { if (i == menu_cursor) {
for (int row = 0; row < 7; row++) { for (int row = 0; row < 7; row++) {
for (int col = 0; col < 5; col++) { for (int col = 0; col < 5; col++) {
@@ -719,7 +863,20 @@ static void ui_draw_menu(void)
} }
oled_text(8, y, items[i]); oled_text(8, y, items[i]);
} }
oled_text(0, 55, "TAP NEXT, HOLD SELECT"); oled_text(0, 49, "UP/DN MOVE; SEL OK");
oled_text(0, 57, "BOOT TAP NEXT / HOLD");
}
static void ui_draw_tune(void)
{
char line[24];
oled_text(0, 0, "RECEIVER TUNING");
snprintf(line, sizeof(line), "%lu.%03lu MHZ",
(unsigned long)(tune_candidate_hz / 1000000UL),
(unsigned long)((tune_candidate_hz / 1000UL) % 1000UL));
oled_text(0, 19, line);
oled_text(0, 32, "STEP 25 KHZ");
oled_text(0, 49, "TAP NEXT / HOLD SET");
} }
static void ui_render(void) static void ui_render(void)
@@ -731,6 +888,8 @@ static void ui_render(void)
ui_draw_menu(); ui_draw_menu();
else if (ui_screen == UI_HISTORY) else if (ui_screen == UI_HISTORY)
ui_draw_history(); ui_draw_history();
else if (ui_screen == UI_TUNE)
ui_draw_tune();
else else
ui_draw_live(); ui_draw_live();
oled_flush(); oled_flush();
@@ -745,7 +904,11 @@ static void ui_short_press(void)
{ {
ui_note_activity(); ui_note_activity();
if (ui_screen == UI_MENU) { if (ui_screen == UI_MENU) {
menu_cursor = (menu_cursor + 1U) % 4U; menu_cursor = (menu_cursor + 1U) % 5U;
} else if (ui_screen == UI_TUNE) {
tune_candidate_hz += TUNE_STEP_HZ;
if (tune_candidate_hz > TUNE_MAX_HZ)
tune_candidate_hz = TUNE_MIN_HZ;
} else if (ui_screen == UI_LIVE) { } else if (ui_screen == UI_LIVE) {
if (live_page && message_active && if (live_page && message_active &&
text_page + 1U < text_page_count(message_len)) { text_page + 1U < text_page_count(message_len)) {
@@ -772,6 +935,12 @@ static void ui_short_press(void)
static void ui_long_press(void) static void ui_long_press(void)
{ {
ui_note_activity(); ui_note_activity();
if (ui_screen == UI_TUNE) {
cc1101_retune(tune_candidate_hz);
ui_screen = UI_MENU;
ui_render();
return;
}
if (ui_screen != UI_MENU) { if (ui_screen != UI_MENU) {
menu_return_screen = ui_screen; menu_return_screen = ui_screen;
ui_screen = UI_MENU; ui_screen = UI_MENU;
@@ -787,11 +956,16 @@ static void ui_long_press(void)
history_head = 0; history_head = 0;
history_cursor = 0; history_cursor = 0;
text_page = 0; text_page = 0;
history_storage_save();
break; break;
case 1: case 1:
ui_screen = UI_LIVE; tune_candidate_hz = radio_frequency_hz;
ui_screen = UI_TUNE;
break; break;
case 2: case 2:
ui_screen = UI_LIVE;
break;
case 3:
ui_screen = UI_HISTORY; ui_screen = UI_HISTORY;
history_cursor = 0; history_cursor = 0;
text_page = 0; text_page = 0;
@@ -803,27 +977,84 @@ static void ui_long_press(void)
ui_render(); ui_render();
} }
static void ui_poll_button(void) static void ui_handle_button(ui_button_id_t button, bool long_press)
{ {
TickType_t now = xTaskGetTickCount(); if (button == BUTTON_BOOT) {
int level = gpio_get_level(PIN_BUTTON_BOOT); if (long_press)
if (level != button_last_level) {
button_last_level = level;
button_debounce_at = now;
return;
}
if (now - button_debounce_at < pdMS_TO_TICKS(30))
return;
if (level == 0 && !button_down) {
button_down = true;
button_pressed_at = now;
} else if (level != 0 && button_down) {
button_down = false;
if (now - button_pressed_at >= pdMS_TO_TICKS(700))
ui_long_press(); ui_long_press();
else else
ui_short_press(); ui_short_press();
return;
}
if (button == BUTTON_SELECT) {
if (ui_screen == UI_MENU || ui_screen == UI_TUNE) {
ui_long_press();
} else if (ui_screen == UI_HISTORY) {
ui_screen = UI_LIVE;
ui_note_activity();
ui_render();
} else {
ui_screen = UI_HISTORY;
history_cursor = 0;
text_page = 0;
ui_note_activity();
ui_render();
}
return;
}
ui_note_activity();
if (ui_screen == UI_MENU) {
if (button == BUTTON_UP)
menu_cursor = (menu_cursor + 4U) % 5U;
else
menu_cursor = (menu_cursor + 1U) % 5U;
} else if (ui_screen == UI_TUNE) {
if (button == BUTTON_UP) {
tune_candidate_hz = tune_candidate_hz <= TUNE_MIN_HZ + TUNE_STEP_HZ
? TUNE_MAX_HZ : tune_candidate_hz - TUNE_STEP_HZ;
} else {
tune_candidate_hz = tune_candidate_hz >= TUNE_MAX_HZ
? TUNE_MIN_HZ : tune_candidate_hz + TUNE_STEP_HZ;
}
} else if (history_count) {
if (ui_screen == UI_LIVE) {
ui_screen = UI_HISTORY;
history_cursor = 0;
} else if (button == BUTTON_UP) {
history_cursor = (history_cursor + 1U) % history_count;
} else {
history_cursor = (history_cursor + history_count - 1U)
% history_count;
}
text_page = 0;
}
ui_render();
}
static void ui_poll_button(void)
{
TickType_t now = xTaskGetTickCount();
for (unsigned i = 0; i < BUTTON_COUNT; i++) {
ui_button_state_t *button = &ui_buttons[i];
int level = gpio_get_level(button->pin);
if (level != button->last_level) {
button->last_level = level;
button->debounce_at = now;
continue;
}
if (now - button->debounce_at < pdMS_TO_TICKS(30))
continue;
if (level == 0 && !button->down) {
button->down = true;
button->pressed_at = now;
} else if (level != 0 && button->down) {
button->down = false;
bool long_press = now - button->pressed_at >= pdMS_TO_TICKS(700);
ui_handle_button((ui_button_id_t)i, long_press);
}
} }
} }
@@ -867,7 +1098,10 @@ static void ui_update_alert(void)
static void ui_hardware_init(void) static void ui_hardware_init(void)
{ {
gpio_config_t button_cfg = { gpio_config_t button_cfg = {
.pin_bit_mask = 1ULL << PIN_BUTTON_BOOT, .pin_bit_mask = (1ULL << PIN_BUTTON_BOOT) |
(1ULL << PIN_BUTTON_UP) |
(1ULL << PIN_BUTTON_DOWN) |
(1ULL << PIN_BUTTON_SELECT),
.mode = GPIO_MODE_INPUT, .mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE, .pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE,
@@ -898,9 +1132,78 @@ static void ui_hardware_init(void)
ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_cfg, &rmt_cfg, &neopixel)); ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_cfg, &rmt_cfg, &neopixel));
ESP_ERROR_CHECK(led_strip_clear(neopixel)); ESP_ERROR_CHECK(led_strip_clear(neopixel));
ESP_ERROR_CHECK(led_strip_refresh(neopixel)); ESP_ERROR_CHECK(led_strip_refresh(neopixel));
button_last_level = gpio_get_level(PIN_BUTTON_BOOT); TickType_t now = xTaskGetTickCount();
button_debounce_at = xTaskGetTickCount(); for (unsigned i = 0; i < BUTTON_COUNT; i++) {
ui_last_activity = button_debounce_at; ui_buttons[i].last_level = gpio_get_level(ui_buttons[i].pin);
ui_buttons[i].debounce_at = now;
}
ui_last_activity = now;
}
static void battery_adc_init(void)
{
#if BATTERY_MONITOR_ENABLE
adc_unit_t unit;
ESP_ERROR_CHECK(adc_oneshot_io_to_channel(PIN_BATTERY_ADC,
&unit,
&battery_adc_channel));
adc_oneshot_unit_init_cfg_t unit_config = {
.unit_id = unit,
.ulp_mode = ADC_ULP_MODE_DISABLE,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_config, &battery_adc_unit));
adc_oneshot_chan_cfg_t channel_config = {
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(battery_adc_unit,
battery_adc_channel,
&channel_config));
adc_cali_curve_fitting_config_t calibration_config = {
.unit_id = unit,
.chan = battery_adc_channel,
.atten = ADC_ATTEN_DB_12,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
esp_err_t err = adc_cali_create_scheme_curve_fitting(
&calibration_config, &battery_adc_calibration);
if (err != ESP_OK) {
ESP_LOGW(TAG, "Battery ADC calibration unavailable: %s",
esp_err_to_name(err));
return;
}
battery_adc_ready = true;
#endif
}
static void battery_adc_update(void)
{
#if BATTERY_MONITOR_ENABLE
if (!battery_adc_ready)
return;
int adc_pin_mv;
esp_err_t err = adc_oneshot_get_calibrated_result(battery_adc_unit,
battery_adc_calibration,
battery_adc_channel,
&adc_pin_mv);
if (err != ESP_OK)
return;
uint64_t divider_total = (uint64_t)BATTERY_DIVIDER_TOP_OHMS +
BATTERY_DIVIDER_BOTTOM_OHMS;
battery_voltage_mv = (uint32_t)(((uint64_t)adc_pin_mv * divider_total +
BATTERY_DIVIDER_BOTTOM_OHMS / 2U) /
BATTERY_DIVIDER_BOTTOM_OHMS);
battery_voltage_valid = true;
ESP_LOGI(TAG, "Battery: %u mV", (unsigned)battery_voltage_mv);
if (ui_screen == UI_LIVE)
ui_render();
#endif
} }
static void message_finish(void) static void message_finish(void)
@@ -1226,6 +1529,7 @@ static void start_sampler(void)
void app_main(void) void app_main(void)
{ {
ESP_LOGI(TAG, "ESP32-H2 POCSAG pager receiver"); ESP_LOGI(TAG, "ESP32-H2 POCSAG pager receiver");
history_storage_init();
gpio_config_t gdo_cfg = { gpio_config_t gdo_cfg = {
.pin_bit_mask = 1ULL << PIN_CC1101_GDO0, .pin_bit_mask = 1ULL << PIN_CC1101_GDO0,
@@ -1246,6 +1550,7 @@ void app_main(void)
setup_spi(); setup_spi();
oled_init(); oled_init();
ui_hardware_init(); ui_hardware_init();
battery_adc_init();
ui_screen = UI_LIVE; ui_screen = UI_LIVE;
ui_render(); ui_render();
@@ -1256,6 +1561,7 @@ void app_main(void)
start_sampler(); start_sampler();
TickType_t last_status = xTaskGetTickCount(); TickType_t last_status = xTaskGetTickCount();
TickType_t last_battery_read = last_status;
while (1) { while (1) {
while (sample_rd != sample_wr) { while (sample_rd != sample_wr) {
@@ -1269,6 +1575,11 @@ void app_main(void)
ui_update_alert(); ui_update_alert();
ui_idle_power_down(); ui_idle_power_down();
if (xTaskGetTickCount() - last_battery_read >= pdMS_TO_TICKS(5000)) {
last_battery_read = xTaskGetTickCount();
battery_adc_update();
}
if (xTaskGetTickCount() - last_status >= pdMS_TO_TICKS(5000)) { if (xTaskGetTickCount() - last_status >= pdMS_TO_TICKS(5000)) {
last_status = xTaskGetTickCount(); last_status = xTaskGetTickCount();
unsigned edge_period_avg = edge_period_count unsigned edge_period_avg = edge_period_count