/* * ESP32-H2 + CC1101 POCSAG pager receiver * Native ESP-IDF; no third-party libraries. * * Hardware: * CC1101: SCLK=GPIO4, MOSI=GPIO5, MISO=GPIO0, CSN=GPIO1, GDO0=GPIO3 * SPI SSD1306 128x64: SCLK=GPIO4, MOSI=GPIO5, CS=GPIO10, * DC=GPIO11, RESET=GPIO12 * * Assumptions: * - CC1101 module has a 26 MHz crystal. * - RF centre frequency is set with RF_FREQ_HZ below. * - OLED is a 7-pin SPI SSD1306 (not I2C, not SH1106). * * This is an experimental receiver. It does not guarantee reception * under weak/noisy signals. Validate RF frequency, deviation, polarity, * and timing against a known-good POCSAG transmitter. */ #include #include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_err.h" #include "esp_log.h" #include "esp_attr.h" #include "driver/gpio.h" #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" #include "pager_config.h" #include "pager_presets.h" #include "pager_tx.h" #include "pocsag_bch.h" static const char *TAG = "pager"; /* ---------------------------- SPI devices ---------------------------- */ static spi_device_handle_t cc_dev; 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; static uint32_t my_ric = MY_RIC; static unsigned my_ric_preset_index; static unsigned tx_ric_preset_index; static unsigned tx_message_index; static TickType_t tx_last_sent; static bool tx_has_sent; static const char *tx_status_text = "HOLD TO SEND"; static bool oled_inverted; static unsigned rx_flash_toggles; static TickType_t rx_flash_next_toggle; static bool hold_cue_visible; static uint8_t hold_cue_button; #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) { spi_transaction_t t = { .length = len * 8, .tx_buffer = data, }; return spi_device_polling_transmit(dev, &t); } static esp_err_t spi_txrx(spi_device_handle_t dev, const uint8_t *tx, uint8_t *rx, size_t len) { spi_transaction_t t = { .length = len * 8, .tx_buffer = tx, .rx_buffer = rx, }; return spi_device_polling_transmit(dev, &t); } static void setup_spi(void) { spi_bus_config_t bus = { .mosi_io_num = PIN_SPI_MOSI, .miso_io_num = PIN_SPI_MISO, .sclk_io_num = PIN_SPI_SCLK, .quadwp_io_num = -1, .quadhd_io_num = -1, .max_transfer_sz = SPI_TRANSFER_CHUNK, }; ESP_ERROR_CHECK(spi_bus_initialize(SPI_HOST_USED, &bus, SPI_DMA_DISABLED)); spi_device_interface_config_t cc_cfg = { .clock_speed_hz = 1000000, .mode = 0, .spics_io_num = PIN_CC1101_CS, .queue_size = 1, }; ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST_USED, &cc_cfg, &cc_dev)); spi_device_interface_config_t oled_cfg = { .clock_speed_hz = 4000000, .mode = 0, .spics_io_num = PIN_OLED_CS, .queue_size = 1, }; ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST_USED, &oled_cfg, &oled_dev)); } /* ------------------------------- CC1101 ------------------------------- */ static void cc_write(uint8_t reg, uint8_t val) { uint8_t b[2] = { reg, val }; ESP_ERROR_CHECK(spi_tx(cc_dev, b, sizeof(b))); } static uint8_t cc_read_status(uint8_t reg) { uint8_t tx[2] = { (uint8_t)(reg | 0xC0), 0 }; uint8_t rx[2] = { 0 }; ESP_ERROR_CHECK(spi_txrx(cc_dev, tx, rx, sizeof(tx))); return rx[1]; } static void cc_strobe(uint8_t cmd) { ESP_ERROR_CHECK(spi_tx(cc_dev, &cmd, 1)); } /* CC1101 frequency word: FREQ = RF * 2^16 / crystal frequency. */ static uint32_t cc1101_set_frequency(uint32_t frequency_hz) { uint64_t numerator = ((uint64_t)frequency_hz << 16) + (CC1101_XTAL_HZ / 2U); uint32_t word = (uint32_t)(numerator / CC1101_XTAL_HZ); cc_write(0x0D, (uint8_t)(word >> 16)); cc_write(0x0E, (uint8_t)(word >> 8)); cc_write(0x0F, (uint8_t)word); return (uint32_t)(((uint64_t)word * CC1101_XTAL_HZ + 32768U) >> 16); } static void cc1101_init(void) { ESP_LOGI(TAG, "Resetting CC1101"); cc_strobe(0x30); /* SRES */ vTaskDelay(pdMS_TO_TICKS(10)); /* * Async serial output on GDO0. Packet engine is bypassed. * MDMCFG4/3 select a wide receiver bandwidth and a 1200-baud * modem rate; software validates the POCSAG preamble rate. */ const uint8_t regs[][2] = { {0x00, 0x0D}, /* IOCFG2 */ {0x01, 0x2E}, /* IOCFG1 */ {0x02, 0x0D}, /* IOCFG0: async data */ {0x06, 0xFF}, /* PKTLEN */ {0x07, 0x04}, /* PKTCTRL1 */ {0x08, 0x32}, /* PKTCTRL0: async serial */ {0x0B, 0x06}, /* FSCTRL1 */ {0x0C, 0x00}, /* FSCTRL0 */ {0x10, 0xB5}, /* MDMCFG4: 1200 baud, wide channel BW */ {0x11, 0x83}, /* MDMCFG3 */ {0x12, 0x00}, /* MDMCFG2: 2-FSK */ {0x13, 0x22}, /* MDMCFG1 */ {0x14, 0xF8}, /* MDMCFG0 */ {0x15, 0x14}, /* DEVIATN */ {0x18, 0x18}, /* MCSM0 */ {0x19, 0x16}, /* FOCCFG */ {0x1A, 0x6C}, /* BSCFG */ {0x1B, 0x43}, /* AGCCTRL2 */ {0x1C, 0x40}, /* AGCCTRL1 */ {0x1D, 0x91}, /* AGCCTRL0 */ }; 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(radio_frequency_hz); ESP_LOGI(TAG, "RF target=%lu Hz, actual=%lu Hz, crystal=%lu Hz", (unsigned long)radio_frequency_hz, (unsigned long)actual_hz, (unsigned long)CC1101_XTAL_HZ); cc_strobe(0x33); /* SCAL */ vTaskDelay(pdMS_TO_TICKS(5)); cc_strobe(0x3A); /* SFRX */ cc_strobe(0x34); /* SRX */ ESP_LOGI(TAG, "CC1101 PARTNUM=0x%02X VERSION=0x%02X", 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]; /* Small 5x7 font for uppercase letters, digits and common punctuation. * Lowercase input is converted to uppercase on display. */ static const uint8_t font[][5] = { {0,0,0,0,0}, /* space */ {0,0,0x5F,0,0}, /* ! */ {0x03,0x00,0x03,0x00,0}, /* " */ {0x14,0x7F,0x14,0x7F,0x14}, /* # */ {0x24,0x2A,0x7F,0x2A,0x12}, /* $ */ {0x23,0x13,0x08,0x64,0x62}, /* % */ {0x36,0x49,0x55,0x22,0x50}, /* & */ {0x05,0x03,0,0,0}, /* ' */ {0x1C,0x22,0x41,0,0}, /* ( */ {0x41,0x22,0x1C,0,0}, /* ) */ {0x14,0x08,0x3E,0x08,0x14}, /* * */ {0x08,0x08,0x3E,0x08,0x08}, /* + */ {0x50,0x30,0,0,0}, /* , */ {0x08,0x08,0x08,0x08,0x08}, /* - */ {0x60,0x60,0,0,0}, /* . */ {0x20,0x10,0x08,0x04,0x02}, /* / */ {0x3E,0x51,0x49,0x45,0x3E}, /* 0 */ {0x00,0x42,0x7F,0x40,0x00}, /* 1 */ {0x42,0x61,0x51,0x49,0x46}, /* 2 */ {0x21,0x41,0x45,0x4B,0x31}, /* 3 */ {0x18,0x14,0x12,0x7F,0x10}, /* 4 */ {0x27,0x45,0x45,0x45,0x39}, /* 5 */ {0x3C,0x4A,0x49,0x49,0x30}, /* 6 */ {0x01,0x71,0x09,0x05,0x03}, /* 7 */ {0x36,0x49,0x49,0x49,0x36}, /* 8 */ {0x06,0x49,0x49,0x29,0x1E}, /* 9 */ {0x7E,0x11,0x11,0x11,0x7E}, /* A */ {0x7F,0x49,0x49,0x49,0x36}, /* B */ {0x3E,0x41,0x41,0x41,0x22}, /* C */ {0x7F,0x41,0x41,0x22,0x1C}, /* D */ {0x7F,0x49,0x49,0x49,0x41}, /* E */ {0x7F,0x09,0x09,0x09,0x01}, /* F */ {0x3E,0x41,0x49,0x49,0x7A}, /* G */ {0x7F,0x08,0x08,0x08,0x7F}, /* H */ {0x00,0x41,0x7F,0x41,0x00}, /* I */ {0x20,0x40,0x41,0x3F,0x01}, /* J */ {0x7F,0x08,0x14,0x22,0x41}, /* K */ {0x7F,0x40,0x40,0x40,0x40}, /* L */ {0x7F,0x02,0x0C,0x02,0x7F}, /* M */ {0x7F,0x04,0x08,0x10,0x7F}, /* N */ {0x3E,0x41,0x41,0x41,0x3E}, /* O */ {0x7F,0x09,0x09,0x09,0x06}, /* P */ {0x3E,0x41,0x51,0x21,0x5E}, /* Q */ {0x7F,0x09,0x19,0x29,0x46}, /* R */ {0x46,0x49,0x49,0x49,0x31}, /* S */ {0x01,0x01,0x7F,0x01,0x01}, /* T */ {0x3F,0x40,0x40,0x40,0x3F}, /* U */ {0x1F,0x20,0x40,0x20,0x1F}, /* V */ {0x3F,0x40,0x38,0x40,0x3F}, /* W */ {0x63,0x14,0x08,0x14,0x63}, /* X */ {0x07,0x08,0x70,0x08,0x07}, /* Y */ {0x61,0x51,0x49,0x45,0x43}, /* Z */ {0x7F,0x41,0x41,0,0}, /* [ */ {0x02,0x04,0x08,0x10,0x20}, /* backslash */ {0,0x41,0x41,0x7F,0}, /* ] */ {0x04,0x02,0x01,0x02,0x04}, /* ^ */ {0x40,0x40,0x40,0x40,0x40}, /* _ */ {0x01,0x02,0x04,0,0}, /* ` */ {0x20,0x54,0x54,0x54,0x78}, /* a */ {0x7F,0x48,0x44,0x44,0x38}, /* b */ {0x38,0x44,0x44,0x44,0x20}, /* c */ {0x38,0x44,0x44,0x48,0x7F}, /* d */ {0x38,0x54,0x54,0x54,0x18}, /* e */ {0x08,0x7E,0x09,0x01,0x02}, /* f */ {0x0C,0x52,0x52,0x52,0x3E}, /* g */ {0x7F,0x08,0x04,0x04,0x78}, /* h */ {0,0x44,0x7D,0x40,0}, /* i */ {0x20,0x40,0x44,0x3D,0}, /* j */ {0x7F,0x10,0x28,0x44,0}, /* k */ {0,0x41,0x7F,0x40,0}, /* l */ {0x7C,0x04,0x18,0x04,0x78}, /* m */ {0x7C,0x08,0x04,0x04,0x78}, /* n */ {0x38,0x44,0x44,0x44,0x38}, /* o */ {0x7C,0x14,0x14,0x14,0x08}, /* p */ {0x08,0x14,0x14,0x18,0x7C}, /* q */ {0x7C,0x08,0x04,0x04,0x08}, /* r */ {0x48,0x54,0x54,0x54,0x20}, /* s */ {0x04,0x3F,0x44,0x40,0x20}, /* t */ {0x3C,0x40,0x40,0x20,0x7C}, /* u */ {0x1C,0x20,0x40,0x20,0x1C}, /* v */ {0x3C,0x40,0x30,0x40,0x3C}, /* w */ {0x44,0x28,0x10,0x28,0x44}, /* x */ {0x0C,0x50,0x50,0x50,0x3C}, /* y */ {0x44,0x64,0x54,0x4C,0x44}, /* z */ }; static void oled_cmd(uint8_t cmd) { gpio_set_level(PIN_OLED_DC, 0); ESP_ERROR_CHECK(spi_tx(oled_dev, &cmd, 1)); } static void oled_power_on(void) { if (!oled_is_on) { oled_cmd(0xAF); oled_is_on = true; } } static void oled_power_off(void) { if (oled_is_on) { oled_cmd(0xAE); oled_is_on = false; } } static void oled_set_inverted(bool inverted) { if (oled_inverted == inverted) return; oled_cmd(inverted ? 0xA7 : 0xA6); oled_inverted = inverted; } static void oled_data(const uint8_t *data, size_t len) { gpio_set_level(PIN_OLED_DC, 1); while (len > 0) { size_t chunk = len > SPI_TRANSFER_CHUNK ? SPI_TRANSFER_CHUNK : len; ESP_ERROR_CHECK(spi_tx(oled_dev, data, chunk)); data += chunk; len -= chunk; } } static void oled_init(void) { gpio_config_t out = { .pin_bit_mask = (1ULL << PIN_OLED_DC) | (1ULL << PIN_OLED_RESET), .mode = GPIO_MODE_OUTPUT, }; ESP_ERROR_CHECK(gpio_config(&out)); gpio_set_level(PIN_OLED_RESET, 0); vTaskDelay(pdMS_TO_TICKS(30)); gpio_set_level(PIN_OLED_RESET, 1); vTaskDelay(pdMS_TO_TICKS(30)); const uint8_t init_seq[] = { 0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40, 0x8D, 0x14, 0x20, 0x00, 0xA1, 0xC8, 0xDA, 0x12, 0x81, 0x7F, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6, 0xAF }; for (size_t i = 0; i < sizeof(init_seq); i++) oled_cmd(init_seq[i]); memset(fb, 0, sizeof(fb)); } static void oled_flush(void) { oled_cmd(0x21); oled_cmd(0); oled_cmd(127); oled_cmd(0x22); oled_cmd(0); oled_cmd(7); oled_data(fb, sizeof(fb)); } static void oled_clear(void) { memset(fb, 0, sizeof(fb)); } static void oled_pixel(int x, int y) { if (x < 0 || x >= 128 || y < 0 || y >= 64) return; fb[(y / 8) * 128 + x] |= (uint8_t)(1U << (y & 7)); } static void oled_char(int x, int y, char c) { if (c >= 'a' && c <= 'z') c = (char)(c - 'a' + 'A'); unsigned idx; if (c == ' ') idx = 0; else if (c >= '!' && c <= '/') idx = (unsigned)(c - ' '); else if (c >= '0' && c <= '9') idx = 16U + (unsigned)(c - '0'); else if (c >= 'A' && c <= 'Z') idx = 26U + (unsigned)(c - 'A'); else if (c >= '[' && c <= '`') idx = 52U + (unsigned)(c - '['); else idx = 0; if (idx >= sizeof(font) / sizeof(font[0])) idx = 0; for (int col = 0; col < 5; col++) { uint8_t bits = font[idx][col]; for (int row = 0; row < 7; row++) if (bits & (1U << row)) oled_pixel(x + col, y + row); } } static void oled_text(int x, int y, const char *s) { while (*s && x <= 122) { oled_char(x, y, *s++); x += 6; } } /* -------------------------- Message history -------------------------- */ typedef struct { uint32_t ric; unsigned baud; unsigned function; char text[MESSAGE_SIZE]; } pager_message_t; 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 = 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" #define OWN_RIC_NVS_KEY "own_ric" 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 tx_send_selected(void); static void ui_select_next_my_ric(void) { my_ric_preset_index = (my_ric_preset_index + 1U) % ric_preset_count; my_ric = ric_presets[my_ric_preset_index].ric; if (history_nvs_ready) { ESP_ERROR_CHECK(nvs_set_u8(history_nvs, OWN_RIC_NVS_KEY, (uint8_t)my_ric_preset_index)); ESP_ERROR_CHECK(nvs_commit(history_nvs)); } } 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; uint8_t saved_ric_index; if (nvs_get_u8(history_nvs, OWN_RIC_NVS_KEY, &saved_ric_index) == ESP_OK && saved_ric_index < ric_preset_count) { my_ric_preset_index = saved_ric_index; my_ric = ric_presets[my_ric_preset_index].ric; } 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) { if (!text || !text[0]) return; if (!monitor_mode && ric != my_ric) return; pager_message_t *m = &history[history_head]; m->ric = ric; m->baud = baud; m->function = function; strlcpy(m->text, text, sizeof(m->text)); history_head = (history_head + 1) % HISTORY_COUNT; if (history_count < HISTORY_COUNT) 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); ui_render(); } #define POCSAG_SYNC 0x7CD215D8UL #define POCSAG_IDLE 0x7A89C197UL /* ---------------------------- POCSAG decode --------------------------- */ typedef enum { RATE_UNKNOWN = 0, RATE_512 = 512, RATE_1200 = 1200, RATE_2400 = 2400 } baud_t; static baud_t current_baud = RATE_UNKNOWN; static uint32_t bit_period_q16; static uint32_t phase_q16; static uint32_t edge_samples; static unsigned hit512, hit1200, hit2400; static uint8_t last_sample; static uint32_t observed_edges; static uint32_t edge_period_sum; static uint32_t edge_period_min = UINT32_MAX; static uint32_t edge_period_max; static unsigned edge_period_count; static uint32_t sync_shift; static unsigned sync_bits; static bool batch_locked; static bool invert_bits; static uint32_t codeword_shift; static unsigned codeword_bits; static unsigned batch_word_index; static uint32_t current_ric; static unsigned current_function; static bool message_active; static char message[MESSAGE_SIZE]; static size_t message_len; static uint32_t alpha_acc; static unsigned alpha_nbits; static unsigned numeric_nibble_count; static const char numeric_chars[16] = { '0','1','2','3','4','5','6','7', '8','9','U',' ','-','[',']','?' }; enum { UI_LIVE, UI_HISTORY, UI_MENU, UI_TUNE, UI_TX }; static TickType_t page_alert_until; static TickType_t alert_toggle_at; 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; static unsigned menu_scroll; 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) { if (age >= history_count) return NULL; unsigned index = (history_head + HISTORY_COUNT - 1U - age) % HISTORY_COUNT; return &history[index]; } static void ui_note_activity(void) { ui_last_activity = xTaskGetTickCount(); oled_power_on(); } static unsigned text_page_count(size_t length) { return length ? (unsigned)((length + 83U) / 84U) : 1U; } static void oled_draw_text_page(int y, const char *text, unsigned page) { size_t offset = (size_t)page * 84U; size_t length = strlen(text); if (offset >= length) return; text += offset; for (unsigned row = 0; row < 4U && *text; row++) { char line[22]; size_t count = strnlen(text, 21U); memcpy(line, text, count); line[count] = '\0'; oled_text(0, y + (int)row * 8, line); text += count; } } static void ui_draw_live(void) { char line[32]; oled_text(0, 0, monitor_mode ? "POCSAG MONITOR" : "MY PAGER"); #if BATTERY_MONITOR_ENABLE if (battery_voltage_valid) { unsigned battery_percent = battery_voltage_mv <= 3300U ? 0U : battery_voltage_mv >= 4200U ? 100U : (unsigned)(((battery_voltage_mv - 3300U) * 100U) / 900U); snprintf(line, sizeof(line), "RIC %lu BAT %u%%", (unsigned long)my_ric, battery_percent); } 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) { unsigned pages = text_page_count(message_len); if (text_page >= pages) text_page = 0; snprintf(line, sizeof(line), "RX %lu F%u %u/%u", (unsigned long)current_ric, current_function, text_page + 1U, pages); oled_text(0, 18, line); oled_draw_text_page(27, message, text_page); return; } pager_message_t *latest = history_at_age(0); if (latest) { snprintf(line, sizeof(line), "LAST %lu F%u", (unsigned long)latest->ric, latest->function); oled_text(0, 20, line); unsigned pages = text_page_count(strlen(latest->text)); if (text_page >= pages) text_page = 0; snprintf(line, sizeof(line), "PAGE %u/%u", text_page + 1U, pages); oled_text(78, 20, line); oled_draw_text_page(31, latest->text, text_page); } else { oled_text(0, 25, display_baud ? "WAITING FOR PAGE" : "WAITING FOR RF"); } } static void ui_draw_history(void) { char line[24]; pager_message_t *entry = history_at_age(history_cursor); oled_text(0, 0, "MESSAGE HISTORY"); if (!entry) { oled_text(0, 18, "NO SAVED PAGES"); oled_text(0, 52, "HOLD FOR MENU"); return; } snprintf(line, sizeof(line), "%u/%u RIC %lu", history_cursor + 1U, history_count, (unsigned long)entry->ric); oled_text(0, 10, line); snprintf(line, sizeof(line), "F%u %u BAUD", entry->function, entry->baud); oled_text(0, 19, line); unsigned pages = text_page_count(strlen(entry->text)); if (text_page >= pages) text_page = 0; snprintf(line, sizeof(line), "PAGE %u/%u", text_page + 1U, pages); oled_text(78, 19, line); oled_draw_text_page(30, entry->text, text_page); } 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)); char own_ric_item[24]; snprintf(own_ric_item, sizeof(own_ric_item), "MY RIC %lu", (unsigned long)my_ric); const char *items[] = { monitor_mode ? "FILTER: ALL RIC" : "FILTER: MY RIC", frequency_item, own_ric_item, "TX TEST PRESETS", "LIVE DISPLAY", "MESSAGE HISTORY", "RETURN" }; oled_text(0, 0, "PAGER MENU"); for (unsigned row = 0; row < 5U; row++) { unsigned item = menu_scroll + row; if (item >= 7U) break; int y = 8 + (int)row * 8; if (item == menu_cursor) { for (int row = 0; row < 7; row++) { for (int col = 0; col < 5; col++) { if (cursor_rows[row] & (1U << col)) oled_pixel(col, y + row); } } } oled_text(8, y, items[item]); } 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_draw_tx(void) { char line[24]; const ric_preset_t *target = &ric_presets[tx_ric_preset_index]; const tx_message_preset_t *preset = &tx_message_presets[tx_message_index]; oled_text(0, 0, POCSAG_TX_ENABLE ? "TX TEST PRESETS" : "TX DISABLED BUILD"); snprintf(line, sizeof(line), "TO %s", target->label); oled_text(0, 12, line); snprintf(line, sizeof(line), "RIC %lu", (unsigned long)target->ric); oled_text(0, 21, line); snprintf(line, sizeof(line), "MSG %s", preset->label); oled_text(0, 27, line); oled_text(0, 36, preset->system_status ? "LIVE SYSTEM SNAPSHOT" : preset->text); oled_text(0, 45, "UP/DN RIC; SEL MSG"); oled_text(0, 55, POCSAG_TX_ENABLE ? tx_status_text : "TX OFF IN BUILD CONFIG"); } static void ui_render(void) { if (!oled_is_on) oled_power_on(); oled_clear(); if (ui_screen == UI_MENU) ui_draw_menu(); else if (ui_screen == UI_HISTORY) ui_draw_history(); else if (ui_screen == UI_TUNE) ui_draw_tune(); else if (ui_screen == UI_TX) ui_draw_tx(); else ui_draw_live(); if (hold_cue_visible) { memset(&fb[7U * 128U], 0, 128U); oled_text(0, 56, hold_cue_button == BUTTON_BOOT ? "RELEASE TO CONTINUE" : "RELEASE TO SELECT"); } oled_flush(); } static void oled_draw_history(void) { ui_render(); } static void ui_move_menu(int direction) { menu_cursor = direction < 0 ? (menu_cursor + 6U) % 7U : (menu_cursor + 1U) % 7U; if (menu_cursor < menu_scroll) menu_scroll = menu_cursor; else if (menu_cursor >= menu_scroll + 5U) menu_scroll = menu_cursor - 4U; } static void ui_next_tx_message(void) { tx_message_index = (tx_message_index + 1U) % tx_message_preset_count; } static void ui_short_press(void) { ui_note_activity(); if (ui_screen == UI_MENU) { ui_move_menu(1); } 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_TX) { ui_next_tx_message(); } else if (ui_screen == UI_LIVE) { if (live_page && message_active && text_page + 1U < text_page_count(message_len)) { text_page++; } else { ui_screen = UI_HISTORY; history_cursor = 0; text_page = 0; } } else if (history_count) { pager_message_t *entry = history_at_age(history_cursor); if (entry && text_page + 1U < text_page_count(strlen(entry->text))) { text_page++; } else { history_cursor = (history_cursor + 1U) % history_count; text_page = 0; } } else { ui_screen = UI_LIVE; } ui_render(); } static void ui_long_press(void) { ui_note_activity(); if (ui_screen == UI_TX) { tx_send_selected(); ui_render(); return; } 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; menu_cursor = 0; ui_render(); return; } switch (menu_cursor) { case 0: monitor_mode = !monitor_mode; history_count = 0; history_head = 0; history_cursor = 0; text_page = 0; history_storage_save(); break; case 1: tune_candidate_hz = radio_frequency_hz; ui_screen = UI_TUNE; break; case 2: ui_select_next_my_ric(); break; case 3: tx_ric_preset_index = my_ric_preset_index; tx_message_index = 0; ui_screen = UI_TX; break; case 4: ui_screen = UI_LIVE; break; case 5: ui_screen = UI_HISTORY; history_cursor = 0; text_page = 0; break; default: ui_screen = menu_return_screen; break; } ui_render(); } static void ui_handle_button(ui_button_id_t button, bool long_press) { if (button == BUTTON_BOOT) { if (long_press) ui_long_press(); else ui_short_press(); return; } if (button == BUTTON_SELECT) { if (ui_screen == UI_TX) { if (long_press) tx_send_selected(); else ui_next_tx_message(); ui_note_activity(); ui_render(); } else 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) ui_move_menu(-1); else ui_move_menu(1); } 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 (ui_screen == UI_TX) { if (button == BUTTON_UP) tx_ric_preset_index = (tx_ric_preset_index + ric_preset_count - 1U) % ric_preset_count; else tx_ric_preset_index = (tx_ric_preset_index + 1U) % ric_preset_count; } 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; if (hold_cue_visible && hold_cue_button == i) { hold_cue_visible = false; ui_render(); } bool long_press = now - button->pressed_at >= pdMS_TO_TICKS(700); ui_handle_button((ui_button_id_t)i, long_press); } else if (level == 0 && button->down && (i == BUTTON_BOOT || i == BUTTON_SELECT) && !hold_cue_visible && now - button->pressed_at >= pdMS_TO_TICKS(700)) { hold_cue_visible = true; hold_cue_button = (uint8_t)i; ui_note_activity(); ui_render(); } } } static void ui_idle_power_down(void) { if (oled_is_on && xTaskGetTickCount() - ui_last_activity >= pdMS_TO_TICKS(OLED_IDLE_OFF_MS)) oled_power_off(); } static void ui_start_own_page_alert(void) { TickType_t now = xTaskGetTickCount(); page_alert_until = now + pdMS_TO_TICKS(OWN_PAGE_ALERT_MS); alert_toggle_at = now; alert_led_on = false; } static void ui_start_display_flash(void) { if (!oled_is_on) oled_power_on(); oled_set_inverted(true); rx_flash_toggles = RX_DISPLAY_FLASHES; rx_flash_next_toggle = xTaskGetTickCount() + pdMS_TO_TICKS(RX_DISPLAY_FLASH_MS); } static void ui_update_display_flash(void) { TickType_t now = xTaskGetTickCount(); if (!rx_flash_toggles || (int32_t)(now - rx_flash_next_toggle) < 0) return; oled_set_inverted(!oled_inverted); rx_flash_toggles--; rx_flash_next_toggle = now + pdMS_TO_TICKS(RX_DISPLAY_FLASH_MS); } static void ui_update_alert(void) { TickType_t now = xTaskGetTickCount(); if ((int32_t)(now - page_alert_until) >= 0) { alert_led_on = false; } else if ((int32_t)(now - alert_toggle_at) >= 0) { alert_led_on = !alert_led_on; alert_toggle_at = now + pdMS_TO_TICKS(150); } bool changed = !alert_output_initialized || alert_led_on != alert_output_state; if (changed) { gpio_set_level(PIN_STATUS_LED, alert_led_on ? 1 : 0); if (alert_led_on) ESP_ERROR_CHECK(led_strip_set_pixel(neopixel, 0, 255, 24, 0)); else ESP_ERROR_CHECK(led_strip_clear(neopixel)); ESP_ERROR_CHECK(led_strip_refresh(neopixel)); alert_output_state = alert_led_on; alert_output_initialized = true; } } static void ui_hardware_init(void) { gpio_config_t button_cfg = { .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, .intr_type = GPIO_INTR_DISABLE, }; ESP_ERROR_CHECK(gpio_config(&button_cfg)); gpio_config_t led_cfg = { .pin_bit_mask = 1ULL << PIN_STATUS_LED, .mode = GPIO_MODE_OUTPUT, }; ESP_ERROR_CHECK(gpio_config(&led_cfg)); gpio_set_level(PIN_STATUS_LED, 0); led_strip_config_t strip_cfg = { .strip_gpio_num = PIN_NEOPIXEL, .max_leds = 1, .led_model = LED_MODEL_WS2812, .color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB, .flags.invert_out = false, }; led_strip_rmt_config_t rmt_cfg = { .clk_src = RMT_CLK_SRC_DEFAULT, .resolution_hz = 10000000, .mem_block_symbols = 64, .flags.with_dma = false, }; 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)); 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) { if (message_active && message_len) { message[message_len] = '\0'; history_add(current_ric, (unsigned)current_baud, current_function, message); oled_draw_history(); } message_active = false; current_ric = 0; current_function = 0; message_len = 0; memset(message, 0, sizeof(message)); alpha_acc = 0; alpha_nbits = 0; numeric_nibble_count = 0; live_page = false; text_page = 0; ui_render(); } static void message_abort(void) { message_active = false; current_ric = 0; current_function = 0; message_len = 0; memset(message, 0, sizeof(message)); alpha_acc = 0; alpha_nbits = 0; numeric_nibble_count = 0; live_page = false; } static void append_message_char(char c) { if (message_len < MESSAGE_SIZE - 1) message[message_len++] = c; } static void decode_numeric(uint32_t payload) { for (int shift = 16; shift >= 0; shift -= 4) { unsigned nibble = (payload >> shift) & 0xF; char c = numeric_chars[nibble]; /* POCSAG numeric filler/padding. */ if (nibble == 0xF) continue; append_message_char(c); numeric_nibble_count++; } } static void decode_alpha(uint32_t payload) { /* Codewords arrive MSB first, but each 7-bit ASCII character is transmitted least-significant bit first. Keep the character accumulator across codeword boundaries. */ for (int bit = 19; bit >= 0; bit--) { unsigned incoming = (payload >> bit) & 1U; alpha_acc |= incoming << alpha_nbits; alpha_nbits++; if (alpha_nbits == 7U) { unsigned ch = alpha_acc & 0x7FU; alpha_acc = 0; alpha_nbits = 0; if (ch == 0x0DU || ch == 0x0AU) append_message_char(' '); else if (ch >= 0x20U && ch <= 0x7EU) append_message_char((char)ch); } } } static void decode_codeword(uint32_t raw, unsigned slot) { if (raw == POCSAG_IDLE) { message_finish(); return; } unsigned fixed = 0; if (!pocsag_bch_correct(&raw, &fixed)) { ESP_LOGW(TAG, "BCH failure, raw=%08lX", (unsigned long)raw); message_abort(); batch_locked = false; sync_bits = 0; sync_shift = 0; codeword_bits = 0; codeword_shift = 0; return; } if (fixed) ESP_LOGD(TAG, "Corrected %u bit(s)", fixed); if (raw == POCSAG_IDLE) { message_finish(); return; } unsigned is_message = (raw >> 31) & 1U; uint32_t payload = (raw >> 11) & 0xFFFFFU; if (!is_message) { message_finish(); unsigned address = payload >> 2; current_function = payload & 3U; unsigned frame = (slot / 2U) & 7U; current_ric = (address << 3) | frame; /* * POCSAG function bits: * 00 / 01: numeric format * 10 / 11: alphanumeric format * * A tone-only page is represented by an address with * no subsequent message codewords, not by function 00. */ message_active = true; message_len = 0; alpha_acc = 0; alpha_nbits = 0; numeric_nibble_count = 0; live_page = monitor_mode || current_ric == my_ric; text_page = 0; if (current_ric == my_ric) ui_start_own_page_alert(); if (live_page) { ui_screen = UI_LIVE; ui_note_activity(); ui_start_display_flash(); } if (monitor_mode || current_ric == my_ric) { ESP_LOGI(TAG, "Address RIC=%lu function=%u", (unsigned long)current_ric, current_function); } if (live_page) ui_render(); } else if (message_active) { if (current_function <= 1) decode_numeric(payload); else decode_alpha(payload); if (live_page && ui_screen == UI_LIVE) { ui_note_activity(); ui_render(); } } } static void process_bit(uint8_t bit) { if (invert_bits) bit ^= 1U; if (!batch_locked) { sync_shift = (sync_shift << 1) | bit; if (sync_bits < 32) sync_bits++; if (sync_bits == 32) { if (sync_shift == POCSAG_SYNC) { batch_locked = true; batch_word_index = 0; codeword_bits = 0; codeword_shift = 0; ESP_LOGI(TAG, "POCSAG sync"); } else if (sync_shift == ~POCSAG_SYNC) { invert_bits = true; batch_locked = true; batch_word_index = 0; codeword_bits = 0; codeword_shift = 0; ESP_LOGI(TAG, "POCSAG sync (inverted)"); } } return; } codeword_shift = (codeword_shift << 1) | bit; codeword_bits++; if (codeword_bits == 32) { decode_codeword(codeword_shift, batch_word_index); batch_word_index++; codeword_bits = 0; codeword_shift = 0; if (batch_word_index == 16) { batch_locked = false; sync_bits = 0; sync_shift = 0; } } } static void set_detected_baud(baud_t rate) { if (current_baud != RATE_UNKNOWN) return; current_baud = rate; bit_period_q16 = (uint32_t)(((uint64_t)SAMPLE_RATE_HZ << 16) / (unsigned)rate); phase_q16 = bit_period_q16 / 2U; sync_shift = 0; sync_bits = 0; batch_locked = false; display_baud = (unsigned)rate; ESP_LOGI(TAG, "Detected baud: %u", (unsigned)rate); oled_draw_history(); } static void process_sample(uint8_t sample) { if (current_baud == RATE_UNKNOWN) { edge_samples++; if (sample != last_sample) { observed_edges++; if (edge_samples <= 200) { edge_period_sum += edge_samples; edge_period_count++; if (edge_samples < edge_period_min) edge_period_min = edge_samples; if (edge_samples > edge_period_max) edge_period_max = edge_samples; } if (edge_samples >= 69 && edge_samples <= 81) { hit512++; hit1200 = hit2400 = 0; } else if (edge_samples >= 28 && edge_samples <= 36) { hit1200++; hit512 = hit2400 = 0; } else if (edge_samples >= 13 && edge_samples <= 19) { hit2400++; hit512 = hit1200 = 0; } else { hit512 = hit1200 = hit2400 = 0; } edge_samples = 0; last_sample = sample; if (hit512 >= 12) set_detected_baud(RATE_512); else if (hit1200 >= 12) set_detected_baud(RATE_1200); else if (hit2400 >= 12) set_detected_baud(RATE_2400); } return; } phase_q16 += 65536U; if (phase_q16 >= bit_period_q16) { phase_q16 -= bit_period_q16; process_bit(sample); } } /* ------------------------------ GPTimer ------------------------------- */ static uint8_t sample_buf[SAMPLE_BUF_SIZE]; static volatile uint32_t sample_wr; static volatile uint32_t sample_rd; static volatile uint32_t sample_drops; static bool IRAM_ATTR sample_alarm_cb(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *ctx) { (void)timer; (void)edata; (void)ctx; uint32_t wr = sample_wr; uint32_t rd = sample_rd; if ((uint32_t)(wr - rd) < SAMPLE_BUF_SIZE) { sample_buf[wr & SAMPLE_BUF_MASK] = gpio_get_level(PIN_CC1101_GDO0) ? 1 : 0; sample_wr = wr + 1; } else { sample_drops++; } return false; } static void start_sampler(void) { gptimer_config_t cfg = { .clk_src = GPTIMER_CLK_SRC_DEFAULT, .direction = GPTIMER_COUNT_UP, .resolution_hz = SAMPLE_TIMER_HZ, }; ESP_ERROR_CHECK(gptimer_new_timer(&cfg, &sample_timer)); gptimer_event_callbacks_t cbs = { .on_alarm = sample_alarm_cb, }; ESP_ERROR_CHECK(gptimer_register_event_callbacks(sample_timer, &cbs, NULL)); gptimer_alarm_config_t alarm = { .alarm_count = SAMPLE_PERIOD_TICKS, .reload_count = 0, .flags.auto_reload_on_alarm = true, }; ESP_ERROR_CHECK(gptimer_set_alarm_action(sample_timer, &alarm)); ESP_ERROR_CHECK(gptimer_enable(sample_timer)); ESP_ERROR_CHECK(gptimer_start(sample_timer)); ESP_LOGI(TAG, "Sample rate nominally %lu Hz", (unsigned long)SAMPLE_RATE_HZ); } /* Send is explicit from the TX screen; pause sampling so our own RF cannot enter RX decode. */ static void tx_send_selected(void) { #if !POCSAG_TX_ENABLE tx_status_text = "ENABLE TX IN CONFIG"; ESP_LOGW(TAG, "TX is disabled; set POCSAG_TX_ENABLE in pager_config.h"); #else TickType_t now = xTaskGetTickCount(); if (tx_has_sent && now - tx_last_sent < pdMS_TO_TICKS(TX_MIN_INTERVAL_MS)) { tx_status_text = "WAIT 10 SECONDS"; return; } char system_message[TX_MAX_MESSAGE_CHARS + 1U]; const tx_message_preset_t *preset = &tx_message_presets[tx_message_index]; const char *selected_text = preset->text; if (preset->system_status) { #if BATTERY_MONITOR_ENABLE if (battery_voltage_valid) { unsigned battery_percent = battery_voltage_mv <= 3300U ? 0U : battery_voltage_mv >= 4200U ? 100U : (unsigned)(((battery_voltage_mv - 3300U) * 100U) / 900U); snprintf(system_message, sizeof(system_message), "SYS %luM %uB H%u BAT%u", (unsigned long)(radio_frequency_hz / 1000000UL), (unsigned)current_baud, history_count, battery_percent); } else #endif { snprintf(system_message, sizeof(system_message), "SYS %luM %uB H%u", (unsigned long)(radio_frequency_hz / 1000000UL), (unsigned)current_baud, history_count); } selected_text = system_message; } const ric_preset_t *target = &ric_presets[tx_ric_preset_index]; tx_status_text = "TRANSMITTING"; ui_render(); ESP_ERROR_CHECK(gptimer_stop(sample_timer)); esp_err_t err = pager_tx_send(cc_dev, PIN_CC1101_GDO0, target->ric, selected_text); sample_wr = 0; sample_rd = 0; sync_shift = 0; sync_bits = 0; batch_locked = false; codeword_bits = 0; codeword_shift = 0; ESP_ERROR_CHECK(gptimer_start(sample_timer)); if (err == ESP_OK) { tx_has_sent = true; tx_last_sent = xTaskGetTickCount(); tx_status_text = "PAGE SENT"; ESP_LOGI(TAG, "Sent test preset to RIC %lu", (unsigned long)target->ric); } else { tx_status_text = "TX ERROR"; ESP_LOGE(TAG, "TX failed: %s", esp_err_to_name(err)); } #endif } /* -------------------------------- main -------------------------------- */ 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, .mode = GPIO_MODE_INPUT, .pull_up_en = GPIO_PULLUP_DISABLE, .pull_down_en = GPIO_PULLDOWN_DISABLE, .intr_type = GPIO_INTR_DISABLE, }; ESP_ERROR_CHECK(gpio_config(&gdo_cfg)); gpio_config_t oled_cs_cfg = { .pin_bit_mask = 1ULL << PIN_OLED_CS, .mode = GPIO_MODE_OUTPUT, }; ESP_ERROR_CHECK(gpio_config(&oled_cs_cfg)); gpio_set_level(PIN_OLED_CS, 1); setup_spi(); oled_init(); ui_hardware_init(); battery_adc_init(); ui_screen = UI_LIVE; ui_render(); pocsag_bch_init(); cc1101_init(); ui_render(); start_sampler(); TickType_t last_status = xTaskGetTickCount(); TickType_t last_battery_read = last_status; while (1) { while (sample_rd != sample_wr) { uint32_t rd = sample_rd; uint8_t s = sample_buf[rd & SAMPLE_BUF_MASK]; sample_rd = rd + 1; process_sample(s); } ui_poll_button(); ui_update_alert(); ui_update_display_flash(); 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 ? edge_period_sum / edge_period_count : 0; ESP_LOGI(TAG, "baud=%u samples=%lu dropped=%lu history=%u " "GDO_edges=%lu period_samples=%lu..%lu avg=%u", (unsigned)current_baud, (unsigned long)sample_wr, (unsigned long)sample_drops, history_count, (unsigned long)observed_edges, (unsigned long)(edge_period_count ? edge_period_min : 0), (unsigned long)edge_period_max, edge_period_avg); observed_edges = 0; edge_period_sum = 0; edge_period_min = UINT32_MAX; edge_period_max = 0; edge_period_count = 0; } vTaskDelay(1); } }