SenseCapSolarBoard.cpp 2.4 KB

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