target.cpp 3.8 KB

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