AP_Common.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. ///
  14. /// @file AP_Common.h
  15. /// @brief Common definitions and utility routines for the ArduPilot
  16. /// libraries.
  17. ///
  18. #pragma once
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. // used to pack structures
  22. #define PACKED __attribute__((__packed__))
  23. // used to weaken symbols
  24. #define WEAK __attribute__((__weak__))
  25. // used to mark a function that may be unused in some builds
  26. #define UNUSED_FUNCTION __attribute__((unused))
  27. // this can be used to optimize individual functions
  28. #define OPTIMIZE(level) __attribute__((optimize(level)))
  29. // sometimes we need to prevent inlining to prevent large stack usage
  30. #define NOINLINE __attribute__((noinline))
  31. #define FMT_PRINTF(a,b) __attribute__((format(printf, a, b)))
  32. #define FMT_SCANF(a,b) __attribute__((format(scanf, a, b)))
  33. #ifdef __has_cpp_attribute
  34. # if __has_cpp_attribute(fallthrough)
  35. # define FALLTHROUGH [[fallthrough]]
  36. # elif __has_cpp_attribute(gnu::fallthrough)
  37. # define FALLTHROUGH [[gnu::fallthrough]]
  38. # endif
  39. #endif
  40. #ifndef FALLTHROUGH
  41. # define FALLTHROUGH
  42. #endif
  43. #ifdef __GNUC__
  44. #define WARN_IF_UNUSED __attribute__ ((warn_unused_result))
  45. #else
  46. #define WARN_IF_UNUSED
  47. #endif
  48. #define NORETURN __attribute__ ((noreturn))
  49. #define ToRad(x) radians(x) // *pi/180
  50. #define ToDeg(x) degrees(x) // *180/pi
  51. /* Declare and implement const and non-const versions of the array subscript
  52. * operator. The object is treated as an array of type_ values. */
  53. #define DEFINE_BYTE_ARRAY_METHODS \
  54. inline uint8_t &operator[](size_t i) { return reinterpret_cast<uint8_t *>(this)[i]; } \
  55. inline uint8_t operator[](size_t i) const { return reinterpret_cast<const uint8_t *>(this)[i]; }
  56. /*
  57. check if bit bitnumber is set in value, returned as a
  58. bool. Bitnumber starts at 0 for the first bit
  59. */
  60. #define BIT_IS_SET(value, bitnumber) (((value) & (1U<<(bitnumber))) != 0)
  61. // get high or low bytes from 2 byte integer
  62. #define LOWBYTE(i) ((uint8_t)(i))
  63. #define HIGHBYTE(i) ((uint8_t)(((uint16_t)(i))>>8))
  64. #define ARRAY_SIZE(_arr) (sizeof(_arr) / sizeof(_arr[0]))
  65. #define UINT16_VALUE(hbyte, lbyte) (static_cast<uint16_t>((hbyte<<8)|lbyte))
  66. /*
  67. * See UNUSED_RESULT. The difference is that it receives @uniq_ as the name to
  68. * be used for its internal variable.
  69. *
  70. * @uniq_: a unique name to use for variable name
  71. * @expr_: the expression to be evaluated
  72. */
  73. #define _UNUSED_RESULT(uniq_, expr_) \
  74. do { \
  75. decltype(expr_) uniq_ __attribute__((unused)); \
  76. uniq_ = expr_; \
  77. } while (0)
  78. /*
  79. * Allow to call a function annotated with warn_unused_result attribute
  80. * without getting a warning, because sometimes this is what we want to do.
  81. *
  82. * @expr_: the expression to be evaluated
  83. */
  84. #define UNUSED_RESULT(expr_) _UNUSED_RESULT(__unique_name_##__COUNTER__, expr_)
  85. // @}
  86. // assert_storage_size template: assert that the memory used to store an
  87. // item is of a specific size.
  88. // example invocation:
  89. // assert_storage_size<class Location, 16> _assert_storage_size_Location;
  90. // templates are used for this because the compiler's output will
  91. // usually contain details of the template instantiation so you can
  92. // see how the actual size differs from the expected size.
  93. template<typename s, size_t s_size, size_t t> struct _assert_storage_size {
  94. static_assert(s_size == t, "wrong size");
  95. };
  96. template<typename s, size_t t> struct assert_storage_size {
  97. _assert_storage_size<s, sizeof(s), t> _member;
  98. };
  99. ////////////////////////////////////////////////////////////////////////////////
  100. /// @name Conversions
  101. ///
  102. /// Conversion macros and factors.
  103. ///
  104. //@{
  105. /*
  106. Return true if value is between lower and upper bound inclusive.
  107. False otherwise.
  108. */
  109. bool is_bounded_int32(int32_t value, int32_t lower_bound, int32_t upper_bound);
  110. bool hex_to_uint8(uint8_t a, uint8_t &res); // return the uint8 value of an ascii hex character