-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncVEML6070.cpp
91 lines (82 loc) · 2.21 KB
/
SyncVEML6070.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
#include "SyncVEML6070.h"
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* VEML6070 RELATED FUNCTIONS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
SyncVEML6070::SyncVEML6070(){
}
/*!
* @brief Initialize communication with the VEML607
* @return void
*/
void SyncVEML6070::begin(veml6070RefreshTime refreshTime){
Wire.begin();
Wire.beginTransmission(VEML6070_ADDR_LSB);
uint8_t error = Wire.endTransmission();
if(error == 0){
_veml6070I2CAddress = VEML6070_ADDR_LSB;
}
else{
Wire.beginTransmission(VEML6070_ADDR_MSB);
error = Wire.endTransmission();
if(error == 0){
_veml6070I2CAddress = VEML6070_ADDR_MSB;
}
}
_commandReg.IT = refreshTime;
writeWithoutRegister(_veml6070I2CAddress, _commandReg.get());
}
/*!
* @brief Read UV sensor value
* @return UV index
*/
uint16_t SyncVEML6070::getUV(){
sleep(false);
waitForNext();
if (Wire.requestFrom(VEML6070_ADDR_MSB, 1) != 1)
return -1;
uint16_t uvi = Wire.read();
uvi <<= 8;
if (Wire.requestFrom(VEML6070_ADDR_LSB, 1) != 1)
return -1;
uvi |= Wire.read();
sleep(true);
return uvi;
}
/*!
* @brief Map the integration time code to the correct multiple. Wait for next reading
* Ref. Datasheet rev 1.6 (Jul 2015), page 08 [table 4]
* Depends on RSET value. 62.5ms for RSET = 300K in datasheet table
* @return void
*/
void SyncVEML6070::waitForNext(){
uint8_t itCount = 1;
for (uint8_t i = _commandReg.IT; i > 0; i--) {
itCount *= 2;
}
delay(62.5 * itCount);
}
/*!
* @brief Enter or exit sleep mode. While in sleep mode the chip draws ~1uA
* @param mode -true: sleep mode, false: wake mode
* @return void
*/
void SyncVEML6070::sleep(bool state){
_commandReg.SD = state;
writeWithoutRegister(_veml6070I2CAddress, _commandReg.get());
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* BASIC FUNCTIONS
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/*!
* @brief Writes an 8 bit value over I2C
* @param I2CAddress -Slave device address
* data -Data
* @return value from selected register (one byte)
*/
void SyncVEML6070::writeWithoutRegister(uint8_t I2CAddress, uint8_t data){
Wire.beginTransmission(I2CAddress);
Wire.write(data);
Wire.endTransmission();
}