KeepteenLT1Board.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <Arduino.h>
  2. #include "KeepteenLT1Board.h"
  3. #include <bluefruit.h>
  4. #include <Wire.h>
  5. static BLEDfu bledfu;
  6. void KeepteenLT1Board::begin() {
  7. // for future use, sub-classes SHOULD call this from their begin()
  8. startup_reason = BD_STARTUP_NORMAL;
  9. btn_prev_state = HIGH;
  10. pinMode(PIN_VBAT_READ, INPUT);
  11. #if defined(PIN_BOARD_SDA) && defined(PIN_BOARD_SCL)
  12. Wire.setPins(PIN_BOARD_SDA, PIN_BOARD_SCL);
  13. #endif
  14. Wire.begin();
  15. }
  16. static void connect_callback(uint16_t conn_handle) {
  17. (void)conn_handle;
  18. MESH_DEBUG_PRINTLN("BLE client connected");
  19. }
  20. static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
  21. (void)conn_handle;
  22. (void)reason;
  23. MESH_DEBUG_PRINTLN("BLE client disconnected");
  24. }
  25. bool KeepteenLT1Board::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("KeepteenLT1_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. }