MeshPocket.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <Arduino.h>
  2. #include "MeshPocket.h"
  3. #include <bluefruit.h>
  4. #include <Wire.h>
  5. static BLEDfu bledfu;
  6. static void connect_callback(uint16_t conn_handle)
  7. {
  8. (void)conn_handle;
  9. MESH_DEBUG_PRINTLN("BLE client connected");
  10. }
  11. static void disconnect_callback(uint16_t conn_handle, uint8_t reason)
  12. {
  13. (void)conn_handle;
  14. (void)reason;
  15. MESH_DEBUG_PRINTLN("BLE client disconnected");
  16. }
  17. void HeltecMeshPocket::begin() {
  18. // for future use, sub-classes SHOULD call this from their begin()
  19. startup_reason = BD_STARTUP_NORMAL;
  20. Serial.begin(115200);
  21. pinMode(PIN_VBAT_READ, INPUT);
  22. pinMode(PIN_USER_BTN, INPUT);
  23. }
  24. bool HeltecMeshPocket::startOTAUpdate(const char* id, char reply[]) {
  25. // Config the peripheral connection with maximum bandwidth
  26. // more SRAM required by SoftDevice
  27. // Note: All config***() function must be called before begin()
  28. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  29. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  30. Bluefruit.begin(1, 0);
  31. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  32. Bluefruit.setTxPower(4);
  33. // Set the BLE device name
  34. Bluefruit.setName("MESH_POCKET_OTA");
  35. Bluefruit.Periph.setConnectCallback(connect_callback);
  36. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  37. // To be consistent OTA DFU should be added first if it exists
  38. bledfu.begin();
  39. // Set up and start advertising
  40. // Advertising packet
  41. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  42. Bluefruit.Advertising.addTxPower();
  43. Bluefruit.Advertising.addName();
  44. /* Start Advertising
  45. - Enable auto advertising if disconnected
  46. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  47. - Timeout for fast mode is 30 seconds
  48. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  49. For recommended advertising interval
  50. https://developer.apple.com/library/content/qa/qa1931/_index.html
  51. */
  52. Bluefruit.Advertising.restartOnDisconnect(true);
  53. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  54. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  55. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  56. strcpy(reply, "OK - started");
  57. return true;
  58. }