Browse Source

Update firmware libraries

Florian Eitel 1 year ago
parent
commit
f4139b27c8
No account linked to committer's email address

+ 500
- 351
firmware/libraries/Adafruit_APDS9960_Library/Adafruit_APDS9960.cpp View File

@@ -1,94 +1,96 @@
1
-/**************************************************************************/
2
-/*! 
3
-    @file     Adafruit_APDS9960.cpp
4
-    @author   Ladyada, Dean Miller (Adafruit Industries)
5
-
6
-    @section LICENSE
7
-
8
-    Software License Agreement (BSD License)
9
-
10
-    Copyright (c) 2017, Adafruit Industries
11
-    All rights reserved.
12
-
13
-    Redistribution and use in source and binary forms, with or without
14
-    modification, are permitted provided that the following conditions are met:
15
-    1. Redistributions of source code must retain the above copyright
16
-    notice, this list of conditions and the following disclaimer.
17
-    2. Redistributions in binary form must reproduce the above copyright
18
-    notice, this list of conditions and the following disclaimer in the
19
-    documentation and/or other materials provided with the distribution.
20
-    3. Neither the name of the copyright holders nor the
21
-    names of its contributors may be used to endorse or promote products
22
-    derived from this software without specific prior written permission.
23
-
24
-    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25
-    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26
-    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
-    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28
-    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29
-    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
-    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31
-    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
-    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
-    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
-*/
35
-/**************************************************************************/
1
+/*!
2
+ *  @file Adafruit_APDS9960.cpp
3
+ *
4
+ *  @mainpage Adafruit APDS9960 Proximity, Light, RGB, and Gesture Sensor
5
+ *
6
+ *  @section author Author
7
+ *
8
+ *  Ladyada, Dean Miller (Adafruit Industries)
9
+ *
10
+ *  @section license License
11
+ *
12
+ *  Software License Agreement (BSD License)
13
+ *
14
+ *  Copyright (c) 2017, Adafruit Industries
15
+ *  All rights reserved.
16
+ *
17
+ *  Redistribution and use in source and binary forms, with or without
18
+ *  modification, are permitted provided that the following conditions are met:
19
+ *  1. Redistributions of source code must retain the above copyright
20
+ *  notice, this list of conditions and the following disclaimer.
21
+ *  2. Redistributions in binary form must reproduce the above copyright
22
+ *  notice, this list of conditions and the following disclaimer in the
23
+ *  documentation and/or other materials provided with the distribution.
24
+ *  3. Neither the name of the copyright holders nor the
25
+ *  names of its contributors may be used to endorse or promote products
26
+ *  derived from this software without specific prior written permission.
27
+ *
28
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29
+ *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30
+ *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31
+ *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
32
+ *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33
+ *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
+ *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35
+ *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
37
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
+ */
36 39
 
37 40
 #ifdef __AVR
38
-  #include <avr/pgmspace.h>
41
+#include <avr/pgmspace.h>
39 42
 #elif defined(ESP8266)
40
-  #include <pgmspace.h>
43
+#include <pgmspace.h>
41 44
 #endif
42
-#include <stdlib.h>
43 45
 #include <math.h>
46
+#include <stdlib.h>
44 47
 
45 48
 #include "Adafruit_APDS9960.h"
46 49
 
