target.cpp 3.8 KB

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