nmea_parser.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. #include "esp_types.h"
  11. #include "esp_event.h"
  12. #include "esp_err.h"
  13. #include "driver/uart.h"
  14. #define GPS_MAX_SATELLITES_IN_USE (12)
  15. #define GPS_MAX_SATELLITES_IN_VIEW (16)
  16. /**
  17. * @brief Declare of NMEA Parser Event base
  18. *
  19. */
  20. ESP_EVENT_DECLARE_BASE(ESP_NMEA_EVENT);
  21. /**
  22. * @brief GPS fix type
  23. *
  24. */
  25. typedef enum {
  26. GPS_FIX_INVALID, /*!< Not fixed */
  27. GPS_FIX_GPS, /*!< GPS */
  28. GPS_FIX_DGPS, /*!< Differential GPS */
  29. } gps_fix_t;
  30. /**
  31. * @brief GPS fix mode
  32. *
  33. */
  34. typedef enum {
  35. GPS_MODE_INVALID = 1, /*!< Not fixed */
  36. GPS_MODE_2D, /*!< 2D GPS */
  37. GPS_MODE_3D /*!< 3D GPS */
  38. } gps_fix_mode_t;
  39. /**
  40. * @brief GPS satellite information
  41. *
  42. */
  43. typedef struct {
  44. uint8_t num; /*!< Satellite number */
  45. uint8_t elevation; /*!< Satellite elevation */
  46. uint16_t azimuth; /*!< Satellite azimuth */
  47. uint8_t snr; /*!< Satellite signal noise ratio */
  48. } gps_satellite_t;
  49. /**
  50. * @brief GPS time
  51. *
  52. */
  53. typedef struct {
  54. uint8_t hour; /*!< Hour */
  55. uint8_t minute; /*!< Minute */
  56. uint8_t second; /*!< Second */
  57. uint16_t thousand; /*!< Thousand */
  58. } gps_time_t;
  59. /**
  60. * @brief GPS date
  61. *
  62. */
  63. typedef struct {
  64. uint8_t day; /*!< Day (start from 1) */
  65. uint8_t month; /*!< Month (start from 1) */
  66. uint16_t year; /*!< Year (start from 2000) */
  67. } gps_date_t;
  68. /**
  69. * @brief NMEA Statement
  70. *
  71. */
  72. typedef enum {
  73. STATEMENT_UNKNOWN = 0, /*!< Unknown statement */
  74. STATEMENT_GGA, /*!< GGA */
  75. STATEMENT_GSA, /*!< GSA */
  76. STATEMENT_RMC, /*!< RMC */
  77. STATEMENT_GSV, /*!< GSV */
  78. STATEMENT_GLL, /*!< GLL */
  79. STATEMENT_VTG /*!< VTG */
  80. } nmea_statement_t;
  81. /**
  82. * @brief GPS object
  83. *
  84. */
  85. typedef struct {
  86. float latitude; /*!< Latitude (degrees) */
  87. float longitude; /*!< Longitude (degrees) */
  88. float altitude; /*!< Altitude (meters) */
  89. gps_fix_t fix; /*!< Fix status */
  90. uint8_t sats_in_use; /*!< Number of satellites in use */
  91. gps_time_t tim; /*!< time in UTC */
  92. gps_fix_mode_t fix_mode; /*!< Fix mode */
  93. uint8_t sats_id_in_use[GPS_MAX_SATELLITES_IN_USE]; /*!< ID list of satellite in use */
  94. float dop_h; /*!< Horizontal dilution of precision */
  95. float dop_p; /*!< Position dilution of precision */
  96. float dop_v; /*!< Vertical dilution of precision */
  97. uint8_t sats_in_view; /*!< Number of satellites in view */
  98. gps_satellite_t sats_desc_in_view[GPS_MAX_SATELLITES_IN_VIEW]; /*!< Information of satellites in view */
  99. gps_date_t date; /*!< Fix date */
  100. bool valid; /*!< GPS validity */
  101. float speed; /*!< Ground speed, unit: m/s */
  102. float cog; /*!< Course over ground */
  103. float variation; /*!< Magnetic variation */
  104. } gps_t;
  105. /**
  106. * @brief Configuration of NMEA Parser
  107. *
  108. */
  109. typedef struct {
  110. struct {
  111. uart_port_t uart_port; /*!< UART port number */
  112. uint32_t rx_pin; /*!< UART Rx Pin number */
  113. uint32_t baud_rate; /*!< UART baud rate */
  114. uart_word_length_t data_bits; /*!< UART data bits length */
  115. uart_parity_t parity; /*!< UART parity */
  116. uart_stop_bits_t stop_bits; /*!< UART stop bits length */
  117. uint32_t event_queue_size; /*!< UART event queue size */
  118. } uart; /*!< UART specific configuration */
  119. } nmea_parser_config_t;
  120. /**
  121. * @brief NMEA Parser Handle
  122. *
  123. */
  124. typedef void *nmea_parser_handle_t;
  125. /**
  126. * @brief Default configuration for NMEA Parser
  127. *
  128. */
  129. #define NMEA_PARSER_CONFIG_DEFAULT() \
  130. { \
  131. .uart = { \
  132. .uart_port = UART_NUM_1, \
  133. .rx_pin = CONFIG_NMEA_PARSER_UART_RXD,\
  134. .baud_rate = 9600, \
  135. .data_bits = UART_DATA_8_BITS, \
  136. .parity = UART_PARITY_DISABLE, \
  137. .stop_bits = UART_STOP_BITS_1, \
  138. .event_queue_size = 16 \
  139. } \
  140. }
  141. /**
  142. * @brief NMEA Parser Event ID
  143. *
  144. */
  145. typedef enum {
  146. GPS_UPDATE, /*!< GPS information has been updated */
  147. GPS_UNKNOWN /*!< Unknown statements detected */
  148. } nmea_event_id_t;
  149. /**
  150. * @brief Init NMEA Parser
  151. *
  152. * @param config Configuration of NMEA Parser
  153. * @return nmea_parser_handle_t handle of NMEA parser
  154. */
  155. nmea_parser_handle_t nmea_parser_init(const nmea_parser_config_t *config);
  156. /**
  157. * @brief Deinit NMEA Parser
  158. *
  159. * @param nmea_hdl handle of NMEA parser
  160. * @return esp_err_t ESP_OK on success, ESP_FAIL on error
  161. */
  162. esp_err_t nmea_parser_deinit(nmea_parser_handle_t nmea_hdl);
  163. /**
  164. * @brief Add user defined handler for NMEA parser
  165. *
  166. * @param nmea_hdl handle of NMEA parser
  167. * @param event_handler user defined event handler
  168. * @param handler_args handler specific arguments
  169. * @return esp_err_t
  170. * - ESP_OK: Success
  171. * - ESP_ERR_NO_MEM: Cannot allocate memory for the handler
  172. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  173. * - Others: Fail
  174. */
  175. esp_err_t nmea_parser_add_handler(nmea_parser_handle_t nmea_hdl, esp_event_handler_t event_handler, void *handler_args);
  176. /**
  177. * @brief Remove user defined handler for NMEA parser
  178. *
  179. * @param nmea_hdl handle of NMEA parser
  180. * @param event_handler user defined event handler
  181. * @return esp_err_t
  182. * - ESP_OK: Success
  183. * - ESP_ERR_INVALIG_ARG: Invalid combination of event base and event id
  184. * - Others: Fail
  185. */
  186. esp_err_t nmea_parser_remove_handler(nmea_parser_handle_t nmea_hdl, esp_event_handler_t event_handler);
  187. #ifdef __cplusplus
  188. }
  189. #endif