-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.cpp
162 lines (123 loc) · 3.36 KB
/
datasource.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
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
#include "datasource.h"
using namespace std;
int readAnalog(char location) {
return analogRead(location);
};
readerFunc analogReader = &readAnalog;
DataSource::DataSource() {
this->reader = &analogReader;
};
void DataSource::setReader(readerFunc *reader) {
this->reader = reader;
};
AnalogSensor::AnalogSensor(char location) : DataSource() {
this->location = location;
}
void AnalogSensor::read() {
measurement = (*reader)(this->location);
}
int AnalogSensor::raw(void) {
return this->measurement;
}
TestSensor::TestSensor(
word minLevel,
word maxLevel,
byte speed
) : DataSource(), GaugeComponent() {
this->minLevel = minLevel;
this->maxLevel = maxLevel;
this->speed = speed;
this->measurement = minLevel;
}
void TestSensor::read(void) {}
void TestSensor::tick(void) {
if (this->measurement > this->maxLevel && this->direction == 1) {
this->direction = 0;
}
if (this->measurement < (this->minLevel + this->speed) && this->direction == 0) {
this->direction = 1;
}
if (this->direction == 1) {
this->measurement += this->speed;
} else {
this->measurement -= this->speed;
}
}
void TestSensor::init(void) {}
String TestSensor::format(void) {
float adjusted = ((float) measurement / 10 - 50);
char charBuf[10];
String formatted = dtostrf(adjusted, 5, 1, charBuf);
return formatted;
};
String TestSensor::unit(void) {
return "unit";
};
int TestSensor::raw(void) {
return measurement;
};
MPXSensor::MPXSensor(char pin, char adcValueOffset, float error) :
AnalogSensor(pin), GaugeComponent() {
this->error = error;
this->adcValueOffset = adcValueOffset;
}
float MPXSensor::toKpaAbs() {
float localMeasurement = (
((float) this->measurement - this->adcValueOffset) /
((float) AnalogSensor::V_RESOLUTION_INV) /
((float) this->getMilliVoltPerKpa() / 1000) +
this->getKpaOffset()) *
(1 + this->error);
return localMeasurement;
}
float MPXSensor::toKpaRel() {
return toKpaAbs() - ((float) PressureSensor::ONE_ATM_KPA / 10);
}
float MPXSensor::toPsiAbs() {
return this->toKpaAbs() *
((float) PressureSensor::ONE_ATM_PSI / 10) /
((float) PressureSensor::ONE_ATM_KPA / 10);
}
float MPXSensor::toPsiRel() {
return this->toKpaRel() *
((float) PressureSensor::ONE_ATM_PSI / 10) /
((float) PressureSensor::ONE_ATM_KPA / 10);
}
void MPXSensor::init(void) {}
String MPXSensor::format(void) {
float level = toPsiRel();
char charBuf[10];
String formatted = dtostrf(level, 5, 1, charBuf);
return formatted;
}
String MPXSensor::unit(void) {
return "psi";
}
void MPXSensor::tick(void) {
this->read();
}
MPX4250Sensor::MPX4250Sensor(
char pin,
byte adcValueOffset,
float error
) : MPXSensor(pin, adcValueOffset, error) {}
char MPX4250Sensor::getMilliVoltPerKpa() {
return this->mV_PER_KPA;
}
char MPX4250Sensor::getKpaOffset() {
return this->KPA_OFFSET_AT_ZERO_V;
}
MPX5500Sensor::MPX5500Sensor(
char pin,
byte adcValueOffset,
float error
) : MPXSensor(pin, adcValueOffset, error) {}
char MPX5500Sensor::getMilliVoltPerKpa() {
return this->mV_PER_KPA;
}
String MPX5500Sensor::format(void) {
float level = toPsiAbs();
char charBuf[10];
String formatted = dtostrf(level, 5, 1, charBuf);
return formatted;
}