target.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <Arduino.h>
  2. #include "target.h"
  3. #include <helpers/ArduinoHelpers.h>
  4. #include <helpers/sensors/MicroNMEALocationProvider.h>
  5. MeshSolarBoard 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, &rtc_clock);
  11. SolarSensorManager sensors = SolarSensorManager(nmea);
  12. #ifdef DISPLAY_CLASS
  13. DISPLAY_CLASS display;
  14. #endif
  15. bool radio_init() {
  16. rtc_clock.begin(Wire);
  17. return radio.std_init(&SPI);
  18. }
  19. mesh::LocalIdentity radio_new_identity() {
  20. RadioNoiseListener rng(radio);
  21. return mesh::LocalIdentity(&rng); // create new random identity
  22. }
  23. void SolarSensorManager::start_gps() {
  24. if (!gps_active) {
  25. gps_active = true;
  26. _location->begin();
  27. }
  28. }
  29. void SolarSensorManager::stop_gps() {
  30. if (gps_active) {
  31. gps_active = false;
  32. _location->stop();
  33. }
  34. }
  35. bool SolarSensorManager::begin() {
  36. Serial1.begin(9600);
  37. // We'll consider GPS detected if we see any data on Serial1
  38. gps_detected = (Serial1.available() > 0);
  39. if (gps_detected) {
  40. MESH_DEBUG_PRINTLN("GPS detected");
  41. } else {
  42. MESH_DEBUG_PRINTLN("No GPS detected");
  43. }
  44. return true;
  45. }
  46. bool SolarSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  47. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  48. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  49. }
  50. return true;
  51. }
  52. void SolarSensorManager::loop() {
  53. static long next_gps_update = 0;
  54. _location->loop();
  55. if (millis() > next_gps_update) {
  56. if (_location->isValid()) {
  57. node_lat = ((double)_location->getLatitude())/1000000.;
  58. node_lon = ((double)_location->getLongitude())/1000000.;
  59. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  60. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  61. }
  62. next_gps_update = millis() + 1000;
  63. }
  64. }
  65. int SolarSensorManager::getNumSettings() const {
  66. return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
  67. }
  68. const char* SolarSensorManager::getSettingName(int i) const {
  69. return (gps_detected && i == 0) ? "gps" : NULL;
  70. }
  71. const char* SolarSensorManager::getSettingValue(int i) const {
  72. if (gps_detected && i == 0) {
  73. return gps_active ? "1" : "0";
  74. }
  75. return NULL;
  76. }
  77. bool SolarSensorManager::setSettingValue(const char* name, const char* value) {
  78. if (gps_detected && strcmp(name, "gps") == 0) {
  79. if (strcmp(value, "0") == 0) {
  80. stop_gps();
  81. } else {
  82. start_gps();
  83. }
  84. return true;
  85. }
  86. return false; // not supported
  87. }