target.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <Arduino.h>
  2. #include "target.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);
  11. NanoG2UltraSensorManager sensors = NanoG2UltraSensorManager(nmea);
  12. #ifdef DISPLAY_CLASS
  13. DISPLAY_CLASS display;
  14. #endif
  15. bool radio_init()
  16. {
  17. rtc_clock.begin(Wire);
  18. return radio.std_init(&SPI);
  19. }
  20. uint32_t radio_get_rng_seed()
  21. {
  22. return radio.random(0x7FFFFFFF);
  23. }
  24. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr)
  25. {
  26. radio.setFrequency(freq);
  27. radio.setSpreadingFactor(sf);
  28. radio.setBandwidth(bw);
  29. radio.setCodingRate(cr);
  30. }
  31. void radio_set_tx_power(uint8_t dbm)
  32. {
  33. radio.setOutputPower(dbm);
  34. }
  35. void NanoG2UltraSensorManager::start_gps() {
  36. MESH_DEBUG_PRINTLN("Starting GPS");
  37. if (!gps_active) {
  38. digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
  39. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
  40. Serial1.begin(9600);
  41. MESH_DEBUG_PRINTLN("Waiting for gps to power up");
  42. delay(1000);
  43. gps_active = true;
  44. }
  45. _location->begin();
  46. }
  47. void NanoG2UltraSensorManager::stop_gps() {
  48. MESH_DEBUG_PRINTLN("Stopping GPS");
  49. if (gps_active) {
  50. digitalWrite(PIN_GPS_STANDBY, LOW); // sleep GPS
  51. gps_active = false;
  52. }
  53. _location->stop();
  54. }
  55. bool NanoG2UltraSensorManager::begin()
  56. {
  57. digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
  58. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX);
  59. Serial1.begin(9600);
  60. MESH_DEBUG_PRINTLN("Checking GPS switch state");
  61. delay(1000);
  62. // Check initial switch state to determine if GPS should be active
  63. if (Serial1.available() > 0) {
  64. MESH_DEBUG_PRINTLN("GPS was on at boot, GPS enabled");
  65. start_gps();
  66. } else {
  67. MESH_DEBUG_PRINTLN("GPS was not on at boot, GPS disabled");
  68. }
  69. return true;
  70. }
  71. bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP &telemetry)
  72. {
  73. if (requester_permissions & TELEM_PERM_LOCATION)
  74. { // does requester have permission?
  75. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  76. }
  77. return true;
  78. }
  79. void NanoG2UltraSensorManager::loop()
  80. {
  81. static long next_gps_update = 0;
  82. if (!gps_active) {
  83. return; // GPS is not active, skip further processing
  84. }
  85. _location->loop();
  86. if (millis() > next_gps_update) {
  87. if (_location->isValid()) {
  88. node_lat = ((double)_location->getLatitude())/1000000.;
  89. node_lon = ((double)_location->getLongitude())/1000000.;
  90. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  91. MESH_DEBUG_PRINTLN("VALID location: lat %f lon %f", node_lat, node_lon);
  92. } else {
  93. MESH_DEBUG_PRINTLN("INVALID location, waiting for fix");
  94. }
  95. MESH_DEBUG_PRINTLN("GPS satellites: %d", _location->satellitesCount());
  96. next_gps_update = millis() + 1000;
  97. }
  98. }
  99. int NanoG2UltraSensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  100. const char *NanoG2UltraSensorManager::getSettingName(int i) const
  101. {
  102. return i == 0 ? "gps" : NULL;
  103. }
  104. const char *NanoG2UltraSensorManager::getSettingValue(int i) const
  105. {
  106. if (i == 0)
  107. {
  108. return gps_active ? "1" : "0";
  109. }
  110. return NULL;
  111. }
  112. bool NanoG2UltraSensorManager::setSettingValue(const char *name, const char *value)
  113. {
  114. if (strcmp(name, "gps") == 0) {
  115. if (strcmp(value, "0") == 0) {
  116. stop_gps();
  117. } else {
  118. start_gps();
  119. }
  120. return true;
  121. }
  122. return false; // not supported
  123. }
  124. mesh::LocalIdentity radio_new_identity()
  125. {
  126. RadioNoiseListener rng(radio);
  127. return mesh::LocalIdentity(&rng); // create new random identity
  128. }