target.cpp 3.6 KB

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