|
| 1 | +#include <Arduino.h> |
| 2 | +#include "XiaoNrf52Board.h" |
| 3 | + |
| 4 | +#include <bluefruit.h> |
| 5 | +#include <Wire.h> |
| 6 | + |
| 7 | +static BLEDfu bledfu; |
| 8 | + |
| 9 | +static void connect_callback(uint16_t conn_handle) |
| 10 | +{ |
| 11 | + (void)conn_handle; |
| 12 | + MESH_DEBUG_PRINTLN("BLE client connected"); |
| 13 | +} |
| 14 | + |
| 15 | +static void disconnect_callback(uint16_t conn_handle, uint8_t reason) |
| 16 | +{ |
| 17 | + (void)conn_handle; |
| 18 | + (void)reason; |
| 19 | + |
| 20 | + MESH_DEBUG_PRINTLN("BLE client disconnected"); |
| 21 | +} |
| 22 | + |
| 23 | +void XiaoNrf52Board::begin() { |
| 24 | + // for future use, sub-classes SHOULD call this from their begin() |
| 25 | + startup_reason = BD_STARTUP_NORMAL; |
| 26 | + |
| 27 | + pinMode(PIN_VBAT, INPUT); |
| 28 | + pinMode(VBAT_ENABLE, OUTPUT); |
| 29 | + digitalWrite(VBAT_ENABLE, HIGH); |
| 30 | + |
| 31 | +#if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL) |
| 32 | + Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL); |
| 33 | +#endif |
| 34 | + |
| 35 | + Wire.begin(); |
| 36 | + |
| 37 | +#ifdef P_LORA_TX_LED |
| 38 | + pinMode(P_LORA_TX_LED, OUTPUT); |
| 39 | + digitalWrite(P_LORA_TX_LED, HIGH); |
| 40 | +#endif |
| 41 | + |
| 42 | +// pinMode(SX126X_POWER_EN, OUTPUT); |
| 43 | +// digitalWrite(SX126X_POWER_EN, HIGH); |
| 44 | + delay(10); // give sx1262 some time to power up |
| 45 | +} |
| 46 | + |
| 47 | +bool XiaoNrf52Board::startOTAUpdate(const char* id, char reply[]) { |
| 48 | + // Config the peripheral connection with maximum bandwidth |
| 49 | + // more SRAM required by SoftDevice |
| 50 | + // Note: All config***() function must be called before begin() |
| 51 | + Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); |
| 52 | + Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16); |
| 53 | + |
| 54 | + Bluefruit.begin(1, 0); |
| 55 | + // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4 |
| 56 | + Bluefruit.setTxPower(4); |
| 57 | + // Set the BLE device name |
| 58 | + Bluefruit.setName("XIAO_NRF52_OTA"); |
| 59 | + |
| 60 | + Bluefruit.Periph.setConnectCallback(connect_callback); |
| 61 | + Bluefruit.Periph.setDisconnectCallback(disconnect_callback); |
| 62 | + |
| 63 | + // To be consistent OTA DFU should be added first if it exists |
| 64 | + bledfu.begin(); |
| 65 | + |
| 66 | + // Set up and start advertising |
| 67 | + // Advertising packet |
| 68 | + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); |
| 69 | + Bluefruit.Advertising.addTxPower(); |
| 70 | + Bluefruit.Advertising.addName(); |
| 71 | + |
| 72 | + /* Start Advertising |
| 73 | + - Enable auto advertising if disconnected |
| 74 | + - Interval: fast mode = 20 ms, slow mode = 152.5 ms |
| 75 | + - Timeout for fast mode is 30 seconds |
| 76 | + - Start(timeout) with timeout = 0 will advertise forever (until connected) |
| 77 | +
|
| 78 | + For recommended advertising interval |
| 79 | + https://developer.apple.com/library/content/qa/qa1931/_index.html |
| 80 | + */ |
| 81 | + Bluefruit.Advertising.restartOnDisconnect(true); |
| 82 | + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms |
| 83 | + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode |
| 84 | + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds |
| 85 | + |
| 86 | + strcpy(reply, "OK - started"); |
| 87 | + return true; |
| 88 | + |
| 89 | + |
| 90 | + return false; |
| 91 | +} |
0 commit comments