WioTrackerL1Board.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include <Arduino.h>
  2. #include "WioTrackerL1Board.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 WioTrackerL1Board::begin() {
  16. // for future use, sub-classes SHOULD call this from their begin()
  17. startup_reason = BD_STARTUP_NORMAL;
  18. btn_prev_state = HIGH;
  19. pinMode(PIN_VBAT_READ, INPUT); // VBAT ADC input
  20. // Set all button pins to INPUT_PULLUP
  21. pinMode(PIN_BUTTON1, INPUT_PULLUP);
  22. pinMode(PIN_BUTTON2, INPUT_PULLUP);
  23. pinMode(PIN_BUTTON3, INPUT_PULLUP);
  24. pinMode(PIN_BUTTON4, INPUT_PULLUP);
  25. pinMode(PIN_BUTTON5, INPUT_PULLUP);
  26. pinMode(PIN_BUTTON6, INPUT_PULLUP);
  27. #if defined(PIN_WIRE_SDA) && defined(PIN_WIRE_SCL)
  28. Wire.setPins(PIN_WIRE_SDA, PIN_WIRE_SCL);
  29. #endif
  30. Wire.begin();
  31. #ifdef P_LORA_TX_LED
  32. pinMode(P_LORA_TX_LED, OUTPUT);
  33. digitalWrite(P_LORA_TX_LED, LOW);
  34. #endif
  35. delay(10); // give sx1262 some time to power up
  36. }
  37. bool WioTrackerL1Board::startOTAUpdate(const char* id, char reply[]) {
  38. // Config the peripheral connection with maximum bandwidth
  39. // more SRAM required by SoftDevice
  40. // Note: All config***() function must be called before begin()
  41. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  42. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  43. Bluefruit.begin(1, 0);
  44. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  45. Bluefruit.setTxPower(4);
  46. // Set the BLE device name
  47. Bluefruit.setName("WioTrackerL1 OTA");
  48. Bluefruit.Periph.setConnectCallback(connect_callback);
  49. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  50. // To be consistent OTA DFU should be added first if it exists
  51. bledfu.begin();
  52. // Set up and start advertising
  53. // Advertising packet
  54. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  55. Bluefruit.Advertising.addTxPower();
  56. Bluefruit.Advertising.addName();
  57. /* Start Advertising
  58. - Enable auto advertising if disconnected
  59. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  60. - Timeout for fast mode is 30 seconds
  61. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  62. For recommended advertising interval
  63. https://developer.apple.com/library/content/qa/qa1931/_index.html
  64. */
  65. Bluefruit.Advertising.restartOnDisconnect(true);
  66. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  67. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  68. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  69. uint8_t mac_addr[6];
  70. memset(mac_addr, 0, sizeof(mac_addr));
  71. Bluefruit.getAddr(mac_addr);
  72. sprintf(reply, "OK - mac: %02X:%02X:%02X:%02X:%02X:%02X",
  73. mac_addr[5], mac_addr[4], mac_addr[3], mac_addr[2], mac_addr[1], mac_addr[0]);
  74. return true;
  75. }