target.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/ArduinoHelpers.h>
  4. #include <helpers/sensors/MicroNMEALocationProvider.h>
  5. TechoBoard board;
  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. VolatileRTCClock fallback_clock;
  9. AutoDiscoverRTCClock rtc_clock(fallback_clock);
  10. MicroNMEALocationProvider nmea = MicroNMEALocationProvider(Serial1);
  11. TechoSensorManager sensors = TechoSensorManager(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. rtc_clock.begin(Wire);
  18. return radio.std_init(&SPI);
  19. }
  20. uint32_t radio_get_rng_seed() {
  21. return radio.random(0x7FFFFFFF);
  22. }
  23. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  24. radio.setFrequency(freq);
  25. radio.setSpreadingFactor(sf);
  26. radio.setBandwidth(bw);
  27. radio.setCodingRate(cr);
  28. }
  29. void radio_set_tx_power(uint8_t dbm) {
  30. radio.setOutputPower(dbm);
  31. }
  32. mesh::LocalIdentity radio_new_identity() {
  33. RadioNoiseListener rng(radio);
  34. return mesh::LocalIdentity(&rng); // create new random identity
  35. }
  36. void TechoSensorManager::start_gps() {
  37. if (!gps_active) {
  38. gps_active = true;
  39. _location->begin();
  40. }
  41. }
  42. void TechoSensorManager::stop_gps() {
  43. if (gps_active) {
  44. gps_active = false;
  45. _location->stop();
  46. }
  47. }
  48. bool TechoSensorManager::begin() {
  49. Serial1.begin(9600);
  50. // GPS enabled pin
  51. pinMode(GPS_EN, OUTPUT);
  52. return true;
  53. }
  54. bool TechoSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  55. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  56. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  57. }
  58. return true;
  59. }
  60. void TechoSensorManager::loop() {
  61. static long next_gps_update = 0;
  62. if (!gps_active) {
  63. return; // GPS is not active, skip further processing
  64. }
  65. _location->loop();
  66. if (millis() > next_gps_update) {
  67. if (_location->isValid()) {
  68. node_lat = ((double)_location->getLatitude())/1000000.;
  69. node_lon = ((double)_location->getLongitude())/1000000.;
  70. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  71. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  72. }
  73. next_gps_update = millis() + 1000;
  74. }
  75. }
  76. int TechoSensorManager::getNumSettings() const {
  77. return 1; // always show GPS setting
  78. }
  79. const char* TechoSensorManager::getSettingName(int i) const {
  80. return (i == 0) ? "gps" : NULL;
  81. }
  82. const char* TechoSensorManager::getSettingValue(int i) const {
  83. if (i == 0) {
  84. return gps_active ? "1" : "0";
  85. }
  86. return NULL;
  87. }
  88. bool TechoSensorManager::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. }