|
| 1 | +/*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= |
| 2 | +Bluetooth State Values: |
| 3 | + BT_OFF = 0, |
| 4 | + BT_NOTCONNECTED, |
| 5 | + BT_CONNECTED, |
| 6 | +
|
| 7 | +Bluetooth States: |
| 8 | +
|
| 9 | + BT_OFF (Using WiFi) |
| 10 | + | ^ |
| 11 | + Use Bluetooth | | Use WiFi |
| 12 | + bluetoothStart | | bluetoothStop |
| 13 | + v | |
| 14 | + BT_NOTCONNECTED |
| 15 | + | ^ |
| 16 | + Client connected | | Client disconnected |
| 17 | + v | |
| 18 | + BT_CONNECTED |
| 19 | +
|
| 20 | +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/ |
| 21 | + |
| 22 | +//---------------------------------------- |
| 23 | +// Constants |
| 24 | +//---------------------------------------- |
| 25 | + |
| 26 | +//---------------------------------------- |
| 27 | +// Locals - compiled out |
| 28 | +//---------------------------------------- |
| 29 | + |
| 30 | +#ifdef COMPILE_BT |
| 31 | + |
| 32 | +static BluetoothSerial bluetoothSerial; |
| 33 | +static volatile byte bluetoothState = BT_OFF; |
| 34 | + |
| 35 | +//---------------------------------------- |
| 36 | +// Bluetooth Routines - compiled out |
| 37 | +//---------------------------------------- |
| 38 | + |
| 39 | +//Call back for when BT connection event happens (connected/disconnect) |
| 40 | +//Used for updating the bluetoothState state machine |
| 41 | +void bluetoothCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { |
| 42 | + if (event == ESP_SPP_SRV_OPEN_EVT) { |
| 43 | + Serial.println(F("Client Connected")); |
| 44 | + bluetoothState = BT_CONNECTED; |
| 45 | + if (productVariant == RTK_SURVEYOR) |
| 46 | + digitalWrite(pin_bluetoothStatusLED, HIGH); |
| 47 | + } |
| 48 | + |
| 49 | + if (event == ESP_SPP_CLOSE_EVT ) { |
| 50 | + Serial.println(F("Client disconnected")); |
| 51 | + bluetoothState = BT_NOTCONNECTED; |
| 52 | + if (productVariant == RTK_SURVEYOR) |
| 53 | + digitalWrite(pin_bluetoothStatusLED, LOW); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#endif //COMPILE_BT |
| 58 | + |
| 59 | +//---------------------------------------- |
| 60 | +// Global Bluetooth Routines |
| 61 | +//---------------------------------------- |
| 62 | + |
| 63 | +//Return the Bluetooth state |
| 64 | +byte bluetoothGetState() |
| 65 | +{ |
| 66 | +#ifdef COMPILE_BT |
| 67 | + return bluetoothState; |
| 68 | +#else //COMPILE_BT |
| 69 | + return BT_OFF; |
| 70 | +#endif //COMPILE_BT |
| 71 | +} |
| 72 | + |
| 73 | +//Determine if the Bluetooth link is congested |
| 74 | +bool bluetoothIsCongested() |
| 75 | +{ |
| 76 | +#ifdef COMPILE_BT |
| 77 | + return bluetoothSerial.isCongested(); |
| 78 | +#else //COMPILE_BT |
| 79 | + return false; |
| 80 | +#endif //COMPILE_BT |
| 81 | +} |
| 82 | + |
| 83 | +//Read data from the Bluetooth device |
| 84 | +int bluetoothReadBytes(uint8_t * buffer, int length) |
| 85 | +{ |
| 86 | +#ifdef COMPILE_BT |
| 87 | + return bluetoothSerial.readBytes(buffer, length); |
| 88 | +#else //COMPILE_BT |
| 89 | + return 0; |
| 90 | +#endif //COMPILE_BT |
| 91 | +} |
| 92 | + |
| 93 | +//Determine if data is available |
| 94 | +bool bluetoothRxDataAvailable() |
| 95 | +{ |
| 96 | +#ifdef COMPILE_BT |
| 97 | + return bluetoothSerial.available(); |
| 98 | +#else //COMPILE_BT |
| 99 | + return false; |
| 100 | +#endif //COMPILE_BT |
| 101 | +} |
| 102 | + |
| 103 | +//Get MAC, start radio |
| 104 | +//Tack device's MAC address to end of friendly broadcast name |
| 105 | +//This allows multiple units to be on at same time |
| 106 | +void bluetoothStart() |
| 107 | +{ |
| 108 | + ntripClientStop(true); |
| 109 | + ntripServerStop(true); |
| 110 | + wifiStop(); |
| 111 | +#ifdef COMPILE_BT |
| 112 | + if (bluetoothState == BT_OFF) |
| 113 | + { |
| 114 | + char stateName[10]; |
| 115 | + if (buttonPreviousState == BUTTON_ROVER) |
| 116 | + strcpy(stateName, "Rover"); |
| 117 | + else |
| 118 | + strcpy(stateName, "Base"); |
| 119 | + |
| 120 | + sprintf(deviceName, "%s %s-%02X%02X", platformPrefix, stateName, unitMACAddress[4], unitMACAddress[5]); //Base mode |
| 121 | + |
| 122 | + if (bluetoothSerial.begin(deviceName, false, settings.sppRxQueueSize, settings.sppTxQueueSize) == false) //localName, isMaster, rxBufferSize, txBufferSize |
| 123 | + { |
| 124 | + Serial.println(F("An error occurred initializing Bluetooth")); |
| 125 | + |
| 126 | + if (productVariant == RTK_SURVEYOR) |
| 127 | + digitalWrite(pin_bluetoothStatusLED, LOW); |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + //Set PIN to 1234 so we can connect to older BT devices, but not require a PIN for modern device pairing |
| 132 | + //See issue: https://github.com/sparkfun/SparkFun_RTK_Firmware/issues/5 |
| 133 | + //https://github.com/espressif/esp-idf/issues/1541 |
| 134 | + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 135 | + esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE; |
| 136 | + |
| 137 | + esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; //Requires pin 1234 on old BT dongle, No prompt on new BT dongle |
| 138 | + //esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_OUT; //Works but prompts for either pin (old) or 'Does this 6 pin appear on the device?' (new) |
| 139 | + |
| 140 | + esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t)); |
| 141 | + |
| 142 | + esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED; |
| 143 | + esp_bt_pin_code_t pin_code; |
| 144 | + pin_code[0] = '1'; |
| 145 | + pin_code[1] = '2'; |
| 146 | + pin_code[2] = '3'; |
| 147 | + pin_code[3] = '4'; |
| 148 | + esp_bt_gap_set_pin(pin_type, 4, pin_code); |
| 149 | + //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- |
| 150 | + |
| 151 | + bluetoothSerial.register_callback(bluetoothCallback); //Controls BT Status LED on Surveyor |
| 152 | + bluetoothSerial.setTimeout(250); |
| 153 | + |
| 154 | + Serial.print(F("Bluetooth broadcasting as: ")); |
| 155 | + Serial.println(deviceName); |
| 156 | + |
| 157 | + //Start task for controlling Bluetooth pair LED |
| 158 | + if (productVariant == RTK_SURVEYOR) |
| 159 | + { |
| 160 | + ledcWrite(ledBTChannel, 255); //Turn on BT LED |
| 161 | + btLEDTask.detach(); //Slow down the BT LED blinker task |
| 162 | + btLEDTask.attach(btLEDTaskPace2Hz, updateBTled); //Rate in seconds, callback |
| 163 | + } |
| 164 | + |
| 165 | + bluetoothState = BT_NOTCONNECTED; |
| 166 | + reportHeapNow(); |
| 167 | + } |
| 168 | +#endif //COMPILE_BT |
| 169 | +} |
| 170 | + |
| 171 | +//This function stops BT so that it can be restarted later |
| 172 | +//It also releases as much system resources as possible so that WiFi/caster is more stable |
| 173 | +void bluetoothStop() |
| 174 | +{ |
| 175 | +#ifdef COMPILE_BT |
| 176 | + if (bluetoothState == BT_NOTCONNECTED || bluetoothState == BT_CONNECTED) |
| 177 | + { |
| 178 | + bluetoothSerial.register_callback(NULL); |
| 179 | + bluetoothSerial.flush(); //Complete any transfers |
| 180 | + bluetoothSerial.disconnect(); //Drop any clients |
| 181 | + bluetoothSerial.end(); //bluetoothSerial.end() will release significant RAM (~100k!) but a bluetoothSerial.start will crash. |
| 182 | + |
| 183 | + log_d("Bluetooth turned off"); |
| 184 | + |
| 185 | + bluetoothState = BT_OFF; |
| 186 | + reportHeapNow(); |
| 187 | + } |
| 188 | +#endif //COMPILE_BT |
| 189 | + online.rxRtcmCorrectionData = false; |
| 190 | +} |
| 191 | + |
| 192 | +//Write data to the Bluetooth device |
| 193 | +int bluetoothWriteBytes(const uint8_t * buffer, int length) |
| 194 | +{ |
| 195 | +#ifdef COMPILE_BT |
| 196 | + //Push new data to BT SPP |
| 197 | + return bluetoothSerial.write(buffer, length); |
| 198 | +#else //COMPILE_BT |
| 199 | + return 0; |
| 200 | +#endif //COMPILE_BT |
| 201 | +} |
0 commit comments