target.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. uint32_t radio_get_rng_seed() {
  20. return radio.random(0x7FFFFFFF);
  21. }
  22. void radio_set_params(float freq, float bw, uint8_t sf, uint8_t cr) {
  23. radio.setFrequency(freq);
  24. radio.setSpreadingFactor(sf);
  25. radio.setBandwidth(bw);
  26. radio.setCodingRate(cr);
  27. radio_driver.updatePreamble(sf);
  28. }
  29. void radio_set_tx_power(int8_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 SolarSensorManager::start_gps() {
  37. if (!gps_active) {
  38. gps_active = true;
  39. _location->begin();
  40. }
  41. }
  42. void SolarSensorManager::stop_gps() {
  43. if (gps_active) {
  44. gps_active = false;
  45. _location->stop();
  46. }
  47. }
  48. bool SolarSensorManager::begin() {
  49. Serial1.begin(9600);
  50. // We'll consider GPS detected if we see any data on Serial1
  51. gps_detected = (Serial1.available() > 0);
  52. if (gps_detected) {
  53. MESH_DEBUG_PRINTLN("GPS detected");
  54. } else {
  55. MESH_DEBUG_PRINTLN("No GPS detected");
  56. }
  57. return true;
  58. }
  59. bool SolarSensorManager::querySensors(uint8_t requester_permissions, CayenneLPP& telemetry) {
  60. if (requester_permissions & TELEM_PERM_LOCATION) { // does requester have permission?
  61. telemetry.addGPS(TELEM_CHANNEL_SELF, node_lat, node_lon, node_altitude);
  62. }
  63. return true;
  64. }
  65. void SolarSensorManager::loop() {
  66. static long next_gps_update = 0;
  67. _location->loop();
  68. if (millis() > next_gps_update) {
  69. if (_location->isValid()) {
  70. node_lat = ((double)_location->getLatitude())/1000000.;
  71. node_lon = ((double)_location->getLongitude())/1000000.;
  72. node_altitude = ((double)_location->getAltitude()) / 1000.0;
  73. MESH_DEBUG_PRINTLN("lat %f lon %f", node_lat, node_lon);
  74. }
  75. next_gps_update = millis() + 1000;
  76. }
  77. }
  78. int SolarSensorManager::getNumSettings() const {
  79. return gps_detected ? 1 : 0; // only show GPS setting if GPS is detected
  80. }
  81. const char* SolarSensorManager::getSettingName(int i) const {
  82. return (gps_detected && i == 0) ? "gps" : NULL;
  83. }
  84. const char* SolarSensorManager::getSettingValue(int i) const {
  85. if (gps_detected && i == 0) {
  86. return gps_active ? "1" : "0";
  87. }
  88. return NULL;
  89. }
  90. bool SolarSensorManager::setSettingValue(const char* name, const char* value) {
  91. if (gps_detected && strcmp(name, "gps") == 0) {
  92. if (strcmp(value, "0") == 0) {
  93. stop_gps();
  94. } else {
  95. start_gps();
  96. }
  97. return true;
  98. }
  99. return false; // not supported
  100. }