target.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/sensors/MicroNMEALocationProvider.h>
  4. MeshadventurerBoard board;
  5. static SPIClass spi;
  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. ESP32RTCClock fallback_clock;
  9. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  10. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
  11. MASensorManager sensors = MASensorManager(nmea);
  12. #ifdef DISPLAY_CLASS
  13. DISPLAY_CLASS display;
  14. #endif
  15. bool radio_init() {
  16. fallback_clock.begin();
  17. rtc_clock.begin(Wire);
  18. #if defined(P_LORA_SCLK)
  19. return radio.std_init(&spi);
  20. #else
  21. return radio.std_init();
  22. #endif
  23. }
  24. uint32_t radio_get_rng_seed() {
  25. return radio.random(0x7FFFFFFF);
  26. }
  27. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  28. radio.setFrequency(freq);
  29. radio.setSpreadingFactor(sf);
  30. radio.setBandwidth(bw);
  31. radio.setCodingRate(cr);
  32. }
  33. void radio_set_tx_power(uint8_t dbm) {
  34. radio.setOutputPower(dbm);
  35. }
  36. mesh::LocalIdentity radio_new_identity() {
  37. RadioNoiseListener rng(radio);
  38. return mesh::LocalIdentity(&rng); // create new random identity
  39. }
  40. void MASensorManager::start_gps() {
  41. if(!gps_active) {
  42. MESH_DEBUG_PRINTLN("starting GPS");
  43. gps_active = true;
  44. }
  45. }
  46. void MASensorManager::stop_gps() {
  47. if(gps_active) {
  48. MESH_DEBUG_PRINTLN("stopping GPS");
  49. gps_active = false;
  50. }
  51. }
  52. bool MASensorManager::begin() {
  53. Serial1.setPins(PIN_GPS_RX, PIN_GPS_TX);
  54. Serial1.begin(9600);
  55. delay(500);
  56. return true;
  57. }
  58. bool MASensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  59. if(requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  60. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  61. }
  62. return true;
  63. }
  64. void MASensorManager::loop() {
  65. static long next_gps_update = 0;
  66. _location->loop();
  67. if(millis() > next_gps_update && gps_active) {
  68. if(_location->isValid()) {
  69. node_lat = ((double)_location->getLatitude()) / 1000000.;
  70. node_lon = ((double)_location->getLongitude()) / 1000000.;
  71. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  72. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  73. }
  74. next_gps_update = millis() + 1000;
  75. }
  76. }
  77. int MASensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  78. const char* MASensorManager::getSettingName(int i) const {
  79. return i == 0 ? "gps" : NULL;
  80. }
  81. const char* MASensorManager::getSettingValue(int i) const {
  82. if(i == 0) {
  83. return gps_active ? "1" : "0";
  84. }
  85. return NULL;
  86. }
  87. bool MASensorManager::setSettingValue(const char* name, const char* value) {
  88. if(strcmp(name, "gps") == 0) {
  89. if(strcmp(value, "0") == 0) {
  90. stop_gps();
  91. } else {
  92. start_gps();
  93. }
  94. return true;
  95. }
  96. return false; // not supported
  97. }