target.cpp 3.8 KB

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