target.cpp 3.4 KB

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