nmea_parser.h 6.8 KB

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