SenseCapSolarBoard.cpp 2.4 KB

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