diff --git a/dependencies.lock b/dependencies.lock new file mode 100644 index 0000000..8e9c53b --- /dev/null +++ b/dependencies.lock @@ -0,0 +1,20 @@ +dependencies: + espressif/led_strip: + component_hash: 027f68f2b57b0dca1d68dff6cc1139d9093825aa808c2bc3ca42bb2ce530fce9 + dependencies: + - name: idf + require: private + version: '>=5.2' + source: + registry_url: https://components.espressif.com/ + type: service + version: 3.1.1 + idf: + source: + type: idf + version: 6.0.0 +direct_dependencies: +- espressif/led_strip +manifest_hash: 60170c6d851bba053ac0e0179cc949cb7c1ffcb4be0730a1b7bebd00b5d30dd0 +target: esp32h2 +version: 3.0.0 diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index cf2c455..dc798c9 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,3 @@ idf_component_register(SRCS "main.c" - INCLUDE_DIRS ".") + INCLUDE_DIRS "." + REQUIRES esp_driver_gpio esp_driver_spi esp_driver_gptimer led_strip) diff --git a/main/idf_component.yml b/main/idf_component.yml new file mode 100644 index 0000000..a96a270 --- /dev/null +++ b/main/idf_component.yml @@ -0,0 +1,2 @@ +dependencies: + espressif/led_strip: "^3.1.1" \ No newline at end of file diff --git a/main/main.c b/main/main.c index 5a38137..1871985 100644 --- a/main/main.c +++ b/main/main.c @@ -33,6 +33,7 @@ #include "driver/gpio.h" #include "driver/spi_master.h" #include "driver/gptimer.h" +#include "led_strip.h" /* ---------------------------- Pins/config ---------------------------- */ @@ -47,10 +48,16 @@ #define PIN_OLED_CS GPIO_NUM_10 #define PIN_OLED_DC GPIO_NUM_11 #define PIN_OLED_RESET GPIO_NUM_12 +#define PIN_BUTTON_BOOT GPIO_NUM_9 +#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 MY_RIC 1234567UL +#define OLED_IDLE_OFF_MS 60000U +#define OWN_PAGE_ALERT_MS 3000U #define SAMPLE_TIMER_HZ 1000000UL #define SAMPLE_PERIOD_TICKS 26UL @@ -70,7 +77,10 @@ static const char *TAG = "pager"; 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 bool oled_is_on = true; +static TickType_t ui_last_activity; static esp_err_t spi_tx(spi_device_handle_t dev, const uint8_t *data, size_t len) @@ -308,6 +318,22 @@ static void oled_cmd(uint8_t cmd) 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_data(const uint8_t *data, size_t len) { gpio_set_level(PIN_OLED_DC, 1); @@ -401,11 +427,6 @@ static void oled_text(int x, int y, const char *s) } } -static void oled_number(char *dst, size_t n, uint32_t value) -{ - snprintf(dst, n, "%lu", (unsigned long)value); -} - /* -------------------------- Message history -------------------------- */ typedef struct { @@ -418,12 +439,23 @@ typedef struct { 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 unsigned ui_screen; +static unsigned menu_cursor; +static bool live_page; +static unsigned text_page; + +static void ui_render(void); +static void ui_note_activity(void); 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; @@ -435,43 +467,11 @@ static void history_add(uint32_t ric, unsigned baud, unsigned function, if (history_count < HISTORY_COUNT) history_count++; + history_cursor = 0; + ui_note_activity(); ESP_LOGI(TAG, "PAGER RIC=%lu function=%u baud=%u: %s", (unsigned long)ric, function, baud, text); -} - -static void oled_draw_history(void) -{ - char line[24]; - oled_clear(); - oled_text(0, 0, "POCSAG RECEIVER"); - - snprintf(line, sizeof(line), "%lu.%03lu MHZ", - (unsigned long)(RF_FREQ_HZ / 1000000UL), - (unsigned long)((RF_FREQ_HZ / 1000UL) % 1000UL)); - oled_text(0, 8, line); - - if (display_baud) - snprintf(line, sizeof(line), "%u BAUD", display_baud); - else - snprintf(line, sizeof(line), "WAITING FOR SIGNAL"); - oled_text(0, 16, line); - - if (history_count) { - pager_message_t *m = &history[(history_head + HISTORY_COUNT - 1U) - % HISTORY_COUNT]; - snprintf(line, sizeof(line), "RIC %lu F%u", - (unsigned long)m->ric, m->function); - oled_text(0, 27, line); - oled_text(0, 37, m->text); - if (strlen(m->text) > 21U) - oled_text(0, 45, m->text + 21); - if (strlen(m->text) > 42U) - oled_text(0, 53, m->text + 42); - } else { - oled_text(0, 32, "NO MESSAGES YET"); - } - - oled_flush(); + ui_render(); } /* ------------------------------- BCH --------------------------------- */ @@ -581,6 +581,328 @@ static const char numeric_chars[16] = { '8','9','U',' ','-','[',']','?' }; +enum { + UI_LIVE, + UI_HISTORY, + UI_MENU +}; + +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 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[24]; + oled_text(0, 0, monitor_mode ? "POCSAG MONITOR" : "MY PAGER"); + snprintf(line, sizeof(line), "RIC %lu", (unsigned long)MY_RIC); + 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 + }; + const char *items[] = { + monitor_mode ? "FILTER: ALL RIC" : "FILTER: MY RIC", + "LIVE DISPLAY", + "MESSAGE HISTORY", + "RETURN" + }; + oled_text(0, 0, "PAGER MENU"); + for (unsigned i = 0; i < 4; i++) { + int y = 11 + (int)i * 10; + if (i == 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[i]); + } + oled_text(0, 55, "TAP NEXT, HOLD SELECT"); +} + +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 + ui_draw_live(); + oled_flush(); +} + +static void oled_draw_history(void) +{ + ui_render(); +} + +static void ui_short_press(void) +{ + ui_note_activity(); + if (ui_screen == UI_MENU) { + menu_cursor = (menu_cursor + 1U) % 4U; + } 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_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; + break; + case 1: + ui_screen = UI_LIVE; + break; + case 2: + ui_screen = UI_HISTORY; + history_cursor = 0; + text_page = 0; + break; + default: + ui_screen = menu_return_screen; + break; + } + ui_render(); +} + +static void ui_poll_button(void) +{ + 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)) + ui_long_press(); + else + ui_short_press(); + } +} + +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_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, + .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)); + button_last_level = gpio_get_level(PIN_BUTTON_BOOT); + button_debounce_at = xTaskGetTickCount(); + ui_last_activity = button_debounce_at; +} + static void message_finish(void) { if (message_active && message_len) { @@ -597,6 +919,9 @@ static void message_finish(void) alpha_acc = 0; alpha_nbits = 0; numeric_nibble_count = 0; + live_page = false; + text_page = 0; + ui_render(); } static void message_abort(void) @@ -609,6 +934,7 @@ static void message_abort(void) alpha_acc = 0; alpha_nbits = 0; numeric_nibble_count = 0; + live_page = false; } static void append_message_char(char c) @@ -707,14 +1033,30 @@ static void decode_codeword(uint32_t raw, unsigned slot) 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(); + } - ESP_LOGI(TAG, "Address RIC=%lu function=%u", - (unsigned long)current_ric, current_function); + 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(); + } } } @@ -903,13 +1245,13 @@ void app_main(void) setup_spi(); oled_init(); - oled_text(0, 0, "POCSAG RECEIVER"); - oled_text(0, 16, "WAITING FOR RF"); - oled_flush(); + ui_hardware_init(); + ui_screen = UI_LIVE; + ui_render(); bch_init(); cc1101_init(); - oled_draw_history(); + ui_render(); start_sampler(); @@ -923,6 +1265,10 @@ void app_main(void) process_sample(s); } + ui_poll_button(); + ui_update_alert(); + ui_idle_power_down(); + if (xTaskGetTickCount() - last_status >= pdMS_TO_TICKS(5000)) { last_status = xTaskGetTickCount(); unsigned edge_period_avg = edge_period_count