This repository has been archived by the owner on Jul 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sensirionflow.cpp
96 lines (81 loc) · 3.45 KB
/
sensirionflow.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
/*
* Copyright (c) 2015, Johannes Winkelmann, [email protected]
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <organization> nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Wire.h>
#include <Arduino.h>
#include "sensirionflow.h"
#include "flowi2chelper.h"
SensirionFlow::SensirionFlow(uint8_t i2cAddress)
: mI2cAddress(i2cAddress),
mScaleFactor(1),
mDimension(-1),
mTimeBase(-1),
mVolumePressureUnit(-1)
{
}
void SensirionFlow::init()
{
const uint8_t CMD_LENGTH = 1;
const uint8_t CMD_READ_USER_REGISTER[CMD_LENGTH] = { 0xE3 };
const uint8_t DATA_LENGTH = 6; // 2 data, 1 crc
uint8_t data[DATA_LENGTH];
// - read user register
if (!I2CHelper::readFromI2C(mI2cAddress, CMD_READ_USER_REGISTER, CMD_LENGTH, data, 3)) {
Serial.print("Failed to read from I2C 1\n");
return;
}
int16_t baseAddress = (data[0] << 8) + data[1];
baseAddress &= 0x70; // EEPROM base address is bits <6..4>
baseAddress >>= 4;
baseAddress *= 0x300;
// - read scale factor
int16_t scaleFactorAddress = (baseAddress + 0x02B6);
scaleFactorAddress <<= 4; // address is a left aligned 12-bit value
uint8_t cmdReadRegister[] = { 0xFA, (scaleFactorAddress >> 8), scaleFactorAddress & 0x00FF };
if (!I2CHelper::readFromI2C(mI2cAddress, cmdReadRegister, 3, data, DATA_LENGTH)) {
Serial.print("Failed to read from I2C 2\n");
return;
}
mScaleFactor = (data[0] << 8) + data[1]; // data[2] = crc
uint16_t measurementUnit = (data[3] << 8) + data[4]; // data[2] = crc
mDimension = measurementUnit & 0xF;
mTimeBase = (measurementUnit >> 4) & 0xF;
mVolumePressureUnit = (measurementUnit >> 8) & 0x1F;
}
float SensirionFlow::readSample()
{
const uint8_t cmdLength = 1;
const uint8_t dataLength = 2;
uint8_t command[cmdLength];
uint8_t data[dataLength];
command[0] = 0xF1;
if (!I2CHelper::readFromI2C(mI2cAddress, command, 1, data, dataLength)) {
Serial.print("Failed to read from I2C 4\n");
return 0;
}
float measurementValue = ((data[0] << 8) + data[1]);
return (measurementValue / mScaleFactor);
}