From 7cdb2ec86a35ee452e777b57eb2d8d557fe1cd78 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 30 Oct 2022 16:49:35 +0100 Subject: [PATCH] Added Bluetooth pairing procedure and pin display --- Bluetooth.h | 35 +++++++++++++++++++++-------------- Config.h | 4 ++++ Display.h | 19 ++++++++++++++++++- Graphics.h | 24 ++++++++++++++++++++++++ RNode_Firmware.ino | 2 -- 5 files changed, 67 insertions(+), 17 deletions(-) diff --git a/Bluetooth.h b/Bluetooth.h index a91000e..1bc79c9 100644 --- a/Bluetooth.h +++ b/Bluetooth.h @@ -12,30 +12,17 @@ char bt_da[BT_DEV_ADDR_LEN]; char bt_dh[BT_DEV_HASH_LEN]; char bt_devname[11]; -bool bt_ready = false; -bool bt_enabled = false; -bool bt_allow_pairing = false; - #if MCU_VARIANT == MCU_ESP32 void bt_confirm_pairing(uint32_t numVal) { + bt_ssp_pin = numVal; if (bt_allow_pairing) { - bt_state = BT_STATE_ON; SerialBT.confirmReply(true); } else { - bt_state = BT_STATE_ON; SerialBT.confirmReply(false); } } - void bt_pairing_complete(boolean success) { - if (success) { - // Pass - } else { - // Pass - } - } - void bt_stop() { if (bt_state != BT_STATE_OFF) { SerialBT.end(); @@ -60,9 +47,28 @@ bool bt_allow_pairing = false; void bt_disable_pairing() { bt_allow_pairing = false; + bt_ssp_pin = 0; bt_state = BT_STATE_ON; } + void bt_pairing_complete(boolean success) { + if (success) { + bt_disable_pairing(); + } else { + bt_ssp_pin = 0; + } + } + + void bt_connection_callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param){ + if(event == ESP_SPP_SRV_OPEN_EVT) { + bt_state = BT_STATE_CONNECTED; + } + + if(event == ESP_SPP_CLOSE_EVT ){ + bt_state = BT_STATE_ON; + } + } + bool bt_setup_hw() { if (!bt_ready) { if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) { bt_enabled = true; } else { bt_enabled = false; } @@ -83,6 +89,7 @@ bool bt_allow_pairing = false; SerialBT.enableSSP(); SerialBT.onConfirmRequest(bt_confirm_pairing); SerialBT.onAuthComplete(bt_pairing_complete); + SerialBT.register_callback(bt_connection_callback); bt_ready = true; return true; diff --git a/Config.h b/Config.h index 2b256ab..c18a769 100644 --- a/Config.h +++ b/Config.h @@ -47,6 +47,10 @@ #define BT_STATE_PAIRING 0x02 #define BT_STATE_CONNECTED 0x03 uint8_t bt_state = BT_STATE_NA; + uint32_t bt_ssp_pin = 0; + bool bt_ready = false; + bool bt_enabled = false; + bool bt_allow_pairing = false; #else #error "The firmware cannot be compiled for the selected MCU variant" #endif diff --git a/Display.h b/Display.h index 9551ed6..4db6b57 100644 --- a/Display.h +++ b/Display.h @@ -11,6 +11,7 @@ Adafruit_SSD1306 display(DISP_W, DISP_H, &Wire, DISP_RST); #define DISP_MODE_UNKNOWN 0x00 #define DISP_MODE_LANDSCAPE 0x01 #define DISP_MODE_PORTRAIT 0x02 +#define DISP_PIN_SIZE 6 uint8_t disp_mode = DISP_MODE_UNKNOWN; uint8_t disp_ext_fb = false; unsigned char fb[512]; @@ -80,6 +81,10 @@ bool display_init() { last_page_flip = millis(); + stat_area.cp437(true); + disp_area.cp437(true); + display.cp437(true); + return true; } #else @@ -245,10 +250,22 @@ void update_stat_area() { const uint8_t pages = 2; uint8_t disp_page = START_PAGE; void draw_disp_area() { - if (!disp_ext_fb) { + if (!disp_ext_fb or bt_ssp_pin != 0) { disp_area.drawBitmap(0, 0, bm_def, disp_area.width(), 37, SSD1306_WHITE, SSD1306_BLACK); + if (!hw_ready || radio_error) { disp_area.drawBitmap(0, 37, bm_hwfail, disp_area.width(), 27, SSD1306_WHITE, SSD1306_BLACK); + } else if (bt_state == BT_STATE_PAIRING and bt_ssp_pin != 0) { + char *pin_str = (char*)malloc(DISP_PIN_SIZE+1); + sprintf(pin_str, "%06d", bt_ssp_pin); + + disp_area.drawBitmap(0, 37, bm_pairing, disp_area.width(), 27, SSD1306_WHITE, SSD1306_BLACK); + for (int i = 0; i < DISP_PIN_SIZE; i++) { + uint8_t numeric = pin_str[i]-48; + uint8_t offset = numeric*5; + disp_area.drawBitmap(7+9*i, 37+16, bm_n_uh+offset, 8, 5, SSD1306_WHITE, SSD1306_BLACK); + } + } else { if (millis()-last_page_flip >= page_interval) { disp_page = (++disp_page%pages); diff --git a/Graphics.h b/Graphics.h index af21830..3e00219 100644 --- a/Graphics.h +++ b/Graphics.h @@ -151,3 +151,27 @@ const unsigned char bm_online [] PROGMEM = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; + +const unsigned char bm_pairing [] PROGMEM = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xf0, 0xe3, 0x98, 0x73, 0x33, 0x87, 0xff, 0xff, 0xf2, 0xc9, 0x99, 0x33, 0x13, 0x3f, 0xff, + 0xff, 0xf0, 0xc1, 0x98, 0x73, 0x03, 0x27, 0xff, 0xff, 0xf3, 0xc9, 0x98, 0x73, 0x23, 0x27, 0xff, + 0xff, 0xf3, 0xc9, 0x99, 0x33, 0x33, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff +}; + +const unsigned char bm_n_uh [] PROGMEM = { + 0x07, 0x27, 0x27, 0x27, 0x07, 0x8f, 0x8f, 0xcf, 0xcf, 0xcf, 0x07, 0xe7, 0x07, 0x3f, 0x07, 0x07, + 0xe7, 0xc7, 0xe7, 0x07, 0x27, 0x27, 0x07, 0xe7, 0xe7, 0x07, 0x3f, 0x07, 0xe7, 0x07, 0x07, 0x3f, + 0x07, 0x27, 0x07, 0x07, 0xc7, 0xcf, 0x9f, 0x1f, 0x07, 0x27, 0x07, 0x27, 0x07, 0x07, 0x27, 0x07, + 0xe7, 0xe7 +}; \ No newline at end of file diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 47e9d31..72b14d1 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -248,8 +248,6 @@ void ISR_VECT receive_callback(int packet_size) { } } - - bool startRadio() { update_radio_lock(); if (!radio_online) {