nano-g2.cpp 3.1 KB

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