MinewsemiME25LS01Board.cpp 2.7 KB

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