ADC: ADC_util.h Source File

ADC 8.0 Analog to Digital Conversor library for the Teensy 3.1/3.2 microprocessor

ADC_util.h
1 /* Teensy 4, 3.x, LC ADC library
2 * https://github.com/pedvide/ADC
3 * Copyright (c) 2019 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
26 /* util.h: Util functions for ino sketches and tests.
27 * This would increase the size of the ADC library because of the strings.
28 */
29
36 #ifndef ADC_UTIL_H
37 #define ADC_UTIL_H
38
39 #include <settings_defines.h>
40
41 using ADC_Error::ADC_ERROR;
42 using namespace ADC_settings;
43
45 namespace ADC_util {
46
48
52 const char* getConversionEnumStr(ADC_CONVERSION_SPEED conv_speed) {
53 switch(conv_speed) {
54 #if defined(ADC_TEENSY_4) // Teensy 4
55 #else
56 case ADC_CONVERSION_SPEED::VERY_LOW_SPEED:
57 return (const char*)"VERY_LOW_SPEED";
58 #endif
59 case ADC_CONVERSION_SPEED::LOW_SPEED:
60 return (const char*)"LOW_SPEED";
61 case ADC_CONVERSION_SPEED::MED_SPEED:
62 return (const char*)"MED_SPEED";
63 case ADC_CONVERSION_SPEED::HIGH_SPEED:
64 return (const char*)"HIGH_SPEED";
65 #if defined(ADC_TEENSY_4) // Teensy 4
66 #else
67 case ADC_CONVERSION_SPEED::VERY_HIGH_SPEED:
68 return (const char*)"VERY_HIGH_SPEED";
69 #endif
70 #if defined(ADC_TEENSY_4) // Teensy 4
71 case ADC_CONVERSION_SPEED::ADACK_10:
72 return (const char*)"ADACK_10";
73 case ADC_CONVERSION_SPEED::ADACK_20:
74 return (const char*)"ADACK_20";
75 #else
76 case ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS:
77 return (const char*)"HIGH_SPEED_16BITS";
78 case ADC_CONVERSION_SPEED::ADACK_2_4:
79 return (const char*)"ADACK_2_4";
80 case ADC_CONVERSION_SPEED::ADACK_4_0:
81 return (const char*)"ADACK_4_0";
82 case ADC_CONVERSION_SPEED::ADACK_5_2:
83 return (const char*)"ADACK_5_2";
84 case ADC_CONVERSION_SPEED::ADACK_6_2:
85 return (const char*)"ADACK_6_2";
86 #endif
87 }
88 return (const char*)"NONE";
89 }
90
92
96 const char* getSamplingEnumStr(ADC_SAMPLING_SPEED samp_speed) {
97 switch(samp_speed) {
98 case ADC_SAMPLING_SPEED::VERY_LOW_SPEED:
99 return (const char*)"VERY_LOW_SPEED";
100 case ADC_SAMPLING_SPEED::LOW_SPEED:
101 return (const char*)"LOW_SPEED";
102 case ADC_SAMPLING_SPEED::MED_SPEED:
103 return (const char*)"MED_SPEED";
104 case ADC_SAMPLING_SPEED::HIGH_SPEED:
105 return (const char*)"HIGH_SPEED";
106 case ADC_SAMPLING_SPEED::VERY_HIGH_SPEED:
107 return (const char*)"VERY_HIGH_SPEED";
108 #if defined(ADC_TEENSY_4) // Teensy 4
109 case ADC_SAMPLING_SPEED::LOW_MED_SPEED:
110 return (const char*)"LOW_MED_SPEED";
111 case ADC_SAMPLING_SPEED::MED_HIGH_SPEED:
112 return (const char*)"MED_HIGH_SPEED";
113 case ADC_SAMPLING_SPEED::HIGH_VERY_HIGH_SPEED:
114 return (const char*)"HIGH_VERY_HIGH_SPEED";
115 #endif
116 }
117 return (const char*)"NONE";
118 }
119
121
125 const char* getStringADCError(ADC_ERROR fail_flag) {
126 if(fail_flag != ADC_ERROR::CLEAR) {
127 switch(fail_flag) {
128 case ADC_ERROR::CALIB:
129 return (const char*)"Calibration";
130 case ADC_ERROR::WRONG_PIN:
131 return (const char*)"Wrong pin";
132 case ADC_ERROR::ANALOG_READ:
133 return (const char*)"Analog read";
134 case ADC_ERROR::COMPARISON:
135 return (const char*)"Comparison";
136 case ADC_ERROR::ANALOG_DIFF_READ:
137 return (const char*)"Analog differential read";
138 case ADC_ERROR::CONT:
139 return (const char*)"Continuous read";
140 case ADC_ERROR::CONT_DIFF:
141 return (const char*)"Continuous differential read";
142 case ADC_ERROR::WRONG_ADC:
143 return (const char*)"Wrong ADC";
144 case ADC_ERROR::SYNCH:
145 return (const char*)"Synchronous";
146 case ADC_ERROR::OTHER:
147 case ADC_ERROR::CLEAR: // silence warnings
148 default:
149 return (const char*)"Unknown";
150 }
151 }
152 return (const char*)"";
153 }
154
156 const uint8_t averages_list[] = {1, 4, 8, 16, 32};
157
158 #if defined(ADC_TEENSY_4) // Teensy 4
159 const uint8_t resolutions_list[] = {8, 10, 12};
161 #else
162 const uint8_t resolutions_list[] = {8, 10, 12, 16};
164 #endif
165
166 #if defined(ADC_TEENSY_4) // Teensy 4
167 const ADC_CONVERSION_SPEED conversion_speed_list[] = {
169 ADC_CONVERSION_SPEED::LOW_SPEED,
170 ADC_CONVERSION_SPEED::MED_SPEED,
171 ADC_CONVERSION_SPEED::HIGH_SPEED,
172 ADC_CONVERSION_SPEED::ADACK_10,
173 ADC_CONVERSION_SPEED::ADACK_20};
174 #else
175 const ADC_CONVERSION_SPEED conversion_speed_list[] = {
177 ADC_CONVERSION_SPEED::VERY_LOW_SPEED,
178 ADC_CONVERSION_SPEED::LOW_SPEED,
179 ADC_CONVERSION_SPEED::MED_SPEED,
180 ADC_CONVERSION_SPEED::HIGH_SPEED,
181 ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS,
182 ADC_CONVERSION_SPEED::VERY_HIGH_SPEED,
183 ADC_CONVERSION_SPEED::ADACK_2_4,
184 ADC_CONVERSION_SPEED::ADACK_4_0,
185 ADC_CONVERSION_SPEED::ADACK_5_2,
186 ADC_CONVERSION_SPEED::ADACK_6_2};
187 #endif
188
189 #if defined(ADC_TEENSY_4) // Teensy 4
190 const ADC_SAMPLING_SPEED sampling_speed_list[] = {
192 ADC_SAMPLING_SPEED::VERY_LOW_SPEED,
193 ADC_SAMPLING_SPEED::LOW_SPEED,
194 ADC_SAMPLING_SPEED::LOW_MED_SPEED,
195 ADC_SAMPLING_SPEED::MED_SPEED,
196 ADC_SAMPLING_SPEED::MED_HIGH_SPEED,
197 ADC_SAMPLING_SPEED::HIGH_SPEED,
198 ADC_SAMPLING_SPEED::HIGH_VERY_HIGH_SPEED,
199 ADC_SAMPLING_SPEED::VERY_HIGH_SPEED
200 };
201 #else
202 const ADC_SAMPLING_SPEED sampling_speed_list[] = {
204 ADC_SAMPLING_SPEED::VERY_LOW_SPEED,
205 ADC_SAMPLING_SPEED::LOW_SPEED,
206 ADC_SAMPLING_SPEED::MED_SPEED,
207 ADC_SAMPLING_SPEED::HIGH_SPEED,
208 ADC_SAMPLING_SPEED::VERY_HIGH_SPEED
209 };
210 #endif
211
212 } // namespace util
213
214 using namespace ADC_util;
215
216 #endif // ADC_UTIL_H
ADC_util::getStringADCError
const char * getStringADCError(ADC_ERROR fail_flag)
Converts the error code to text.
Definition: ADC_util.h:125
ADC_settings::ADC_CONVERSION_SPEED::ADACK_5_2
@ ADACK_5_2
ADC_util::getSamplingEnumStr
const char * getSamplingEnumStr(ADC_SAMPLING_SPEED samp_speed)
Convert the sampling speed code to text.
Definition: ADC_util.h:96
ADC_util
Util functions for ino sketches and tests.
Definition: ADC_util.h:45
ADC_settings::ADC_CONVERSION_SPEED::ADACK_4_0
@ ADACK_4_0
ADC_settings::ADC_CONVERSION_SPEED::ADACK_2_4
@ ADACK_2_4
ADC_settings::ADC_CONVERSION_SPEED::LOW_SPEED
@ LOW_SPEED
ADC_settings
Board-dependent settings.
Definition: settings_defines.h:37
ADC_settings::ADC_SAMPLING_SPEED::VERY_HIGH_SPEED
@ VERY_HIGH_SPEED
ADC_settings::ADC_SAMPLING_SPEED::MED_SPEED
@ MED_SPEED
ADC_util::averages_list
const uint8_t averages_list[]
List of possible averages.
Definition: ADC_util.h:156
ADC_settings::ADC_CONVERSION_SPEED::VERY_LOW_SPEED
@ VERY_LOW_SPEED
ADC_util::sampling_speed_list
const ADC_SAMPLING_SPEED sampling_speed_list[]
List of possible sampling speeds.
Definition: ADC_util.h:203
ADC_settings::ADC_SAMPLING_SPEED
ADC_SAMPLING_SPEED
Definition: settings_defines.h:514
ADC_settings::ADC_CONVERSION_SPEED::VERY_HIGH_SPEED
@ VERY_HIGH_SPEED
ADC_util::getConversionEnumStr
const char * getConversionEnumStr(ADC_CONVERSION_SPEED conv_speed)
Convert the conversion speed code to text.
Definition: ADC_util.h:52
ADC_settings::ADC_CONVERSION_SPEED::ADACK_6_2
@ ADACK_6_2
ADC_settings::ADC_CONVERSION_SPEED::HIGH_SPEED
@ HIGH_SPEED
ADC_settings::ADC_CONVERSION_SPEED::MED_SPEED
@ MED_SPEED
ADC_util::conversion_speed_list
const ADC_CONVERSION_SPEED conversion_speed_list[]
List of possible conversion speeds.
Definition: ADC_util.h:176
ADC_Error::ADC_ERROR
ADC_ERROR
ADC errors.
Definition: settings_defines.h:566
ADC_util::resolutions_list
const uint8_t resolutions_list[]
List of possible resolutions.
Definition: ADC_util.h:163
ADC_settings::ADC_SAMPLING_SPEED::VERY_LOW_SPEED
@ VERY_LOW_SPEED
ADC_settings::ADC_SAMPLING_SPEED::LOW_SPEED
@ LOW_SPEED
ADC_settings::ADC_SAMPLING_SPEED::HIGH_SPEED
@ HIGH_SPEED
ADC_settings::ADC_CONVERSION_SPEED::HIGH_SPEED_16BITS
@ HIGH_SPEED_16BITS
ADC_settings::ADC_CONVERSION_SPEED
ADC_CONVERSION_SPEED
Definition: settings_defines.h:487