target.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/sensors/MicroNMEALocationProvider.h>
  4. HeltecV3Board board;
  5. #if defined(P_LORA_SCLK)
  6. static SPIClass spi;
  7. RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, spi);
  8. #else
  9. RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY);
  10. #endif
  11. WRAPPER_CLASS radio_driver(radio, board);
  12. ESP32RTCClock fallback_clock;
  13. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  14. // GPS_EN (GPIO35) drives N-ch MOSFET → P-ch high-side switch; GPS_RESET (GPIO36) active LOW
  15. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1, &rtc_clock, GPS_RESET, GPS_EN, &board.periph_power);
  16. HWTSensorManager sensors = HWTSensorManager(nmea);
  17. #ifdef DISPLAY_CLASS
  18. DISPLAY_CLASS display(&board.periph_power); // peripheral power pin is shared
  19. MomentaryButton user_btn(PIN_USER_BTN, 1000, true);
  20. #endif
  21. bool radio_init() {
  22. fallback_clock.begin();
  23. rtc_clock.begin(Wire);
  24. #if defined(P_LORA_SCLK)
  25. return radio.std_init(&spi);
  26. #else
  27. return radio.std_init();
  28. #endif
  29. }
  30. mesh::LocalIdentity radio_new_identity() {
  31. RadioNoiseListener rng(radio);
  32. return mesh::LocalIdentity(&rng); // create new random identity
  33. }
  34. void HWTSensorManager::start_gps() {
  35. if (!gps_active) {
  36. _location->begin(); // Claims periph_power via RefCountedDigitalPin
  37. gps_active = true;
  38. Serial1.println("$CFGSYS,h35155*68"); // Configure GPS for all constellations
  39. }
  40. }
  41. void HWTSensorManager::stop_gps() {
  42. if (gps_active) {
  43. gps_active = false;
  44. _location->stop(); // Releases periph_power via RefCountedDigitalPin
  45. }
  46. }
  47. bool HWTSensorManager::begin() {
  48. // init GPS port
  49. Serial1.begin(115200, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX);
  50. return true;
  51. }
  52. bool HWTSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  53. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  54. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  55. }
  56. return true;
  57. }
  58. void HWTSensorManager::loop() {
  59. static long next_gps_update = 0;
  60. _location->loop();
  61. if (millis() > next_gps_update) {
  62. if (gps_active && _location->isValid()) {
  63. node_lat = ((double)_location->getLatitude())/1000000.;
  64. node_lon = ((double)_location->getLongitude())/1000000.;
  65. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  66. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  67. }
  68. next_gps_update = millis() + 1000;
  69. }
  70. }
  71. int HWTSensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  72. const char* HWTSensorManager::getSettingName(int i) const {
  73. return i == 0 ? "gps" : NULL;
  74. }
  75. const char* HWTSensorManager::getSettingValue(int i) const {
  76. if (i == 0) {
  77. return gps_active ? "1" : "0";
  78. }
  79. return NULL;
  80. }
  81. bool HWTSensorManager::setSettingValue(const char* name, const char* value) {
  82. if (strcmp(name, "gps") == 0) {
  83. if (strcmp(value, "0") == 0) {
  84. stop_gps();
  85. } else {
  86. start_gps();
  87. }
  88. return true;
  89. }
  90. return false; // not supported
  91. }