nano-g2.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. pinMode(GPS_EN, OUTPUT); // Initialize GPS power pin
  28. Wire.begin();
  29. pinMode(SX126X_POWER_EN, OUTPUT);
  30. digitalWrite(SX126X_POWER_EN, HIGH);
  31. delay(10);
  32. }
  33. uint16_t NanoG2Ultra::getBattMilliVolts()
  34. {
  35. int adcvalue = 0;
  36. analogReference(AR_INTERNAL_3_0);
  37. analogReadResolution(12);
  38. delay(10);
  39. // ADC range is 0..3000mV and resolution is 12-bit (0..4095)
  40. adcvalue = analogRead(PIN_VBAT_READ);
  41. // Convert the raw value to compensated mv, taking the resistor-
  42. // divider into account (providing the actual LIPO voltage)
  43. return (uint16_t)((float)adcvalue * REAL_VBAT_MV_PER_LSB);
  44. }
  45. bool NanoG2Ultra::startOTAUpdate(const char *id, char reply[])
  46. {
  47. // Config the peripheral connection with maximum bandwidth
  48. // more SRAM required by SoftDevice
  49. // Note: All config***() function must be called before begin()
  50. Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
  51. Bluefruit.configPrphConn(92, BLE_GAP_EVENT_LENGTH_MIN, 16, 16);
  52. Bluefruit.begin(1, 0);
  53. // Set max power. Accepted values are: -40, -30, -20, -16, -12, -8, -4, 0, 4
  54. Bluefruit.setTxPower(4);
  55. // Set the BLE device name
  56. Bluefruit.setName("NANO_G2_OTA");
  57. Bluefruit.Periph.setConnectCallback(connect_callback);
  58. Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
  59. // To be consistent OTA DFU should be added first if it exists
  60. bledfu.begin();
  61. // Set up and start advertising
  62. // Advertising packet
  63. Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
  64. Bluefruit.Advertising.addTxPower();
  65. Bluefruit.Advertising.addName();
  66. /* Start Advertising
  67. - Enable auto advertising if disconnected
  68. - Interval: fast mode = 20 ms, slow mode = 152.5 ms
  69. - Timeout for fast mode is 30 seconds
  70. - Start(timeout) with timeout = 0 will advertise forever (until connected)
  71. For recommended advertising interval
  72. https://developer.apple.com/library/content/qa/qa1931/_index.html
  73. */
  74. Bluefruit.Advertising.restartOnDisconnect(true);
  75. Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
  76. Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
  77. Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
  78. strcpy(reply, "OK - started");
  79. return true;
  80. }
  81. #endif