You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 regels
2.1 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(uint16_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::NO_FIX) break;
  27. mainunit::deepSleep(GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS / 1000);
  28. timeout -= GPS_DEFAULT_INTERMEDIATE_TIMEOUT_MS;
  29. } while (timeout > 1);
  30. if (currentStatus > SIM808_GPS_STATUS::NO_FIX) {
  31. lastStatus = currentStatus;
  32. hardware::sim808::device.getGpsPosition(lastPosition);
  33. }
  34. return currentStatus;
  35. }
  36. void getVelocity(uint8_t &velocity) {
  37. hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::SPEED, &velocity);
  38. }
  39. void getTime(tmElements_t &time) {
  40. char *timeStr;
  41. char buffer[5];
  42. hardware::sim808::device.getGpsField(lastPosition, SIM808_GPS_FIELD::UTC, &timeStr);
  43. VERBOSE_FORMAT("getTime", "%s", timeStr);
  44. time.Year = CalendarYrToTm(details::parseSubstring(buffer, timeStr + TIME_YEAR_OFFSET, 4));
  45. time.Month = details::parseSubstring(buffer, timeStr + TIME_MONTH_OFFSET, 2);
  46. time.Day = details::parseSubstring(buffer, timeStr + TIME_DAY_OFFSET, 2);
  47. time.Hour = details::parseSubstring(buffer, timeStr + TIME_HOUR_OFFSET, 2);
  48. time.Minute = details::parseSubstring(buffer, timeStr + TIME_MINUTE_OFFSET, 2);
  49. time.Second = details::parseSubstring(buffer, timeStr + TIME_SECOND_OFFSET, 2);
  50. VERBOSE_FORMAT("getTime", "%d/%d/%d %d:%d:%d", tmYearToCalendar(time.Year), time.Month, time.Day, time.Hour, time.Minute, time.Second);
  51. }
  52. }