target.cpp 3.5 KB

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