forked from seancaulfield/VEML6075
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VEML6075.cpp
130 lines (98 loc) · 2.92 KB
/
VEML6075.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
/*
* VEML6075.cpp
*
* Arduino library for the Vishay VEML6075 UVA/UVB i2c sensor.
*
* Author: Sean Caulfield <[email protected]>
*
* License: GPLv2.0
*
*/
#include "VEML6075.h"
VEML6075::VEML6075() {
// Despite the datasheet saying this isn't the default on startup, it appears
// like it is. So tell the thing to actually start gathering data.
this->config = 0;
this->config |= VEML6075_CONF_SD_OFF;
// App note only provided math for this one...
this->config |= VEML6075_CONF_IT_100MS;
}
bool VEML6075::begin() {
Wire.begin();
if (this->getDevID() != VEML6075_DEVID) {
return false;
}
// Write config to make sure device is enabled
this->write16(VEML6075_REG_CONF, this->config);
return true;
}
// Poll sensor for latest values and cache them
void VEML6075::poll() {
this->raw_uva = this->read16(VEML6075_REG_UVA);
this->raw_uvb = this->read16(VEML6075_REG_UVB);
this->raw_dark = this->read16(VEML6075_REG_DUMMY);
this->raw_vis = this->read16(VEML6075_REG_UVCOMP1);
this->raw_ir = this->read16(VEML6075_REG_UVCOMP2);
}
uint16_t VEML6075::getRawUVA() {
return this->raw_uva;
}
uint16_t VEML6075::getRawUVB() {
return this->raw_uvb;
}
uint16_t VEML6075::getRawDark() {
return this->raw_dark;
}
uint16_t VEML6075::getRawVisComp() {
return this->raw_vis;
}
uint16_t VEML6075::getRawIRComp() {
return this->raw_ir;
}
uint16_t VEML6075::getDevID() {
return this->read16(VEML6075_REG_DEVID);
}
float VEML6075::getUVA() {
float comp_vis = this->raw_vis - this->raw_dark;
float comp_ir = this->raw_ir - this->raw_dark;
float comp_uva = this->raw_uva - this->raw_dark;
comp_uva -= (VEML6075_UVI_UVA_VIS_COEFF * comp_vis) - (VEML6075_UVI_UVA_IR_COEFF * comp_ir);
return comp_uva;
}
float VEML6075::getUVB() {
float comp_vis = this->raw_vis - this->raw_dark;
float comp_ir = this->raw_ir - this->raw_dark;
float comp_uvb = this->raw_uvb - this->raw_dark;
comp_uvb -= (VEML6075_UVI_UVB_VIS_COEFF * comp_vis) - (VEML6075_UVI_UVB_IR_COEFF * comp_ir);
return comp_uvb;
}
float VEML6075::getUVIndex() {
float uva_weighted = this->getUVA() * VEML6075_UVI_UVA_RESPONSE;
float uvb_weighted = this->getUVB() * VEML6075_UVI_UVB_RESPONSE;
return (uva_weighted + uvb_weighted) / 2.0;
}
uint16_t VEML6075::read16(uint8_t reg) {
uint8_t msb = 0;
uint8_t lsb = 0;
Wire.beginTransmission(VEML6075_ADDR);
Wire.write(reg);
Wire.endTransmission(false);
Wire.requestFrom(VEML6075_ADDR, 2, true);
lsb = Wire.read();
msb = Wire.read();
return (msb << 8) | lsb;
}
void VEML6075::write16(uint8_t reg, uint16_t data) {
Wire.beginTransmission(VEML6075_ADDR);
Wire.write(reg);
Wire.write((uint8_t)(0xFF & (data >> 0))); // LSB
Wire.write((uint8_t)(0xFF & (data >> 8))); // MSB
Wire.endTransmission();
}
void VEML6075::sleep(bool mode) {
if (mode)
this->config |= 1; // Go to sleep
else
this->config &= 254; // Wake up
this->write16(VEML6075_REG_CONF, this->config);
}