-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVoltageSensor.cpp
94 lines (86 loc) · 2.45 KB
/
VoltageSensor.cpp
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
#include "Arduino.h"
#include <esp_adc_cal.h>
#include "system.h"
#include "VoltageSensor.h"
VoltageSensor::VoltageSensor()
{
this->atten = ADC_ATTEN_DB_0;
this->battery_min = BATTERY_MIN;
this->battery_max = BATTERY_MAX;
this->type = READING_TYPE_VOLTAGE;
this->source = 0;
this->value = 0;
this->value_parsed = 0.0f;
}
VoltageSensor::VoltageSensor(byte pin)
{
this->pin = pin;
this->analog = true;
this->atten = ADC_ATTEN_DB_0;
this->battery_min = BATTERY_MIN;
this->battery_max = BATTERY_MAX;
this->type = READING_TYPE_VOLTAGE;
this->source = this->pin;
this->value = 0;
this->value_parsed = 0.0f;
}
VoltageSensor::VoltageSensor(byte pin, esp_adc_cal_characteristics_t *adc_chars)
{
this->pin = pin;
this->analog = true;
this->atten = ADC_ATTEN_DB_0;
this->adc_chars = adc_chars;
this->battery_min = BATTERY_MIN;
this->battery_max = BATTERY_MAX;
this->type = READING_TYPE_VOLTAGE;
this->source = this->pin;
this->value = 0;
this->value_parsed = 0.0f;
}
void VoltageSensor::display() {
Serial.println("#####");
Serial.println("Voltage Sensor");
Sensor::display();
}
int VoltageSensor::read() {
//Read voltage a few times as the first reading after sleep result is often garbage
for (byte i = 0; i < 2; i++) {
adc1_channel_t channelNum;
//Set number of cycles per sample 1 - 255
analogSetCycles(16);
switch (this->pin) {
case (36):
channelNum = ADC1_CHANNEL_0;
break;
case (39):
channelNum = ADC1_CHANNEL_3;
break;
case (34):
channelNum = ADC1_CHANNEL_6;
break;
case (32):
channelNum = ADC1_CHANNEL_4;
break;
case (33):
channelNum = ADC1_CHANNEL_5;
break;
case (27):
channelNum = ADC1_CHANNEL_7;
break;
}
adc1_config_channel_atten(channelNum, this->atten);
this->value = esp_adc_cal_raw_to_voltage(analogRead(pin), adc_chars);
this->value = this->value / (R2 / (R1 + R2)); //reverse voltage divider
delay(2000);
}
VoltageSensor::parse();
return this->value;
}
float VoltageSensor::parse() {
this->value_parsed = (((float)this->value - (float)this->battery_min) / ((float)this->battery_max - (float)this->battery_min)) * 100;
this->value_parsed = roundf(this->value_parsed * 100) / 100;
if (this->value_parsed > 100) {
this->value_parsed = 100.0f;
}
return this->value_parsed ;
}