MeshSolarBoard.cpp 2.3 KB

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