-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnalogIn.h
199 lines (176 loc) · 4.37 KB
/
AnalogIn.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
* AnalogIn.h
*
* Created on: Ene 2018
* Author: raulMrello
*
* Portabilidad del driver AnalogIn
* NOTAS:
* ESP32 dispone de dos ADC dependiendo de la fuente analógica:
* ADC1 utiliza entradas del sistema como la tensión Vdd, sensores cap, etc...
* ADC2 utiliza pines GPIO, por lo tanto en función de la entrada seleccionada se utilizará uno u otro ADC.
*
*/
#ifndef MBED_ANALOGIN_H
#define MBED_ANALOGIN_H
#include "mbed_api.h"
#include "Mutex.h"
class AnalogIn {
public:
/** Create an AnalogIn, connected to the specified pin
*
* @param pin AnalogIn pin to connect to
*/
AnalogIn(PinName pin) {
switch(pin){
case GPIO_NUM_36:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_0;
break;
case GPIO_NUM_37:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_1;
break;
case GPIO_NUM_38:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_2;
break;
case GPIO_NUM_39:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_3;
break;
case GPIO_NUM_33:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_4;
break;
case GPIO_NUM_32:
_channel = ADC_CHANNEL_5;
_unit = ADC_UNIT_1;
break;
case GPIO_NUM_34:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_6;
break;
case GPIO_NUM_35:
_unit = ADC_UNIT_1;
_channel = ADC_CHANNEL_7;
break;
case GPIO_NUM_4:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_0;
break;
case GPIO_NUM_0:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_1;
break;
case GPIO_NUM_2:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_2;
break;
case GPIO_NUM_15:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_3;
break;
case GPIO_NUM_13:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_4;
break;
case GPIO_NUM_12:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_5;
break;
case GPIO_NUM_14:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_6;
break;
case GPIO_NUM_27:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_7;
break;
case GPIO_NUM_25:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_8;
break;
case GPIO_NUM_26:
_unit = ADC_UNIT_2;
_channel = ADC_CHANNEL_9;
break;
default:
return;
}
lock();
if(_unit == ADC_UNIT_1){
adc1_config_channel_atten((adc1_channel_t)_channel, ADC_ATTEN_DB_11);
adc1_config_width(ADC_WIDTH_BIT_12);
}
else{
adc2_config_channel_atten((adc2_channel_t)_channel, ADC_ATTEN_DB_11);
}
unlock();
}
/** Read the input voltage, represented as a float in the range [0.0, 1.0]
*
* @returns A floating-point value representing the current input voltage, measured as a percentage
*/
float read() {
return ((float)read_u16())/Scale;
}
/** Read the input voltage, represented as an unsigned short in the range [0x0, 0xFFFF]
*
* @returns
* 16-bit unsigned short representing the current input voltage, normalised to a 16-bit value
*/
unsigned short read_u16() {
if(_unit == ADC_UNIT_1){
lock();
int data = adc1_get_raw((adc1_channel_t)_channel);
unlock();
if(data != -1){
return (unsigned short)data;
}
return 0;
}
else{
lock();
int data = -1;
if(adc2_get_raw((adc2_channel_t)_channel, ADC_WIDTH_12Bit, &data) == ESP_OK){
unlock();
return (unsigned short)data;
}
unlock();
return 0;
}
}
/** An operator shorthand for read()
*
* The float() operator can be used as a shorthand for read() to simplify common code sequences
*
* Example:
* @code
* float x = volume.read();
* float x = volume;
*
* if(volume.read() > 0.25) { ... }
* if(volume > 0.25) { ... }
* @endcode
*/
operator float() {
// Underlying call is thread safe
return read();
}
virtual ~AnalogIn() {
// Do nothing
}
protected:
static const uint16_t Scale = 4095; //!< Fondo de escala por defecto para 12-bits
virtual void lock() {
_mutex.lock();
}
virtual void unlock() {
_mutex.unlock();
}
adc_unit_t _unit;
adc_channel_t _channel;
static Mutex _mutex;
};
#endif