forked from Seeed-Studio/Grove_Sunlight_Sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSI114X.cpp
208 lines (182 loc) · 6.17 KB
/
SI114X.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
/*
SI114X.cpp
A library for Grove - Sunlight Sensor v1.0
Copyright (c) 2015 seeed technology inc.
Website : www.seeed.cc
Author : Fuhua.Chen
Modified Time: June 2015
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "SI114X.h"
#include "Wire.h"
/* --------------------------------------------------------//
default init
*/
void SI114X::DeInit(void) {
//ENABLE UV reading
//these reg must be set to the fixed value
WriteByte(SI114X_UCOEFF0, 0x29);
WriteByte(SI114X_UCOEFF1, 0x89);
WriteByte(SI114X_UCOEFF2, 0x02);
WriteByte(SI114X_UCOEFF3, 0x00);
WriteParamData(SI114X_CHLIST, SI114X_CHLIST_ENUV | SI114X_CHLIST_ENALSIR | SI114X_CHLIST_ENALSVIS |
SI114X_CHLIST_ENPS1);
//
//set LED1 CURRENT(22.4mA)(It is a normal value for many LED)
//
WriteParamData(SI114X_PS1_ADCMUX, SI114X_ADCMUX_LARGE_IR);
WriteByte(SI114X_PS_LED21, SI114X_LED_CURRENT_22MA);
WriteParamData(SI114X_PSLED12_SELECT, SI114X_PSLED12_SELECT_PS1_LED1); //
//
//PS ADC SETTING
//
WriteParamData(SI114X_PS_ADC_GAIN, SI114X_ADC_GAIN_DIV1);
WriteParamData(SI114X_PS_ADC_COUNTER, SI114X_ADC_COUNTER_511ADCCLK);
WriteParamData(SI114X_PS_ADC_MISC, SI114X_ADC_MISC_HIGHRANGE | SI114X_ADC_MISC_ADC_RAWADC);
//
//VIS ADC SETTING
//
WriteParamData(SI114X_ALS_VIS_ADC_GAIN, SI114X_ADC_GAIN_DIV1);
WriteParamData(SI114X_ALS_VIS_ADC_COUNTER, SI114X_ADC_COUNTER_511ADCCLK);
WriteParamData(SI114X_ALS_VIS_ADC_MISC, SI114X_ADC_MISC_HIGHRANGE);
//
//IR ADC SETTING
//
WriteParamData(SI114X_ALS_IR_ADC_GAIN, SI114X_ADC_GAIN_DIV1);
WriteParamData(SI114X_ALS_IR_ADC_COUNTER, SI114X_ADC_COUNTER_511ADCCLK);
WriteParamData(SI114X_ALS_IR_ADC_MISC, SI114X_ADC_MISC_HIGHRANGE);
//
//interrupt enable
//
WriteByte(SI114X_INT_CFG, SI114X_INT_CFG_INTOE);
WriteByte(SI114X_IRQ_ENABLE, SI114X_IRQEN_ALS);
//
//AUTO RUN
//
WriteByte(SI114X_MEAS_RATE0, 0xFF);
WriteByte(SI114X_COMMAND, SI114X_PSALS_AUTO);
}
/* --------------------------------------------------------//
Init the si114x and begin to collect data
*/
bool SI114X::Begin(void) {
Wire.begin();
//
//Init IIC and reset si1145
//
if (ReadByte(SI114X_PART_ID) != 0X45) {
return false;
}
Reset();
//
//INIT
//
DeInit();
return true;
}
/* --------------------------------------------------------//
reset the si114x
inclue IRQ reg, command regs...
*/
void SI114X::Reset(void) {
WriteByte(SI114X_MEAS_RATE0, 0);
WriteByte(SI114X_MEAS_RATE1, 0);
WriteByte(SI114X_IRQ_ENABLE, 0);
WriteByte(SI114X_IRQ_MODE1, 0);
WriteByte(SI114X_IRQ_MODE2, 0);
WriteByte(SI114X_INT_CFG, 0);
WriteByte(SI114X_IRQ_STATUS, 0xFF);
WriteByte(SI114X_COMMAND, SI114X_RESET);
delay(10);
WriteByte(SI114X_HW_KEY, 0x17);
delay(10);
}
/* --------------------------------------------------------//
write one byte into si114x's reg
*/
void SI114X::WriteByte(uint8_t Reg, uint8_t Value) {
Wire.beginTransmission(SI114X_ADDR);
Wire.write(Reg);
Wire.write(Value);
Wire.endTransmission();
}
/* --------------------------------------------------------//
read one byte data from si114x
*/
uint8_t SI114X::ReadByte(uint8_t Reg) {
Wire.beginTransmission(SI114X_ADDR);
Wire.write(Reg);
Wire.endTransmission();
Wire.requestFrom(SI114X_ADDR, 1);
return Wire.read();
}
/* --------------------------------------------------------//
read half word(2 bytes) data from si114x
*/
uint16_t SI114X::ReadHalfWord(uint8_t Reg) {
uint16_t Value;
Wire.beginTransmission(SI114X_ADDR);
Wire.write(Reg);
Wire.endTransmission();
Wire.requestFrom(SI114X_ADDR, 2);
Value = Wire.read();
Value |= (uint16_t)Wire.read() << 8;
return Value;
}
/* --------------------------------------------------------//
read param data
*/
uint8_t SI114X::ReadParamData(uint8_t Reg) {
WriteByte(SI114X_COMMAND, Reg | SI114X_QUERY);
return ReadByte(SI114X_RD);
}
/* --------------------------------------------------------//
writ param data
*/
uint8_t SI114X::WriteParamData(uint8_t Reg, uint8_t Value) {
//write Value into PARAMWR reg first
WriteByte(SI114X_WR, Value);
WriteByte(SI114X_COMMAND, Reg | SI114X_SET);
//SI114X writes value out to PARAM_RD,read and confirm its right
return ReadByte(SI114X_RD);
}
/* --------------------------------------------------------//
Read Visible Value
*/
uint16_t SI114X::ReadVisible(void) {
return ReadHalfWord(SI114X_ALS_VIS_DATA0);
}
/* --------------------------------------------------------//
Read IR Value
*/
uint16_t SI114X::ReadIR(void) {
return ReadHalfWord(SI114X_ALS_IR_DATA0);
}
/* --------------------------------------------------------//
Read Proximity Value
*/
uint16_t SI114X::ReadProximity(uint8_t PSn) {
return ReadHalfWord(PSn);
}
/* --------------------------------------------------------//
Read UV Value
this function is a int value ,but the real value must be div 100
*/
uint16_t SI114X::ReadUV(void) {
return (ReadHalfWord(SI114X_AUX_DATA0_UVINDEX0));
}