PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
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.

ADC_util.h 7.3KB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /* Teensy 4.x, 3.x, LC ADC library
  2. * https://github.com/pedvide/ADC
  3. * Copyright (c) 2020 Pedro Villanueva
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /* util.h: Util functions for ino sketches and tests.
  26. * This would increase the size of the ADC library because of the strings.
  27. */
  28. /*! \page util ADC util
  29. Util functions for ino sketches and tests.
  30. This would increase the size of the ADC library because of the strings.
  31. See the namespace ADC_util for all functions.
  32. */
  33. #ifndef ADC_UTIL_H
  34. #define ADC_UTIL_H
  35. #include <settings_defines.h>
  36. using ADC_Error::ADC_ERROR;
  37. using namespace ADC_settings;
  38. //! Util functions for ino sketches and tests.
  39. namespace ADC_util
  40. {
  41. //! Convert the conversion speed code to text
  42. /** Convert the conversion speed code to text
  43. * \param conv_speed The conversion speed code
  44. * \return the corresponding text
  45. */
  46. const char *getConversionEnumStr(ADC_CONVERSION_SPEED conv_speed)
  47. {
  48. switch (conv_speed)
  49. {
  50. #if defined(ADC_TEENSY_4) // Teensy 4
  51. #else
  52. case ADC_CONVERSION_SPEED::VERY_LOW_SPEED:
  53. return (const char *)"VERY_LOW_SPEED";
  54. #endif
  55. case ADC_CONVERSION_SPEED::LOW_SPEED:
  56. return (const char *)"LOW_SPEED";
  57. case ADC_CONVERSION_SPEED::MED_SPEED:
  58. return (const char *)"MED_SPEED";
  59. case ADC_CONVERSION_SPEED::HIGH_SPEED:
  60. return (const char *)"HIGH_SPEED";
  61. #if defined(ADC_TEENSY_4) // Teensy 4
  62. #else
  63. case ADC_CONVERSION_SPEED::VERY_HIGH_SPEED:
  64. return (const char *)"VERY_HIGH_SPEED";
  65. #endif
  66. #if defined(ADC_TEENSY_4) // Teensy 4
  67. case ADC_CONVERSION_SPEED::ADACK_10:
  68. return (const char *)"ADACK_10";
  69. case ADC_CONVERSION_SPEED::ADACK_20:
  70. return (const char *)"ADACK_20";
  71. #else
  72. case ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS:
  73. return (const char *)"HIGH_SPEED_16BITS";
  74. case ADC_CONVERSION_SPEED::ADACK_2_4:
  75. return (const char *)"ADACK_2_4";
  76. case ADC_CONVERSION_SPEED::ADACK_4_0:
  77. return (const char *)"ADACK_4_0";
  78. case ADC_CONVERSION_SPEED::ADACK_5_2:
  79. return (const char *)"ADACK_5_2";
  80. case ADC_CONVERSION_SPEED::ADACK_6_2:
  81. return (const char *)"ADACK_6_2";
  82. #endif
  83. }
  84. return (const char *)"NONE";
  85. }
  86. //! Convert the sampling speed code to text
  87. /** Convert the sampling speed code to text
  88. * \param samp_speed The sampling speed code
  89. * \return the corresponding text
  90. */
  91. const char *getSamplingEnumStr(ADC_SAMPLING_SPEED samp_speed)
  92. {
  93. switch (samp_speed)
  94. {
  95. case ADC_SAMPLING_SPEED::VERY_LOW_SPEED:
  96. return (const char *)"VERY_LOW_SPEED";
  97. case ADC_SAMPLING_SPEED::LOW_SPEED:
  98. return (const char *)"LOW_SPEED";
  99. case ADC_SAMPLING_SPEED::MED_SPEED:
  100. return (const char *)"MED_SPEED";
  101. case ADC_SAMPLING_SPEED::HIGH_SPEED:
  102. return (const char *)"HIGH_SPEED";
  103. case ADC_SAMPLING_SPEED::VERY_HIGH_SPEED:
  104. return (const char *)"VERY_HIGH_SPEED";
  105. #if defined(ADC_TEENSY_4) // Teensy 4
  106. case ADC_SAMPLING_SPEED::LOW_MED_SPEED:
  107. return (const char *)"LOW_MED_SPEED";
  108. case ADC_SAMPLING_SPEED::MED_HIGH_SPEED:
  109. return (const char *)"MED_HIGH_SPEED";
  110. case ADC_SAMPLING_SPEED::HIGH_VERY_HIGH_SPEED:
  111. return (const char *)"HIGH_VERY_HIGH_SPEED";
  112. #endif
  113. }
  114. return (const char *)"NONE";
  115. }
  116. //! Converts the error code to text.
  117. /** Converts the error code to text.
  118. * \param fail_flag The error code
  119. * \return the corresponding text
  120. */
  121. const char *getStringADCError(ADC_ERROR fail_flag)
  122. {
  123. if (fail_flag != ADC_ERROR::CLEAR)
  124. {
  125. switch (fail_flag)
  126. {
  127. case ADC_ERROR::CALIB:
  128. return (const char *)"Calibration";
  129. case ADC_ERROR::WRONG_PIN:
  130. return (const char *)"Wrong pin";
  131. case ADC_ERROR::ANALOG_READ:
  132. return (const char *)"Analog read";
  133. case ADC_ERROR::COMPARISON:
  134. return (const char *)"Comparison";
  135. case ADC_ERROR::ANALOG_DIFF_READ:
  136. return (const char *)"Analog differential read";
  137. case ADC_ERROR::CONT:
  138. return (const char *)"Continuous read";
  139. case ADC_ERROR::CONT_DIFF:
  140. return (const char *)"Continuous differential read";
  141. case ADC_ERROR::WRONG_ADC:
  142. return (const char *)"Wrong ADC";
  143. case ADC_ERROR::SYNCH:
  144. return (const char *)"Synchronous";
  145. case ADC_ERROR::OTHER:
  146. case ADC_ERROR::CLEAR: // silence warnings
  147. default:
  148. return (const char *)"Unknown";
  149. }
  150. }
  151. return (const char *)"";
  152. }
  153. //! List of possible averages
  154. const uint8_t averages_list[] = {1, 4, 8, 16, 32};
  155. #if defined(ADC_TEENSY_4) // Teensy 4
  156. //! List of possible resolutions
  157. const uint8_t resolutions_list[] = {8, 10, 12};
  158. #else
  159. //! List of possible resolutions
  160. const uint8_t resolutions_list[] = {8, 10, 12, 16};
  161. #endif
  162. #if defined(ADC_TEENSY_4) // Teensy 4
  163. //! List of possible conversion speeds
  164. const ADC_CONVERSION_SPEED conversion_speed_list[] = {
  165. ADC_CONVERSION_SPEED::LOW_SPEED,
  166. ADC_CONVERSION_SPEED::MED_SPEED,
  167. ADC_CONVERSION_SPEED::HIGH_SPEED,
  168. ADC_CONVERSION_SPEED::ADACK_10,
  169. ADC_CONVERSION_SPEED::ADACK_20};
  170. #else
  171. //! List of possible conversion speeds
  172. const ADC_CONVERSION_SPEED conversion_speed_list[] = {
  173. ADC_CONVERSION_SPEED::VERY_LOW_SPEED,
  174. ADC_CONVERSION_SPEED::LOW_SPEED,
  175. ADC_CONVERSION_SPEED::MED_SPEED,
  176. ADC_CONVERSION_SPEED::HIGH_SPEED,
  177. ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS,
  178. ADC_CONVERSION_SPEED::VERY_HIGH_SPEED,
  179. ADC_CONVERSION_SPEED::ADACK_2_4,
  180. ADC_CONVERSION_SPEED::ADACK_4_0,
  181. ADC_CONVERSION_SPEED::ADACK_5_2,
  182. ADC_CONVERSION_SPEED::ADACK_6_2};
  183. #endif
  184. #if defined(ADC_TEENSY_4) // Teensy 4
  185. //! List of possible sampling speeds
  186. const ADC_SAMPLING_SPEED sampling_speed_list[] = {
  187. ADC_SAMPLING_SPEED::VERY_LOW_SPEED,
  188. ADC_SAMPLING_SPEED::LOW_SPEED,
  189. ADC_SAMPLING_SPEED::LOW_MED_SPEED,
  190. ADC_SAMPLING_SPEED::MED_SPEED,
  191. ADC_SAMPLING_SPEED::MED_HIGH_SPEED,
  192. ADC_SAMPLING_SPEED::HIGH_SPEED,
  193. ADC_SAMPLING_SPEED::HIGH_VERY_HIGH_SPEED,
  194. ADC_SAMPLING_SPEED::VERY_HIGH_SPEED};
  195. #else
  196. //! List of possible sampling speeds
  197. const ADC_SAMPLING_SPEED sampling_speed_list[] = {
  198. ADC_SAMPLING_SPEED::VERY_LOW_SPEED,
  199. ADC_SAMPLING_SPEED::LOW_SPEED,
  200. ADC_SAMPLING_SPEED::MED_SPEED,
  201. ADC_SAMPLING_SPEED::HIGH_SPEED,
  202. ADC_SAMPLING_SPEED::VERY_HIGH_SPEED};
  203. #endif
  204. } // namespace ADC_util
  205. using namespace ADC_util;
  206. #endif // ADC_UTIL_H