-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDFRobot_RainfallSensor.cpp
More file actions
198 lines (183 loc) · 5.66 KB
/
DFRobot_RainfallSensor.cpp
File metadata and controls
198 lines (183 loc) · 5.66 KB
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
/*!
* @file DFRobot_RainfallSensor.cpp
* @brief Define infrastructure of DFRobot_RainfallSensor class
* @details This library implements all the functions of communicating with the SEN0575 device, including configuring device parameters and reading device data.
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @license The MIT License (MIT)
* @author [fary](feng.yang@dfrobot.com)
* @version V1.0
* @date 2023-01-28
* @url https://github.com/DFRobot/DFRobot_RainfallSensor
*/
#include "DFRobot_RainfallSensor.h"
DFRobot_RainfallSensor:: DFRobot_RainfallSensor(uint8_t mode){
_mode = mode;
pid = 0;
vid = 0;
}
bool DFRobot_RainfallSensor::begin(void)
{
return getPidVid();
}
String DFRobot_RainfallSensor::getFirmwareVersion(void)
{
uint16_t version = 0;
if(_mode == IIC_MODE){
uint8_t buff[2] = {0};
readRegister(I2C_REG_VERSION, (void*)buff, 2);
version = buff[0] | ( ((uint16_t)buff[1]) << 8 );
}else{
version = readRegister(eInputRegVersionSEN0575);
}
return String( version >> 12 ) + '.' + String( ( ( version >> 8 ) & 0x0F ) ) + '.' + String( ( ( version >> 4 ) & 0x0F ) ) + '.' + String( ( version & 0x0F ) );
}
bool DFRobot_RainfallSensor::getPidVid(void)
{
bool ret = false;
if( _mode == IIC_MODE ){
uint8_t buff[4] = {0};
readRegister( I2C_REG_PID, (void*)buff, 4 );
pid = buff[0] | ( ( (uint16_t)buff[1] ) << 8 ) | ( ( (uint32_t)( buff[3] & 0xC0 ) ) << 10 );
vid = buff[2] | (uint16_t)( ( buff[3] & 0x3F ) << 8 );
}else{
pid = readRegister( eInputRegPidSEN0575 );
vid = readRegister( eInputRegVidSEN0575 );
pid = ( vid & 0xC000 ) << 2 | pid;
vid = vid & 0x3FFF;
}
if( ( vid == 0x3343 ) && ( pid == 0x100C0 ) ){
ret = true;
}
return ret;
}
float DFRobot_RainfallSensor::getRainfall(void)
{
uint32_t rainfall=0;
if(_mode == IIC_MODE){
uint8_t buff[4]={0};
readRegister( I2C_REG_CUMULATIVE_RAINFALL, (void*)buff, 4 );
rainfall = buff[0] | ( ( (uint32_t)buff[1] ) << 8 ) | ( ( (uint32_t)buff[2] ) << 16 ) | ( ( (uint32_t)buff[3]) << 24 );
}else{
rainfall = readRegister( eInputRegCumulativeRainFallHSEN0575 );
rainfall = rainfall << 16 | readRegister( eInputRegCumulativeRainFallLSEN0575 );
}
return rainfall / 10000.0;
}
float DFRobot_RainfallSensor::getRainfall(uint8_t hour)
{
uint32_t rainfall = 0 ;
if(_mode == IIC_MODE){
writeRegister(I2C_REG_RAIN_HOUR, (void*)&hour, 1);
uint8_t buff[4] = {0};
if( readRegister(I2C_REG_TIME_RAINFALL, (void*)buff, 4 ) == 0 ){
return -1;
}
rainfall = buff[0] | ( ( (uint32_t)buff[1] ) << 8 ) | ( ( (uint32_t)buff[2]) << 16 ) | ( ( (uint32_t)buff[3] ) << 24 );
}else{
writeRegister( eHoldingRegRainHourSEN0575, hour );
rainfall = readRegister( eInputRegTimeRainFallHSEN0575 );
rainfall = rainfall << 16 | readRegister( eInputRegTimeRainFallLSEN0575 );
}
return rainfall / 10000.0;
}
uint32_t DFRobot_RainfallSensor::getRawData(void)
{
uint32_t rawdata = 0;
if(_mode == IIC_MODE){
uint8_t buff[4] = { 0 };
readRegister( I2C_REG_RAW_DATA, (void*)buff, 4 );
rawdata = buff[0] | ( ( (uint32_t)buff[1] ) << 8 ) | ( ( (uint32_t)buff[2] ) << 16 ) | ( ( (uint32_t)buff[3]) << 24 );
}else{
rawdata = readRegister( eInputRegRawDataHSEN0575 );
rawdata = rawdata << 16 | readRegister( eInputRegRawDataLSEN0575 );
}
return rawdata;
}
uint8_t DFRobot_RainfallSensor::setRainAccumulatedValue(float value)
{
uint8_t ret = 0;
uint16_t data = value * 10000;
if(_mode == IIC_MODE){
uint8_t buff[2] = { 0 };
buff[0] = ( data & 0xFF );
buff[1] = ( data >> 8 );
ret = writeRegister( I2C_REG_BASE_RAINFALL, (void*)buff, 2 );
}else{
ret = writeRegister( eHoldingRegBaseRainFallSEN0575, data );
}
return ret;
}
float DFRobot_RainfallSensor::getSensorWorkingTime(void)
{
uint16_t WorkingTime = 0 ;
if(_mode == IIC_MODE){
uint8_t buff[2] = { 0 };
readRegister( I2C_REG_SYS_TIME, (void*)buff, 2 );
WorkingTime = buff[0] | ( ( (uint32_t)buff[1] ) << 8 );
}else{
WorkingTime = readRegister( eInputRegSysWorkingTimeSEN0575 );
}
return WorkingTime / 60.0;
}
DFRobot_RainfallSensor_UART::DFRobot_RainfallSensor_UART(Stream *s)
:DFRobot_RainfallSensor(UART_MODE),DFRobot_RTU(s)
{
_deviceAddr = 0xC0;
}
bool DFRobot_RainfallSensor_UART::begin(void)
{
return DFRobot_RainfallSensor::begin();
}
uint16_t DFRobot_RainfallSensor_UART::readRegister(uint16_t reg)
{
setTimeoutTimeMs(1000);
return readInputRegister( (uint8_t)_deviceAddr, (uint16_t)reg );
}
uint16_t DFRobot_RainfallSensor_UART::writeRegister(uint16_t reg,uint16_t data)
{
uint16_t ret=writeHoldingRegister( _deviceAddr, reg, data );
delay(30);
return ret;
}
DFRobot_RainfallSensor_I2C::DFRobot_RainfallSensor_I2C(TwoWire *pWire)
:DFRobot_RainfallSensor(IIC_MODE),_pWire(pWire)
{
_deviceAddr = 0x1D;
}
bool DFRobot_RainfallSensor_I2C::begin(void)
{
_pWire->begin();
return DFRobot_RainfallSensor::begin();
}
uint8_t DFRobot_RainfallSensor_I2C::writeRegister(uint8_t reg, void* pBuf, size_t size)
{
if(pBuf == NULL){
return 1;
}
uint8_t * _pBuf = (uint8_t *)pBuf;
_pWire->beginTransmission(_deviceAddr);
_pWire->write(reg);
for( uint16_t i = 0; i < size; i++ ){
_pWire->write(_pBuf[i]);
}
_pWire->endTransmission();
delay(100);
return 0;
}
uint8_t DFRobot_RainfallSensor_I2C::readRegister(uint8_t reg, void* pBuf, size_t size)
{
if(pBuf == NULL){
return 0;
}
uint8_t * _pBuf = (uint8_t *)pBuf;
_pWire->beginTransmission(_deviceAddr);
_pWire->write(reg);
if(_pWire->endTransmission() != 0 ){
return 0;
}
_pWire->requestFrom(_deviceAddr, (uint8_t)size );
for( uint8_t i=0; i<size; i++ ){
_pBuf[i] = _pWire->read();
}
return size;
}