target.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/ArduinoHelpers.h>
  4. #include <helpers/sensors/MicroNMEALocationProvider.h>
  5. T114Board board;
  6. RADIO_CLASS radio = new Module(P_LORA_NSS, P_LORA_DIO_1, P_LORA_RESET, P_LORA_BUSY, SPI);
  7. WRAPPER_CLASS radio_driver(radio, board);
  8. VolatileRTCClock fallback_clock;
  9. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  10. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
  11. T114SensorManager sensors = T114SensorManager(nmea);
  12. #ifdef DISPLAY_CLASS
  13. DISPLAY_CLASS display;
  14. #endif
  15. #ifndef LORA_CR
  16. #define LORA_CR 5
  17. #endif
  18. bool radio_init() {
  19. rtc_clock.begin(Wire);
  20. #ifdef SX126X_DIO3_TCXO_VOLTAGE
  21. float tcxo = SX126X_DIO3_TCXO_VOLTAGE;
  22. #else
  23. float tcxo = 1.6f;
  24. #endif
  25. SPI.setPins(P_LORA_MISO, P_LORA_SCLK, P_LORA_MOSI);
  26. SPI.begin();
  27. int status = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, LORA_TX_POWER, 8, tcxo);
  28. if (status != RADIOLIB_ERR_NONE) {
  29. Serial.print("ERROR: radio init failed: ");
  30. Serial.println(status);
  31. return false; // fail
  32. }
  33. radio.setCRC(1);
  34. #ifdef SX126X_CURRENT_LIMIT
  35. radio.setCurrentLimit(SX126X_CURRENT_LIMIT);
  36. #endif
  37. #ifdef SX126X_DIO2_AS_RF_SWITCH
  38. radio.setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
  39. #endif
  40. #ifdef SX126X_RX_BOOSTED_GAIN
  41. radio.setRxBoostedGainMode(SX126X_RX_BOOSTED_GAIN);
  42. #endif
  43. return true; // success
  44. }
  45. uint32_t radio_get_rng_seed() {
  46. return radio.random(0x7FFFFFFF);
  47. }
  48. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  49. radio.setFrequency(freq);
  50. radio.setSpreadingFactor(sf);
  51. radio.setBandwidth(bw);
  52. radio.setCodingRate(cr);
  53. }
  54. void radio_set_tx_power(uint8_t dbm) {
  55. radio.setOutputPower(dbm);
  56. }
  57. mesh::LocalIdentity radio_new_identity() {
  58. RadioNoiseListener rng(radio);
  59. return mesh::LocalIdentity(&rng); // create new random identity
  60. }
  61. void T114SensorManager::start_gps() {
  62. if (!gps_active) {
  63. gps_active = true;
  64. _location->begin();
  65. }
  66. }
  67. void T114SensorManager::stop_gps() {
  68. if (gps_active) {
  69. gps_active = false;
  70. _location->stop();
  71. }
  72. }
  73. bool T114SensorManager::begin() {
  74. Serial1.begin(9600);
  75. // Try to detect if GPS is physically connected to determine if we should expose the setting
  76. pinMode(GPS_EN, OUTPUT);
  77. digitalWrite(GPS_EN, HIGH); // Power on GPS
  78. // Give GPS a moment to power up and send data
  79. delay(1500);
  80. // We'll consider GPS detected if we see any data on Serial1
  81. gps_detected = (Serial1.available() > 0);
  82. if (gps_detected) {
  83. MESH_DEBUG_PRINTLN("GPS detected");
  84. digitalWrite(GPS_EN, LOW); // Power off GPS until the setting is changed
  85. } else {
  86. MESH_DEBUG_PRINTLN("No GPS detected");
  87. digitalWrite(GPS_EN, LOW);
  88. }
  89. return true;
  90. }
  91. bool T114SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  92. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  93. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  94. }
  95. return true;
  96. }
  97. void T114SensorManager::loop() {
  98. static long next_gps_update = 0;
  99. _location->loop();
  100. if (millis() > next_gps_update) {
  101. if (_location->isValid()) {
  102. node_lat = ((double)_location->getLatitude())/1000000.;
  103. node_lon = ((double)_location->getLongitude())/1000000.;
  104. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  105. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  106. }
  107. next_gps_update = millis() + 1000;
  108. }
  109. }
  110. int T114SensorManager::getNumSettings() const {
  111. return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
  112. }
  113. const char* T114SensorManager::getSettingName(int i) const {
  114. return (gps_detected && i == 0) ? "gps" : NULL;
  115. }
  116. const char* T114SensorManager::getSettingValue(int i) const {
  117. if (gps_detected && i == 0) {
  118. return gps_active ? "1" : "0";
  119. }
  120. return NULL;
  121. }
  122. bool T114SensorManager::setSettingValue(const char* name, const char* value) {
  123. if (gps_detected && strcmp(name, "gps") == 0) {
  124. if (strcmp(value, "0") == 0) {
  125. stop_gps();
  126. } else {
  127. start_gps();
  128. }
  129. return true;
  130. }
  131. return false; // not supported
  132. }