target.cpp 3.1 KB

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