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

+348 -37
View File
@@ -4,8 +4,8 @@
*
* Hardware:
* CC1101: SCLK=GPIO4, MOSI=GPIO5, MISO=GPIO0, CSN=GPIO1, GDO0=GPIO3
* SPI SSD1306 128x64: SCLK=GPIO4, MOSI=GPIO5, CS=GPIO13,
* DC=GPIO14, RESET=GPIO12
* SPI SSD1306 128x64: SCLK=GPIO4, MOSI=GPIO5, CS=GPIO10,
* DC=GPIO11, RESET=GPIO12
*
* Assumptions:
* - CC1101 module has a 26 MHz crystal.
@@ -34,6 +34,10 @@
#include "driver/spi_master.h"
#include "driver/gptimer.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 ---------------------------- */
@@ -49,16 +53,28 @@
#define PIN_OLED_DC GPIO_NUM_11
#define PIN_OLED_RESET GPIO_NUM_12
#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_NEOPIXEL GPIO_NUM_8
/* Change this one value to tune the receiver. Rebuild and flash afterwards. */
#define RF_FREQ_HZ 435000000UL
#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 OLED_IDLE_OFF_MS 60000U
#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_PERIOD_TICKS 26UL
#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 led_strip_handle_t neopixel;
static unsigned display_baud;
static uint32_t radio_frequency_hz = RF_FREQ_HZ;
static bool oled_is_on = true;
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,
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++)
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",
(unsigned long)RF_FREQ_HZ,
(unsigned long)radio_frequency_hz,
(unsigned long)actual_hz,
(unsigned long)CC1101_XTAL_HZ);
@@ -219,6 +245,18 @@ static void cc1101_init(void)
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 ------------------------------ */
static uint8_t fb[1024];
@@ -440,15 +478,82 @@ static pager_message_t history[HISTORY_COUNT];
static unsigned history_count = 0;
static unsigned history_head = 0;
static unsigned history_cursor;
static bool monitor_mode;
static bool monitor_mode = true;
static unsigned ui_screen;
static unsigned menu_cursor;
static bool live_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_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,
const char *text)
{
@@ -468,6 +573,7 @@ static void history_add(uint32_t ric, unsigned baud, unsigned function,
history_count++;
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);
@@ -584,19 +690,40 @@ static const char numeric_chars[16] = {
enum {
UI_LIVE,
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 alert_toggle_at;
static int button_last_level = 1;
static bool button_down;
static bool alert_led_on;
static bool alert_output_state;
static bool alert_output_initialized;
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)
{
@@ -638,9 +765,21 @@ static void oled_draw_text_page(int y, const char *text, unsigned page)
static void ui_draw_live(void)
{
char line[24];
char line[32];
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);
#endif
oled_text(0, 9, line);
if (live_page && message_active) {
@@ -700,15 +839,20 @@ static void ui_draw_menu(void)
static const uint8_t cursor_rows[7] = {
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[] = {
monitor_mode ? "FILTER: ALL RIC" : "FILTER: MY RIC",
frequency_item,
"LIVE DISPLAY",
"MESSAGE HISTORY",
"RETURN"
};
oled_text(0, 0, "PAGER MENU");
for (unsigned i = 0; i < 4; i++) {
int y = 11 + (int)i * 10;
for (unsigned i = 0; i < 5; i++) {
int y = 8 + (int)i * 8;
if (i == menu_cursor) {
for (int row = 0; row < 7; row++) {
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(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)
@@ -731,6 +888,8 @@ static void ui_render(void)
ui_draw_menu();
else if (ui_screen == UI_HISTORY)
ui_draw_history();
else if (ui_screen == UI_TUNE)
ui_draw_tune();
else
ui_draw_live();
oled_flush();
@@ -745,7 +904,11 @@ static void ui_short_press(void)
{
ui_note_activity();
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) {
if (live_page && message_active &&
text_page + 1U < text_page_count(message_len)) {
@@ -772,6 +935,12 @@ static void ui_short_press(void)
static void ui_long_press(void)
{
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) {
menu_return_screen = ui_screen;
ui_screen = UI_MENU;
@@ -787,11 +956,16 @@ static void ui_long_press(void)
history_head = 0;
history_cursor = 0;
text_page = 0;
history_storage_save();
break;
case 1:
ui_screen = UI_LIVE;
tune_candidate_hz = radio_frequency_hz;
ui_screen = UI_TUNE;
break;
case 2:
ui_screen = UI_LIVE;
break;
case 3:
ui_screen = UI_HISTORY;
history_cursor = 0;
text_page = 0;
@@ -803,27 +977,84 @@ static void ui_long_press(void)
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();
int level = gpio_get_level(PIN_BUTTON_BOOT);
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))
if (button == BUTTON_BOOT) {
if (long_press)
ui_long_press();
else
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)
{
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,
.pull_up_en = GPIO_PULLUP_ENABLE,
.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_clear(neopixel));
ESP_ERROR_CHECK(led_strip_refresh(neopixel));
button_last_level = gpio_get_level(PIN_BUTTON_BOOT);
button_debounce_at = xTaskGetTickCount();
ui_last_activity = button_debounce_at;
TickType_t now = xTaskGetTickCount();
for (unsigned i = 0; i < BUTTON_COUNT; i++) {
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)
@@ -1226,6 +1529,7 @@ static void start_sampler(void)
void app_main(void)
{
ESP_LOGI(TAG, "ESP32-H2 POCSAG pager receiver");
history_storage_init();
gpio_config_t gdo_cfg = {
.pin_bit_mask = 1ULL << PIN_CC1101_GDO0,
@@ -1246,6 +1550,7 @@ void app_main(void)
setup_spi();
oled_init();
ui_hardware_init();
battery_adc_init();
ui_screen = UI_LIVE;
ui_render();
@@ -1256,6 +1561,7 @@ void app_main(void)
start_sampler();
TickType_t last_status = xTaskGetTickCount();
TickType_t last_battery_read = last_status;
while (1) {
while (sample_rd != sample_wr) {
@@ -1269,6 +1575,11 @@ void app_main(void)
ui_update_alert();
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)) {
last_status = xTaskGetTickCount();
unsigned edge_period_avg = edge_period_count