target.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. } else {
  60. MESH_DEBUG_PRINTLN("No GPS detected");
  61. }
  62. digitalWrite(GPS_EN, LOW); // Power off GPS until the setting is changed
  63. return true;
  64. }
  65. bool T114SensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  66. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  67. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  68. }
  69. return true;
  70. }
  71. void T114SensorManager::loop() {
  72. static long next_gps_update = 0;
  73. _location->loop();
  74. if (millis() > next_gps_update) {
  75. if (_location->isValid()) {
  76. node_lat = ((double)_location->getLatitude())/1000000.;
  77. node_lon = ((double)_location->getLongitude())/1000000.;
  78. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  79. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  80. }
  81. next_gps_update = millis() + 1000;
  82. }
  83. }
  84. int T114SensorManager::getNumSettings() const {
  85. return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
  86. }
  87. const char* T114SensorManager::getSettingName(int i) const {
  88. return (gps_detected && i == 0) ? "gps" : NULL;
  89. }
  90. const char* T114SensorManager::getSettingValue(int i) const {
  91. if (gps_detected && i == 0) {
  92. return gps_active ? "1" : "0";
  93. }
  94. return NULL;
  95. }
  96. bool T114SensorManager::setSettingValue(const char* name, const char* value) {
  97. if (gps_detected && strcmp(name, "gps") == 0) {
  98. if (strcmp(value, "0") == 0) {
  99. stop_gps();
  100. } else {
  101. start_gps();
  102. }
  103. return true;
  104. }
  105. return false; // not supported
  106. }