target.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
  15. HWTSensorManager sensors = HWTSensorManager(nmea);
  16. #ifndef LORA_CR
  17. #define LORA_CR 5
  18. #endif
  19. bool radio_init() {
  20. fallback_clock.begin();
  21. rtc_clock.begin(Wire);
  22. #ifdef SX126X_DIO3_TCXO_VOLTAGE
  23. float tcxo = SX126X_DIO3_TCXO_VOLTAGE;
  24. #else
  25. float tcxo = 1.6f;
  26. #endif
  27. #if defined(P_LORA_SCLK)
  28. spi.begin(P_LORA_SCLK, P_LORA_MISO, P_LORA_MOSI);
  29. #endif
  30. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo);
  31. if (status != RADIOLIB_ERR_NONE) {
  32. Serial.print("ERROR: radio init failed: ");
  33. Serial.println(status);
  34. return false; // fail
  35. }
  36. radio.setCRC(1);
  37. #ifdef SX126X_CURRENT_LIMIT
  38. radio.setCurrentLimit(SX126X_CURRENT_LIMIT);
  39. #endif
  40. #ifdef SX126X_DIO2_AS_RF_SWITCH
  41. radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
  42. #endif
  43. #ifdef SX126X_RX_BOOSTED_GAIN
  44. radio.setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
  45. #endif
  46. return true; // success
  47. }
  48. uint32_t radio_get_rng_seed() {
  49. return radio.random(0x7FFFFFFF);
  50. }
  51. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  52. radio.setFrequency(freq);
  53. radio.setSpreadingFactor(sf);
  54. radio.setBandwidth(bw);
  55. radio.setCodingRate(cr);
  56. }
  57. void radio_set_tx_power(uint8_t dbm) {
  58. radio.setOutputPower(dbm);
  59. }
  60. mesh::LocalIdentity radio_new_identity() {
  61. RadioNoiseListener rng(radio);
  62. return mesh::LocalIdentity(&rng); // create new random identity
  63. }
  64. void HWTSensorManager::start_gps() {
  65. if (!gps_active) {
  66. board.periph_power.claim();
  67. gps_active = true;
  68. Serial1.println("$CFGSYS,h35155*68");
  69. }
  70. }
  71. void HWTSensorManager::stop_gps() {
  72. if (gps_active) {
  73. gps_active = false;
  74. board.periph_power.release();
  75. }
  76. }
  77. bool HWTSensorManager::begin() {
  78. // init GPS port
  79. Serial1.begin(115200, SERIAL_8N1, PIN_GPS_RX, PIN_GPS_TX);
  80. return true;
  81. }
  82. bool HWTSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  83. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  84. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, 0.0f);
  85. }
  86. return true;
  87. }
  88. void HWTSensorManager::loop() {
  89. static long next_gps_update = 0;
  90. _location->loop();
  91. if (millis() > next_gps_update) {
  92. if (gps_active && _location->isValid()) {
  93. node_lat = ((double)_location->getLatitude())/1000000.;
  94. node_lon = ((double)_location->getLongitude())/1000000.;
  95. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  96. }
  97. next_gps_update = millis() + 1000;
  98. }
  99. }
  100. int HWTSensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  101. const char* HWTSensorManager::getSettingName(int i) const {
  102. return i == 0 ? "gps" : NULL;
  103. }
  104. const char* HWTSensorManager::getSettingValue(int i) const {
  105. if (i == 0) {
  106. return gps_active ? "1" : "0";
  107. }
  108. return NULL;
  109. }
  110. bool HWTSensorManager::setSettingValue(const char* name, const char* value) {
  111. if (strcmp(name, "gps") == 0) {
  112. if (strcmp(value, "0") == 0) {
  113. stop_gps();
  114. } else {
  115. start_gps();
  116. }
  117. return true;
  118. }
  119. return false; // not supported
  120. }