forked from ouki-wang/DFRobot_Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFRobot_Sensor.cpp
163 lines (147 loc) · 3.7 KB
/
DFRobot_Sensor.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
/*!
* @file DFRobot_Sensor.cpp
* @brief 定义DFRobot_Sensor 类的基础结构,基础方法的实现
*
* @copyright [DFRobot](http://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
* @author [Ouki]([email protected])
* @version V1.0
* @date 2019-07-13
* @https://github.com/ouki-wang/DFRobot_Sensor
*/
#include <DFRobot_Sensor.h>
DFRobot_Sensor::DFRobot_Sensor(uint8_t mode):
_mode(*((uint8_t*)&mode))
{
}
int DFRobot_Sensor::begin(void)
{
uint8_t id=0;
if(readReg(SENSOR_ADDR_ID,&id,1) != 0){
DBG("bus data access error");
return ERR_DATA_BUS;
}
DBG("real sensor id=");DBG(id);
if(id != DFRobot_Sensor_ID){
return ERR_IC_VERSION;
}
writeReg(SENSOR_ADDR_CONFIG,&_mode,1);
return ERR_OK;
}
uint16_t DFRobot_Sensor::soundStrengthDB(void)
{
sCombinedData_t data;
readReg(SENSOR_ADDR_DATA, &data, sizeof(data));
DBG("sound reg raw data is");
DBG(data.sound);
return data.sound << 3;
}
uint32_t DFRobot_Sensor::lightStrengthLux(void)
{
sCombinedData_t data;
readReg(SENSOR_ADDR_DATA, &data, sizeof(data));
DBG("light reg raw data is");
DBG(data.light);
return data.light * 10000;
}
void DFRobot_Sensor::setLED(uint8_t r, uint8_t g, uint8_t b)
{
sColor_t data={.b=b>>3,.g=g>>2,.r=r>>3};
writeReg(SENSOR_ADDR_LED, &data, sizeof(data));
}
void DFRobot_Sensor::setLED(uint16_t color)
{
writeReg(SENSOR_ADDR_LED, &color, sizeof(color));
}
uint8_t DFRobot_Sensor::switchMode(uint8_t mode)
{
uint8_t tmp;
#ifdef ENABLE_DBG
readReg(SENSOR_ADDR_CONFIG, &tmp, sizeof(tmp));
DBG("before switch Mode, ModeReg = ");
DBG(mode);
#endif
writeReg(SENSOR_ADDR_CONFIG, &mode, sizeof(mode));
#ifdef ENABLE_DBG
readReg(SENSOR_ADDR_CONFIG, &tmp, sizeof(tmp));
DBG("after switch Mode, ModeReg = ");
DBG(tmp);
#endif
}
int DFRobot_Sensor_IIC::begin(void)
{
Wire.begin();
return DFRobot_Sensor::begin();
}
DFRobot_Sensor_IIC::DFRobot_Sensor_IIC(TwoWire *pWire, uint8_t mode)
:DFRobot_Sensor(mode)
{
_deviceAddr = DFRobot_Sensor_IIC_ADDR;
_pWire = pWire;
}
void DFRobot_Sensor_IIC::writeReg(uint8_t reg, void* pBuf, size_t size)
{
uint8_t * _pBuf = (uint8_t *)pBuf;
_pWire->beginTransmission(_deviceAddr);
_pWire->write(®, 1);
for(uint16_t i = 0; i < size; i++){
_pWire->write(_pBuf[i]);
}
_pWire->endTransmission();
}
uint8_t DFRobot_Sensor_IIC::readReg(uint8_t reg, void* pBuf, size_t size)
{
uint8_t * _pBuf = (uint8_t *)pBuf;
_pWire->beginTransmission(_deviceAddr);
_pWire->write(®, 1);
if( _pWire->endTransmission() != 0){
return 0;
}
_pWire->requestFrom(_deviceAddr, (uint8_t) size);
for(uint16_t i = 0; i < size; i++){
_pBuf[i] = _pWire->read();
}
_pWire->endTransmission();
return size;
}
DFRobot_Sensor_SPI::DFRobot_Sensor_SPI(SPIClass *pSpi, uint8_t csPin, uint8_t mode)
:DFRobot_Sensor(mode)
{
_pSpi = pSpi;
_csPin = csPin;
}
int DFRobot_Sensor_SPI::begin(void)
{
pinMode(_csPin, OUTPUT);
_pSpi->begin();
return DFRobot_Sensor::begin();
}
void DFRobot_Sensor_SPI::writeReg(uint8_t reg, void* pBuf, size_t size)
{
uint8_t * _pBuf = (uint8_t *)pBuf;
_pSpi->beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(_csPin, 0);
_pSpi->transfer(reg);
while(size--) {
_pSpi->transfer(*_pBuf);
_pBuf ++;
}
digitalWrite(_csPin, 1);
SPI.endTransaction();
}
uint8_t DFRobot_Sensor_SPI::readReg(uint8_t reg, void* pBuf, size_t size)
{
uint8_t * _pBuf = (uint8_t *)pBuf;
size_t count = 0;
_pSpi->beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));
digitalWrite(_csPin, 0);
_pSpi->transfer(reg);
while(size--) {
*_pBuf = SPI.transfer(0x00);
_pBuf++;
count++;
}
digitalWrite(_csPin, 1);
_pSpi->endTransaction();
return count;
}