]> git.tnoah.ca Git - lora-radio.git/commitdiff
Added Bluetooth pairing procedure and pin display
authorMark Qvist <mark@unsigned.io>
Sun, 30 Oct 2022 15:49:35 +0000 (16:49 +0100)
committerMark Qvist <mark@unsigned.io>
Sun, 30 Oct 2022 15:49:35 +0000 (16:49 +0100)
Bluetooth.h
Config.h
Display.h
Graphics.h
RNode_Firmware.ino

index a91000e3abffdc60c6d4bcf7de3c9173ebbf202a..1bc79c9677b8e5af92afa82a07b7db9bdf829fe4 100644 (file)
@@ -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;
index 2b256abbc3a1c57e7b18c5293829da9967f92c1c..c18a769c54ee136402a96975f9600fc0e5db7dc2 100644 (file)
--- a/Config.h
+++ b/Config.h
         #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
index 9551ed66873ab2bbd4592bf8ac635f14064175a1..4db6b57ae213a4a1ee9126dec1cdf18ba97d9df5 100644 (file)
--- 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);
index af2183017b8477dff75c87e40d5a3b9e2cc88bc0..3e00219ea82361ac7b0b122c8b569c17d16bd150 100644 (file)
@@ -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
index 47e9d31d031be5d38be2def55b7e5beedea99dbc..72b14d1a940490e89527d24f0921a562ddd98d30 100644 (file)
@@ -248,8 +248,6 @@ void ISR_VECT receive_callback(int packet_size) {
   }
 }
 
-
-
 bool startRadio() {
   update_radio_lock();
   if (!radio_online) {