target.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. MomentaryButton joystick_left(JOYSTICK_LEFT, 1000, true);
  16. MomentaryButton joystick_right(JOYSTICK_RIGHT, 1000, true);
  17. #endif
  18. bool radio_init() {
  19. rtc_clock.begin(Wire);
  20. return radio.std_init(&SPI);
  21. }
  22. uint32_t radio_get_rng_seed() {
  23. return radio.random(0x7FFFFFFF);
  24. }
  25. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  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. radio.setOutputPower(dbm);
  33. }
  34. void WioTrackerL1SensorManager::start_gps()
  35. {
  36. if (!gps_active)
  37. {
  38. MESH_DEBUG_PRINTLN("starting GPS");
  39. digitalWrite(PIN_GPS_STANDBY, HIGH);
  40. gps_active = true;
  41. }
  42. }
  43. void WioTrackerL1SensorManager::stop_gps()
  44. {
  45. if (gps_active)
  46. {
  47. MESH_DEBUG_PRINTLN("stopping GPS");
  48. digitalWrite(PIN_GPS_STANDBY, LOW);
  49. gps_active = false;
  50. }
  51. }
  52. bool WioTrackerL1SensorManager::begin()
  53. {
  54. Serial1.setPins(PIN_GPS_TX, PIN_GPS_RX); // be sure to tx into rx and rx into tx
  55. Serial1.begin(GPS_BAUDRATE);
  56. pinMode(PIN_GPS_STANDBY, OUTPUT);
  57. digitalWrite(PIN_GPS_STANDBY, HIGH); // Wake GPS from standby
  58. delay(500);
  59. // We'll consider GPS detected if we see any data on Serial1
  60. if (Serial1.available() > 0)
  61. {
  62. MESH_DEBUG_PRINTLN("GPS detected");
  63. }
  64. else
  65. {
  66. MESH_DEBUG_PRINTLN("No GPS detected");
  67. }
  68. digitalWrite(PIN_GPS_STANDBY, LOW); // Put GPS back into standby mode
  69. return true;
  70. }
  71. bool WioTrackerL1SensorManager::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 WioTrackerL1SensorManager::loop()
  80. {
  81. static long next_gps_update = 0;
  82. _location->loop();
  83. if (millis() > next_gps_update && gps_active) // don't bother if gps position is not enabled
  84. {
  85. if (_location->isValid())
  86. {
  87. node_lat = ((double)_location->getLatitude()) / 1000000.;
  88. node_lon = ((double)_location->getLongitude()) / 1000000.;
  89. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  90. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  91. }
  92. next_gps_update = millis() + (1000 * 60); // after initial update, only check every minute TODO: should be configurable
  93. }
  94. }
  95. int WioTrackerL1SensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  96. const char *WioTrackerL1SensorManager::getSettingName(int i) const
  97. {
  98. return i == 0 ? "gps" : NULL;
  99. }
  100. const char *WioTrackerL1SensorManager::getSettingValue(int i) const
  101. {
  102. if (i == 0)
  103. {
  104. return gps_active ? "1" : "0";
  105. }
  106. return NULL;
  107. }
  108. bool WioTrackerL1SensorManager::setSettingValue(const char *name, const char *value)
  109. {
  110. if (strcmp(name, "gps") == 0)
  111. {
  112. if (strcmp(value, "0") == 0)
  113. {
  114. stop_gps();
  115. }
  116. else
  117. {
  118. start_gps();
  119. }
  120. return true;
  121. }
  122. return false; // not supported
  123. }
  124. mesh::LocalIdentity radio_new_identity() {
  125. RadioNoiseListener rng(radio);
  126. return mesh::LocalIdentity(&rng); // create new random identity
  127. }