target.cpp 3.8 KB

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