AskSin++
Veml6070.h
1 /***************************************************
2  This is a library for the Adafruit VEML6070 UV Sensor Breakout
3 
4  Designed specifically to work with the VEML6070 sensor from Adafruit
5  ----> https://www.adafruit.com/products/2899
6 
7  These sensors use I2C to communicate, 2 pins are required to
8  interface
9  Adafruit invests time and resources providing this open source code,
10  please support Adafruit and open-source hardware by purchasing
11  products from Adafruit!
12 
13  Written by Limor Fried/Ladyada for Adafruit Industries.
14  BSD license, all text above must be included in any redistribution
15  ****************************************************/
16 //- -----------------------------------------------------------------------------------------------------------------------
17 // AskSin++
18 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
19 // 2018-07-04 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
20 //- -----------------------------------------------------------------------------------------------------------------------
21 
22 #ifndef __SENSORS_VEML6070_h__
23 #define __SENSORS_VEML6070_h__
24 
25 #define VEML6070_ADDR_H 0x39
26 #define VEML6070_ADDR_L 0x38
27 
28 #if (ARDUINO >= 100)
29 #include "Arduino.h"
30 #else
31 #include "WProgram.h"
32 #endif
33 
34 #include <Sensors.h>
35 #include <Wire.h>
36 
37 typedef enum veml6070_integrationtime {
38  VEML6070_HALF_T,
39  VEML6070_1_T,
40  VEML6070_2_T,
41  VEML6070_4_T,
42 } veml6070_integrationtime_t;
43 
44 namespace as {
45 
46 template <uint8_t INTEGRATION_TIME=VEML6070_1_T>
47 class Veml6070 : public Sensor {
48  uint16_t _uvvalue;
49  uint8_t _uvindex;
50 public:
51  Veml6070 () {}
52 
53  uint16_t readUV() {
54  if (Wire.requestFrom(VEML6070_ADDR_H, 1) != 1) return -1;
55  uint16_t uvi = Wire.read();
56  uvi <<= 8;
57  if (Wire.requestFrom(VEML6070_ADDR_L, 1) != 1) return -1;
58  uvi |= Wire.read();
59  return uvi;
60  }
61 
62  void init () {
63  Wire.begin();
64  Wire.beginTransmission(VEML6070_ADDR_L);
65  Wire.write((INTEGRATION_TIME << 2) | 0x02);
66  Wire.endTransmission();
67  delay(500);
68  _present = (readUV() < 65535);
69  }
70 
71  void measure (__attribute__((unused)) bool async=false) {
72  if( present() == true ) {
73  _uvvalue = readUV();
74  if (INTEGRATION_TIME == VEML6070_HALF_T) {
75  DPRINTLN(F("UV Index calc. with VEML6070_HALF_T not supported"));
76  return;
77  }
78  uint8_t _integration_time_factor = (INTEGRATION_TIME == VEML6070_4_T) ? 4 : INTEGRATION_TIME;
79  _uvindex = (_uvvalue < (12 * (187 * _integration_time_factor))) ? (_uvvalue + 1) / (187 * _integration_time_factor) : 11;
80  }
81  }
82  uint16_t UVValue () { return _uvvalue; }
83  uint8_t UVIndex () { return _uvindex; }
84 };
85 
86 }
87 
88 #endif
as::Veml6070
Definition: Veml6070.h:47
as::Sensor
Definition: Sensors.h:11