master
Mark Qvist 5 months ago
parent 59e1836975
commit fe9538de7e
  1. 50
      Bluetooth.h

@ -28,8 +28,8 @@
#elif HAS_BLE == true
#include "esp_bt_main.h"
#include "esp_bt_device.h"
// TODO: Remove
#define SerialBT Serial
#include "BleSerial.h"
BleSerial SerialBT;
#endif
#elif MCU_VARIANT == MCU_NRF52
@ -260,30 +260,30 @@ char bt_devname[11];
#endif
#elif MCU_VARIANT == MCU_NRF52
uint8_t eeprom_read(uint32_t mapped_addr);
uint8_t eeprom_read(uint32_t mapped_addr);
void bt_stop() {
void bt_stop() {
if (bt_state != BT_STATE_OFF) {
bt_allow_pairing = false;
bt_state = BT_STATE_OFF;
}
}
}
void bt_disable_pairing() {
void bt_disable_pairing() {
bt_allow_pairing = false;
bt_ssp_pin = 0;
bt_state = BT_STATE_ON;
}
}
void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
void bt_pairing_complete(uint16_t conn_handle, uint8_t auth_status) {
if (auth_status == BLE_GAP_SEC_STATUS_SUCCESS) {
bt_disable_pairing();
} else {
bt_ssp_pin = 0;
}
}
}
bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool match_request) {
for (int i = 0; i < 6; i++) {
// multiply by tens however many times needed to make numbers appear in order
bt_ssp_pin += ((int)passkey[i] - 48) * pow(10, 5-i);
@ -295,18 +295,18 @@ bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool ma
}
}
return false;
}
}
void bt_connect_callback(uint16_t conn_handle) {
void bt_connect_callback(uint16_t conn_handle) {
bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED;
}
}
void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
bt_state = BT_STATE_ON;
}
}
bool bt_setup_hw() {
bool bt_setup_hw() {
if (!bt_ready) {
#if HAS_EEPROM
if (EEPROM.read(eeprom_addr(ADDR_CONF_BT)) == BT_ENABLE_BYTE) {
@ -346,9 +346,9 @@ bool bt_setup_hw() {
} else { return false; }
} else { return false; }
}
}
void bt_start() {
void bt_start() {
if (bt_state == BT_STATE_OFF) {
Bluefruit.setName(bt_devname);
bledis.setManufacturer(BLE_MANUFACTURER);
@ -375,9 +375,9 @@ void bt_start() {
bt_state = BT_STATE_ON;
}
}
}
bool bt_init() {
bool bt_init() {
bt_state = BT_STATE_OFF;
if (bt_setup_hw()) {
if (bt_enabled && !console_active) bt_start();
@ -385,18 +385,18 @@ bool bt_init() {
} else {
return false;
}
}
}
void bt_enable_pairing() {
void bt_enable_pairing() {
if (bt_state == BT_STATE_OFF) bt_start();
bt_allow_pairing = true;
bt_pairing_started = millis();
bt_state = BT_STATE_PAIRING;
}
}
void update_bt() {
void update_bt() {
if (bt_allow_pairing && millis()-bt_pairing_started >= BT_PAIRING_TIMEOUT) {
bt_disable_pairing();
}
}
}
#endif

Loading…
Cancel
Save