target.cpp 2.9 KB

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