-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfs6122.cpp
225 lines (184 loc) · 7 KB
/
fs6122.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**************************************************************************/
/*!
@file fs6122.cpp
@author Patrick Wilkie (Melt Collective)
@license BSD (see license.txt)
This is a library for the FS6122 Bi-Directional Medical Respiratory Sensor
----> http://www.siargo.us/fs6122-mass-flow-sensor.html
Melt Collective invests time and resources providing this open
source code. Please support Melt Collective and open-source
hardware development, either directly through your contributions,
or by buying our products and sharing our creations.
Some code adapted from Adafruit_MMA8451 library!!! Thank you to Adafruit :)
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include <fs6122.h>
/**************************************************************************/
/*!
@brief Abstract away platform differences in Arduino wire library
*/
/**************************************************************************/
static inline uint8_t i2cread(void) {
#if ARDUINO >= 100
return Wire.read();
#else
return Wire.receive();
#endif
}
static inline void i2cwrite(uint8_t x) {
#if ARDUINO >= 100
Wire.write((uint8_t)x);
#else
Wire.send(x);
#endif
}
/**************************************************************************/
/*!
@brief Writes 8-bits to the specified destination register
*/
/**************************************************************************/
void fs6122::writeRegister8(uint8_t reg, uint8_t value) {
Wire.beginTransmission(_i2caddr);
i2cwrite((uint8_t)reg);
i2cwrite((uint8_t)(value));
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads 8-bits from the specified register
*/
/**************************************************************************/
uint8_t fs6122::readRegister8(uint8_t reg) {
//undocumented version of requestFrom handles repeated starts on Arduino Due
#ifdef __SAM3X8E__
Wire.requestFrom(_i2caddr, 1, reg, 1, true);
#else
//I don't know - maybe the other verion of requestFrom works on all platforms.
// honestly, I don't want to go through and test them all. Doing it this way
// is already known to work on everything else
Wire.beginTransmission(_i2caddr);
i2cwrite(reg);
Wire.endTransmission(false); // MMA8451 + friends uses repeated start!!
Wire.requestFrom(_i2caddr, 1); // requests 1 byte from i2c device register.
#endif
if (! Wire.available()) return -1;
return (i2cread());
}
/**************************************************************************/
/*!
@brief Write the i2c address for the FS6122
*/
/**************************************************************************/
void fs6122::write_address(uint8_t value)
{
writeRegister8(FS6122_WRITE_ADDRESS, value);
}
/**************************************************************************/
/*!
@brief Read the i2c address from the FS6122
*/
/**************************************************************************/
void fs6122::read_address(void)
{
addr = readRegister8(FS6122_READ_ADDRESS);
}
/**************************************************************************/
/*!
@brief Instantiates a new fs6122 class in I2C mode
*/
/**************************************************************************/
fs6122::fs6122(int64_t sensorID) {
_sensorID = sensorID;
}
/**************************************************************************/
/*!
@brief Setups the HW (reads coefficients values, etc.)
*/
/**************************************************************************/
bool fs6122::begin(uint8_t i2caddr) {
Wire.begin();
_i2caddr = i2caddr;
writeRegister8(FS6122_WRITE_FILTERDEPTH, 0xFE); // set filter depth to maximum (254)
return true;
}
/**************************************************************************/
/*!
@brief Reads flow rate and pressure
*/
/**************************************************************************/
void fs6122::read_flowrate_pressure(void) {
// read flow rate and pressure at once
Wire.beginTransmission(_i2caddr);
i2cwrite(FS6122_READ_FLOWRATE_PRESSURE);
Wire.endTransmission(false); // many devices use repeated start!!
Wire.requestFrom(_i2caddr, 8); // request 8 bytes of data
flow_rate = Wire.read(); flow_rate <<= 8; // shift by byte each read
flow_rate |= Wire.read(); flow_rate <<= 8;
flow_rate |= Wire.read(); flow_rate <<= 8;
flow_rate |= Wire.read(); flow_rate >>= 2; // value is 14 bit, so shift by 2
pressure = Wire.read(); pressure <<= 8;
pressure |= Wire.read(); pressure <<= 8;
pressure |= Wire.read(); pressure <<= 8;
pressure |= Wire.read(); pressure >>= 2; // value is 14 bit, so shift by 2
uint32_t divider = 1000; // divide both values by 1000 (TODO: CHECK: is this a problem, using 1024 instead?!)
flow_rate_slpm = (float)flow_rate / divider;
pressure_cmh2o = (float)pressure / divider;
}
/**************************************************************************/
/*!
@brief Reads temperature in degrees celcius
*/
/**************************************************************************/
void fs6122::read_temperature(void) {
// read flow rate and pressure at once
Wire.beginTransmission(_i2caddr);
i2cwrite(FS6122_READ_TEMPERATURE);
Wire.endTransmission(false); // many devices use repeated start!!
Wire.requestFrom(_i2caddr, 2); // request 2 bytes of data
temperature = Wire.read(); temperature <<= 8; // shift by byte each read
temperature |= Wire.read();
uint16_t divider = 100; // divide both values by 1000
temperature_c = (float)temperature / divider;
}
/**************************************************************************/
/*!
@brief Reads relative humidity
*/
/**************************************************************************/
void fs6122::read_humidity(void) {
// read flow rate and pressure at once
Wire.beginTransmission(_i2caddr);
i2cwrite(FS6122_READ_HUMIDITY);
Wire.endTransmission(false); // many devices use repeated start!!
Wire.requestFrom(_i2caddr, 2); // request 2 bytes of data
humidity = Wire.read(); humidity <<= 8; // shift by byte each read
humidity |= Wire.read();
uint16_t divider = 100; // divide both values by 1000
humidity_prh = (float)humidity / divider;
}
/**************************************************************************/
/*!
@brief Calibrate the offset of flow rate for the FS6122
*/
/**************************************************************************/
void fs6122::cal_flowrate(int8_t value)
{
writeRegister8(FS6122_CAL_FLOWRATE_OFFSET, value);
}
/**************************************************************************/
/*!
@brief Calibrate the offset of pressure for the FS6122
*/
/**************************************************************************/
void fs6122::cal_pressure(int8_t value)
{
writeRegister8(FS6122_CAL_PRESSURE_OFFSET, value);
}