summaryrefslogtreecommitdiff
path: root/src/driver/veml6075.cc
blob: dd6df2537e120c968c9037fecbdd0e1ef16a5b72 (plain)
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
/*
 * Copyright 2020 Birte Kristina Friesel
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Driver for VEML6075 Ambient Light Sensor.
 */
#include "driver/veml6075.h"
#if defined(CONFIG_meta_driver_hardware_i2c)
#include "driver/i2c.h"
#elif defined(CONFIG_driver_softi2c)
#include "driver/soft_i2c.h"
#endif

bool VEML6075::init()
{
	txbuf[0] = 0x00;
	txbuf[1] = 0x00; // 50ms integration time, normal dynamic
	if (i2c.xmit(address, 2, txbuf, 0, rxbuf) != 0) {
		return false;
	}
	return true;
}

bool VEML6075::readUVCounts(float *uva, float *uvb)
{
	uint16_t uva_counts, uvb_counts, comp_visible, comp_ir;
	txbuf[0] = 0x07;
	if (i2c.xmit(address, 1, txbuf, 2, rxbuf) != 0) {
		return false;
	}
	uva_counts = ((uint16_t)rxbuf[1] << 8) + rxbuf[0];

	txbuf[0] = 0x09;
	if (i2c.xmit(address, 1, txbuf, 2, rxbuf) != 0) {
		return false;
	}
	uvb_counts = ((uint16_t)rxbuf[1] << 8) + rxbuf[0];

	txbuf[0] = 0x0a;
	if (i2c.xmit(address, 1, txbuf, 2, rxbuf) != 0) {
		return false;
	}
	comp_visible = ((uint16_t)rxbuf[1] << 8) + rxbuf[0];

	txbuf[0] = 0x0b;
	if (i2c.xmit(address, 1, txbuf, 2, rxbuf) != 0) {
		return false;
	}
	comp_ir = ((uint16_t)rxbuf[1] << 8) + rxbuf[0];

	*uva = uva_counts - uva_a_coef * comp_visible - uva_b_coef * comp_ir;
	*uvb = uvb_counts - uvb_c_coef * comp_visible - uvb_d_coef * comp_ir;

	return true;
}

bool VEML6075::readUV(float *uva, float *uvb)
{
	float uva_counts, uvb_counts;
	if (!readUVCounts(&uva_counts, &uvb_counts)) {
		return false;
	}

	*uva = uva_counts * uva_counts_per_uwcm2;
	*uvb = uvb_counts * uvb_counts_per_uwcm2;
	return true;
}

bool VEML6075::readUVI(float *uva, float *uvb)
{
	float uva_counts, uvb_counts;
	if (!readUVCounts(&uva_counts, &uvb_counts)) {
		return false;
	}

	*uva = uva_counts * uva_uvi_response;
	*uvb = uvb_counts * uvb_uvi_response;
	return true;
}

VEML6075 veml6075(0x10);