MeshSolarBoard.cpp 2.2 KB

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