47
-/*========================================================================*/
48
-/*                          PRIVATE FUNCTIONS                             */
49
-/*========================================================================*/
50
-
51
-/**************************************************************************/
52 50
 /*!
53
-    @brief  Implements missing powf function
54
-*/
55
-/**************************************************************************/
56
-float powf(const float x, const float y)
57
-{
51
+ *  @brief  Implements missing powf function
52
+ *  @param  x
53
+ *          Base number
54
+ *  @param  y
55
+ *          Exponent
56
+ *  @return x raised to the power of y
57
+ */
58
+float powf(const float x, const float y) {
58 59
   return (float)(pow((double)x, (double)y));
59 60
 }
60 61
 
61
-/**************************************************************************/
62 62
 /*!
63
-    Enables the device
64
-    Disables the device (putting it in lower power sleep mode)
65
-*/
66
-/**************************************************************************/
67
-void Adafruit_APDS9960::enable(boolean en)
68
-{
63
+ *  @brief  Enables the device
64
+ *          Disables the device (putting it in lower power sleep mode)
65
+ *  @param  en
66
+ *          Enable (True/False)
67
+ */
68
+void Adafruit_APDS9960::enable(boolean en) {
69 69
   _enable.PON = en;
70 70
   this->write8(APDS9960_ENABLE, _enable.get());
71 71
 }
72 72
 
73
-/*========================================================================*/
74
-/*                           PUBLIC FUNCTIONS                             */
75
-/*========================================================================*/
76
-
77
-/**************************************************************************/
78 73
 /*!
79
-    Initializes I2C and configures the sensor (call this function before
80
-    doing anything else)
81
-*/
82
-/**************************************************************************/
83
-boolean Adafruit_APDS9960::begin(uint16_t iTimeMS, apds9960AGain_t aGain, uint8_t addr) 
84
-{
74
+ *  @brief  Initializes I2C and configures the sensor
75
+ *  @param  iTimeMS
76
+ *          Integration time
77
+ *  @param  aGain
78
+ *          Gain
79
+ *  @param  addr
80
+ *          I2C address
81
+ *  @param  *theWire
82
+ *          Wire object
83
+ *  @return True if initialization was successful, otherwise false.
84
+ */
85
+boolean Adafruit_APDS9960::begin(uint16_t iTimeMS, apds9960AGain_t aGain,
86
+                                 uint8_t addr, TwoWire *theWire) {
87
+  _wire = theWire;
85 88
   _i2c_init();
86 89
   _i2caddr = addr;
87
-   
90
+
88 91
   /* Make sure we're actually connected */
89 92
   uint8_t x = read8(APDS9960_ID);
90
-  if (x != 0xAB)
91
-  {
93
+  if (x != 0xAB) {
92 94
     return false;
93 95
   }
94 96
 
@@ -100,7 +102,7 @@ boolean Adafruit_APDS9960::begin(uint16_t iTimeMS, apds9960AGain_t aGain, uint8_
100 102
   enableGesture(false);
101 103
   enableProximity(false);
102 104
   enableColor(false);
103
-  
105
+
104 106
   disableColorInterrupt();
105 107
   disableProximityInterrupt();
106 108
   clearInterrupt();
@@ -110,43 +112,47 @@ boolean Adafruit_APDS9960::begin(uint16_t iTimeMS, apds9960AGain_t aGain, uint8_
110 112
   delay(10);
111 113
   enable(true);
112 114
   delay(10);
113
-  
114
-  //default to all gesture dimensions
115
+
116
+  // default to all gesture dimensions
115 117
   setGestureDimensions(APDS9960_DIMENSIONS_ALL);
116 118
   setGestureFIFOThreshold(APDS9960_GFIFO_4);
117 119
   setGestureGain(APDS9960_GGAIN_4);
118 120
   setGestureProximityThreshold(50);
119 121
   resetCounts();
120
-  
122
+
121 123
   _gpulse.GPLEN = APDS9960_GPULSE_32US;
122
-  _gpulse.GPULSE = 9; //10 pulses
124
+  _gpulse.GPULSE = 9; // 10 pulses
123 125
   this->write8(APDS9960_GPULSE, _gpulse.get());
124
-  
126
+
125 127
   return true;
126 128
 }
127
-  
128
-/**************************************************************************/
129
+
129 130
 /*!
130
-    Sets the integration time for the ADC of the APDS9960, in millis
131
-*/
132
-/**************************************************************************/
133
-void Adafruit_APDS9960::setADCIntegrationTime(uint16_t iTimeMS)
134
-{
131
+ *  @brief  Sets the integration time for the ADC of the APDS9960, in millis
132
+ *  @param  iTimeMS
133
+ *          Integration time
134
+ */
135
+void Adafruit_APDS9960::setADCIntegrationTime(uint16_t iTimeMS) {
135 136
   float temp;
136 137
 
137 138
   // convert ms into 2.78ms increments
138 139
   temp = iTimeMS;
139 140
   temp /= 2.78;
140 141
   temp = 256 - temp;
141
-  if (temp > 255) temp = 255;
142
-  if (temp < 0)   temp = 0;
143
-  
142
+  if (temp > 255)
143
+    temp = 255;
144
+  if (temp < 0)
145
+    temp = 0;
146
+
144 147
   /* Update the timing register */
145 148
   write8(APDS9960_ATIME, (uint8_t)temp);
146 149
 }
147 150
 
148
-float Adafruit_APDS9960::getADCIntegrationTime(void)
149
-{
151
+/*!
152
+ *  @brief  Returns the integration time for the ADC of the APDS9960, in millis
153
+ *  @return Integration time
154
+ */
155
+float Adafruit_APDS9960::getADCIntegrationTime() {
150 156
   float temp;
151 157
 
152 158
   temp = read8(APDS9960_ATIME);
@@ -157,158 +163,221 @@ float Adafruit_APDS9960::getADCIntegrationTime(void)
157 163
   return temp;
158 164
 }
159 165
 
160
-/**************************************************************************/
161 166
 /*!
162
-    Adjusts the color/ALS gain on the APDS9960 (adjusts the sensitivity to light)
163
-*/
164
-/**************************************************************************/
165
-void Adafruit_APDS9960::setADCGain(apds9960AGain_t aGain)
166
-{
167
+ *  @brief  Adjusts the color/ALS gain on the APDS9960 (adjusts the sensitivity
168
+ *          to light)
169
+ *  @param  aGain
170
+ *          Gain
171
+ */
172
+void Adafruit_APDS9960::setADCGain(apds9960AGain_t aGain) {
167 173
   _control.AGAIN = aGain;
168 174
 
169 175
   /* Update the timing register */
170 176
   write8(APDS9960_CONTROL, _control.get());
171 177
 }
172 178
 
173
-apds9960AGain_t Adafruit_APDS9960::getADCGain(void)
174
-{
175
-  return (apds9960AGain_t) ( read8(APDS9960_CONTROL) & 0x03 );
179
+/*!
180
+ *  @brief  Returns the ADC gain
181
+ *  @return ADC gain
182
+ */
183
+apds9960AGain_t Adafruit_APDS9960::getADCGain() {
184
+  return (apds9960AGain_t)(read8(APDS9960_CONTROL) & 0x03);
176 185
 }
177 186
 
178
-
179
-/**************************************************************************/
180 187
 /*!
181
-    Adjusts the Proximity gain on the APDS9960
182
-*/
183
-/**************************************************************************/
184
-void Adafruit_APDS9960::setProxGain(apds9960PGain_t pGain)
185
-{
188
+ *  @brief  Adjusts the Proximity gain on the APDS9960
189
+ *  @param  pGain
190
+ *          Gain
191
+ */
192
+void Adafruit_APDS9960::setProxGain(apds9960PGain_t pGain) {
186 193
   _control.PGAIN = pGain;
187 194
 
188 195
   /* Update the timing register */
189 196
   write8(APDS9960_CONTROL, _control.get());
190 197
 }
191 198
 
192
-apds9960PGain_t Adafruit_APDS9960::getProxGain(void)
193
-{
194
-  return (apds9960PGain_t) ( read8(APDS9960_CONTROL) & 0x0C );
199
+/*!
200
+ *  @brief  Returns the Proximity gain on the APDS9960
201
+ *  @return Proxmity gain
202
+ */
203
+apds9960PGain_t Adafruit_APDS9960::getProxGain() {
204
+  return (apds9960PGain_t)(read8(APDS9960_CONTROL) & 0x0C);
195 205
 }
196 206
 
207
+/*!
208
+ *  @brief  Sets number of proxmity pulses
209
+ *  @param  pLen
210
+ *          Pulse Length
211
+ *  @param  pulses
212
+ *          Number of pulses
213
+ */
197 214
 void Adafruit_APDS9960::setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses) {
198
-  if (pulses < 1)  pulses = 1;
199
-  if (pulses > 64) pulses = 64;
215
+  if (pulses < 1)
216
+    pulses = 1;
217
+  if (pulses > 64)
218
+    pulses = 64;
200 219
   pulses--;
201
-  
220
+
202 221
   _ppulse.PPLEN = pLen;
203 222
   _ppulse.PPULSE = pulses;
204
-  
223
+
205 224
   write8(APDS9960_PPULSE, _ppulse.get());
206
-} 
225
+}
207 226
 
208
-/**************************************************************************/
209 227
 /*!
210
-    Enable proximity readings on APDS9960
211
-*/
212
-/**************************************************************************/
213
-void Adafruit_APDS9960::enableProximity(boolean en)
214
-{
228
+ *  @brief  Enable proximity readings on APDS9960
229
+ *  @param  en
230
+ *          Enable (True/False)
231
+ */
232
+void Adafruit_APDS9960::enableProximity(boolean en) {
215 233
   _enable.PEN = en;
216 234
 
217 235
   write8(APDS9960_ENABLE, _enable.get());
218 236
 }
219 237
 
238
+/*!
239
+ *  @brief  Enable proximity interrupts
240
+ */
220 241
 void Adafruit_APDS9960::enableProximityInterrupt() {
221
-	_enable.PIEN = 1;
222
-	write8(APDS9960_ENABLE, _enable.get());
223
-	clearInterrupt();
242
+  _enable.PIEN = 1;
243
+  write8(APDS9960_ENABLE, _enable.get());
244
+  clearInterrupt();
224 245
 }
225 246
 
247
+/*!
248
+ *  @brief  Disable proximity interrupts
249
+ */
226 250
 void Adafruit_APDS9960::disableProximityInterrupt() {
227
-	_enable.PIEN = 0;
228
-	write8(APDS9960_ENABLE, _enable.get());
251
+  _enable.PIEN = 0;
252
+  write8(APDS9960_ENABLE, _enable.get());
229 253
 }
230 254
 
231
-void Adafruit_APDS9960::setProximityInterruptThreshold(uint8_t low, uint8_t high, uint8_t persistance){
232
-	write8(APDS9960_PILT, low);
233
-	write8(APDS9960_PIHT, high); 
234
-  
235
-	if (persistance > 7) persistance = 7;
236
-	_pers.PPERS = persistance;
237
-	write8(APDS9960_PERS,_pers.get());
255
+/*!
256
+ *  @brief  Set proxmity interrupt thresholds
257
+ *  @param  low
258
+ *          Low threshold
259
+ *  @param  high
260
+ *          High threshold
261
+ *  @param  persistance
262
+ *          Persistance
263
+ */
264
+void Adafruit_APDS9960::setProximityInterruptThreshold(uint8_t low,
265
+                                                       uint8_t high,
266
+                                                       uint8_t persistance) {
267
+  write8(APDS9960_PILT, low);
268
+  write8(APDS9960_PIHT, high);
269
+
270
+  if (persistance > 7)
271
+    persistance = 7;
272
+  _pers.PPERS = persistance;
273
+  write8(APDS9960_PERS, _pers.get());
238 274
 }
239 275
 
240
-bool Adafruit_APDS9960::getProximityInterrupt()
241
-{
242
-	_status.set(this->read8(APDS9960_STATUS));
243
-	return _status.PINT;
276
+/*!
277
+ *  @brief  Returns proxmity interrupt status
278
+ *  @return True if enabled, false otherwise.
279
+ */
280
+bool Adafruit_APDS9960::getProximityInterrupt() {
281
+  _status.set(this->read8(APDS9960_STATUS));
282
+  return _status.PINT;
244 283
 };
245 284
 
246
-/**************************************************************************/
247 285
 /*!
248
-    Read proximity data
249
-*/
250
-/**************************************************************************/
251
-uint8_t Adafruit_APDS9960::readProximity(void)
252
-{
253
-  return read8(APDS9960_PDATA);
254
-}
255
-
286
+ *  @brief  Read proximity data
287
+ *  @return Proximity
288
+ */
289
+uint8_t Adafruit_APDS9960::readProximity() { return read8(APDS9960_PDATA); }
256 290
 
257
-bool Adafruit_APDS9960::gestureValid()
258
-{
259
-	_gstatus.set(this->read8(APDS9960_GSTATUS));
260
-	return _gstatus.GVALID;
291
+/*!
292
+ *  @brief  Returns validity status of a gesture
293
+ *  @return Status (True/False)
294
+ */
295
+bool Adafruit_APDS9960::gestureValid() {
296
+  _gstatus.set(this->read8(APDS9960_GSTATUS));
297
+  return _gstatus.GVALID;
261 298
 }
262 299
 
263
-void Adafruit_APDS9960::setGestureDimensions(uint8_t dims)
264
-{
265
-	_gconf3.GDIMS = dims;
266
-	this->write8(APDS9960_GCONF3, _gconf3.get());
300
+/*!
301
+ *  @brief  Sets gesture dimensions
302
+ *  @param  dims
303
+ *          Dimensions (APDS9960_DIMENSIONS_ALL, APDS9960_DIMENSIONS_UP_DOWM,
304
+ *          APDS9960_DIMENSIONS_UP_DOWN, APGS9960_DIMENSIONS_LEFT_RIGHT)
305
+ */
306
+void Adafruit_APDS9960::setGestureDimensions(uint8_t dims) {
307
+  _gconf3.GDIMS = dims;
308
+  this->write8(APDS9960_GCONF3, _gconf3.get());
267 309
 }
268 310
 
269
-void Adafruit_APDS9960::setGestureFIFOThreshold(uint8_t thresh)
270
-{
271
-	_gconf1.GFIFOTH = thresh;
272
-	this->write8(APDS9960_GCONF1, _gconf1.get());
311
+/*!
312
+ *  @brief  Sets gesture FIFO Threshold
313
+ *  @param  thresh
314
+ *          Threshold (APDS9960_GFIFO_1, APDS9960_GFIFO_4, APDS9960_GFIFO_8,
315
+ *          APDS9960_GFIFO_16)
316
+ */
317
+void Adafruit_APDS9960::setGestureFIFOThreshold(uint8_t thresh) {
318
+  _gconf1.GFIFOTH = thresh;
319
+  this->write8(APDS9960_GCONF1, _gconf1.get());
273 320
 }
274 321
 
275
-void Adafruit_APDS9960::setGestureGain(uint8_t gain)
276
-{
277
-	_gconf2.GGAIN = gain;
278
-	this->write8(APDS9960_GCONF2, _gconf2.get());
322
+/*!
323
+ *  @brief  Sets gesture sensor gain
324
+ *  @param  gain
325
+ *          Gain (APDS9960_GAIN_1, APDS9960_GAIN_2, APDS9960_GAIN_4,
326
+ *          APDS9960_GAIN_8)
327
+ */
328
+void Adafruit_APDS9960::setGestureGain(uint8_t gain) {
329
+  _gconf2.GGAIN = gain;
330
+  this->write8(APDS9960_GCONF2, _gconf2.get());
279 331
 }
280 332
 
281
-void Adafruit_APDS9960::setGestureProximityThreshold(uint8_t thresh)
282
-{
283
-	this->write8(APDS9960_GPENTH, thresh);
333
+/*!
334
+ *  @brief  Sets gesture sensor threshold
335
+ *  @param  thresh
336
+ *          Threshold
337
+ */
338
+void Adafruit_APDS9960::setGestureProximityThreshold(uint8_t thresh) {
339
+  this->write8(APDS9960_GPENTH, thresh);
284 340
 }
285 341
 
286
-void Adafruit_APDS9960::setGestureOffset(uint8_t offset_up, uint8_t offset_down, uint8_t offset_left, uint8_t offset_right)
287
-{
288
-	this->write8(APDS9960_GOFFSET_U, offset_up);
289
-	this->write8(APDS9960_GOFFSET_D, offset_down);
290
-	this->write8(APDS9960_GOFFSET_L, offset_left);
291
-	this->write8(APDS9960_GOFFSET_R, offset_right);
342
+/*!
343
+ *  @brief  Sets gesture sensor offset
344
+ *  @param  offset_up
345
+ *          Up offset
346
+ *  @param  offset_down
347
+ *          Down offset
348
+ *  @param  offset_left
349
+ *          Left offset
350
+ *  @param  offset_right
351
+ *          Right offset
352
+ */
353
+void Adafruit_APDS9960::setGestureOffset(uint8_t offset_up, uint8_t offset_down,
354
+                                         uint8_t offset_left,
355
+                                         uint8_t offset_right) {
356
+  this->write8(APDS9960_GOFFSET_U, offset_up);
357
+  this->write8(APDS9960_GOFFSET_D, offset_down);
358
+  this->write8(APDS9960_GOFFSET_L, offset_left);
359
+  this->write8(APDS9960_GOFFSET_R, offset_right);
292 360
 }
293 361
 
294
-/**************************************************************************/
295 362
 /*!
296
-    Enable gesture readings on APDS9960
297
-*/
298
-/**************************************************************************/
299
-void Adafruit_APDS9960::enableGesture(boolean en)
300
-{
301
-  if(!en){
302
-	  _gconf4.GMODE = 0;
303
-	  write8(APDS9960_GCONF4, _gconf4.get());
363
+ *  @brief  Enable gesture readings on APDS9960
364
+ *  @param  en
365
+ *          Enable (True/False)
366
+ */
367
+void Adafruit_APDS9960::enableGesture(boolean en) {
368
+  if (!en) {
369
+    _gconf4.GMODE = 0;
370
+    write8(APDS9960_GCONF4, _gconf4.get());
304 371
   }
305 372
   _enable.GEN = en;
306 373
   write8(APDS9960_ENABLE, _enable.get());
307 374
   resetCounts();
308 375
 }
309 376
 
310
-void Adafruit_APDS9960::resetCounts()
311
-{
377
+/*!
378
+ *  @brief  Resets gesture counts
379
+ */
380
+void Adafruit_APDS9960::resetCounts() {
312 381
   gestCnt = 0;
313 382
   UCount = 0;
314 383
   DCount = 0;
@@ -316,125 +385,144 @@ void Adafruit_APDS9960::resetCounts()
316 385
   RCount = 0;
317 386
 }
318 387
 
319
-uint8_t Adafruit_APDS9960::readGesture(void) 
320
-{
321
-	uint8_t toRead, bytesRead;
322
-	uint8_t buf[256];
323
-	unsigned long t;
324
-	uint8_t gestureReceived;
325
-	while(1){
326
-		int up_down_diff = 0;
327
-		int left_right_diff = 0;
328
-		gestureReceived = 0;
329
-		if(!gestureValid()) return 0;
330
-		
331
-		delay(30);
332
-		toRead = this->read8(APDS9960_GFLVL);
333
-		
334
-		bytesRead = this->read(APDS9960_GFIFO_U, buf, toRead);
335
-
336
-    if(abs((int)buf[0] - (int)buf[1]) > 13)
388
+/*!
389
+ *  @brief  Reads gesture
390
+ *  @return Received gesture (APDS9960_DOWN APDS9960_UP, APDS9960_LEFT
391
+ *          APDS9960_RIGHT)
392
+ */
393
+uint8_t Adafruit_APDS9960::readGesture() {
394
+  uint8_t toRead, bytesRead;
395
+  uint8_t buf[256];
396
+  unsigned long t = 0;
397
+  uint8_t gestureReceived;
398
+  while (1) {
399
+    int up_down_diff = 0;
400
+    int left_right_diff = 0;
401
+    gestureReceived = 0;
402
+    if (!gestureValid())
403
+      return 0;
404
+
405
+    delay(30);
406
+    toRead = this->read8(APDS9960_GFLVL);
407
+
408
+    // bytesRead is unused but produces sideffects needed for readGesture to work
409
+    bytesRead = this->read(APDS9960_GFIFO_U, buf, toRead);
410
+
411
+    if (abs((int)buf[0] - (int)buf[1]) > 13)
337 412
       up_down_diff += (int)buf[0] - (int)buf[1];
338 413
 
339
-    if(abs((int)buf[2] - (int)buf[3]) > 13)
414
+    if (abs((int)buf[2] - (int)buf[3]) > 13)
340 415
       left_right_diff += (int)buf[2] - (int)buf[3];
341 416
 
342
-		if(up_down_diff != 0){
343
-			if(up_down_diff < 0){
344
-				 if( DCount > 0){
345
-					 gestureReceived = APDS9960_UP;
346
-				 }
347
-				 else UCount++;
348
-			}
349
-			else if(up_down_diff > 0){
350
-				 if( UCount > 0){
351
-					 gestureReceived = APDS9960_DOWN;
352
-				 }
353
-				 else DCount++;
354
-			}
355
-		}
356
-		
357
-		if(left_right_diff != 0){
358
-			if(left_right_diff < 0){
359
-				 if( RCount > 0){
360
-					 gestureReceived = APDS9960_LEFT;
361
-				 }
362
-				 else LCount++;
363
-			}
364
-			else if(left_right_diff > 0){
365
-				 if( LCount > 0){
366
-					 gestureReceived = APDS9960_RIGHT;
367
-				 }
368
-				 else RCount++;
369
-			}
370
-		}
371
-		
372
-		if(up_down_diff != 0 || left_right_diff != 0) t = millis();
373
-		
374
-		if(gestureReceived || millis() - t > 300){
375
-			 resetCounts();
376
-			 return gestureReceived;
377
-		}
378
-	}
379
-}
380
-
381
-/**************************************************************************/
382
-/*!
383
-    Set LED brightness for proximity/gesture
384
-*/
385
-/**************************************************************************/
386
-void Adafruit_APDS9960::setLED(apds9960LedDrive_t drive, apds9960LedBoost_t boost) {
417
+    if (up_down_diff != 0) {
418
+      if (up_down_diff < 0) {
419
+        if (DCount > 0) {
420
+          gestureReceived = APDS9960_UP;
421
+        } else
422
+          UCount++;
423
+      } else if (up_down_diff > 0) {
424
+        if (UCount > 0) {
425
+          gestureReceived = APDS9960_DOWN;
426
+        } else
427
+          DCount++;
428
+      }
429
+    }
430
+
431
+    if (left_right_diff != 0) {
432
+      if (left_right_diff < 0) {
433
+        if (RCount > 0) {
434
+          gestureReceived = APDS9960_LEFT;
435
+        } else
436
+          LCount++;
437
+      } else if (left_right_diff > 0) {
438
+        if (LCount > 0) {
439
+          gestureReceived = APDS9960_RIGHT;
440
+        } else
441
+          RCount++;
442
+      }
443
+    }
444
+
445
+    if (up_down_diff != 0 || left_right_diff != 0)
446
+      t = millis();
447
+
448
+    if (gestureReceived || millis() - t > 300) {
449
+      resetCounts();
450
+      return gestureReceived;
451
+    }
452
+  }
453
+}
454
+
455
+/*!
456
+ *  @brief  Set LED brightness for proximity/gesture
457
+ *  @param  drive
458
+ *          LED Drive
459
+ *  @param  boost
460
+ *          LED Boost
461
+ */
462
+void Adafruit_APDS9960::setLED(apds9960LedDrive_t drive,
463
+                               apds9960LedBoost_t boost) {
387 464
   // set BOOST
388 465
   _config2.LED_BOOST = boost;
389 466
   write8(APDS9960_CONFIG2, _config2.get());
390
-  
467
+
391 468
   _control.LDRIVE = drive;
392 469
   write8(APDS9960_CONTROL, _control.get());
393 470
 }
394 471
 
395
-/**************************************************************************/
396 472
 /*!
397
-    Enable proximity readings on APDS9960
398
-*/
399
-/**************************************************************************/
400
-void Adafruit_APDS9960::enableColor(boolean en)
401
-{
473
+ *  @brief  Enable proximity readings on APDS9960
474
+ *  @param  en
475
+ *          Enable (True/False)
476
+ */
477
+void Adafruit_APDS9960::enableColor(boolean en) {
402 478
   _enable.AEN = en;
403 479
   write8(APDS9960_ENABLE, _enable.get());
404 480
 }
405 481
 
406
-bool Adafruit_APDS9960::colorDataReady()
407
-{
408
-	_status.set(this->read8(APDS9960_STATUS));
409
-	return _status.AVALID;
482
+/*!
483
+ *  @brief  Returns status of color data
484
+ *  @return True if color data ready, False otherwise
485
+ */
486
+bool Adafruit_APDS9960::colorDataReady() {
487
+  _status.set(this->read8(APDS9960_STATUS));
488
+  return _status.AVALID;
410 489
 }
411 490
 
412
-/**************************************************************************/
413 491
 /*!
414
-    @brief  Reads the raw red, green, blue and clear channel values
415
-*/
416
-/**************************************************************************/
417
-void Adafruit_APDS9960::getColorData (uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
418
-{
419
-  
492
+ *  @brief  Reads the raw red, green, blue and clear channel values
493
+ *  @param  *r
494
+ *          Red value
495
+ *  @param  *g
496
+ *          Green value
497
+ *  @param  *b
498
+ *          Blue value
499
+ *  @param  *c
500
+ *          Clear channel value
501
+ */
502
+void Adafruit_APDS9960::getColorData(uint16_t *r, uint16_t *g, uint16_t *b,
503
+                                     uint16_t *c) {
504
+
420 505
   *c = read16R(APDS9960_CDATAL);
421 506
   *r = read16R(APDS9960_RDATAL);
422 507
   *g = read16R(APDS9960_GDATAL);
423 508
   *b = read16R(APDS9960_BDATAL);
424
-
425 509
 }
426 510
 
427
-/**************************************************************************/
428 511
 /*!
429
-    @brief  Converts the raw R/G/B values to color temperature in degrees
430
-            Kelvin
431
-*/
432
-/**************************************************************************/
433
-uint16_t Adafruit_APDS9960::calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b)
434
-{
435
-  float X, Y, Z;      /* RGB to XYZ correlation      */
436
-  float xc, yc;       /* Chromaticity co-ordinates   */
437
-  float n;            /* McCamy's formula            */
512
+ *  @brief  Converts the raw R/G/B values to color temperature in degrees Kelvin
513
+ *  @param  r
514
+ *          Red value
515
+ *  @param  g
516
+ *          Green value
517
+ *  @param  b
518
+ *          Blue value
519
+ *  @return Color temperature
520
+ */
521
+uint16_t Adafruit_APDS9960::calculateColorTemperature(uint16_t r, uint16_t g,
522
+                                                      uint16_t b) {
523
+  float X, Y, Z; /* RGB to XYZ correlation      */
524
+  float xc, yc;  /* Chromaticity co-ordinates   */
525
+  float n;       /* McCamy's formula            */
438 526
   float cct;
439 527
 
440 528
   /* 1. Map RGB values to their XYZ counterparts.    */
@@ -443,7 +531,7 @@ uint16_t Adafruit_APDS9960::calculateColorTemperature(uint16_t r, uint16_t g, ui
443 531
   /* Note: Y = Illuminance or lux                    */
444 532
   X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
445 533
   Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
446
-  Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);
534
+  Z = (-0.68202F * r) + (0.77073F * g) + (0.56332F * b);
447 535
 
448 536
   /* 2. Calculate the chromaticity co-ordinates      */
449 537
   xc = (X) / (X + Y + Z);
@@ -453,20 +541,24 @@ uint16_t Adafruit_APDS9960::calculateColorTemperature(uint16_t r, uint16_t g, ui
453 541
   n = (xc - 0.3320F) / (0.1858F - yc);
454 542
 
455 543
   /* Calculate the final CCT */
456
-  cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
544
+  cct =
545
+      (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
457 546
 
458 547
   /* Return the results in degrees Kelvin */
459 548
   return (uint16_t)cct;
460 549
 }
461 550
 
462
-/**************************************************************************/
463 551
 /*!
464
-    @brief  Calculate ambient light values
465
-*/
466
-/**************************************************************************/
467
-
468
-uint16_t Adafruit_APDS9960::calculateLux(uint16_t r, uint16_t g, uint16_t b)
469
-{
552
+ *  @brief  Calculate ambient light values
553
+ *  @param  r
554
+ *          Red value
555
+ *  @param  g
556
+ *          Green value
557
+ *  @param  b
558
+ *          Blue value
559
+ *  @return LUX value
560
+ */
561
+uint16_t Adafruit_APDS9960::calculateLux(uint16_t r, uint16_t g, uint16_t b) {
470 562
   float illuminance;
471 563
 
472 564
   /* This only uses RGB ... how can we integrate clear or calculate lux */
@@ -476,102 +568,159 @@ uint16_t Adafruit_APDS9960::calculateLux(uint16_t r, uint16_t g, uint16_t b)
476 568
   return (uint16_t)illuminance;
477 569
 }
478 570
 
571
+/*!
572
+ *  @brief  Enables color interrupt
573
+ */
479 574
 void Adafruit_APDS9960::enableColorInterrupt() {
480 575
   _enable.AIEN = 1;
481 576
   write8(APDS9960_ENABLE, _enable.get());
482 577
 }
483 578
 
579
+/*!
580
+ *  @brief  Disables color interrupt
581
+ */
484 582
 void Adafruit_APDS9960::disableColorInterrupt() {
485
-	_enable.AIEN = 0;
486
-	write8(APDS9960_ENABLE, _enable.get());
583
+  _enable.AIEN = 0;
584
+  write8(APDS9960_ENABLE, _enable.get());
487 585
 }
488 586
 
489
-void Adafruit_APDS9960::clearInterrupt(void) {
587
+/*!
588
+ *  @brief  Clears interrupt
589
+ */
590
+void Adafruit_APDS9960::clearInterrupt() {
490 591
   this->write(APDS9960_AICLEAR, NULL, 0);
491 592
 }
492 593
 
493
-
594
+/*!
595
+ *  @brief  Sets interrupt limits
596
+ *  @param  low
597
+ *          Low limit
598
+ *  @param  high
599
+ *          High limit
600
+ */
494 601
 void Adafruit_APDS9960::setIntLimits(uint16_t low, uint16_t high) {
495
-   write8(APDS9960_AILTIL, low & 0xFF);
496
-   write8(APDS9960_AILTH, low >> 8);
497
-   write8(APDS9960_AIHTL, high & 0xFF);
498
-   write8(APDS9960_AIHTH, high >> 8);
602
+  write8(APDS9960_AILTIL, low & 0xFF);
603
+  write8(APDS9960_AILTH, low >> 8);
604
+  write8(APDS9960_AIHTL, high & 0xFF);
605
+  write8(APDS9960_AIHTH, high >> 8);
499 606
 }
500 607
 
501
-void Adafruit_APDS9960::write8(byte reg, byte value)
502
-{
503
-	this->write(reg, &value, 1);
608
+/*!
609
+ *  @brief  Writes specified value to given register
610
+ *  @param  reg
611
+ *          Register to write to
612
+ *  @param  value
613
+ *          Value to write
614
+ */
615
+void Adafruit_APDS9960::write8(byte reg, byte value) {
616
+  this->write(reg, &value, 1);
504 617
 }
505 618
 
506
-uint8_t Adafruit_APDS9960::read8(byte reg)
507
-{
508
-	uint8_t ret;
509
-	this->read(reg, &ret, 1);
510
-	
511
-	return ret;
619
+/*!
620
+ *  @brief  Reads 8 bits from specified register
621
+ *  @param  reg
622
+ *          Register to write to
623
+ *  @return Value in register
624
+ */
625
+uint8_t Adafruit_APDS9960::read8(byte reg) {
626
+  uint8_t ret;
627
+  this->read(reg, &ret, 1);
628
+
629
+  return ret;
512 630
 }
513 631
 
514
-uint32_t Adafruit_APDS9960::read32(uint8_t reg)
515
-{
516
-	uint8_t ret[4];
517
-	this->read(reg, ret, 4);
518
-	
519
-	return (ret[0] << 24) | (ret[1] << 16) | (ret[2] << 8) | ret[3];
632
+/*!
633
+ *  @brief  Reads 32 bits from specified register
634
+ *  @param  reg
635
+ *          Register to write to
636
+ *  @return Value in register
637
+ */
638
+uint32_t Adafruit_APDS9960::read32(uint8_t reg) {
639
+  uint8_t ret[4];
640
+  this->read(reg, ret, 4);
641
+
642
+  return (ret[0] << 24) | (ret[1] << 16) | (ret[2] << 8) | ret[3];
520 643
 }
521 644
 
522
-uint16_t Adafruit_APDS9960::read16(uint8_t reg)
523
-{
524
-	uint8_t ret[2];
525
-	this->read(reg, ret, 2);
526
-	
527
-	return (ret[0] << 8) | ret[1];
645
+/*!
646
+ *  @brief  Reads 16 bites from specified register
647
+ *  @param  reg
648
+ *          Register to write to
649
+ *  @return Value in register
650
+ */
651
+uint16_t Adafruit_APDS9960::read16(uint8_t reg) {
652
+  uint8_t ret[2];
653
+  this->read(reg, ret, 2);
654
+
655
+  return (ret[0] << 8) | ret[1];
528 656
 }
529 657
 
530
-uint16_t Adafruit_APDS9960::read16R(uint8_t reg)
531
-{
658
+/*!
659
+ *  @brief  Reads 16 bites from specified register
660
+ *  @param  reg
661
+ *          Register to write to
662
+ *  @return Value in register
663
+ */
664
+uint16_t Adafruit_APDS9960::read16R(uint8_t reg) {
532 665
   uint8_t ret[2];
533 666
   this->read(reg, ret, 2);
534
-  
667
+
535 668
   return (ret[1] << 8) | ret[0];
536 669
 }
537 670
 
538
-void Adafruit_APDS9960::_i2c_init()
539
-{
540
-	Wire.begin();
541
-}
542
-
543
-uint8_t Adafruit_APDS9960::read(uint8_t reg, uint8_t *buf, uint8_t num)
544
-{
545
-	uint8_t value;
546
-	uint8_t pos = 0;
547
-	bool eof = false;
548
-	
549
-	//on arduino we need to read in 32 byte chunks
550
-	while(pos < num && !eof){
551
-		
552
-		uint8_t read_now = min(32, num - pos);
553
-		Wire.beginTransmission((uint8_t)_i2caddr);
554
-		Wire.write((uint8_t)reg + pos);
555
-		Wire.endTransmission();
556
-		
557
-		Wire.requestFrom((uint8_t)_i2caddr, read_now);
558
-		
559
-		for(int i=0; i<read_now; i++){
560
-			if(!Wire.available()){
561
-				eof = true;
562
-				break;
563
-			}
564
-			buf[pos] = Wire.read();
565
-			pos++;
566
-		}
567
-	}
568
-	return pos;
569
-}
570
-
571
-void Adafruit_APDS9960::write(uint8_t reg, uint8_t *buf, uint8_t num)
572
-{
573
-	Wire.beginTransmission((uint8_t)_i2caddr);
574
-	Wire.write((uint8_t)reg);
575
-	Wire.write((uint8_t *)buf, num);
576
-	Wire.endTransmission();
671
+/*!
672
+ *  @brief  Begins I2C communication
673
+ */
674
+void Adafruit_APDS9960::_i2c_init() { _wire->begin(); }
675
+
676
+/*!
677
+ *  @brief  Reads num bytes from specified register into a given buffer
678
+ *  @param  reg
679
+ *          Register
680
+ *  @param  *buf
681
+ *          Buffer
682
+ *  @param  num
683
+ *          Number of bytes
684
+ *  @return Position after reading
685
+ */
686
+uint8_t Adafruit_APDS9960::read(uint8_t reg, uint8_t *buf, uint8_t num) {
687
+  uint8_t pos = 0;
688
+  bool eof = false;
689
+
690
+  // on arduino we need to read in 32 byte chunks
691
+  while (pos < num && !eof) {
692
+
693
+    uint8_t read_now = min(32, num - pos);
694
+    _wire->beginTransmission((uint8_t)_i2caddr);
695
+    _wire->write((uint8_t)reg + pos);
696
+    _wire->endTransmission();
697
+
698
+    _wire->requestFrom((uint8_t)_i2caddr, read_now);
699
+
700
+    for (int i = 0; i < read_now; i++) {
701
+      if (!_wire->available()) {
702
+        eof = true;
703
+        break;
704
+      }
705
+      buf[pos] = _wire->read();
706
+      pos++;
707
+    }
708
+  }
709
+  return pos;
710
+}
711
+
712
+/*!
713
+ *  @brief  Writes num bytes from specified buffer into a given register
714
+ *  @param  reg
715
+ *          Register
716
+ *  @param  *buf
717
+ *          Buffer
718
+ *  @param  num
719
+ *          Number of bytes
720
+ */
721
+void Adafruit_APDS9960::write(uint8_t reg, uint8_t *buf, uint8_t num) {
722
+  _wire->beginTransmission((uint8_t)_i2caddr);
723
+  _wire->write((uint8_t)reg);
724
+  _wire->write((uint8_t *)buf, num);
725
+  _wire->endTransmission();
577 726
 }

+ 484
- 511
firmware/libraries/Adafruit_APDS9960_Library/Adafruit_APDS9960.h View File

@@ -1,558 +1,531 @@
1
-/**************************************************************************/
2
-/*! 
3
-    @file     Adafruit_APDS9960.h
4
-    @author   Ladyada, Dean Miller (Adafruit Industries)
5
-
6
-    @section LICENSE
7
-
8
-    Software License Agreement (BSD License)
9
-
10
-    Copyright (c) 2017, Adafruit Industries
11
-    All rights reserved.
12
-
13
-    Redistribution and use in source and binary forms, with or without
14
-    modification, are permitted provided that the following conditions are met:
15
-    1. Redistributions of source code must retain the above copyright
16
-    notice, this list of conditions and the following disclaimer.
17
-    2. Redistributions in binary form must reproduce the above copyright
18
-    notice, this list of conditions and the following disclaimer in the
19
-    documentation and/or other materials provided with the distribution.
20
-    3. Neither the name of the copyright holders nor the
21
-    names of its contributors may be used to endorse or promote products
22
-    derived from this software without specific prior written permission.
23
-
24
-    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
25
-    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26
-    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
-    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
28
-    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29
-    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
-    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31
-    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
-    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
-    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
-*/
35
-/**************************************************************************/
1
+/*!
2
+ *  @file Adafruit_APDS9960.h
3
+ *
4
+ *  Software License Agreement (BSD License)
5
+ *
6
+ *  Copyright (c) 2017, Adafruit Industries
7
+ *  All rights reserved.
8
+ *
9
+ *  Redistribution and use in source and binary forms, with or without
10
+ *  modification, are permitted provided that the following conditions are met:
11
+ *  1. Redistributions of source code must retain the above copyright
12
+ *  notice, this list of conditions and the following disclaimer.
13
+ *  2. Redistributions in binary form must reproduce the above copyright
14
+ *  notice, this list of conditions and the following disclaimer in the
15
+ *  documentation and/or other materials provided with the distribution.
16
+ *  3. Neither the name of the copyright holders nor the
17
+ *  names of its contributors may be used to endorse or promote products
18
+ *  derived from this software without specific prior written permission.
19
+ *
20
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
21
+ *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22
+ *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
24
+ *  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25
+ *  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26
+ *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27
+ *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29
+ *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
36 31
 #ifndef _APDS9960_H_
37 32
 #define _APDS9960_H_
38 33
 
39 34
 #include <Arduino.h>
40 35
 #include <Wire.h>
41 36
 
42
-#define I2CDEBUG
43
-
44
-/*=========================================================================
45
-    I2C ADDRESS/BITS
46
-    -----------------------------------------------------------------------*/
47
-    #define APDS9960_ADDRESS                (0x39)
48
-/*=========================================================================*/
49
-
50
-/*=========================================================================
51
-    REGISTERS
52
-    -----------------------------------------------------------------------*/
53
-
54
-	enum 
55
-	{
56
-			APDS9960_RAM = 0x00,
57
-			APDS9960_ENABLE = 0x80,
58
-			APDS9960_ATIME = 0x81,
59
-			APDS9960_WTIME = 0x83,
60
-			APDS9960_AILTIL = 0x84,
61
-			APDS9960_AILTH = 0x85,
62
-			APDS9960_AIHTL = 0x86,
63
-			APDS9960_AIHTH = 0x87,
64
-			APDS9960_PILT = 0x89,
65
-			APDS9960_PIHT = 0x8B,
66
-			APDS9960_PERS = 0x8C,
67
-			APDS9960_CONFIG1 = 0x8D,
68
-			APDS9960_PPULSE = 0x8E,
69
-			APDS9960_CONTROL = 0x8F,
70
-			APDS9960_CONFIG2 = 0x90,
71
-			APDS9960_ID = 0x92,
72
-			APDS9960_STATUS = 0x93,
73
-			APDS9960_CDATAL = 0x94,
74
-			APDS9960_CDATAH = 0x95,
75
-			APDS9960_RDATAL = 0x96,
76
-			APDS9960_RDATAH = 0x97,
77
-			APDS9960_GDATAL = 0x98,
78
-			APDS9960_GDATAH = 0x99,
79
-			APDS9960_BDATAL = 0x9A,
80
-			APDS9960_BDATAH = 0x9B,
81
-			APDS9960_PDATA = 0x9C,
82
-			APDS9960_POFFSET_UR = 0x9D,
83
-			APDS9960_POFFSET_DL = 0x9E,
84
-			APDS9960_CONFIG3 = 0x9F,
85
-			APDS9960_GPENTH = 0xA0,
86
-			APDS9960_GEXTH = 0xA1,
87
-			APDS9960_GCONF1 = 0xA2,
88
-			APDS9960_GCONF2 = 0xA3,
89
-			APDS9960_GOFFSET_U = 0xA4,
90
-			APDS9960_GOFFSET_D = 0xA5,
91
-			APDS9960_GOFFSET_L = 0xA7,
92
-			APDS9960_GOFFSET_R = 0xA9,
93
-			APDS9960_GPULSE = 0xA6,
94
-			APDS9960_GCONF3 = 0xAA,
95
-			APDS9960_GCONF4 = 0xAB,
96
-			APDS9960_GFLVL = 0xAE,
97
-			APDS9960_GSTATUS = 0xAF,
98
-			APDS9960_IFORCE = 0xE4,
99
-			APDS9960_PICLEAR = 0xE5,
100
-			APDS9960_CICLEAR = 0xE6,
101
-			APDS9960_AICLEAR = 0xE7,
102
-			APDS9960_GFIFO_U = 0xFC,
103
-			APDS9960_GFIFO_D = 0xFD,
104
-			APDS9960_GFIFO_L = 0xFE,
105
-			APDS9960_GFIFO_R = 0xFF,
106
-	};
107
-
108
-
109
-/*=========================================================================*/
110
-
111
-typedef enum
112
-{
113
-  APDS9960_AGAIN_1X                = 0x00,   /**<  No gain  */
114
-  APDS9960_AGAIN_4X                = 0x01,   /**<  2x gain  */
115
-  APDS9960_AGAIN_16X               = 0x02,   /**<  16x gain */
116
-  APDS9960_AGAIN_64X               = 0x03    /**<  64x gain */
117
-}
118
-apds9960AGain_t;
119
-
120
-
121
-typedef enum
122
-{
123
-  APDS9960_PGAIN_1X                = 0x00,   /**<  1x gain  */
124
-  APDS9960_PGAIN_2X                = 0x04,   /**<  2x gain  */
125
-  APDS9960_PGAIN_4X                = 0x08,   /**<  4x gain  */
126
-  APDS9960_PGAIN_8X                = 0x0C    /**<  8x gain  */
127
-}
128
-apds9960PGain_t;
129
-
130
-
131
-typedef enum
132
-{
133
-  APDS9960_PPULSELEN_4US                = 0x00,   /**<  4uS   */
134
-  APDS9960_PPULSELEN_8US                = 0x40,   /**<  8uS   */
135
-  APDS9960_PPULSELEN_16US               = 0x80,   /**<  16uS  */
136
-  APDS9960_PPULSELEN_32US               = 0xC0    /**<  32uS  */
137
-}
138
-apds9960PPulseLen_t;
139
-
140
-
141
-typedef enum
142
-{
143
-  APDS9960_LEDDRIVE_100MA              = 0x00,   /**<  100mA   */
144
-  APDS9960_LEDDRIVE_50MA               = 0x40,   /**<  50mA    */
145
-  APDS9960_LEDDRIVE_25MA               = 0x80,   /**<  25mA    */
146
-  APDS9960_LEDDRIVE_12MA               = 0xC0    /**<  12.5mA  */
147
-}
148
-apds9960LedDrive_t;
149
-
150
-
151
-typedef enum
152
-{
153
-  APDS9960_LEDBOOST_100PCNT               = 0x00,   /**<  100%   */
154
-  APDS9960_LEDBOOST_150PCNT               = 0x10,   /**<  150%   */
155
-  APDS9960_LEDBOOST_200PCNT               = 0x20,   /**<  200%   */
156
-  APDS9960_LEDBOOST_300PCNT               = 0x30    /**<  300%   */
157
-}
158
-apds9960LedBoost_t;
159
-
160
-enum
161
-{
162
-	APDS9960_DIMENSIONS_ALL		= 0x00,
163
-	APDS9960_DIMENSIONS_UP_DOWM  = 0x01,
164
-	APGS9960_DIMENSIONS_LEFT_RIGHT = 0x02,	
37
+#define APDS9960_ADDRESS (0x39) /**< I2C Address */
38
+
39
+/** I2C Registers */
40
+enum {
41
+  APDS9960_RAM = 0x00,
42
+  APDS9960_ENABLE = 0x80,
43
+  APDS9960_ATIME = 0x81,
44
+  APDS9960_WTIME = 0x83,
45
+  APDS9960_AILTIL = 0x84,
46
+  APDS9960_AILTH = 0x85,
47
+  APDS9960_AIHTL = 0x86,
48
+  APDS9960_AIHTH = 0x87,
49
+  APDS9960_PILT = 0x89,
50
+  APDS9960_PIHT = 0x8B,
51
+  APDS9960_PERS = 0x8C,
52
+  APDS9960_CONFIG1 = 0x8D,
53
+  APDS9960_PPULSE = 0x8E,
54
+  APDS9960_CONTROL = 0x8F,
55
+  APDS9960_CONFIG2 = 0x90,
56
+  APDS9960_ID = 0x92,
57
+  APDS9960_STATUS = 0x93,
58
+  APDS9960_CDATAL = 0x94,
59
+  APDS9960_CDATAH = 0x95,
60
+  APDS9960_RDATAL = 0x96,
61
+  APDS9960_RDATAH = 0x97,
62
+  APDS9960_GDATAL = 0x98,
63
+  APDS9960_GDATAH = 0x99,
64
+  APDS9960_BDATAL = 0x9A,
65
+  APDS9960_BDATAH = 0x9B,
66
+  APDS9960_PDATA = 0x9C,
67
+  APDS9960_POFFSET_UR = 0x9D,
68
+  APDS9960_POFFSET_DL = 0x9E,
69
+  APDS9960_CONFIG3 = 0x9F,
70
+  APDS9960_GPENTH = 0xA0,
71
+  APDS9960_GEXTH = 0xA1,
72
+  APDS9960_GCONF1 = 0xA2,
73
+  APDS9960_GCONF2 = 0xA3,
74
+  APDS9960_GOFFSET_U = 0xA4,
75
+  APDS9960_GOFFSET_D = 0xA5,
76
+  APDS9960_GOFFSET_L = 0xA7,
77
+  APDS9960_GOFFSET_R = 0xA9,
78
+  APDS9960_GPULSE = 0xA6,
79
+  APDS9960_GCONF3 = 0xAA,
80
+  APDS9960_GCONF4 = 0xAB,
81
+  APDS9960_GFLVL = 0xAE,
82
+  APDS9960_GSTATUS = 0xAF,
83
+  APDS9960_IFORCE = 0xE4,
84
+  APDS9960_PICLEAR = 0xE5,
85
+  APDS9960_CICLEAR = 0xE6,
86
+  APDS9960_AICLEAR = 0xE7,
87
+  APDS9960_GFIFO_U = 0xFC,
88
+  APDS9960_GFIFO_D = 0xFD,
89
+  APDS9960_GFIFO_L = 0xFE,
90
+  APDS9960_GFIFO_R = 0xFF,
165 91
 };
166 92
 
167
-enum
168
-{
169
-	APDS9960_GFIFO_1	= 0x00,
170
-	APDS9960_GFIFO_4	= 0x01,
171
-	APDS9960_GFIFO_8	= 0x02,
172
-	APDS9960_GFIFO_16	= 0x03,
93
+/** ADC gain settings */
94
+typedef enum {
95
+  APDS9960_AGAIN_1X = 0x00,  /**< No gain */
96
+  APDS9960_AGAIN_4X = 0x01,  /**< 2x gain */
97
+  APDS9960_AGAIN_16X = 0x02, /**< 16x gain */
98
+  APDS9960_AGAIN_64X = 0x03  /**< 64x gain */
99
+} apds9960AGain_t;
100
+
101
+/** Proxmity gain settings */
102
+typedef enum {
103
+  APDS9960_PGAIN_1X = 0x00, /**< 1x gain */
104
+  APDS9960_PGAIN_2X = 0x04, /**< 2x gain */
105
+  APDS9960_PGAIN_4X = 0x08, /**< 4x gain */
106
+  APDS9960_PGAIN_8X = 0x0C  /**< 8x gain */
107
+} apds9960PGain_t;
108
+
109
+/** Pulse length settings */
110
+typedef enum {
111
+  APDS9960_PPULSELEN_4US = 0x00,  /**< 4uS */
112
+  APDS9960_PPULSELEN_8US = 0x40,  /**< 8uS */
113
+  APDS9960_PPULSELEN_16US = 0x80, /**< 16uS */
114
+  APDS9960_PPULSELEN_32US = 0xC0  /**< 32uS */
115
+} apds9960PPulseLen_t;
116
+
117
+/** LED drive settings */
118
+typedef enum {
119
+  APDS9960_LEDDRIVE_100MA = 0x00, /**< 100mA */
120
+  APDS9960_LEDDRIVE_50MA = 0x40,  /**< 50mA */
121
+  APDS9960_LEDDRIVE_25MA = 0x80,  /**< 25mA */
122
+  APDS9960_LEDDRIVE_12MA = 0xC0   /**< 12.5mA */
123
+} apds9960LedDrive_t;
124
+
125
+/** LED boost settings */
126
+typedef enum {
127
+  APDS9960_LEDBOOST_100PCNT = 0x00, /**< 100% */
128
+  APDS9960_LEDBOOST_150PCNT = 0x10, /**< 150% */
129
+  APDS9960_LEDBOOST_200PCNT = 0x20, /**< 200% */
130
+  APDS9960_LEDBOOST_300PCNT = 0x30  /**< 300% */
131
+} apds9960LedBoost_t;
132
+
133
+/** Dimensions */
134
+enum {
135
+  APDS9960_DIMENSIONS_ALL = 0x00,        // All dimensions
136
+  APDS9960_DIMENSIONS_UP_DOWN = 0x01,    // Up/Down dimensions
137
+  APGS9960_DIMENSIONS_LEFT_RIGHT = 0x02, // Left/Right dimensions
173 138
 };
174 139
 
175
-enum
176
-{
177
-	APDS9960_GGAIN_1	= 0x00,
178
-	APDS9960_GGAIN_2	= 0x01,
179
-	APDS9960_GGAIN_4	= 0x02,
180
-	APDS9960_GGAIN_8	= 0x03,
140
+/** FIFO Interrupts */
141
+enum {
142
+  APDS9960_GFIFO_1 = 0x00,  // Generate interrupt after 1 dataset in FIFO
143
+  APDS9960_GFIFO_4 = 0x01,  // Generate interrupt after 2 datasets in FIFO
144
+  APDS9960_GFIFO_8 = 0x02,  // Generate interrupt after 3 datasets in FIFO
145
+  APDS9960_GFIFO_16 = 0x03, // Generate interrupt after 4 datasets in FIFO
181 146
 };
182 147
 
183
-enum
184
-{
185
-	APDS9960_GPULSE_4US		= 0x00,
186
-	APDS9960_GPULSE_8US		= 0x01,
187
-	APDS9960_GPULSE_16US	= 0x02,
188
-	APDS9960_GPULSE_32US	= 0x03,
148
+/** Gesture Gain */
149
+enum {
150
+  APDS9960_GGAIN_1 = 0x00, // Gain 1x
151
+  APDS9960_GGAIN_2 = 0x01, // Gain 2x
152
+  APDS9960_GGAIN_4 = 0x02, // Gain 4x
153
+  APDS9960_GGAIN_8 = 0x03, // Gain 8x
189 154
 };
190 155
 
191
-#define APDS9960_TIME_MULT 2.78 //millisec
156
+/** Pulse Lenghts */
157
+enum {
158
+  APDS9960_GPULSE_4US = 0x00,  // Pulse 4us
159
+  APDS9960_GPULSE_8US = 0x01,  // Pulse 8us
160
+  APDS9960_GPULSE_16US = 0x02, // Pulse 16us
161
+  APDS9960_GPULSE_32US = 0x03, // Pulse 32us
162
+};
192 163
 
193
-#define APDS9960_UP 0x01
194
-#define APDS9960_DOWN 0x02
195
-#define APDS9960_LEFT 0x03
196
-#define APDS9960_RIGHT 0x04
164
+#define APDS9960_UP 0x01    /**< Gesture Up */
165
+#define APDS9960_DOWN 0x02  /**< Gesture Down */
166
+#define APDS9960_LEFT 0x03  /**< Gesture Left */
167
+#define APDS9960_RIGHT 0x04 /**< Gesture Right */
197 168
 
169
+/*!
170
+ *  @brief  Class that stores state and functions for interacting with
171
+ *          APDS9960 Sensor
172
+ */
198 173
 class Adafruit_APDS9960 {
199
- public:
200
-  Adafruit_APDS9960(void) {};
201
-  ~Adafruit_APDS9960(void) {};
202
-  
203
-  boolean  begin(uint16_t iTimeMS = 10, apds9960AGain_t = APDS9960_AGAIN_4X, uint8_t addr = APDS9960_ADDRESS);
204
-  void     setADCIntegrationTime(uint16_t iTimeMS);
205
-  float    getADCIntegrationTime(void);
206
-  void     setADCGain(apds9960AGain_t gain);
207
-  apds9960AGain_t getADCGain(void);
208
-  void     setLED(apds9960LedDrive_t drive, apds9960LedBoost_t boost);
174
+public:
175
+  Adafruit_APDS9960(){};
176
+  ~Adafruit_APDS9960(){};
177
+
178
+  boolean begin(uint16_t iTimeMS = 10, apds9960AGain_t = APDS9960_AGAIN_4X,
179
+                uint8_t addr = APDS9960_ADDRESS, TwoWire *theWire = &Wire);
180
+  void setADCIntegrationTime(uint16_t iTimeMS);
181
+  float getADCIntegrationTime();
182
+  void setADCGain(apds9960AGain_t gain);
183
+  apds9960AGain_t getADCGain();
184
+  void setLED(apds9960LedDrive_t drive, apds9960LedBoost_t boost);
209 185
 
210 186
   // proximity
211
-  void     enableProximity(boolean en = true);
212
-  void     setProxGain(apds9960PGain_t gain);
213
-  apds9960PGain_t getProxGain(void);
214
-  void     setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses);
215
-  void	   enableProximityInterrupt();
216
-  void	   disableProximityInterrupt();
217
-  uint8_t  readProximity(void);
218
-  void	   setProximityInterruptThreshold(uint8_t low, uint8_t high, uint8_t persistance = 4);
219
-  bool	   getProximityInterrupt();
187
+  void enableProximity(boolean en = true);
188
+  void setProxGain(apds9960PGain_t gain);
189
+  apds9960PGain_t getProxGain();
190
+  void setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses);
191
+  void enableProximityInterrupt();
192
+  void disableProximityInterrupt();
193
+  uint8_t readProximity();
194
+  void setProximityInterruptThreshold(uint8_t low, uint8_t high,
195
+                                      uint8_t persistance = 4);
196
+  bool getProximityInterrupt();
220 197
 
221 198
   // gesture
222
-  void     enableGesture(boolean en = true);
223
-  bool	   gestureValid();
224
-  void	   setGestureDimensions(uint8_t dims);
225
-  void	   setGestureFIFOThreshold(uint8_t thresh);
226
-  void	   setGestureGain(uint8_t gain);
227
-  void	   setGestureProximityThreshold(uint8_t thresh);
228
-  void	   setGestureOffset(uint8_t offset_up, uint8_t offset_down, uint8_t offset_left, uint8_t offset_right);
229
-  uint8_t  readGesture(void);
230
-  void	   resetCounts();
199
+  void enableGesture(boolean en = true);
200
+  bool gestureValid();
201
+  void setGestureDimensions(uint8_t dims);
202
+  void setGestureFIFOThreshold(uint8_t thresh);
203
+  void setGestureGain(uint8_t gain);
204
+  void setGestureProximityThreshold(uint8_t thresh);
205
+  void setGestureOffset(uint8_t offset_up, uint8_t offset_down,
206
+                        uint8_t offset_left, uint8_t offset_right);
207
+  uint8_t readGesture();
208
+  void resetCounts();
231 209
 
232 210
   // light & color
233
-  void     enableColor(boolean en = true);
234
-  bool	   colorDataReady();
235
-  void     getColorData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
211
+  void enableColor(boolean en = true);
212
+  bool colorDataReady();
213
+  void getColorData(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c);
236 214
   uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
237 215
   uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
238 216
   void enableColorInterrupt();
239 217
   void disableColorInterrupt();
240
-  void clearInterrupt(void);
218
+  void clearInterrupt();
241 219
   void setIntLimits(uint16_t l, uint16_t h);
242 220
 
243 221
   // turn on/off elements
244
-  void     enable(boolean en = true);
245
-
222
+  void enable(boolean en = true);
246 223
 
247
- private:
224
+private:
248 225
   uint8_t _i2caddr;
249
- 
226
+  TwoWire *_wire;
227
+
250 228
   uint32_t read32(uint8_t reg);
251 229
   uint16_t read16(uint8_t reg);
252 230
   uint16_t read16R(uint8_t reg);
253
-  
254
-  void      write8(byte reg, byte value);
255
-  uint8_t   read8(byte reg);
256
-  
231
+
232
+  void write8(byte reg, byte value);
233
+  uint8_t read8(byte reg);
234
+
257 235
   uint8_t gestCnt;
258
-  
236
+
259 237
   uint8_t UCount;
260 238
   uint8_t DCount;
261
-  
239
+
262 240
   uint8_t LCount;
263 241
   uint8_t RCount;
264
-  
242
+
265 243
   uint8_t read(uint8_t reg, uint8_t *buf, uint8_t num);
266 244
   void write(uint8_t reg, uint8_t *buf, uint8_t num);
267 245
   void _i2c_init();
268
-  
246
+
269 247
   struct enable {
270
-		
271
-		//power on
272
-		uint8_t PON : 1;
273
-		
274
-		//ALS enable
275
-		uint8_t AEN : 1;
276
-		
277
-		//Proximity detect enable
278
-		uint8_t PEN : 1;
279
-		
280
-		//wait timer enable
281
-		uint8_t WEN : 1;
282
-		
283
-		//ALS interrupt enable
284
-		uint8_t AIEN : 1;
285
-		
286
-		//proximity interrupt enable
287
-		uint8_t PIEN : 1;
288
-		
289
-		//gesture enable
290
-		uint8_t GEN : 1;
291
-		
292
-		uint8_t get() {
293
-			return (GEN << 6) | (PIEN << 5) | (AIEN << 4) | (WEN << 3) | (PEN << 2) | (AEN << 1) | PON;
294
-		};
295
-	};
296
-	struct enable _enable;
297
-	
298
-	struct pers {
299
-		//ALS Interrupt Persistence. Controls rate of Clear channel interrupt to the host processor
300
-		uint8_t APERS : 4;
301
-		
302
-		//proximity interrupt persistence, controls rate of prox interrupt to host processor
303
-		uint8_t PPERS : 4;
304
-		
305
-		uint8_t get(){
306
-			return (PPERS << 4) | APERS;
307
-		};
308
-	};
309
-	pers _pers;
310
-	
311
-	struct config1 {
312
-		uint8_t WLONG : 1;
313
-		
314
-		uint8_t get(){
315
-			return WLONG << 1;
316
-		};
317
-	};
318
-	config1 _config1;
319
-	
320
-	struct ppulse {
321
-		
322
-		/*Proximity Pulse Count. Specifies the number of proximity pulses to be generated on LDR.
323
-		Number of pulses is set by PPULSE value plus 1.
324
-		*/
325
-		uint8_t PPULSE : 6;
326
-		
327
-		//Proximity Pulse Length. Sets the LED-ON pulse width during a proximity LDR pulse.
328
-		uint8_t PPLEN : 2;
329
-		
330
-		uint8_t get(){
331
-			return (PPLEN << 6) | PPULSE;
332
-		}
333
-	};
334
-	ppulse _ppulse;
335
-	
336
-	struct control {
337
-		//ALS and Color gain control
338
-		uint8_t AGAIN : 2;
339
-		
340
-		//proximity gain control
341
-		uint8_t PGAIN : 2;
342
-		
343
-		//led drive strength
344
-		uint8_t LDRIVE : 2;
345
-		
346
-		uint8_t get(){
347
-			return (LDRIVE << 6) | (PGAIN << 2) | AGAIN;
348
-		}
349
-	};
350
-	control _control;
351
-	
352
-	struct config2 {
353
-		/* Additional LDR current during proximity and gesture LED pulses. Current value, set by LDRIVE,
354
-		is increased by the percentage of LED_BOOST.
355
-		*/
356
-		uint8_t LED_BOOST : 2;
357
-		
358
-		//clear photodiode saturation int enable
359
-		uint8_t CPSIEN : 1;
360
-		
361
-		//proximity saturation interrupt enable
362
-		uint8_t PSIEN : 1;
363
-		
364
-		uint8_t get(){
365
-			return (PSIEN << 7) | (CPSIEN << 6) | (LED_BOOST << 4) | 1;
366
-		}
367
-	};
368
-	config2 _config2;
369
-	
370
-	struct status {
371
-		/* ALS Valid. Indicates that an ALS cycle has completed since AEN was asserted or since a read
372
-		from any of the ALS/Color data registers.
373
-		*/
374
-		uint8_t AVALID : 1;
375
-		
376
-		/* Proximity Valid. Indicates that a proximity cycle has completed since PEN was asserted or since
377
-		PDATA was last read. A read of PDATA automatically clears PVALID.
378
-		*/
379
-		uint8_t PVALID : 1;
380
-		
381
-		/* Gesture Interrupt. GINT is asserted when GFVLV becomes greater than GFIFOTH or if GVALID
382
-		has become asserted when GMODE transitioned to zero. The bit is reset when FIFO is
383
-		completely emptied (read).
384
-		*/
385
-		uint8_t GINT : 1;
386
-		
387
-		//ALS Interrupt. This bit triggers an interrupt if AIEN in ENABLE is set.
388
-		uint8_t AINT : 1;
389
-		
390
-		//Proximity Interrupt. This bit triggers an interrupt if PIEN in ENABLE is set.
391
-		uint8_t PINT : 1;
392
-		
393
-		/* Indicates that an analog saturation event occurred during a previous proximity or gesture
394
-		cycle. Once set, this bit remains set until cleared by clear proximity interrupt special function
395
-		command (0xE5 PICLEAR) or by disabling Prox (PEN=0). This bit triggers an interrupt if PSIEN
396
-		is set.
397
-		*/
398
-		uint8_t PGSAT : 1;
399
-		
400
-		/* Clear Photodiode Saturation. When asserted, the analog sensor was at the upper end of its
401
-		dynamic range. The bit can be de-asserted by sending a Clear channel interrupt command
402
-		(0xE6 CICLEAR) or by disabling the ADC (AEN=0). This bit triggers an interrupt if CPSIEN is set.
403
-		*/
404
-		uint8_t CPSAT : 1;
405
-		
406
-		void set(uint8_t data){
407
-			AVALID = data & 0x01;
408
-			PVALID = (data >> 1) & 0x01;
409
-			GINT = (data >> 2) & 0x01;
410
-			AINT = (data >> 4) & 0x01;
411
-			PINT = (data >> 5) & 0x01;
412
-			PGSAT = (data >> 6) & 0x01;
413
-			CPSAT = (data >> 7) & 0x01;
414
-		}
415
-	};
416
-	status _status;
417
-	
418
-	struct config3 {
419
-		//proximity mask
420
-		uint8_t PMASK_R : 1;
421
-		uint8_t PMASK_L : 1;
422
-		uint8_t PMASK_D : 1;
423
-		uint8_t PMASK_U : 1;
424
-		
425
-		/* Sleep After Interrupt. When enabled, the device will automatically enter low power mode
426
-		when the INT pin is asserted and the state machine has progressed to the SAI decision block.
427
-		Normal operation is resumed when INT pin is cleared over I2C.
428
-		*/
429
-		uint8_t SAI : 1;
430
-		
431
-		/* Proximity Gain Compensation Enable. This bit provides gain compensation when proximity
432
-		photodiode signal is reduced as a result of sensor masking. If only one diode of the diode pair
433
-		is contributing, then only half of the signal is available at the ADC; this results in a maximum
434
-		ADC value of 127. Enabling PCMP enables an additional gain of 2X, resulting in a maximum
435
-		ADC value of 255.
436
-		*/
437
-		uint8_t PCMP : 1;
438
-		
439
-		uint8_t get(){
440
-			return (PCMP << 5) | (SAI << 4) | (PMASK_U << 3) | (PMASK_D << 2) | (PMASK_L << 1) | PMASK_R;
441
-		}
442
-	};
443
-	config3 _config3;
444
-	
445
-	struct gconf1 {
446
-		/* Gesture Exit Persistence. When a number of consecutive “gesture end” occurrences become
447
-		equal or greater to the GEPERS value, the Gesture state machine is exited.
448
-		*/
449
-		uint8_t GEXPERS : 2;
450
-		
451
-		/* Gesture Exit Mask. Controls which of the gesture detector photodiodes (UDLR) will be included
452
-		to determine a “gesture end” and subsequent exit of the gesture state machine. Unmasked
453
-		UDLR data will be compared with the value in GTHR_OUT. Field value bits correspond to UDLR
454
-		detectors.
455
-		*/
456
-		uint8_t GEXMSK : 4;
457
-		
458
-		/* Gesture FIFO Threshold. This value is compared with the FIFO Level (i.e. the number of UDLR
459
-		datasets) to generate an interrupt (if enabled).
460
-		*/
461
-		uint8_t GFIFOTH : 2;
462
-		
463
-		uint8_t get(){
464
-			return (GFIFOTH << 6) | (GEXMSK << 2) | GEXPERS;
465
-		}
466
-	};
467
-	gconf1 _gconf1;
468
-	
469
-	struct gconf2 {
470
-		/* Gesture Wait Time. The GWTIME controls the amount of time in a low power mode between
471
-		gesture detection cycles.
472
-		*/
473
-		uint8_t GWTIME : 3;
474
-		
475
-		//Gesture LED Drive Strength. Sets LED Drive Strength in gesture mode.
476
-		uint8_t GLDRIVE : 2;
477
-		
478
-		//Gesture Gain Control. Sets the gain of the proximity receiver in gesture mode.
479
-		uint8_t GGAIN : 2;
480
-		
481
-		uint8_t get(){
482
-			return (GGAIN << 5) | (GLDRIVE << 3) | GWTIME;
483
-		}
484
-	};
485
-	gconf2 _gconf2;
486
-	
487
-	struct gpulse {
488
-		/* Number of Gesture Pulses. Specifies the number of pulses to be generated on LDR.
489
-		Number of pulses is set by GPULSE value plus 1.
490
-		*/
491
-		uint8_t GPULSE : 6;
492
-		
493
-		//Gesture Pulse Length. Sets the LED_ON pulse width during a Gesture LDR Pulse. 
494
-		uint8_t GPLEN : 2;
495
-		
496
-		uint8_t get(){
497
-			return (GPLEN << 6) | GPULSE;
498
-		}
499
-	};
500
-	gpulse _gpulse;
501
-	
502
-	struct gconf3 {
503
-		/* Gesture Dimension Select. Selects which gesture photodiode pairs are enabled to gather
504
-		results during gesture.
505
-		*/
506
-		uint8_t GDIMS : 2;
507
-		
508
-		uint8_t get(){
509
-			return GDIMS;
510
-		}
511
-	};
512
-	gconf3 _gconf3;
513
-	
514
-	struct gconf4 {
515
-		/* Gesture Mode. Reading this bit reports if the gesture state machine is actively running, 1
516
-		= Gesture, 0= ALS, Proximity, Color. Writing a 1 to this bit causes immediate entry in to the
517
-		gesture state machine (as if GPENTH had been exceeded). Writing a 0 to this bit causes exit of
518
-		gesture when current analog conversion has finished (as if GEXTH had been exceeded).
519
-		*/
520
-		uint8_t GMODE : 1;
521
-		
522
-		/* Gesture interrupt enable. Gesture Interrupt Enable. When asserted, all gesture related
523
-		interrupts are unmasked.
524
-		*/
525
-		uint8_t GIEN : 2;
526
-		
527
-		uint8_t get(){
528
-			return (GIEN << 1) | GMODE;
529
-		}
530
-		void set(uint8_t data){
531
-			GIEN = (data >> 1) & 0x01;
532
-			GMODE = data & 0x01;
533
-		}
534
-	};
535
-	gconf4 _gconf4;
536
-	
537
-	struct gstatus {
538
-		/* Gesture FIFO Data. GVALID bit is sent when GFLVL becomes greater than GFIFOTH (i.e. FIFO has
539
-		enough data to set GINT). GFIFOD is reset when GMODE = 0 and the GFLVL=0 (i.e. All FIFO data
540
-		has been read).
541
-		*/
542
-		uint8_t GVALID : 1;
543
-		
544
-		/* Gesture FIFO Overflow. A setting of 1 indicates that the FIFO has filled to capacity and that new
545
-		gesture detector data has been lost.
546
-		*/
547
-		uint8_t GFOV : 1;
548
-		
549
-		void set(uint8_t data){
550
-			GFOV = (data >> 1) & 0x01;
551
-			GVALID = data & 0x01;
552
-		}
553
-	};
554
-	gstatus _gstatus;
555
-  
248
+
249
+    // power on
250
+    uint8_t PON : 1;
251
+
252
+    // ALS enable
253
+    uint8_t AEN : 1;
254
+
255
+    // Proximity detect enable
256
+    uint8_t PEN : 1;
257
+
258
+    // wait timer enable
259
+    uint8_t WEN : 1;
260
+
261
+    // ALS interrupt enable
262
+    uint8_t AIEN : 1;
263
+
264
+    // proximity interrupt enable
265
+    uint8_t PIEN : 1;
266
+
267
+    // gesture enable
268
+    uint8_t GEN : 1;
269
+
270
+    uint8_t get() {
271
+      return (GEN << 6) | (PIEN << 5) | (AIEN << 4) | (WEN << 3) | (PEN << 2) |
272
+             (AEN << 1) | PON;
273
+    };
274
+  };
275
+  struct enable _enable;
276
+
277
+  struct pers {
278
+    // ALS Interrupt Persistence. Controls rate of Clear channel interrupt to
279
+    // the host processor
280
+    uint8_t APERS : 4;
281
+
282
+    // proximity interrupt persistence, controls rate of prox interrupt to host
283
+    // processor
284
+    uint8_t PPERS : 4;
285
+
286
+    uint8_t get() { return (PPERS << 4) | APERS; };
287
+  };
288
+  pers _pers;
289
+
290
+  struct config1 {
291
+    uint8_t WLONG : 1;
292
+
293
+    uint8_t get() { return WLONG << 1; };
294
+  };
295
+  config1 _config1;
296
+
297
+  struct ppulse {
298
+
299
+    /*Proximity Pulse Count. Specifies the number of proximity pulses to be
300
+    generated on LDR. Number of pulses is set by PPULSE value plus 1.
301
+    */
302
+    uint8_t PPULSE : 6;
303
+
304
+    // Proximity Pulse Length. Sets the LED-ON pulse width during a proximity
305
+    // LDR pulse.
306
+    uint8_t PPLEN : 2;
307
+
308
+    uint8_t get() { return (PPLEN << 6) | PPULSE; }
309
+  };
310
+  ppulse _ppulse;
311
+
312
+  struct control {
313
+    // ALS and Color gain control
314
+    uint8_t AGAIN : 2;
315
+
316
+    // proximity gain control
317
+    uint8_t PGAIN : 2;
318
+
319
+    // led drive strength
320
+    uint8_t LDRIVE : 2;
321
+
322
+    uint8_t get() { return (LDRIVE << 6) | (PGAIN << 2) | AGAIN; }
323
+  };
324
+  control _control;
325
+
326
+  struct config2 {
327
+    /* Additional LDR current during proximity and gesture LED pulses. Current
328
+    value, set by LDRIVE, is increased by the percentage of LED_BOOST.
329
+    */
330
+    uint8_t LED_BOOST : 2;
331
+
332
+    // clear photodiode saturation int enable
333
+    uint8_t CPSIEN : 1;
334
+
335
+    // proximity saturation interrupt enable
336
+    uint8_t PSIEN : 1;
337
+
338
+    uint8_t get() {
339
+      return (PSIEN << 7) | (CPSIEN << 6) | (LED_BOOST << 4) | 1;
340
+    }
341
+  };
342
+  config2 _config2;
343
+
344
+  struct status {
345
+    /* ALS Valid. Indicates that an ALS cycle has completed since AEN was
346
+    asserted or since a read from any of the ALS/Color data registers.
347
+    */
348
+    uint8_t AVALID : 1;
349
+
350
+    /* Proximity Valid. Indicates that a proximity cycle has completed since PEN
351
+    was asserted or since PDATA was last read. A read of PDATA automatically
352
+    clears PVALID.
353
+    */
354
+    uint8_t PVALID : 1;
355
+
356
+    /* Gesture Interrupt. GINT is asserted when GFVLV becomes greater than
357
+    GFIFOTH or if GVALID has become asserted when GMODE transitioned to zero.
358
+    The bit is reset when FIFO is completely emptied (read).
359
+    */
360
+    uint8_t GINT : 1;
361
+
362
+    // ALS Interrupt. This bit triggers an interrupt if AIEN in ENABLE is set.
363
+    uint8_t AINT : 1;
364
+
365
+    // Proximity Interrupt. This bit triggers an interrupt if PIEN in ENABLE is
366
+    // set.
367
+    uint8_t PINT : 1;
368
+
369
+    /* Indicates that an analog saturation event occurred during a previous
370
+    proximity or gesture cycle. Once set, this bit remains set until cleared by
371
+    clear proximity interrupt special function command (0xE5 PICLEAR) or by
372
+    disabling Prox (PEN=0). This bit triggers an interrupt if PSIEN is set.
373
+    */
374
+    uint8_t PGSAT : 1;
375
+
376
+    /* Clear Photodiode Saturation. When asserted, the analog sensor was at the
377
+    upper end of its dynamic range. The bit can be de-asserted by sending a
378
+    Clear channel interrupt command (0xE6 CICLEAR) or by disabling the ADC
379
+    (AEN=0). This bit triggers an interrupt if CPSIEN is set.
380
+    */
381
+    uint8_t CPSAT : 1;
382
+
383
+    void set(uint8_t data) {
384
+      AVALID = data & 0x01;
385
+      PVALID = (data >> 1) & 0x01;
386
+      GINT = (data >> 2) & 0x01;
387
+      AINT = (data >> 4) & 0x01;
388
+      PINT = (data >> 5) & 0x01;
389
+      PGSAT = (data >> 6) & 0x01;
390
+      CPSAT = (data >> 7) & 0x01;
391
+    }
392
+  };
393
+  status _status;
394
+
395
+  struct config3 {
396
+    // proximity mask
397
+    uint8_t PMASK_R : 1;
398
+    uint8_t PMASK_L : 1;
399
+    uint8_t PMASK_D : 1;
400
+    uint8_t PMASK_U : 1;
401
+
402
+    /* Sleep After Interrupt. When enabled, the device will automatically enter
403
+    low power mode when the INT pin is asserted and the state machine has
404
+    progressed to the SAI decision block. Normal operation is resumed when INT
405
+    pin is cleared over I2C.
406
+    */
407
+    uint8_t SAI : 1;
408
+
409
+    /* Proximity Gain Compensation Enable. This bit provides gain compensation
410
+    when proximity photodiode signal is reduced as a result of sensor masking.
411
+    If only one diode of the diode pair is contributing, then only half of the
412
+    signal is available at the ADC; this results in a maximum ADC value of 127.
413
+    Enabling PCMP enables an additional gain of 2X, resulting in a maximum ADC
414
+    value of 255.
415
+    */
416
+    uint8_t PCMP : 1;
417
+
418
+    uint8_t get() {
419
+      return (PCMP << 5) | (SAI << 4) | (PMASK_U << 3) | (PMASK_D << 2) |
420
+             (PMASK_L << 1) | PMASK_R;
421
+    }
422
+  };
423
+  config3 _config3;
424
+
425
+  struct gconf1 {
426
+    /* Gesture Exit Persistence. When a number of consecutive “gesture end”
427
+    occurrences become equal or greater to the GEPERS value, the Gesture state
428
+    machine is exited.
429
+    */
430
+    uint8_t GEXPERS : 2;
431
+
432
+    /* Gesture Exit Mask. Controls which of the gesture detector photodiodes
433
+    (UDLR) will be included to determine a “gesture end” and subsequent exit
434
+    of the gesture state machine. Unmasked UDLR data will be compared with the
435
+    value in GTHR_OUT. Field value bits correspond to UDLR detectors.
436
+    */
437
+    uint8_t GEXMSK : 4;
438
+
439
+    /* Gesture FIFO Threshold. This value is compared with the FIFO Level (i.e.
440
+    the number of UDLR datasets) to generate an interrupt (if enabled).
441
+    */
442
+    uint8_t GFIFOTH : 2;
443
+
444
+    uint8_t get() { return (GFIFOTH << 6) | (GEXMSK << 2) | GEXPERS; }
445
+  };
446
+  gconf1 _gconf1;
447
+
448
+  struct gconf2 {
449
+    /* Gesture Wait Time. The GWTIME controls the amount of time in a low power
450
+    mode between gesture detection cycles.
451
+    */
452
+    uint8_t GWTIME : 3;
453
+
454
+    // Gesture LED Drive Strength. Sets LED Drive Strength in gesture mode.
455
+    uint8_t GLDRIVE : 2;
456
+
457
+    // Gesture Gain Control. Sets the gain of the proximity receiver in gesture
458
+    // mode.
459
+    uint8_t GGAIN : 2;
460
+
461
+    uint8_t get() { return (GGAIN << 5) | (GLDRIVE << 3) | GWTIME; }
462
+  };
463
+  gconf2 _gconf2;
464
+
465
+  struct gpulse {
466
+    /* Number of Gesture Pulses. Specifies the number of pulses to be generated
467
+    on LDR. Number of pulses is set by GPULSE value plus 1.
468
+    */
469
+    uint8_t GPULSE : 6;
470
+
471
+    // Gesture Pulse Length. Sets the LED_ON pulse width during a Gesture LDR
472
+    // Pulse.
473
+    uint8_t GPLEN : 2;
474
+
475
+    uint8_t get() { return (GPLEN << 6) | GPULSE; }
476
+  };
477
+  gpulse _gpulse;
478
+
479
+  struct gconf3 {
480
+    /* Gesture Dimension Select. Selects which gesture photodiode pairs are
481
+    enabled to gather results during gesture.
482
+    */
483
+    uint8_t GDIMS : 2;
484
+
485
+    uint8_t get() { return GDIMS; }
486
+  };
487
+  gconf3 _gconf3;
488
+
489
+  struct gconf4 {
490
+    /* Gesture Mode. Reading this bit reports if the gesture state machine is
491
+    actively running, 1 = Gesture, 0= ALS, Proximity, Color. Writing a 1 to this
492
+    bit causes immediate entry in to the gesture state machine (as if GPENTH had
493
+    been exceeded). Writing a 0 to this bit causes exit of gesture when current
494
+    analog conversion has finished (as if GEXTH had been exceeded).
495
+    */
496
+    uint8_t GMODE : 1;
497
+
498
+    /* Gesture interrupt enable. Gesture Interrupt Enable. When asserted, all
499
+    gesture related interrupts are unmasked.
500
+    */
501
+    uint8_t GIEN : 2;
502
+
503
+    uint8_t get() { return (GIEN << 1) | GMODE; }
504
+    void set(uint8_t data) {
505
+      GIEN = (data >> 1) & 0x01;
506
+      GMODE = data & 0x01;
507
+    }
508
+  };
509
+  gconf4 _gconf4;
510
+
511
+  struct gstatus {
512
+    /* Gesture FIFO Data. GVALID bit is sent when GFLVL becomes greater than
513
+    GFIFOTH (i.e. FIFO has enough data to set GINT). GFIFOD is reset when GMODE
514
+    = 0 and the GFLVL=0 (i.e. All FIFO data has been read).
515
+    */
516
+    uint8_t GVALID : 1;
517
+
518
+    /* Gesture FIFO Overflow. A setting of 1 indicates that the FIFO has filled
519
+    to capacity and that new gesture detector data has been lost.
520
+    */
521
+    uint8_t GFOV : 1;
522
+
523
+    void set(uint8_t data) {
524
+      GFOV = (data >> 1) & 0x01;
525
+      GVALID = data & 0x01;
526
+    }
527
+  };
528
+  gstatus _gstatus;
556 529
 };
557 530
 
558 531
 #endif

+ 18
- 0
firmware/libraries/Adafruit_APDS9960_Library/README.md View File

@@ -0,0 +1,18 @@
1
+# Adafruit APDS9960 Library [![Build Status](https://travis-ci.com/adafruit/Adafruit_APDS9960.svg?branch=master)](https://travis-ci.com/adafruit/Adafruit_APDS9960)
2
+
3
+<a href="https://www.adafruit.com/product/3595"><img src="assets/board.jpg?raw=true" width="500px"></a>
4
+
5
+This is the Adafruit APDS9960 Proximity, Light, RGB, and Gesture sensor Library
6
+
7
+Tested and works great with the Adafruit APDS9960 Board 
8
+* http://www.adafruit.com/products/3595
9
+
10
+This chip uses I2C to communicate, 2 pins are required to interface
11
+
12
+Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
13
+
14
+Written by Dean Miller, Limor Fried for Adafruit Industries.  
15
+BSD license, check license.txt for more information
16
+All text above must be included in any redistribution
17
+
18
+To install, use the Arduino Library Manager and search for "Adafruit APDS9960 Library" and install the library.

BIN
firmware/libraries/Adafruit_APDS9960_Library/assets/board.jpg View File


+ 127
- 0
firmware/libraries/Adafruit_APDS9960_Library/code-of-conduct.md View File

@@ -0,0 +1,127 @@
1
+# Adafruit Community Code of Conduct
2
+
3
+## Our Pledge
4
+
5
+In the interest of fostering an open and welcoming environment, we as
6
+contributors and leaders pledge to making participation in our project and
7
+our community a harassment-free experience for everyone, regardless of age, body
8
+size, disability, ethnicity, gender identity and expression, level or type of
9
+experience, education, socio-economic status, nationality, personal appearance,
10
+race, religion, or sexual identity and orientation.
11
+
12
+## Our Standards
13
+
14
+We are committed to providing a friendly, safe and welcoming environment for
15
+all.
16
+
17
+Examples of behavior that contributes to creating a positive environment
18
+include:
19
+
20
+* Be kind and courteous to others
21
+* Using welcoming and inclusive language
22
+* Being respectful of differing viewpoints and experiences
23
+* Collaborating with other community members
24
+* Gracefully accepting constructive criticism
25
+* Focusing on what is best for the community
26
+* Showing empathy towards other community members
27
+
28
+Examples of unacceptable behavior by participants include:
29
+
30
+* The use of sexualized language or imagery and sexual attention or advances
31
+* The use of inappropriate images, including in a community member's avatar
32
+* The use of inappropriate language, including in a community member's nickname
33
+* Any spamming, flaming, baiting or other attention-stealing behavior
34
+* Excessive or unwelcome helping; answering outside the scope of the question
35
+  asked
36
+* Trolling, insulting/derogatory comments, and personal or political attacks
37
+* Public or private harassment
38
+* Publishing others' private information, such as a physical or electronic
39
+  address, without explicit permission
40
+* Other conduct which could reasonably be considered inappropriate
41
+
42
+The goal of the standards and moderation guidelines outlined here is to build
43
+and maintain a respectful community. We ask that you don’t just aim to be
44
+"technically unimpeachable", but rather try to be your best self. 
45
+
46
+We value many things beyond technical expertise, including collaboration and
47
+supporting others within our community. Providing a positive experience for
48
+other community members can have a much more significant impact than simply
49
+providing the correct answer.
50
+
51
+## Our Responsibilities
52
+
53
+Project leaders are responsible for clarifying the standards of acceptable
54
+behavior and are expected to take appropriate and fair corrective action in
55
+response to any instances of unacceptable behavior.
56
+
57
+Project leaders have the right and responsibility to remove, edit, or
58
+reject messages, comments, commits, code, issues, and other contributions
59
+that are not aligned to this Code of Conduct, or to ban temporarily or
60
+permanently any community member for other behaviors that they deem
61
+inappropriate, threatening, offensive, or harmful.
62
+
63
+## Moderation
64
+
65
+Instances of behaviors that violate the Adafruit Community Code of Conduct
66
+may be reported by any member of the community. Community members are
67
+encouraged to report these situations, including situations they witness
68
+involving other community members.
69
+
70
+You may report in the following ways:
71
+
72
+In any situation, you may send an email to <support@adafruit.com>.
73
+
74
+On the Adafruit Discord, you may send an open message from any channel
75
+to all Community Helpers by tagging @community helpers. You may also send an
76
+open message from any channel, or a direct message to @kattni#1507,
77
+@tannewt#4653, @Dan Halbert#1614, @cater#2442, @sommersoft#0222, or
78
+@Andon#8175.
79
+
80
+Email and direct message reports will be kept confidential.
81
+
82
+In situations on Discord where the issue is particularly egregious, possibly
83
+illegal, requires immediate action, or violates the Discord terms of service,
84
+you should also report the message directly to Discord.
85
+
86
+These are the steps for upholding our community’s standards of conduct.
87
+
88
+1. Any member of the community may report any situation that violates the
89
+Adafruit Community Code of Conduct. All reports will be reviewed and
90
+investigated.
91
+2. If the behavior is an egregious violation, the community member who
92
+committed the violation may be banned immediately, without warning.
93
+3. Otherwise, moderators will first respond to such behavior with a warning.
94
+4. Moderators follow a soft "three strikes" policy - the community member may
95
+be given another chance, if they are receptive to the warning and change their
96
+behavior.
97
+5. If the community member is unreceptive or unreasonable when warned by a
98
+moderator, or the warning goes unheeded, they may be banned for a first or
99
+second offense. Repeated offenses will result in the community member being
100
+banned.
101
+
102
+## Scope
103
+
104
+This Code of Conduct and the enforcement policies listed above apply to all
105
+Adafruit Community venues. This includes but is not limited to any community
106
+spaces (both public and private), the entire Adafruit Discord server, and
107
+Adafruit GitHub repositories. Examples of Adafruit Community spaces include
108
+but are not limited to meet-ups, audio chats on the Adafruit Discord, or
109
+interaction at a conference.
110
+
111
+This Code of Conduct applies both within project spaces and in public spaces
112
+when an individual is representing the project or its community. As a community
113
+member, you are representing our community, and are expected to behave
114
+accordingly.
115
+
116
+## Attribution
117
+
118
+This Code of Conduct is adapted from the [Contributor Covenant][homepage],
119
+version 1.4, available at
120
+<https://www.contributor-covenant.org/version/1/4/code-of-conduct.html>,
121
+and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html).
122
+
123
+For other projects adopting the Adafruit Community Code of
124
+Conduct, please contact the maintainers of those projects for enforcement.
125
+If you wish to use this code of conduct for your own project, consider
126
+explicitly mentioning your moderation policy or making a copy with your
127
+own moderation policy so as to avoid confusion.

+ 1
- 1
firmware/libraries/Adafruit_APDS9960_Library/library.properties View File

@@ -1,5 +1,5 @@
1 1
 name=Adafruit APDS9960 Library
2
-version=1.0.5
2
+version=1.1.1
3 3
 author=Adafruit
4 4
 maintainer=Adafruit <info@adafruit.com>
5 5
 sentence=This is a library for the Adafruit APDS9960 gesture/proximity/color/light sensor.

+ 26
- 0
firmware/libraries/Adafruit_APDS9960_Library/license.txt View File

@@ -0,0 +1,26 @@
1
+Software License Agreement (BSD License)
2
+
3
+Copyright (c) 2012, Adafruit Industries
4
+All rights reserved.
5
+
6
+Redistribution and use in source and binary forms, with or without
7
+modification, are permitted provided that the following conditions are met:
8
+1. Redistributions of source code must retain the above copyright
9
+notice, this list of conditions and the following disclaimer.
10
+2. Redistributions in binary form must reproduce the above copyright
11
+notice, this list of conditions and the following disclaimer in the
12
+documentation and/or other materials provided with the distribution.
13
+3. Neither the name of the copyright holders nor the
14
+names of its contributors may be used to endorse or promote products
15
+derived from this software without specific prior written permission.
16
+
17
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
18
+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 470
- 440
firmware/libraries/Adafruit_BME280_Library/Adafruit_BME280.cpp View File

@@ -1,530 +1,560 @@
1
-/***************************************************************************
2
-  This is a library for the BME280 humidity, temperature & pressure sensor
3
-
4
-  Designed specifically to work with the Adafruit BME280 Breakout
5
-  ----> http://www.adafruit.com/products/2650
6
-
7
-  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
8
-  to interface.
9
-
10
-  Adafruit invests time and resources providing this open source code,
11
-  please support Adafruit andopen-source hardware by purchasing products
12
-  from Adafruit!
1
+/*!
2
+ * @file Adafruit_BME280.cpp
3
+ *
4
+ * @mainpage Adafruit BME280 humidity, temperature & pressure sensor
5
+ *
6
+ * @section intro_sec Introduction
7
+ *
8
+ *  Driver for the BME280 humidity, temperature & pressure sensor
9
+ *
10
+ * These sensors use I2C or SPI to communicate, 2 or 4 pins are required
11
+ * to interface.
12
+ *
13
+ * Designed specifically to work with the Adafruit BME280 Breakout
14
+ * ----> http://www.adafruit.com/products/2652
15
+ *
16
+ *  Adafruit invests time and resources providing this open source code,
17
+ *  please support Adafruit and open-source hardware by purchasing
18
+ *  products from Adafruit!
19
+ *
20
+ * @section author Author
21
+ *
22
+ * Written by Kevin "KTOWN" Townsend for Adafruit Industries.
23
+ *
24
+ * @section license License
25
+ *
26
+ * BSD license, all text here must be included in any redistribution.
27
+ * See the LICENSE file for details.
28
+ *
29
+ */
13 30
 
14
-  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15
-  BSD license, all text above must be included in any redistribution
16
- ***************************************************************************/
31
+#include "Adafruit_BME280.h"
17 32
 #include "Arduino.h"
18
-#include <Wire.h>
19 33
 #include <SPI.h>
20
-#include "Adafruit_BME280.h"
21
-
22
-/***************************************************************************
23
- PRIVATE FUNCTIONS
24
- ***************************************************************************/
25
-Adafruit_BME280::Adafruit_BME280()
26
-    : _cs(-1), _mosi(-1), _miso(-1), _sck(-1)
27
-{ }
34
+#include <Wire.h>
28 35
 
29
-Adafruit_BME280::Adafruit_BME280(int8_t cspin)
30
-    : _cs(cspin), _mosi(-1), _miso(-1), _sck(-1)
31
-{ }
36
+/*!
37
+ *  @brief  class constructor
38
+ */
39
+Adafruit_BME280::Adafruit_BME280() : _cs(-1), _mosi(-1), _miso(-1), _sck(-1) {}
32 40
 
33
-Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin)
34
-    : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin)
35
-{ }
41
+/*!
42
+ *   @brief  class constructor if using hardware SPI
43
+ *   @param  cspin the chip select pin to use
44
+ *   @param  *theSPI
45
+ *           optional SPI object
46
+ */
47
+Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
48
+  _cs = cspin;
49
+  _mosi = _miso = _sck = -1;
50
+  _spi = theSPI;
51
+}
36 52
 
53
+/*!
54
+ *   @brief  class constructor if using software SPI
55
+ *   @param cspin the chip select pin to use
56
+ *   @param mosipin the MOSI pin to use
57
+ *   @param misopin the MISO pin to use
58
+ *   @param sckpin the SCK pin to use
59
+ */
60
+Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
61
+                                 int8_t sckpin)
62
+    : _cs(cspin), _mosi(mosipin), _miso(misopin), _sck(sckpin) {}
37 63
 
38
-/**************************************************************************/
39 64
 /*!
40
-    @brief  Initialise sensor with given parameters / settings
41
-*/
42
-/**************************************************************************/
43
-bool Adafruit_BME280::begin(TwoWire *theWire)
44
-{
45
-	_wire = theWire;
46
-	_i2caddr = BME280_ADDRESS;
47
-	return init();
65
+ *   @brief  Initialise sensor with given parameters / settings
66
+ *   @param theWire the I2C object to use
67
+ *   @returns true on success, false otherwise
68
+ */
69
+bool Adafruit_BME280::begin(TwoWire *theWire) {
70
+  _wire = theWire;
71
+  _i2caddr = BME280_ADDRESS;
72
+  return init();
48 73
 }
49 74
 
50
-bool Adafruit_BME280::begin(uint8_t addr)
51
-{
52
-	_i2caddr = addr;
53
-	_wire = &Wire;
54
-	return init();
75
+/*!
76
+ *   @brief  Initialise sensor with given parameters / settings
77
+ *   @param addr the I2C address the device can be found on
78
+ *   @returns true on success, false otherwise
79
+ */
80
+bool Adafruit_BME280::begin(uint8_t addr) {
81
+  _i2caddr = addr;
82
+  _wire = &Wire;
83
+  return init();
55 84
 }
56 85
 
57
-bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire)
58
-{
59
-    _i2caddr = addr;
60
-	_wire = theWire;
61
-	return init();
86
+/*!
87
+ *   @brief  Initialise sensor with given parameters / settings
88
+ *   @param addr the I2C address the device can be found on
89
+ *   @param theWire the I2C object to use
90
+ *   @returns true on success, false otherwise
91
+ */
92
+bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
93
+  _i2caddr = addr;
94
+  _wire = theWire;
95
+  return init();
62 96
 }
63 97
 
64
-bool Adafruit_BME280::begin(void)
65
-{
66
-    _i2caddr = BME280_ADDRESS;
67
-	_wire = &Wire;
68
-	return init();
98
+/*!
99
+ *   @brief  Initialise sensor with given parameters / settings
100
+ *   @returns true on success, false otherwise
101
+ */
102
+bool Adafruit_BME280::begin(void) {
103
+  bool status = false;
104
+  _i2caddr = BME280_ADDRESS;
105
+  _wire = &Wire;
106
+  status = init();
107
+  if (!status) {
108
+    _i2caddr = BME280_ADDRESS_ALTERNATE;
109
+    status = init();
110
+  }
111
+  return status;
69 112
 }
70 113
 
71
-bool Adafruit_BME280::init()
72
-{
73
-    // init I2C or SPI sensor interface
74
-    if (_cs == -1) {
75
-        // I2C
76
-        _wire -> begin();
114
+/*!
115
+ *   @brief  Initialise sensor with given parameters / settings
116
+ *   @returns true on success, false otherwise
117
+ */
118
+bool Adafruit_BME280::init() {
119
+  // init I2C or SPI sensor interface
120
+  if (_cs == -1) {
121
+    // I2C
122
+    _wire->begin();
123
+  } else {
124
+    digitalWrite(_cs, HIGH);
125
+    pinMode(_cs, OUTPUT);
126
+    if (_sck == -1) {
127
+      // hardware SPI
128
+      _spi->begin();
77 129
     } else {
78
-        digitalWrite(_cs, HIGH);
79
-        pinMode(_cs, OUTPUT);
80
-        if (_sck == -1) {
81
-            // hardware SPI
82
-            SPI.begin();
83
-        } else {
84
-            // software SPI
85
-            pinMode(_sck, OUTPUT);
86
-            pinMode(_mosi, OUTPUT);
87
-            pinMode(_miso, INPUT);
88
-        }
130
+      // software SPI
131
+      pinMode(_sck, OUTPUT);
132
+      pinMode(_mosi, OUTPUT);
133
+      pinMode(_miso, INPUT);
89 134
     }
135
+  }
90 136
 
91
-    // check if sensor, i.e. the chip ID is correct
92
-    if (read8(BME280_REGISTER_CHIPID) != 0x60)
93
-        return false;
137
+  // check if sensor, i.e. the chip ID is correct
138
+  _sensorID = read8(BME280_REGISTER_CHIPID);
139
+  if (_sensorID != 0x60)
140
+    return false;
94 141
 
95
-    // reset the device using soft-reset
96
-    // this makes sure the IIR is off, etc.
97
-    write8(BME280_REGISTER_SOFTRESET, 0xB6);
142
+  // reset the device using soft-reset
143
+  // this makes sure the IIR is off, etc.
144
+  write8(BME280_REGISTER_SOFTRESET, 0xB6);
98 145
 
99
-    // wait for chip to wake up.
100
-    delay(300);
146
+  // wait for chip to wake up.
147
+  delay(300);
101 148
 
102
-    // if chip is still reading calibration, delay
103
-    while (isReadingCalibration())
104
-          delay(100);
149
+  // if chip is still reading calibration, delay
150
+  while (isReadingCalibration())
151
+    delay(100);
105 152
 
106
-    readCoefficients(); // read trimming parameters, see DS 4.2.2
153
+  readCoefficients(); // read trimming parameters, see DS 4.2.2
107 154
 
108
-    setSampling(); // use defaults
155
+  setSampling(); // use defaults
109 156
 
110
-    delay(100);
157
+  delay(100);
111 158
 
112
-    return true;
159
+  return true;
113 160
 }
114 161
 
115
-/**************************************************************************/
116 162
 /*!
117
-    @brief  setup sensor with given parameters / settings
118
-    
119
-    This is simply a overload to the normal begin()-function, so SPI users
120
-    don't get confused about the library requiring an address.
121
-*/
122
-/**************************************************************************/
123
-
124
-
125
-void Adafruit_BME280::setSampling(sensor_mode       mode,
126
-		 sensor_sampling   tempSampling,
127
-		 sensor_sampling   pressSampling,
128
-		 sensor_sampling   humSampling,
129
-		 sensor_filter     filter,
130
-		 standby_duration  duration) {
131
-    _measReg.mode     = mode;
132
-    _measReg.osrs_t   = tempSampling;
133
-    _measReg.osrs_p   = pressSampling;
134
-        
135
-    
136
-    _humReg.osrs_h    = humSampling;
137
-    _configReg.filter = filter;
138
-    _configReg.t_sb   = duration;
139
-
140
-    
141
-    // you must make sure to also set REGISTER_CONTROL after setting the
142
-    // CONTROLHUMID register, otherwise the values won't be applied (see DS 5.4.3)
143
-    write8(BME280_REGISTER_CONTROLHUMID, _humReg.get());
144
-    write8(BME280_REGISTER_CONFIG, _configReg.get());
145
-    write8(BME280_REGISTER_CONTROL, _measReg.get());
163
+ *   @brief  setup sensor with given parameters / settings
164