target.cpp 3.1 KB

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