target.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, &rtc_clock);
  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. radio_driver.updatePreamble(sf);
  34. }
  35. void radio_set_tx_power(int8_t dbm) {
  36. radio.setOutputPower(dbm);
  37. }
  38. mesh::LocalIdentity radio_new_identity() {
  39. RadioNoiseListener rng(radio);
  40. return mesh::LocalIdentity(&rng); // create new random identity
  41. }
  42. void MASensorManager::start_gps() {
  43. if(!gps_active) {
  44. MESH_DEBUG_PRINTLN("starting GPS");
  45. gps_active = true;
  46. }
  47. }
  48. void MASensorManager::stop_gps() {
  49. if(gps_active) {
  50. MESH_DEBUG_PRINTLN("stopping GPS");
  51. gps_active = false;
  52. }
  53. }
  54. bool MASensorManager::begin() {
  55. Serial1.setPins(PIN_GPS_RX, PIN_GPS_TX);
  56. Serial1.begin(9600);
  57. delay(500);
  58. return true;
  59. }
  60. bool MASensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  61. if(requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  62. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  63. }
  64. return true;
  65. }
  66. void MASensorManager::loop() {
  67. static long next_gps_update = 0;
  68. _location->loop();
  69. if(millis() > next_gps_update && gps_active) {
  70. if(_location->isValid()) {
  71. node_lat = ((double)_location->getLatitude()) / 1000000.;
  72. node_lon = ((double)_location->getLongitude()) / 1000000.;
  73. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  74. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  75. }
  76. next_gps_update = millis() + 1000;
  77. }
  78. }
  79. int MASensorManager::getNumSettings() const { return 1; } // just one supported: "gps" (power switch)
  80. const char* MASensorManager::getSettingName(int i) const {
  81. return i == 0 ? "gps" : NULL;
  82. }
  83. const char* MASensorManager::getSettingValue(int i) const {
  84. if(i == 0) {
  85. return gps_active ? "1" : "0";
  86. }
  87. return NULL;
  88. }
  89. bool MASensorManager::setSettingValue(const char* name, const char* value) {
  90. if(strcmp(name, "gps") == 0) {
  91. if(strcmp(value, "0") == 0) {
  92. stop_gps();
  93. } else {
  94. start_gps();
  95. }
  96. return true;
  97. }
  98. return false; // not supported
  99. }