target.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "target.h"
  2. #include <Arduino.h>
  3. #include <helpers/ArduinoHelpers.h>
  4. #include <helpers/sensors/MicroNMEALocationProvider.h>
  5. NanoG2Ultra board;
  6. RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI);
  7. WRAPPER_CLASS radio_driver(radio, board);
  8. VolatileRTCClock fallback_clock;
  9. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  10. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock);
  11. NanoG2UltraSensorManager sensors = NanoG2UltraSensorManager(nmea);
  12. #ifdef DISPLAY_CLASS
  13. DISPLAY_CLASS display;
  14. MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
  15. #endif
  16. bool radio_init() {
  17. rtc_clock.begin(Wire);
  18. return radio.std_init(&SPI);
  19. }
  20. void NanoG2UltraSensorManager::start_gps() {
  21. MESH_DEBUG_PRINTLN("Starting GPS");
  22. if (!gps_active) {
  23. digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
  24. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
  25. Serial1.begin(9600);
  26. MESH_DEBUG_PRINTLN("Waiting for gps to power up");
  27. delay(1000);
  28. gps_active = true;
  29. }
  30. _location->begin();
  31. }
  32. void NanoG2UltraSensorManager::stop_gps() {
  33. MESH_DEBUG_PRINTLN("Stopping GPS");
  34. if (gps_active) {
  35. digitalWrite(PIN_GPS_STANDBY, LOW); // sleep GPS
  36. gps_active = false;
  37. }
  38. _location->stop();
  39. }
  40. bool NanoG2UltraSensorManager::begin() {
  41. digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
  42. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
  43. Serial1.begin(9600);
  44. MESH_DEBUG_PRINTLN("Checking GPS switch state");
  45. delay(1000);
  46. // Check initial switch state to determine if GPS should be active
  47. if (Serial1.available() > 0) {
  48. MESH_DEBUG_PRINTLN("GPS was on at boot, GPS enabled");
  49. start_gps();
  50. } else {
  51. MESH_DEBUG_PRINTLN("GPS was not on at boot, GPS disabled");
  52. }
  53. return true;
  54. }
  55. bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP &telemetry) {
  56. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  57. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  58. }
  59. return true;
  60. }
  61. void NanoG2UltraSensorManager::loop() {
  62. static long next_gps_update = 0;
  63. if (!gps_active) {
  64. return; // GPS is not active, skip further processing
  65. }
  66. _location->loop();
  67. if (millis() > next_gps_update) {
  68. if (_location->isValid()) {
  69. node_lat = ((double)_location->getLatitude()) / 1000000.;
  70. node_lon = ((double)_location->getLongitude()) / 1000000.;
  71. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  72. MESH_DEBUG_PRINTLN("VALID location: lat %f lon %f", node_lat, node_lon);
  73. } else {
  74. MESH_DEBUG_PRINTLN("INVALID location, waiting for fix");
  75. }
  76. MESH_DEBUG_PRINTLN("GPS satellites: %d", _location->satellitesCount());
  77. next_gps_update = millis() + 1000;
  78. }
  79. }
  80. int NanoG2UltraSensorManager::getNumSettings() const {
  81. return 1;
  82. } // just one supported: "gps" (power switch)
  83. const char *NanoG2UltraSensorManager::getSettingName(int i) const {
  84. return i == 0 ? "gps" : NULL;
  85. }
  86. const char *NanoG2UltraSensorManager::getSettingValue(int i) const {
  87. if (i == 0) {
  88. return gps_active ? "1" : "0";
  89. }
  90. return NULL;
  91. }
  92. bool NanoG2UltraSensorManager::setSettingValue(const char *name, const char *value) {
  93. if (strcmp(name, "gps") == 0) {
  94. if (strcmp(value, "0") == 0) {
  95. stop_gps();
  96. } else {
  97. start_gps();
  98. }
  99. return true;
  100. }
  101. return false; // not supported
  102. }
  103. mesh::LocalIdentity radio_new_identity() {
  104. RadioNoiseListener rng(radio);
  105. return mesh::LocalIdentity(&rng); // create new random identity
  106. }