ThinkNodeM1Board.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "ThinkNodeM1Board.h"
  2. #include <Arduino.h>
  3. #ifdef THINKNODE_M1
  4. #include <Wire.h>
  5. #include <bluefruit.h>
  6. static BLEDfu bledfu;
  7. static void connect_callback(uint16_t conn_handle) {
  8. (void)conn_handle;
  9. MESH_DEBUG_PRINTLN("BLE client connected");
  10. }
  11. static void disconnect_callback(uint16_t conn_handle, uint8_t reason) {
  12. (void)conn_handle;
  13. (void)reason;
  14. MESH_DEBUG_PRINTLN("BLE client disconnected");
  15. }
  16. void ThinkNodeM1Board::begin() {
  17. // for future use, sub-classes SHOULD call this from their begin()
  18. startup_reason = BD_STARTUP_NORMAL;
  19. Wire.begin();
  20. #ifdef P_LORA_TX_LED
  21. pinMode(P_LORA_TX_LED, OUTPUT);
  22. digitalWrite(P_LORA_TX_LED, LOW);
  23. #endif
  24. pinMode(SX126X_POWER_EN, OUTPUT);
  25. digitalWrite(SX126X_POWER_EN, HIGH);
  26. delay(10); // give sx1262 some time to power up
  27. }
  28. uint16_t ThinkNodeM1Board::getBattMilliVolts() {
  29. int adcvalue = 0;
  30. analogReference(AR_INTERNAL_3_0);
  31. analogReadResolution(12);
  32. delay(10);
  33. // ADC range is 0..3000mV and resolution is 12-bit (0..4095)
  34. adcvalue = analogRead(PIN_VBAT_READ);
  35. // Convert the raw value to compensated mv, taking the resistor-
  36. // divider into account (providing the actual LIPO voltage)
  37. return (uint16_t)((float)adcvalue * REAL_VBAT_MV_PER_LSB);
  38. }
  39. bool ThinkNodeM1Board::startOTAUpdate(const char *id, char reply[]) {
  40. // Config the peripheral connection with maximum bandwidth
  41. // more SRAM required by SoftDevice
  42. // Note: All config***() function must be called before begin()
  43. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  44. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  45. Bluefruit.begin(1, 0);
  46. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  47. Bluefruit.setTxPower(4);
  48. // Set the BLE device name
  49. Bluefruit.setName("THINKNODE_M1_OTA");
  50. Bluefruit.Periph.setConnectCallback(connect_callback);
  51. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  52. // To be consistent OTA DFU should be added first if it exists
  53. bledfu.begin();
  54. // Set up and start advertising
  55. // Advertising packet
  56. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  57. Bluefruit.Advertising.addTxPower();
  58. Bluefruit.Advertising.addName();
  59. /* Start Advertising
  60. - Enable auto advertising if disconnected
  61. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  62. - Timeout for fast mode is 30 seconds
  63. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  64. For recommended advertising interval
  65. https://developer.apple.com/library/content/qa/qa1931/_index.html
  66. */
  67. Bluefruit.Advertising.restartOnDisconnect(true);
  68. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  69. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  70. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  71. strcpy(reply, "OK - started");
  72. return true;
  73. }
  74. #endif