No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

74 líneas
2.3 KiB

  1. #include "Gps.h"
  2. #include "Config.h"
  3. #include "Debug.h"
  4. #include "Hardware.h"
  5. #include "MainUnit.h"
  6. #define LOGGER_NAME "Gps"
  7. #define TIME_YEAR_OFFSET 0
  8. #define TIME_MONTH_OFFSET 4
  9. #define TIME_DAY_OFFSET 6
  10. #define TIME_HOUR_OFFSET 8
  11. #define TIME_MINUTE_OFFSET 10
  12. #define TIME_SECOND_OFFSET 12
  13. namespace gps {
  14. namespace details {
  15. uint8_t parseSubstring(char *buffer, char *start, uint8_t size) {
  16. strlcpy(buffer, start, size + 1);
  17. return static_cast<uint8_t>(strtoul(buffer, NULL, 10));
  18. }
  19. }
  20. char lastPosition[GPS_POSITION_SIZE];
  21. SIM808_GPS_STATUS lastStatus;
  22. SIM808_GPS_STATUS acquireCurrentPosition(int32_t timeout) {
  23. SIM808_GPS_STATUS currentStatus = SIM808_GPS_STATUS::OFF;
  24. do {
  25. currentStatus = hardware::sim808::device.getGpsStatus();
  26. if (currentStatus > SIM808_GPS_STATUS::FIX) break; //if we have an accurate fix, break right now
  27. NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus);
  28. mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000);
  29. timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
  30. } while (timeout > 1);
  31. if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
  32. lastStatus = currentStatus;
  33. hardware::sim808::device.getGpsPosition(lastPosition);
  34. }
  35. NOTICE_FORMAT("acquireCurrentPosition", "%d", currentStatus);
  36. return currentStatus;
  37. }
  38. uint8_t getVelocity() {
  39. uint8_t velocity;
  40. if (!hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity)) velocity = 0;
  41. VERBOSE_FORMAT("getVelocity", "%d", velocity);
  42. return velocity;
  43. }
  44. void getTime(tmElements_t &time) {
  45. char *timeStr;
  46. char buffer[5];
  47. hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr);
  48. VERBOSE_FORMAT("getTime", "%s", timeStr);
  49. time.Year = CalendarYrToTm(details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4));
  50. time.Month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2);
  51. time.Day = details::parseSubstring(buffer, timeStr + TIME_DAY_OFFSET, 2);
  52. time.Hour = details::parseSubstring(buffer, timeStr + TIME_HOUR_OFFSET, 2);
  53. time.Minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2);
  54. time.Second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2);
  55. NOTICE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  56. }
  57. }