target.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. {
  37. if (!gps_active)
  38. {
  39. MESH_DEBUG_PRINTLN("starting GPS");
  40. digitalWrite(PIN_GPS_STANDBY, HIGH);
  41. gps_active = true;
  42. }
  43. }
  44. void NanoG2UltraSensorManager::stop_gps()
  45. {
  46. if (gps_active)
  47. {
  48. MESH_DEBUG_PRINTLN("stopping GPS");
  49. digitalWrite(PIN_GPS_STANDBY, LOW);
  50. gps_active = false;
  51. }
  52. }
  53. bool NanoG2UltraSensorManager::begin()
  54. {
  55. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); // be sure to tx into rx and rx into tx
  56. Serial1.begin(115200);
  57. pinMode(PIN_GPS_STANDBY, OUTPUT);
  58. digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
  59. delay(500);
  60. // We'll consider GPS detected if we see any data on Serial1
  61. if (Serial1.available() > 0)
  62. {
  63. MESH_DEBUG_PRINTLN("GPS detected");
  64. }
  65. else
  66. {
  67. MESH_DEBUG_PRINTLN("No GPS detected");
  68. }
  69. digitalWrite(GPS_EN, LOW); // Put GPS back into standby mode
  70. return true;
  71. }
  72. bool NanoG2UltraSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP &telemetry)
  73. {
  74. if (requester_permissions & TELEM_PERM_LOCATION)
  75. { // does requester have permission?
  76. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  77. }
  78. return true;
  79. }
  80. void NanoG2UltraSensorManager::loop()
  81. {
  82. static long next_gps_update = 0;
  83. _location->loop();
  84. if (millis() > next_gps_update && gps_active) // don't bother if gps position is not enabled
  85. {
  86. if (_location->isValid())
  87. {
  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("lat %f lon %f", node_lat, node_lon);
  92. }
  93. next_gps_update = millis() + (1000 * 60); // after initial update, only check every minute TODO: should be configurable
  94. }
  95. }
  96. int NanoG2UltraSensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  97. const char *NanoG2UltraSensorManager::getSettingName(int i) const
  98. {
  99. return i == 0 ? "gps" : NULL;
  100. }
  101. const char *NanoG2UltraSensorManager::getSettingValue(int i) const
  102. {
  103. if (i == 0)
  104. {
  105. return gps_active ? "1" : "0";
  106. }
  107. return NULL;
  108. }
  109. bool NanoG2UltraSensorManager::setSettingValue(const char *name, const char *value)
  110. {
  111. if (strcmp(name, "gps") == 0)
  112. {
  113. if (strcmp(value, "0") == 0)
  114. {
  115. stop_gps();
  116. }
  117. else
  118. {
  119. start_gps();
  120. }
  121. return true;
  122. }
  123. return false; // not supported
  124. }
  125. mesh::LocalIdentity radio_new_identity()
  126. {
  127. RadioNoiseListener rng(radio);
  128. return mesh::LocalIdentity(&rng); // create new random identity
  129. }