1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_VL6180X.h
3
+ *
4
+ * Device driver for the VL6180X ToF sensor.
5
+ *
6
+ * Adafruit invests time and resources providing this open source code,
7
+ * please support Adafruit and open-source hardware by purchasing
8
+ * products from Adafruit!
9
+ *
10
+ * Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+ #ifndef WipperSnapper_I2C_Driver_VL6180X_H
16
+ #define WipperSnapper_I2C_Driver_VL6180X_H
17
+
18
+ #include " WipperSnapper_I2C_Driver.h"
19
+ #include " Wippersnapper.h"
20
+ #include < Adafruit_VL6180X.h>
21
+
22
+ /* *************************************************************************/
23
+ /* !
24
+ @brief Class that provides a driver interface for a VL6180X sensor.
25
+ */
26
+ /* *************************************************************************/
27
+ class WipperSnapper_I2C_Driver_VL6180X : public WipperSnapper_I2C_Driver {
28
+ public:
29
+ /* ******************************************************************************/
30
+ /* !
31
+ @brief Constructor for a VL6180X sensor.
32
+ @param i2c
33
+ The I2C interface.
34
+ @param sensorAddress
35
+ 7-bit device address.
36
+ */
37
+ /* ******************************************************************************/
38
+ WipperSnapper_I2C_Driver_VL6180X (TwoWire *i2c, uint16_t sensorAddress)
39
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
40
+ _i2c = i2c;
41
+ _sensorAddress = sensorAddress;
42
+ }
43
+
44
+ /* ******************************************************************************/
45
+ /* !
46
+ @brief Destructor for an VL6180X sensor.
47
+ */
48
+ /* ******************************************************************************/
49
+ ~WipperSnapper_I2C_Driver_VL6180X () {
50
+ // Called when a VL6180X component is deleted.
51
+ delete _vl6180x;
52
+ }
53
+
54
+ /* ******************************************************************************/
55
+ /* !
56
+ @brief Initializes the VL6180X sensor and begins I2C.
57
+ @returns True if initialized successfully, False otherwise.
58
+ */
59
+ /* ******************************************************************************/
60
+ bool begin () {
61
+ _vl6180x = new Adafruit_VL6180X (_sensorAddress);
62
+ return _vl6180x->begin (_i2c);
63
+ }
64
+
65
+ /* ******************************************************************************/
66
+ /* !
67
+ @brief Gets the VL6180X's current proximity.
68
+ @param proximityEvent
69
+ Pointer to an Adafruit_Sensor event.
70
+ @returns True if the proximity was obtained successfully, False
71
+ otherwise.
72
+ */
73
+ /* ******************************************************************************/
74
+ bool getEventProximity (sensors_event_t *proximityEvent) {
75
+ uint8_t range = _vl6180x->readRange ();
76
+ uint8_t status = _vl6180x->readRangeStatus ();
77
+
78
+ if (status != VL6180X_ERROR_NONE) {
79
+ if ((status >= VL6180X_ERROR_SYSERR_1) &&
80
+ (status <= VL6180X_ERROR_SYSERR_5)) {
81
+ WS_DEBUG_PRINTLN (" VL6180X: System error" );
82
+ } else if (status == VL6180X_ERROR_ECEFAIL) {
83
+ WS_DEBUG_PRINTLN (" VL6180X: ECE failure" );
84
+ } else if (status == VL6180X_ERROR_NOCONVERGE) {
85
+ WS_DEBUG_PRINTLN (" VL6180X: No convergence" );
86
+ } else if (status == VL6180X_ERROR_RANGEIGNORE) {
87
+ WS_DEBUG_PRINTLN (" VL6180X: Ignoring range" );
88
+ } else if (status == VL6180X_ERROR_SNR) {
89
+ WS_DEBUG_PRINTLN (" VL6180X: Signal/Noise error" );
90
+ } else if (status == VL6180X_ERROR_RAWUFLOW) {
91
+ WS_DEBUG_PRINTLN (" VL6180X: Raw reading underflow" );
92
+ } else if (status == VL6180X_ERROR_RAWOFLOW) {
93
+ WS_DEBUG_PRINTLN (" VL6180X: Raw reading overflow" );
94
+ } else if (status == VL6180X_ERROR_RANGEUFLOW) {
95
+ WS_DEBUG_PRINTLN (" VL6180X: Range reading underflow" );
96
+ } else if (status == VL6180X_ERROR_RANGEOFLOW) {
97
+ WS_DEBUG_PRINTLN (" VL6180X: Range reading overflow" );
98
+ }
99
+ return false ;
100
+ }
101
+
102
+ if (range <= 0 || range > 150 ) {
103
+ return false ;
104
+ }
105
+ proximityEvent->data [0 ] = range;
106
+ return true ;
107
+ }
108
+
109
+ /* ******************************************************************************/
110
+ /* !
111
+ @brief Performs a light sensor reading.
112
+ @param lightEvent
113
+ Light sensor reading, in lux.
114
+ @returns True if the sensor event was obtained successfully, False
115
+ otherwise.
116
+ */
117
+ /* ******************************************************************************/
118
+ bool getEventLight (sensors_event_t *lightEvent) {
119
+ // TODO: Update when I2C Sensor Properties allow setting custom Gain, etc.
120
+ float notRealLux = _vl6180x->readLux (VL6180X_ALS_GAIN_5);
121
+ // Gain_5 results in max 41.6klux with cover glass - See 2.10.3 in datasheet
122
+ if (notRealLux < 0 || notRealLux > 41600 ) {
123
+ return false ;
124
+ }
125
+ lightEvent->light = notRealLux;
126
+ return true ;
127
+ }
128
+
129
+ protected:
130
+ Adafruit_VL6180X *_vl6180x; // /< Pointer to VL6180X temperature sensor object
131
+ };
132
+
133
+ #endif // WipperSnapper_I2C_Driver_VL6180X
0 commit comments