target.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. uint32_t radio_get_rng_seed() {
  31. return radio.random(0x7FFFFFFF);
  32. }
  33. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  34. radio.setFrequency(freq);
  35. radio.setSpreadingFactor(sf);
  36. radio.setBandwidth(bw);
  37. radio.setCodingRate(cr);
  38. }
  39. void radio_set_tx_power(int8_t dbm) {
  40. radio.setOutputPower(dbm);
  41. }
  42. mesh::LocalIdentity radio_new_identity() {
  43. RadioNoiseListener rng(radio);
  44. return mesh::LocalIdentity(&rng); // create new random identity
  45. }
  46. void HWTSensorManager::start_gps() {
  47. if (!gps_active) {
  48. _location->begin(); // Claims periph_power via RefCountedDigitalPin
  49. gps_active = true;
  50. Serial1.println("$CFGSYS,h35155*68"); // Configure GPS for all constellations
  51. }
  52. }
  53. void HWTSensorManager::stop_gps() {
  54. if (gps_active) {
  55. gps_active = false;
  56. _location->stop(); // Releases periph_power via RefCountedDigitalPin
  57. }
  58. }
  59. bool HWTSensorManager::begin() {
  60. // init GPS port
  61. Serial1.begin(115200, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX);
  62. return true;
  63. }
  64. bool HWTSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  65. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  66. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  67. }
  68. return true;
  69. }
  70. void HWTSensorManager::loop() {
  71. static long next_gps_update = 0;
  72. _location->loop();
  73. if (millis() > next_gps_update) {
  74. if (gps_active && _location->isValid()) {
  75. node_lat = ((double)_location->getLatitude())/1000000.;
  76. node_lon = ((double)_location->getLongitude())/1000000.;
  77. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  78. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  79. }
  80. next_gps_update = millis() + 1000;
  81. }
  82. }
  83. int HWTSensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  84. const char* HWTSensorManager::getSettingName(int i) const {
  85. return i == 0 ? "gps" : NULL;
  86. }
  87. const char* HWTSensorManager::getSettingValue(int i) const {
  88. if (i == 0) {
  89. return gps_active ? "1" : "0";
  90. }
  91. return NULL;
  92. }
  93. bool HWTSensorManager::setSettingValue(const char* name, const char* value) {
  94. if (strcmp(name, "gps") == 0) {
  95. if (strcmp(value, "0") == 0) {
  96. stop_gps();
  97. } else {
  98. start_gps();
  99. }
  100. return true;
  101. }
  102. return false; // not supported
  103. }