target.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. 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(uint8_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 T114SensorManager::start_gps() {
  37. if (!gps_active) {
  38. gps_active = true;
  39. _location->begin();
  40. }
  41. }
  42. void T114SensorManager::stop_gps() {
  43. if (gps_active) {
  44. gps_active = false;
  45. _location->stop();
  46. }
  47. }
  48. bool T114SensorManager::begin() {
  49. Serial1.begin(9600);
  50. // Try to detect if GPS is physically connected to determine if we should expose the setting
  51. pinMode(GPS_EN, OUTPUT);
  52. digitalWrite(GPS_EN, HIGH); // Power on GPS
  53. // Give GPS a moment to power up and send data
  54. delay(1500);
  55. // We'll consider GPS detected if we see any data on Serial1
  56. gps_detected = (Serial1.available() > 0);
  57. if (gps_detected) {
  58. MESH_DEBUG_PRINTLN("GPS detected");
  59. digitalWrite(GPS_EN, LOW); // Power off GPS until the setting is changed
  60. } else {
  61. MESH_DEBUG_PRINTLN("No GPS detected");
  62. digitalWrite(GPS_EN, LOW);
  63. }
  64. return true;
  65. }
  66. bool T114SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  67. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  68. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  69. }
  70. return true;
  71. }
  72. void T114SensorManager::loop() {
  73. static long next_gps_update = 0;
  74. _location->loop();
  75. if (millis() > next_gps_update) {
  76. if (_location->isValid()) {
  77. node_lat = ((double)_location->getLatitude())/1000000.;
  78. node_lon = ((double)_location->getLongitude())/1000000.;
  79. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  80. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  81. }
  82. next_gps_update = millis() + 1000;
  83. }
  84. }
  85. int T114SensorManager::getNumSettings() const {
  86. return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
  87. }
  88. const char* T114SensorManager::getSettingName(int i) const {
  89. return (gps_detected && i == 0) ? "gps" : NULL;
  90. }
  91. const char* T114SensorManager::getSettingValue(int i) const {
  92. if (gps_detected && i == 0) {
  93. return gps_active ? "1" : "0";
  94. }
  95. return NULL;
  96. }
  97. bool T114SensorManager::setSettingValue(const char* name, const char* value) {
  98. if (gps_detected && strcmp(name, "gps") == 0) {
  99. if (strcmp(value, "0") == 0) {
  100. stop_gps();
  101. } else {
  102. start_gps();
  103. }
  104. return true;
  105. }
  106. return false; // not supported
  107. }