target.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/sensors/MicroNMEALocationProvider.h>
  4. MeshadventurerBoard board;
  5. static SPIClass spi;
  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. ESP32RTCClock fallback_clock;
  9. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  10. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock);
  11. MASensorManager sensors = MASensorManager(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. fallback_clock.begin();
  18. rtc_clock.begin(Wire);
  19. #if defined(P_LORA_SCLK)
  20. return radio.std_init(&spi);
  21. #else
  22. return radio.std_init();
  23. #endif
  24. }
  25. mesh::LocalIdentity radio_new_identity() {
  26. RadioNoiseListener rng(radio);
  27. return mesh::LocalIdentity(&rng); // create new random identity
  28. }
  29. void MASensorManager::start_gps() {
  30. if(!gps_active) {
  31. MESH_DEBUG_PRINTLN("starting GPS");
  32. gps_active = true;
  33. }
  34. }
  35. void MASensorManager::stop_gps() {
  36. if(gps_active) {
  37. MESH_DEBUG_PRINTLN("stopping GPS");
  38. gps_active = false;
  39. }
  40. }
  41. bool MASensorManager::begin() {
  42. Serial1.setPins(PIN_GPS_RX, PIN_GPS_TX);
  43. Serial1.begin(9600);
  44. delay(500);
  45. return true;
  46. }
  47. bool MASensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  48. if(requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  49. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  50. }
  51. return true;
  52. }
  53. void MASensorManager::loop() {
  54. static long next_gps_update = 0;
  55. _location->loop();
  56. if(millis() > next_gps_update && gps_active) {
  57. if(_location->isValid()) {
  58. node_lat = ((double)_location->getLatitude()) / 1000000.;
  59. node_lon = ((double)_location->getLongitude()) / 1000000.;
  60. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  61. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  62. }
  63. next_gps_update = millis() + 1000;
  64. }
  65. }
  66. int MASensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  67. const char* MASensorManager::getSettingName(int i) const {
  68. return i == 0 ? "gps" : NULL;
  69. }
  70. const char* MASensorManager::getSettingValue(int i) const {
  71. if(i == 0) {
  72. return gps_active ? "1" : "0";
  73. }
  74. return NULL;
  75. }
  76. bool MASensorManager::setSettingValue(const char* name, const char* value) {
  77. if(strcmp(name, "gps") == 0) {
  78. if(strcmp(value, "0") == 0) {
  79. stop_gps();
  80. } else {
  81. start_gps();
  82. }
  83. return true;
  84. }
  85. return false; // not supported
  86. }