target.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/ArduinoHelpers.h>
  4. #include <helpers/sensors/MicroNMEALocationProvider.h>
  5. ThinkNodeM1Board 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. ThinkNodeM1SensorManager sensors = ThinkNodeM1SensorManager(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 ThinkNodeM1SensorManager::start_gps() {
  62. if (!gps_active) {
  63. gps_active = true;
  64. _location->begin();
  65. }
  66. }
  67. void ThinkNodeM1SensorManager::stop_gps() {
  68. if (gps_active) {
  69. gps_active = false;
  70. _location->stop();
  71. }
  72. }
  73. bool ThinkNodeM1SensorManager::begin() {
  74. Serial1.begin(9600);
  75. // Initialize GPS switch pin
  76. pinMode(PIN_GPS_SWITCH, INPUT);
  77. last_gps_switch_state = digitalRead(PIN_GPS_SWITCH);
  78. // Initialize GPS power pin
  79. pinMode(GPS_EN, OUTPUT);
  80. // Check initial switch state to determine if GPS should be active
  81. if (last_gps_switch_state == HIGH) { // Switch is HIGH when ON
  82. start_gps();
  83. }
  84. return true;
  85. }
  86. bool ThinkNodeM1SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  87. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  88. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  89. }
  90. return true;
  91. }
  92. void ThinkNodeM1SensorManager::loop() {
  93. static long next_gps_update = 0;
  94. static long last_switch_check = 0;
  95. // Check GPS switch state every second
  96. if (millis() - last_switch_check > 1000) {
  97. bool current_switch_state = digitalRead(PIN_GPS_SWITCH);
  98. // Detect switch state change
  99. if (current_switch_state != last_gps_switch_state) {
  100. last_gps_switch_state = current_switch_state;
  101. if (current_switch_state == HIGH) { // Switch is ON
  102. MESH_DEBUG_PRINTLN("GPS switch ON");
  103. start_gps();
  104. } else { // Switch is OFF
  105. MESH_DEBUG_PRINTLN("GPS switch OFF");
  106. stop_gps();
  107. }
  108. }
  109. last_switch_check = millis();
  110. }
  111. if (!gps_active) {
  112. return; // GPS is not active, skip further processing
  113. }
  114. _location->loop();
  115. if (millis() > next_gps_update) {
  116. if (_location->isValid()) {
  117. node_lat = ((double)_location->getLatitude())/1000000.;
  118. node_lon = ((double)_location->getLongitude())/1000000.;
  119. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  120. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  121. }
  122. next_gps_update = millis() + 1000;
  123. }
  124. }
  125. int ThinkNodeM1SensorManager::getNumSettings() const {
  126. return 1; // always show GPS setting
  127. }
  128. const char* ThinkNodeM1SensorManager::getSettingName(int i) const {
  129. return (i == 0) ? "gps" : NULL;
  130. }
  131. const char* ThinkNodeM1SensorManager::getSettingValue(int i) const {
  132. if (i == 0) {
  133. return gps_active ? "1" : "0";
  134. }
  135. return NULL;
  136. }
  137. bool ThinkNodeM1SensorManager::setSettingValue(const char* name, const char* value) {
  138. if (strcmp(name, "gps") == 0) {
  139. if (strcmp(value, "0") == 0) {
  140. stop_gps();
  141. } else {
  142. start_gps();
  143. }
  144. return true;
  145. }
  146. return false; // not supported
  147. }