nano-g2.cpp 2.9 KB

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