ThinknodeM3Board.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include <Arduino.h>
  2. #include "ThinknodeM3Board.h"
  3. #include <Wire.h>
  4. #include <bluefruit.h>
  5. void ThinknodeM3Board::begin() {
  6. // for future use, sub-classes SHOULD call this from their begin()
  7. startup_reason = BD_STARTUP_NORMAL;
  8. btn_prev_state = HIGH;
  9. sd_power_mode_set(NRF_POWER_MODE_LOWPWR);
  10. // Enable DC/DC converter for improved power efficiency
  11. NRF_POWER->DCDCEN = 1;
  12. Wire.begin();
  13. delay(10); // give sx1262 some time to power up
  14. }
  15. #if 0
  16. static BLEDfu bledfu;
  17. static void connect_callback(uint16_t conn_handle) {
  18. (void)conn_handle;
  19. MESH_DEBUG_PRINTLN("BLE client connected");
  20. }
  21. static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
  22. (void)conn_handle;
  23. (void)reason;
  24. MESH_DEBUG_PRINTLN("BLE client disconnected");
  25. }
  26. bool TrackerThinknodeM3Board::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("T1000E_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. }
  61. #endif