-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnalogSensor.h
75 lines (68 loc) · 1.78 KB
/
AnalogSensor.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
#ifndef ANALOG_SENSOR_H
#define ANALOG_SENSOR_H
#include <Arduino.h>
#include <String.h>
#include <ArduinoJson.h>
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include "Filter.h"
#include "LowPassFilter.h"
class AnalogSensor
{
public:
AnalogSensor()
{
mFilter = nullptr;
mAtten = ADC_ATTEN_11db;
}
void loadFromJson(JsonObject json)
{
mPin = json["pin"].as<int>();
mChannel = (adc1_channel_t) mPin;
mName = json["name"].as<String>();
if (mFilter != nullptr)
{
delete mFilter;
mFilter = nullptr;
}
if (json.containsKey("filter"))
{
if (json["filter"]["type"].as<String>().compareTo("lowpass") == 0)
mFilter = new LowPassFilter();
if (mFilter != nullptr)
mFilter->loadFromJson(json["filter"]);
}
if (json.containsKey("attenuation"))
{
String attenuation = json["attenuation"].as<String>();
if (attenuation.compareTo("0dB") == 0)
mAtten = ADC_ATTEN_0db;
else if (attenuation.compareTo("2.5dB") == 0)
mAtten = ADC_ATTEN_2_5db;
else if (attenuation.compareTo("6dB") == 0)
mAtten = ADC_ATTEN_6db;
else if (attenuation.compareTo("11dB") == 0)
mAtten = ADC_ATTEN_11db;
}
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(mChannel, mAtten);
esp_adc_cal_characterize(ADC_UNIT_1, mAtten, ADC_WIDTH_BIT_12, 1100, &mADCChars);
}
virtual float readValue(float dt)
{
float value = ((float) (esp_adc_cal_raw_to_voltage(adc1_get_raw(mChannel), &mADCChars)));
// Convert millivolts to volts
value /= 1000.0;
if (mFilter != nullptr)
value = mFilter->filter(value, dt);
return value;
}
private:
int mPin;
String mName;
Filter * mFilter;
adc1_channel_t mChannel;
adc_atten_t mAtten;
esp_adc_cal_characteristics_t mADCChars;
};
#endif /* ANALOG_SENSOR_H */