-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSi7005.cpp
128 lines (101 loc) · 3.42 KB
/
Si7005.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
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Jonas Bo Jalling
* ----------------------------------------------------------------------------
*/
#include "Arduino.h"
#include "Si7005.h"
#include <Wire.h>
Si7005::Si7005( int pin )
{
pinMode( pin, OUTPUT );
digitalWrite( pin, HIGH );
_cs_pin = pin;
_last_temperature = 25.0;
_config_reg = 0;
}
bool Si7005::detectSensor( )
{
byte deviceID;
digitalWrite( _cs_pin, LOW ); // Enable the sensor
delay( WAKE_UP_TIME ); // Wait for it to wake up
Wire.beginTransmission( SI7005_ADR );
Wire.write( REG_ID ); // Select the ID register
Wire.endTransmission( false ); // We don't want to release the bus - this is important!
Wire.requestFrom( SI7005_ADR, 1 );
deviceID = Wire.read( ); // Read the ID from the sensor
digitalWrite( _cs_pin, HIGH ); // Disable the sensor
if ( ( deviceID & ID_SAMPLE ) == ID_SI7005 )
{
return true;
}
else
{
return false;
}
}
int Si7005::_doMeasurement( byte configValue )
{
int rawData;
byte measurementStatus;
digitalWrite( _cs_pin, LOW ); // Enable the sensor
delay( WAKE_UP_TIME ); // Wait for it to wake up
Wire.beginTransmission( SI7005_ADR );
Wire.write(REG_CONFIG); // Select the CONFIG register
Wire.write( CONFIG_START | configValue | _config_reg ); // Start measurement of the selected type (Temperature / humidity)
Wire.endTransmission( );
measurementStatus = STATUS_NOT_READY; // Wait for the measurement to finish
while ( measurementStatus & STATUS_NOT_READY )
{
Wire.beginTransmission( SI7005_ADR );
Wire.write( REG_STATUS );
Wire.endTransmission( false ); // We don't want to release the bus - this is important!
Wire.requestFrom( SI7005_ADR, 1 );
measurementStatus = Wire.read( );
}
Wire.beginTransmission( SI7005_ADR );
Wire.write( REG_DATA ); // Select the DATA register
Wire.endTransmission( false );
Wire.requestFrom( SI7005_ADR, 2 ); // Read 2 bytes from the sensor
while ( Wire.available( ) < 2 ) // Wait for data
{;;}
rawData = ( Wire.read() << 8 ); // MSB
rawData |= Wire.read( ); // LSB
digitalWrite( _cs_pin, HIGH ); // Disable the sensor
return rawData;
}
float Si7005::getTemperature( )
{
float rawTemperature;
rawTemperature = _doMeasurement( CONFIG_TEMPERATURE ) >> 2;
_last_temperature = ( rawTemperature / TEMPERATURE_SLOPE ) - TEMPERATURE_OFFSET;
return _last_temperature;
}
float Si7005::getHumidity( )
{
float rawHumidity, curve, linearHumidity;
rawHumidity = _doMeasurement( CONFIG_HUMIDITY ) >> 4;
curve = ( rawHumidity / HUMIDITY_SLOPE ) - HUMIDITY_OFFSET;
linearHumidity = curve - ( (curve * curve) * a2 + curve * a1 + a0);
linearHumidity = linearHumidity + ( _last_temperature - 30 ) * ( linearHumidity * q1 + q0 );
return linearHumidity;
}
void Si7005::enableHeater( )
{
_config_reg |= CONFIG_HEAT;
}
void Si7005::disableHeater( )
{
_config_reg ^= CONFIG_HEAT;
}
void Si7005::enableFastMeasurements( )
{
_config_reg |= CONFIG_FAST;
}
void Si7005::disableFastMeasurements( )
{
_config_reg ^= CONFIG_FAST;
}