-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSFM3X00.cpp
168 lines (116 loc) · 3.05 KB
/
SFM3X00.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
163
164
165
166
167
168
/*
* =====================================================================================
*
* Filename: SFM3X000.cpp
*
* Description: Senserion SFM3X00 library
*
* Version: 1.0
* Created: 04/16/2020 16:59:40
*
* Organization: Public Invention
*
* License: Sensirion BSD 3-Clause License
*
* =====================================================================================
*/
#include "SFM3X00.h"
void SFM3X00::sendCommand(uint16_t command)
{
//Serial.println();
//Serial.println(command, HEX);
uint8_t b1 = (command & 0xFF00) >> 8;
//Serial.println(b1, HEX);
uint8_t b0 = command & 0x00FF;
//Serial.println(b0, HEX);
Wire.beginTransmission(byte(this->sensorAddress));
Wire.write(byte(b1));
Wire.write(byte(b0));
Wire.endTransmission();
}
uint16_t SFM3X00::readData()
{
uint8_t b[2];
Wire.requestFrom(this->sensorAddress, 2);
b[1] = Wire.read();
b[0] = Wire.read();
uint16_t c {0};
c = (b[1] << 8) | b[0];
//Serial.println(c, HEX);
//Serial.println(c, DEC);
return c;
}
uint32_t SFM3X00::requestSerialNumber()
{
sendCommand(READ_SERIAL_NUMBER_U);
uint16_t upperBytes = readData();
sendCommand(READ_SERIAL_NUMBER_L);
uint16_t lowerBytes = readData();
uint32_t serialNumber {0};
serialNumber = ((uint32_t)upperBytes << 16) | lowerBytes;
return serialNumber;
}
uint32_t SFM3X00::requestArticleNumber()
{
sendCommand(READ_ARTICLE_NUMBER_U);
uint16_t upperBytes = readData();
sendCommand(READ_ARTICLE_NUMBER_L);
uint16_t lowerBytes = readData();
uint32_t articleNumber {0};
articleNumber = ((uint32_t)upperBytes << 16) | lowerBytes;
return articleNumber;
}
uint16_t SFM3X00::requestScaleFactor()
{
sendCommand(READ_SCALE_FACTOR);
int16_t scaleFactor = readData();
return scaleFactor;
}
uint16_t SFM3X00::requestOffset()
{
sendCommand(READ_FLOW_OFFSET);
uint16_t offset = readData();
return offset;
}
void SFM3X00::setupFlowSensor()
{
this->serialNumber = requestSerialNumber();
this->articleNumber = requestArticleNumber();
this->flowOffset = requestOffset();
this->flowScale = requestScaleFactor();
if(this-> flowScale == 800.0)
{
this->minFlow = SFM3400_MIN;
this->maxFlow = SFM3400_MAX;
}
else if(this-> flowScale == 120.0)
{
this->minFlow = SFM3200_MIN;
this->maxFlow = SFM3200_MAX;
}
}
void SFM3X00::startContinuousMeasurement()
{
sendCommand(START_CONTINUOUS_MEASUREMENT);
}
void SFM3X00::begin()
{
this->setupFlowSensor();
this->startContinuousMeasurement();
}
float SFM3X00::readFlow()
{
uint16_t rawFlow = readData();
float flow = ((float)rawFlow - this->flowOffset) / this->flowScale;
return flow;
}
bool SFM3X00::checkRange(uint16_t rawFlow)
{
return ((rawFlow <= this->minFlow) || (rawFlow >= this-> maxFlow));
}
bool SFM3X00::checkRange(float computedFlow)
{
float min_f = ((float)this->minFlow - this->flowOffset) / this->flowScale;
float max_f = ((float)this->maxFlow - this->flowOffset) / this->flowScale;
return ((computedFlow <= min_f) || (computedFlow >= max_f));
}