target.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. bool radio_init() {
  16. rtc_clock.begin(Wire);
  17. return radio.std_init(&SPI);
  18. }
  19. uint32_t radio_get_rng_seed() {
  20. return radio.random(0x7FFFFFFF);
  21. }
  22. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  23. radio.setFrequency(freq);
  24. radio.setSpreadingFactor(sf);
  25. radio.setBandwidth(bw);
  26. radio.setCodingRate(cr);
  27. }
  28. void radio_set_tx_power(uint8_t dbm) {
  29. radio.setOutputPower(dbm);
  30. }
  31. mesh::LocalIdentity radio_new_identity() {
  32. RadioNoiseListener rng(radio);
  33. return mesh::LocalIdentity(&rng); // create new random identity
  34. }
  35. void ThinkNodeM1SensorManager::start_gps() {
  36. if (!gps_active) {
  37. gps_active = true;
  38. _location->begin();
  39. }
  40. }
  41. void ThinkNodeM1SensorManager::stop_gps() {
  42. if (gps_active) {
  43. gps_active = false;
  44. _location->stop();
  45. }
  46. }
  47. bool ThinkNodeM1SensorManager::begin() {
  48. Serial1.begin(9600);
  49. // Initialize GPS switch pin
  50. pinMode(PIN_GPS_SWITCH, INPUT);
  51. last_gps_switch_state = digitalRead(PIN_GPS_SWITCH);
  52. // Initialize GPS power pin
  53. pinMode(GPS_EN, OUTPUT);
  54. // Check initial switch state to determine if GPS should be active
  55. if (last_gps_switch_state == HIGH) { // Switch is HIGH when ON
  56. start_gps();
  57. }
  58. return true;
  59. }
  60. bool ThinkNodeM1SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  61. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  62. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  63. }
  64. return true;
  65. }
  66. void ThinkNodeM1SensorManager::loop() {
  67. static long next_gps_update = 0;
  68. static long last_switch_check = 0;
  69. // Check GPS switch state every second
  70. if (millis() - last_switch_check > 1000) {
  71. bool current_switch_state = digitalRead(PIN_GPS_SWITCH);
  72. // Detect switch state change
  73. if (current_switch_state != last_gps_switch_state) {
  74. last_gps_switch_state = current_switch_state;
  75. if (current_switch_state == HIGH) { // Switch is ON
  76. MESH_DEBUG_PRINTLN("GPS switch ON");
  77. start_gps();
  78. } else { // Switch is OFF
  79. MESH_DEBUG_PRINTLN("GPS switch OFF");
  80. stop_gps();
  81. }
  82. }
  83. last_switch_check = millis();
  84. }
  85. if (!gps_active) {
  86. return; // GPS is not active, skip further processing
  87. }
  88. _location->loop();
  89. if (millis() > next_gps_update) {
  90. if (_location->isValid()) {
  91. node_lat = ((double)_location->getLatitude())/1000000.;
  92. node_lon = ((double)_location->getLongitude())/1000000.;
  93. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  94. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  95. }
  96. next_gps_update = millis() + 1000;
  97. }
  98. }
  99. int ThinkNodeM1SensorManager::getNumSettings() const {
  100. return 1; // always show GPS setting
  101. }
  102. const char* ThinkNodeM1SensorManager::getSettingName(int i) const {
  103. return (i == 0) ? "gps" : NULL;
  104. }
  105. const char* ThinkNodeM1SensorManager::getSettingValue(int i) const {
  106. if (i == 0) {
  107. return gps_active ? "1" : "0";
  108. }
  109. return NULL;
  110. }
  111. bool ThinkNodeM1SensorManager::setSettingValue(const char* name, const char* value) {
  112. if (strcmp(name, "gps") == 0) {
  113. if (strcmp(value, "0") == 0) {
  114. stop_gps();
  115. } else {
  116. start_gps();
  117. }
  118. return true;
  119. }
  120. return false; // not supported
  121. }