TechoBoard.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include <Arduino.h>
  2. #include "TechoBoard.h"
  3. #ifdef LILYGO_TECHO
  4. #include <bluefruit.h>
  5. #include <Wire.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 TechoBoard::begin() {
  17. // for future use, sub-classes SHOULD call this from their begin()
  18. startup_reason = BD_STARTUP_NORMAL;
  19. Wire.begin();
  20. pinMode(SX126X_POWER_EN, OUTPUT);
  21. digitalWrite(SX126X_POWER_EN, HIGH);
  22. delay(10); // give sx1262 some time to power up
  23. }
  24. uint16_t TechoBoard::getBattMilliVolts() {
  25. int adcvalue = 0;
  26. analogReference(AR_INTERNAL_3_0);
  27. analogReadResolution(12);
  28. delay(10);
  29. // ADC range is 0..3000mV and resolution is 12-bit (0..4095)
  30. adcvalue = analogRead(PIN_VBAT_READ);
  31. // Convert the raw value to compensated mv, taking the resistor-
  32. // divider into account (providing the actual LIPO voltage)
  33. return (uint16_t)((float)adcvalue * REAL_VBAT_MV_PER_LSB);
  34. }
  35. bool TechoBoard::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("TECHO_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. }
  70. #endif