faketecBoard.cpp 2.5 KB

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