Browse Source

Move all dependencies to the repository

This step seems bold, but is saves us so much hassle. Even better, we have a
reliable codebase, with all the dependencies (and their versions) we
need in order to build the project. If a library got an update, we can
replace it inplace if the code is still compatible.
Aaron Fischer 1 year ago
parent
commit
f24858d162
100 changed files with 86946 additions and 10 deletions
  1. 1
    2
      firmware/config.h
  2. 24
    7
      firmware/firmware.ino
  3. 0
    1
      firmware/influxdb.ino
  4. 577
    0
      firmware/libraries/Adafruit_APDS9960_Library/Adafruit_APDS9960.cpp
  5. 558
    0
      firmware/libraries/Adafruit_APDS9960_Library/Adafruit_APDS9960.h
  6. 59
    0
      firmware/libraries/Adafruit_APDS9960_Library/examples/color_sensor/color_sensor.ino
  7. 47
    0
      firmware/libraries/Adafruit_APDS9960_Library/examples/gesture_sensor/gesture_sensor.ino
  8. 56
    0
      firmware/libraries/Adafruit_APDS9960_Library/examples/proximity_sensor/proximity_sensor.ino
  9. 9
    0
      firmware/libraries/Adafruit_APDS9960_Library/library.properties
  10. 530
    0
      firmware/libraries/Adafruit_BME280_Library/Adafruit_BME280.cpp
  11. 300
    0
      firmware/libraries/Adafruit_BME280_Library/Adafruit_BME280.h
  12. 59
    0
      firmware/libraries/Adafruit_BME280_Library/README.md
  13. 157
    0
      firmware/libraries/Adafruit_BME280_Library/examples/advancedsettings/advancedsettings.ino
  14. 82
    0
      firmware/libraries/Adafruit_BME280_Library/examples/bme280test/bme280test.ino
  15. 9
    0
      firmware/libraries/Adafruit_BME280_Library/library.properties
  16. 154
    0
      firmware/libraries/Adafruit_Unified_Sensor/Adafruit_Sensor.h
  17. 218
    0
      firmware/libraries/Adafruit_Unified_Sensor/README.md
  18. 9
    0
      firmware/libraries/Adafruit_Unified_Sensor/library.properties
  19. 176
    0
      firmware/libraries/ESP8266Influxdb/ESP8266Influxdb.cpp
  20. 72
    0
      firmware/libraries/ESP8266Influxdb/ESP8266Influxdb.h
  21. 1
    0
      firmware/libraries/ESP8266Influxdb/README.md
  22. 63
    0
      firmware/libraries/ESP8266Influxdb/examples/influxdb_write/influxdb_write.ino
  23. 24
    0
      firmware/libraries/ESP8266Influxdb/keywords.txt
  24. 27
    0
      firmware/libraries/WiFi/README.adoc
  25. 127
    0
      firmware/libraries/WiFi/examples/ConnectNoEncryption/ConnectNoEncryption.ino
  26. 132
    0
      firmware/libraries/WiFi/examples/ConnectWithWEP/ConnectWithWEP.ino
  27. 122
    0
      firmware/libraries/WiFi/examples/ConnectWithWPA/ConnectWithWPA.ino
  28. 119
    0
      firmware/libraries/WiFi/examples/ScanNetworks/ScanNetworks.ino
  29. 132
    0
      firmware/libraries/WiFi/examples/SimpleWebServerWiFi/SimpleWebServerWiFi.ino
  30. 117
    0
      firmware/libraries/WiFi/examples/WiFiChatServer/WiFiChatServer.ino
  31. 182
    0
      firmware/libraries/WiFi/examples/WiFiUdpNtpClient/WiFiUdpNtpClient.ino
  32. 118
    0
      firmware/libraries/WiFi/examples/WiFiUdpSendReceiveString/WiFiUdpSendReceiveString.ino
  33. 126
    0
      firmware/libraries/WiFi/examples/WiFiWebClient/WiFiWebClient.ino
  34. 131
    0
      firmware/libraries/WiFi/examples/WiFiWebClientRepeating/WiFiWebClientRepeating.ino
  35. 138
    0
      firmware/libraries/WiFi/examples/WiFiWebServer/WiFiWebServer.ino
  36. BIN
      firmware/libraries/WiFi/extras/binary/wifiHD.elf
  37. 16358
    0
      firmware/libraries/WiFi/extras/binary/wifiHD.hex
  38. BIN
      firmware/libraries/WiFi/extras/binary/wifiHD_2_1.elf
  39. BIN
      firmware/libraries/WiFi/extras/binary/wifi_dnld.elf
  40. 10470
    0
      firmware/libraries/WiFi/extras/binary/wifi_dnld.hex
  41. BIN
      firmware/libraries/WiFi/extras/binary/wifi_dnld_2_1.elf
  42. 121
    0
      firmware/libraries/WiFi/extras/scripts/ArduinoWifiShield_upgrade.sh
  43. 4045
    0
      firmware/libraries/WiFi/extras/wifiHD/.cproject
  44. 77
    0
      firmware/libraries/WiFi/extras/wifiHD/.project
  45. BIN
      firmware/libraries/WiFi/extras/wifiHD/Release/wifiHD.elf
  46. 16358
    0
      firmware/libraries/WiFi/extras/wifiHD/Release/wifiHD.hex
  47. 170
    0
      firmware/libraries/WiFi/extras/wifiHD/src/CONFIG/conf_access.h
  48. 83
    0
      firmware/libraries/WiFi/extras/wifiHD/src/CONFIG/conf_at45dbx.h
  49. 108
    0
      firmware/libraries/WiFi/extras/wifiHD/src/CONFIG/conf_ebi.h
  50. 73
    0
      firmware/libraries/WiFi/extras/wifiHD/src/CONFIG/conf_sd_mmc_spi.h
  51. 74
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/ASM/trampoline.x
  52. 237
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/arduino.h
  53. 346
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/led.c
  54. 191
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/ARDUINO/led.h
  55. 433
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/EVK1105/evk1105.h
  56. 346
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/EVK1105/led.c
  57. 187
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/EVK1105/led.h
  58. 120
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/board.h
  59. 120
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/BOARDS/board.h.my
  60. 653
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/MEMORY/DATA_FLASH/AT45DBX/at45dbx.c
  61. 270
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/MEMORY/DATA_FLASH/AT45DBX/at45dbx.h
  62. 234
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/MEMORY/DATA_FLASH/AT45DBX/at45dbx_mem.c
  63. 164
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/MEMORY/DATA_FLASH/AT45DBX/at45dbx_mem.h
  64. BIN
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/UCR1/GCC/lib_ucr1_hd_sdio_v2.7.0.a
  65. BIN
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/UCR1/GCC/lib_ucr1_hd_spi_v2.7.0.a
  66. BIN
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/UCR1/GCC/lib_ucr1_hd_wl_sta_intwpa_v2.7.0.a
  67. BIN
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/UCR2/GCC/lib_ucr2_hd_sdio_v2.7.0.a
  68. BIN
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/UCR2/GCC/lib_ucr2_hd_spi_v2.7.0.a
  69. BIN
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/UCR2/GCC/lib_ucr2_hd_wl_sta_intwpa_v2.7.0.a
  70. 1
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/v2.7.0/revision.txt
  71. 1687
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/wl_api.h
  72. 19287
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/wl_fw.h
  73. 35
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/wl_os.h
  74. 172
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/wl_sdio.h
  75. 185
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/wl_spi.h
  76. 154
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/COMPONENTS/WIFI/HD/wlap_api.h
  77. 309
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/CPU/CYCLE_COUNTER/cycle_counter.h
  78. 995
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/EBI/SMC/smc.c
  79. 68
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/EBI/SMC/smc.h
  80. 183
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/EIC/eic.c
  81. 275
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/EIC/eic.h
  82. 1117
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/FLASHC/flashc.c
  83. 1002
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/FLASHC/flashc.h
  84. 458
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/GPIO/gpio.c
  85. 583
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/GPIO/gpio.h
  86. 239
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/INTC/exception.x
  87. 214
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/INTC/intc.c
  88. 100
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/INTC/intc.h
  89. 296
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PDCA/pdca.c
  90. 251
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PDCA/pdca.h
  91. 546
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PM/pm.c
  92. 493
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PM/pm.h
  93. 268
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PM/pm_conf_clocks.c
  94. 566
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PM/power_clocks_lib.c
  95. 379
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/PM/power_clocks_lib.h
  96. 213
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.c
  97. 191
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/RTC/rtc.h
  98. 443
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/SPI/spi.c
  99. 342
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/SPI/spi.h
  100. 0
    0
      firmware/libraries/WiFi/extras/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/TC/tc.c

+ 1
- 2
firmware/config.h View File

@@ -1,4 +1,3 @@
1
-
2 1
 #ifndef __CONFIG_H__
3 2
 #define __CONFIG_H__
4 3
 
@@ -44,4 +43,4 @@
44 43
 
45 44
 #define INITIAL_WEBSERVER_TIME 20
46 45
 
47
-#endif
46
+#endif

+ 24
- 7
firmware/firmware.ino View File

@@ -1,16 +1,33 @@
1
-#include <WiFiClient.h>
1
+// Standard ESP8266 libs
2 2
 #include <ESP8266mDNS.h>
3 3
 #include <ESP8266HTTPUpdateServer.h>
4 4
 #include <ESP8266WiFi.h>
5 5
 #include <DNSServer.h>
6 6
 #include <ESP8266WebServer.h>
7
-#include <WiFiManager.h>       // WiFiManager
8
-#include <ESP8266Influxdb.h>   // https://github.com/hwwong/ESP8266Influxdb auchecken und den ordner in das arduino\library verzeichnis kopieren
9
-#include <Adafruit_Sensor.h>   // Package Adafruit Unified Sensor
10
-#include <Adafruit_APDS9960.h> // Adafruit APDS9960 - https://www.makerfabs.com/index.php?route=product/product&product_id=281
11
-#include <Adafruit_BME280.h>   // BME280 - https://www.roboter-bausatz.de/1704/bmp280-barometer-luftdrucksensor?gclid=EAIaIQobChMIlpumj8Hp2wIVFWYbCh01PgmFEAQYAyABEgIwBvD_BwE
12 7
 
8
+// External libs
9
+// We decided to put these libs inside the project folder, because we have a lot
10
+// of issues with finding the right libs (some libs need a specific version, some
11
+// libs have no unique name, some libs are broken, ...). This is not a fine
12
+// software engineering practice, but it was too much of a hassle to get this right,
13
+// so we decided to put everything in version control. This way, we also can control
14
+// the version of the lib, which makes the build process of this project more
15
+// robust to changes.
16
+// IMPORTANT: You need to set the sketch location of your Arduino IDE to the firmware
17
+// folder in order to use the libs. (File -> Preferences -> Sketchbook Location)
18
+#include <WiFiClient.h>          // WiFiClient
19
+#include <WiFiManager.h>         // WiFiManager
20
+#include <ESP8266Influxdb.h>     // https://github.com/hwwong/ESP8266Influxdb auchecken und den ordner in das arduino\library verzeichnis kopieren
21
+#include <Adafruit_Sensor.h>     // Package Adafruit Unified Sensor
22
+#include <Adafruit_APDS9960.h>   // Adafruit APDS9960 - https://www.makerfabs.com/index.php?route=product/product&product_id=281
23
+#include <Adafruit_BME280.h>     // BME280 - https://www.roboter-bausatz.de/1704/bmp280-barometer-luftdrucksensor?gclid=EAIaIQobChMIlpumj8Hp2wIVFWYbCh01PgmFEAQYAyABEgIwBvD_BwE
24
+
25
+// Project includes
13 26
 #include "config.h"
27
+
28
+// IMPORTANT: If you compile the sourcecode and it can't find this file, this is
29
+// indended :) You need to create a config_user.h with your own settings. Use the
30
+// config_user.h.example as a starting point.
14 31
 #include "config_user.h"
15 32
 
16 33
 //*************************************************************************//
@@ -246,4 +263,4 @@ void _loop() {
246 263
 
247 264
 }
248 265
 
249
-//*************************************************************************//
266
+//*************************************************************************//

+ 0
- 1
firmware/influxdb.ino View File

@@ -1,4 +1,3 @@
1
-
2 1
 void pushToInfluxDB(String device, float sensorValues[]) {
3 2
 
4 3
   uint8_t tries = 0;

+ 577
- 0
firmware/libraries/Adafruit_APDS9960_Library/Adafruit_APDS9960.cpp View File

@@ -0,0 +1,577 @@
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
+/**************************************************************************/
36
+
37
+#ifdef __AVR
38
+  #include <avr/pgmspace.h>
39
+#elif defined(ESP8266)
40
+  #include <pgmspace.h>
41
+#endif
42
+#include <stdlib.h>
43
+#include <math.h>
44
+
45
+#include "Adafruit_APDS9960.h"
46
+
47
+/*========================================================================*/
48
+/*                          PRIVATE FUNCTIONS                             */
49
+/*========================================================================*/
50
+
51
+/**************************************************************************/
52
+/*!
53
+    @brief  Implements missing powf function
54
+*/
55
+/**************************************************************************/
56
+float powf(const float x, const float y)
57
+{
58
+  return (float)(pow((double)x, (double)y));
59
+}
60
+
61
+/**************************************************************************/
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
+{
69
+  _enable.PON = en;
70
+  this->write8(APDS9960_ENABLE, _enable.get());
71
+}
72
+
73
+/*========================================================================*/
74
+/*                           PUBLIC FUNCTIONS                             */
75
+/*========================================================================*/
76
+
77
+/**************************************************************************/
78
+/*!
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
+{
85
+  _i2c_init();
86
+  _i2caddr = addr;
87
+   
88
+  /* Make sure we're actually connected */
89
+  uint8_t x = read8(APDS9960_ID);
90
+  if (x != 0xAB)
91
+  {
92
+    return false;
93
+  }
94
+
95
+  /* Set default integration time and gain */
96
+  setADCIntegrationTime(iTimeMS);
97
+  setADCGain(aGain);
98
+
99
+  // disable everything to start
100
+  enableGesture(false);
101
+  enableProximity(false);
102
+  enableColor(false);
103
+  
104
+  disableColorInterrupt();
105
+  disableProximityInterrupt();
106
+  clearInterrupt();
107
+
108
+  /* Note: by default, the device is in power down mode on bootup */
109
+  enable(false);
110
+  delay(10);
111
+  enable(true);
112
+  delay(10);
113
+  
114
+  //default to all gesture dimensions
115
+  setGestureDimensions(APDS9960_DIMENSIONS_ALL);
116
+  setGestureFIFOThreshold(APDS9960_GFIFO_4);
117
+  setGestureGain(APDS9960_GGAIN_4);
118
+  setGestureProximityThreshold(50);
119
+  resetCounts();
120
+  
121
+  _gpulse.GPLEN = APDS9960_GPULSE_32US;
122
+  _gpulse.GPULSE = 9; //10 pulses
123
+  this->write8(APDS9960_GPULSE, _gpulse.get());
124
+  
125
+  return true;
126
+}
127
+  
128
+/**************************************************************************/
129
+/*!
130
+    Sets the integration time for the ADC of the APDS9960, in millis
131
+*/
132
+/**************************************************************************/
133
+void Adafruit_APDS9960::setADCIntegrationTime(uint16_t iTimeMS)
134
+{
135
+  float temp;
136
+
137
+  // convert ms into 2.78ms increments
138
+  temp = iTimeMS;
139
+  temp /= 2.78;
140
+  temp = 256 - temp;
141
+  if (temp > 255) temp = 255;
142
+  if (temp < 0)   temp = 0;
143
+  
144
+  /* Update the timing register */
145
+  write8(APDS9960_ATIME, (uint8_t)temp);
146
+}
147
+
148
+float Adafruit_APDS9960::getADCIntegrationTime(void)
149
+{
150
+  float temp;
151
+
152
+  temp = read8(APDS9960_ATIME);
153
+
154
+  // convert to units of 2.78 ms
155
+  temp = 256 - temp;
156
+  temp *= 2.78;
157
+  return temp;
158
+}
159
+
160
+/**************************************************************************/
161
+/*!
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
+  _control.AGAIN = aGain;
168
+
169
+  /* Update the timing register */
170
+  write8(APDS9960_CONTROL, _control.get());
171
+}
172
+
173
+apds9960AGain_t Adafruit_APDS9960::getADCGain(void)
174
+{
175
+  return (apds9960AGain_t) ( read8(APDS9960_CONTROL) & 0x03 );
176
+}
177
+
178
+
179
+/**************************************************************************/
180
+/*!
181
+    Adjusts the Proximity gain on the APDS9960
182
+*/
183
+/**************************************************************************/
184
+void Adafruit_APDS9960::setProxGain(apds9960PGain_t pGain)
185
+{
186
+  _control.PGAIN = pGain;
187
+
188
+  /* Update the timing register */
189
+  write8(APDS9960_CONTROL, _control.get());
190
+}
191
+
192
+apds9960PGain_t Adafruit_APDS9960::getProxGain(void)
193
+{
194
+  return (apds9960PGain_t) ( read8(APDS9960_CONTROL) & 0x0C );
195
+}
196
+
197
+void Adafruit_APDS9960::setProxPulse(apds9960PPulseLen_t pLen, uint8_t pulses) {
198
+  if (pulses < 1)  pulses = 1;
199
+  if (pulses > 64) pulses = 64;
200
+  pulses--;
201
+  
202
+  _ppulse.PPLEN = pLen;
203
+  _ppulse.PPULSE = pulses;
204
+  
205
+  write8(APDS9960_PPULSE, _ppulse.get());
206
+} 
207
+
208
+/**************************************************************************/
209
+/*!
210
+    Enable proximity readings on APDS9960
211
+*/
212
+/**************************************************************************/
213
+void Adafruit_APDS9960::enableProximity(boolean en)
214
+{
215
+  _enable.PEN = en;
216
+
217
+  write8(APDS9960_ENABLE, _enable.get());
218
+}
219
+
220
+void Adafruit_APDS9960::enableProximityInterrupt() {
221
+	_enable.PIEN = 1;
222
+	write8(APDS9960_ENABLE, _enable.get());
223
+	clearInterrupt();
224
+}
225
+
226
+void Adafruit_APDS9960::disableProximityInterrupt() {
227
+	_enable.PIEN = 0;
228
+	write8(APDS9960_ENABLE, _enable.get());
229
+}
230
+
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());
238
+}
239
+
240
+bool Adafruit_APDS9960::getProximityInterrupt()
241
+{
242
+	_status.set(this->read8(APDS9960_STATUS));
243
+	return _status.PINT;
244
+};
245
+
246
+/**************************************************************************/
247
+/*!
248
+    Read proximity data
249
+*/
250
+/**************************************************************************/
251
+uint8_t Adafruit_APDS9960::readProximity(void)
252
+{
253
+  return read8(APDS9960_PDATA);
254
+}
255
+
256
+
257
+bool Adafruit_APDS9960::gestureValid()
258
+{
259
+	_gstatus.set(this->read8(APDS9960_GSTATUS));
260
+	return _gstatus.GVALID;
261
+}
262
+
263
+void Adafruit_APDS9960::setGestureDimensions(uint8_t dims)
264
+{
265
+	_gconf3.GDIMS = dims;
266
+	this->write8(APDS9960_GCONF3, _gconf3.get());
267
+}
268
+
269
+void Adafruit_APDS9960::setGestureFIFOThreshold(uint8_t thresh)
270
+{
271
+	_gconf1.GFIFOTH = thresh;
272
+	this->write8(APDS9960_GCONF1, _gconf1.get());
273
+}
274
+
275
+void Adafruit_APDS9960::setGestureGain(uint8_t gain)
276
+{
277
+	_gconf2.GGAIN = gain;
278
+	this->write8(APDS9960_GCONF2, _gconf2.get());
279
+}
280
+
281
+void Adafruit_APDS9960::setGestureProximityThreshold(uint8_t thresh)
282
+{
283
+	this->write8(APDS9960_GPENTH, thresh);
284
+}
285
+
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);
292
+}
293
+
294
+/**************************************************************************/
295
+/*!
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());
304
+  }
305
+  _enable.GEN = en;
306
+  write8(APDS9960_ENABLE, _enable.get());
307
+  resetCounts();
308
+}
309
+
310
+void Adafruit_APDS9960::resetCounts()
311
+{
312
+  gestCnt = 0;
313
+  UCount = 0;
314
+  DCount = 0;
315
+  LCount = 0;
316
+  RCount = 0;
317
+}
318
+
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)
337
+      up_down_diff += (int)buf[0] - (int)buf[1];
338
+
339
+    if(abs((int)buf[2] - (int)buf[3]) > 13)
340
+      left_right_diff += (int)buf[2] - (int)buf[3];
341
+
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) {
387
+  // set BOOST
388
+  _config2.LED_BOOST = boost;
389
+  write8(APDS9960_CONFIG2, _config2.get());
390
+  
391
+  _control.LDRIVE = drive;
392
+  write8(APDS9960_CONTROL, _control.get());
393
+}
394
+
395
+/**************************************************************************/
396
+/*!
397
+    Enable proximity readings on APDS9960
398
+*/
399
+/**************************************************************************/
400
+void Adafruit_APDS9960::enableColor(boolean en)
401
+{
402
+  _enable.AEN = en;
403
+  write8(APDS9960_ENABLE, _enable.get());
404
+}
405
+
406
+bool Adafruit_APDS9960::colorDataReady()
407
+{
408
+	_status.set(this->read8(APDS9960_STATUS));
409
+	return _status.AVALID;
410
+}
411
+
412
+/**************************************************************************/
413
+/*!
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
+  
420
+  *c = read16R(APDS9960_CDATAL);
421
+  *r = read16R(APDS9960_RDATAL);
422
+  *g = read16R(APDS9960_GDATAL);
423
+  *b = read16R(APDS9960_BDATAL);
424
+
425
+}
426
+
427
+/**************************************************************************/
428
+/*!
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            */
438
+  float cct;
439
+
440
+  /* 1. Map RGB values to their XYZ counterparts.    */
441
+  /* Based on 6500K fluorescent, 3000K fluorescent   */
442
+  /* and 60W incandescent values for a wide range.   */
443
+  /* Note: Y = Illuminance or lux                    */
444
+  X = (-0.14282F * r) + (1.54924F * g) + (-0.95641F * b);
445
+  Y = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
446
+  Z = (-0.68202F * r) + (0.77073F * g) + ( 0.56332F * b);
447
+
448
+  /* 2. Calculate the chromaticity co-ordinates      */
449
+  xc = (X) / (X + Y + Z);
450
+  yc = (Y) / (X + Y + Z);
451
+
452
+  /* 3. Use McCamy's formula to determine the CCT    */
453
+  n = (xc - 0.3320F) / (0.1858F - yc);
454
+
455
+  /* Calculate the final CCT */
456
+  cct = (449.0F * powf(n, 3)) + (3525.0F * powf(n, 2)) + (6823.3F * n) + 5520.33F;
457
+
458
+  /* Return the results in degrees Kelvin */
459
+  return (uint16_t)cct;
460
+}
461
+
462
+/**************************************************************************/
463
+/*!
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
+{
470
+  float illuminance;
471
+
472
+  /* This only uses RGB ... how can we integrate clear or calculate lux */
473
+  /* based exclusively on clear since this might be more reliable?      */
474
+  illuminance = (-0.32466F * r) + (1.57837F * g) + (-0.73191F * b);
475
+
476
+  return (uint16_t)illuminance;
477
+}
478
+
479
+void Adafruit_APDS9960::enableColorInterrupt() {
480
+  _enable.AIEN = 1;
481
+  write8(APDS9960_ENABLE, _enable.get());
482
+}
483
+
484
+void Adafruit_APDS9960::disableColorInterrupt() {
485
+	_enable.AIEN = 0;
486
+	write8(APDS9960_ENABLE, _enable.get());
487
+}
488
+
489
+void Adafruit_APDS9960::clearInterrupt(void) {
490
+  this->write(APDS9960_AICLEAR, NULL, 0);
491
+}
492
+
493
+
494
+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);
499
+}
500
+
501
+void Adafruit_APDS9960::write8(byte reg, byte value)
502
+{
503
+	this->write(reg, &value, 1);
504
+}
505
+
506
+uint8_t Adafruit_APDS9960::read8(byte reg)
507
+{
508
+	uint8_t ret;
509
+	this->read(reg, &ret, 1);
510
+	
511
+	return ret;
512
+}
513
+
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];
520
+}
521
+
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];
528
+}
529
+
530
+uint16_t Adafruit_APDS9960::read16R(uint8_t reg)
531
+{
532
+  uint8_t ret[2];
533
+  this->read(reg, ret, 2);
534
+  
535
+  return (ret[1] << 8) | ret[0];
536
+}
537
+
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();
577
+}

+ 558
- 0
firmware/libraries/Adafruit_APDS9960_Library/Adafruit_APDS9960.h View File

@@ -0,0 +1,558 @@
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
+/**************************************************************************/
36
+#ifndef _APDS9960_H_
37
+#define _APDS9960_H_
38
+
39
+#include <Arduino.h>
40
+#include <Wire.h>
41
+
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,	
165
+};
166
+
167
+enum
168
+{
169
+	APDS9960_GFIFO_1	= 0x00,
170
+	APDS9960_GFIFO_4	= 0x01,
171
+	APDS9960_GFIFO_8	= 0x02,
172
+	APDS9960_GFIFO_16	= 0x03,
173
+};
174
+
175
+enum
176
+{
177
+	APDS9960_GGAIN_1	= 0x00,
178
+	APDS9960_GGAIN_2	= 0x01,
179
+	APDS9960_GGAIN_4	= 0x02,
180
+	APDS9960_GGAIN_8	= 0x03,
181
+};
182
+
183
+enum
184
+{
185
+	APDS9960_GPULSE_4US		= 0x00,
186
+	APDS9960_GPULSE_8US		= 0x01,
187
+	APDS9960_GPULSE_16US	= 0x02,
188
+	APDS9960_GPULSE_32US	= 0x03,
189
+};
190
+
191
+#define APDS9960_TIME_MULT 2.78 //millisec
192
+
193
+#define APDS9960_UP 0x01
194
+#define APDS9960_DOWN 0x02
195
+#define APDS9960_LEFT 0x03
196
+#define APDS9960_RIGHT 0x04
197
+
198
+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);
209
+
210
+  // 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();
220
+
221
+  // 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();
231
+
232
+  // 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);
236
+  uint16_t calculateColorTemperature(uint16_t r, uint16_t g, uint16_t b);
237
+  uint16_t calculateLux(uint16_t r, uint16_t g, uint16_t b);
238
+  void enableColorInterrupt();
239
+  void disableColorInterrupt();
240
+  void clearInterrupt(void);
241
+  void setIntLimits(uint16_t l, uint16_t h);
242
+
243
+  // turn on/off elements
244
+  void     enable(boolean en = true);
245
+
246
+
247
+ private:
248
+  uint8_t _i2caddr;
249
+ 
250
+  uint32_t read32(uint8_t reg);
251
+  uint16_t read16(uint8_t reg);
252
+  uint16_t read16R(uint8_t reg);
253
+  
254
+  void      write8(byte reg, byte value);
255
+  uint8_t   read8(byte reg);
256
+  
257
+  uint8_t gestCnt;
258
+  
259
+  uint8_t UCount;
260
+  uint8_t DCount;
261
+  
262
+  uint8_t LCount;
263
+  uint8_t RCount;
264
+  
265
+  uint8_t read(uint8_t reg, uint8_t *buf, uint8_t num);
266
+  void write(uint8_t reg, uint8_t *buf, uint8_t num);
267
+  void _i2c_init();
268
+  
269
+  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
+  
556
+};
557
+
558
+#endif

+ 59
- 0
firmware/libraries/Adafruit_APDS9960_Library/examples/color_sensor/color_sensor.ino View File

@@ -0,0 +1,59 @@
1
+/***************************************************************************
2
+  This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor
3
+
4
+  This sketch puts the sensor in color mode and reads the RGB and clear values.
5
+
6
+  Designed specifically to work with the Adafruit APDS9960 breakout
7
+  ----> http://www.adafruit.com/products/3595
8
+
9
+  These sensors use I2C to communicate. The device's I2C address is 0x39
10
+
11
+  Adafruit invests time and resources providing this open source code,
12
+  please support Adafruit andopen-source hardware by purchasing products
13
+  from Adafruit!
14
+
15
+  Written by Dean Miller for Adafruit Industries.
16
+  BSD license, all text above must be included in any redistribution
17
+ ***************************************************************************/
18
+
19
+#include "Adafruit_APDS9960.h"
20
+Adafruit_APDS9960 apds;
21
+
22
+void setup() {
23
+  Serial.begin(115200);
24
+
25
+  if(!apds.begin()){
26
+    Serial.println("failed to initialize device! Please check your wiring.");
27
+  }
28
+  else Serial.println("Device initialized!");
29
+
30
+  //enable color sensign mode
31
+  apds.enableColor(true);
32
+}
33
+
34
+void loop() {
35
+  //create some variables to store the color data in
36
+  uint16_t r, g, b, c;
37
+  
38
+  //wait for color data to be ready
39
+  while(!apds.colorDataReady()){
40
+    delay(5);
41
+  }
42
+
43
+  //get the data and print the different channels
44
+  apds.getColorData(&r, &g, &b, &c);
45
+  Serial.print("red: ");
46
+  Serial.print(r);
47
+  
48
+  Serial.print(" green: ");
49
+  Serial.print(g);
50
+  
51
+  Serial.print(" blue: ");
52
+  Serial.print(b);
53
+  
54
+  Serial.print(" clear: ");
55
+  Serial.println(c);
56
+  Serial.println();
57
+  
58
+  delay(500);
59
+}

+ 47
- 0
firmware/libraries/Adafruit_APDS9960_Library/examples/gesture_sensor/gesture_sensor.ino View File

@@ -0,0 +1,47 @@
1
+/***************************************************************************
2
+  This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor
3
+
4
+  This sketch puts the sensor in gesture mode and decodes gestures.
5
+  To use this, first put your hand close to the sensor to enable gesture mode.
6
+  Then move your hand about 6" from the sensor in the up -> down, down -> up, 
7
+  left -> right, or right -> left direction.
8
+
9
+  Designed specifically to work with the Adafruit APDS9960 breakout
10
+  ----> http://www.adafruit.com/products/3595
11
+
12
+  These sensors use I2C to communicate. The device's I2C address is 0x39
13
+
14
+  Adafruit invests time and resources providing this open source code,
15
+  please support Adafruit andopen-source hardware by purchasing products
16
+  from Adafruit!
17
+
18
+  Written by Dean Miller for Adafruit Industries.
19
+  BSD license, all text above must be included in any redistribution
20
+ ***************************************************************************/
21
+
22
+#include "Adafruit_APDS9960.h"
23
+Adafruit_APDS9960 apds;
24
+
25
+// the setup function runs once when you press reset or power the board
26
+void setup() {
27
+  Serial.begin(115200);
28
+  
29
+  if(!apds.begin()){
30
+    Serial.println("failed to initialize device! Please check your wiring.");
31
+  }
32
+  else Serial.println("Device initialized!");
33
+
34
+  //gesture mode will be entered once proximity mode senses something close
35
+  apds.enableProximity(true);
36
+  apds.enableGesture(true);
37
+}
38
+
39
+// the loop function runs over and over again forever
40
+void loop() {
41
+  //read a gesture from the device
42
+    uint8_t gesture = apds.readGesture();
43
+    if(gesture == APDS9960_DOWN) Serial.println("v");
44
+    if(gesture == APDS9960_UP) Serial.println("^");
45
+    if(gesture == APDS9960_LEFT) Serial.println("<");
46
+    if(gesture == APDS9960_RIGHT) Serial.println(">");
47
+}

+ 56
- 0
firmware/libraries/Adafruit_APDS9960_Library/examples/proximity_sensor/proximity_sensor.ino View File

@@ -0,0 +1,56 @@
1
+/***************************************************************************
2
+  This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor
3
+
4
+  This sketch puts the sensor in proximity mode and enables the interrupt
5
+  to fire when proximity goes over a set value
6
+
7
+  Designed specifically to work with the Adafruit APDS9960 breakout
8
+  ----> http://www.adafruit.com/products/3595
9
+
10
+  These sensors use I2C to communicate. The device's I2C address is 0x39
11
+
12
+  Adafruit invests time and resources providing this open source code,
13
+  please support Adafruit andopen-source hardware by purchasing products
14
+  from Adafruit!
15
+
16
+  Written by Dean Miller for Adafruit Industries.
17
+  BSD license, all text above must be included in any redistribution
18
+ ***************************************************************************/
19
+
20
+#include "Adafruit_APDS9960.h"
21
+
22
+//the pin that the interrupt is attached to
23
+#define INT_PIN 3
24
+
25
+//create the APDS9960 object
26
+Adafruit_APDS9960 apds;
27
+
28
+void setup() {
29
+  Serial.begin(115200);
30
+  pinMode(INT_PIN, INPUT_PULLUP);
31
+
32
+  if(!apds.begin()){
33
+    Serial.println("failed to initialize device! Please check your wiring.");
34
+  }
35
+  else Serial.println("Device initialized!");
36
+
37
+  //enable proximity mode
38
+  apds.enableProximity(true);
39
+
40
+  //set the interrupt threshold to fire when proximity reading goes above 175
41
+  apds.setProximityInterruptThreshold(0, 175);
42
+
43
+  //enable the proximity interrupt
44
+  apds.enableProximityInterrupt();
45
+}
46
+
47
+void loop() {
48
+
49
+  //print the proximity reading when the interrupt pin goes low
50
+  if(!digitalRead(INT_PIN)){
51
+    Serial.println(apds.readProximity());
52
+
53
+    //clear the interrupt
54
+    apds.clearInterrupt();
55
+  }
56
+}

+ 9
- 0
firmware/libraries/Adafruit_APDS9960_Library/library.properties View File

@@ -0,0 +1,9 @@
1
+name=Adafruit APDS9960 Library
2
+version=1.0.5
3
+author=Adafruit
4
+maintainer=Adafruit <info@adafruit.com>
5
+sentence=This is a library for the Adafruit APDS9960 gesture/proximity/color/light sensor.
6
+paragraph=This is a library for the Adafruit APDS9960 gesture/proximity/color/light sensor.
7
+category=Sensors
8
+url=https://github.com/adafruit/Adafruit_APDS9960
9
+architectures=*

+ 530
- 0
firmware/libraries/Adafruit_BME280_Library/Adafruit_BME280.cpp View File

@@ -0,0 +1,530 @@
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!
13
+
14
+  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15
+  BSD license, all text above must be included in any redistribution
16
+ ***************************************************************************/
17
+#include "Arduino.h"
18
+#include <Wire.h>
19
+#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
+{ }
28
+
29
+Adafruit_BME280::Adafruit_BME280(int8_t cspin)
30
+    : _cs(cspin), _mosi(-1), _miso(-1), _sck(-1)
31
+{ }
32
+
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
+{ }
36
+
37
+
38
+/**************************************************************************/
39
+/*!
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();
48
+}
49
+
50
+bool Adafruit_BME280::begin(uint8_t addr)
51
+{
52
+	_i2caddr = addr;
53
+	_wire = &Wire;
54
+	return init();
55
+}
56
+
57
+bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire)
58
+{
59
+    _i2caddr = addr;
60
+	_wire = theWire;
61
+	return init();
62
+}
63
+
64
+bool Adafruit_BME280::begin(void)
65
+{
66
+    _i2caddr = BME280_ADDRESS;
67
+	_wire = &Wire;
68
+	return init();
69
+}
70
+
71
+bool Adafruit_BME280::init()
72
+{
73
+    // init I2C or SPI sensor interface
74
+    if (_cs == -1) {
75
+        // I2C
76
+        _wire -> begin();
77
+    } 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
+        }
89
+    }
90
+
91
+    // check if sensor, i.e. the chip ID is correct
92
+    if (read8(BME280_REGISTER_CHIPID) != 0x60)
93
+        return false;
94
+
95
+    // reset the device using soft-reset
96
+    // this makes sure the IIR is off, etc.
97
+    write8(BME280_REGISTER_SOFTRESET, 0xB6);
98
+
99
+    // wait for chip to wake up.
100
+    delay(300);
101
+
102
+    // if chip is still reading calibration, delay
103
+    while (isReadingCalibration())
104
+          delay(100);
105
+
106
+    readCoefficients(); // read trimming parameters, see DS 4.2.2
107
+
108
+    setSampling(); // use defaults
109
+
110
+    delay(100);
111
+
112
+    return true;
113
+}
114
+
115
+/**************************************************************************/
116
+/*!
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());
146
+}
147
+
148
+
149
+/**************************************************************************/
150
+/*!
151
+    @brief  Encapsulate hardware and software SPI transfer into one function
152
+*/
153
+/**************************************************************************/
154
+uint8_t Adafruit_BME280::spixfer(uint8_t x) {
155
+    // hardware SPI
156
+    if (_sck == -1)
157
+        return SPI.transfer(x);
158
+
159
+    // software SPI
160
+    uint8_t reply = 0;
161
+    for (int i=7; i>=0; i--) {
162
+        reply <<= 1;
163
+        digitalWrite(_sck, LOW);
164
+        digitalWrite(_mosi, x & (1<<i));
165
+        digitalWrite(_sck, HIGH);
166
+        if (digitalRead(_miso))
167
+            reply |= 1;
168
+        }
169
+    return reply;
170
+}
171
+
172
+
173
+/**************************************************************************/
174
+/*!
175
+    @brief  Writes an 8 bit value over I2C or SPI
176
+*/
177
+/**************************************************************************/
178
+void Adafruit_BME280::write8(byte reg, byte value) {
179
+    if (_cs == -1) {
180
+        _wire -> beginTransmission((uint8_t)_i2caddr);
181
+        _wire -> write((uint8_t)reg);
182
+        _wire -> write((uint8_t)value);
183
+        _wire -> endTransmission();
184
+    } else {
185
+        if (_sck == -1)
186
+            SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
187
+        digitalWrite(_cs, LOW);
188
+        spixfer(reg & ~0x80); // write, bit 7 low
189
+        spixfer(value);
190
+        digitalWrite(_cs, HIGH);
191
+    if (_sck == -1)
192
+        SPI.endTransaction(); // release the SPI bus
193
+    }
194
+}
195
+
196
+
197
+/**************************************************************************/
198
+/*!
199
+    @brief  Reads an 8 bit value over I2C or SPI
200
+*/
201
+/**************************************************************************/
202
+uint8_t Adafruit_BME280::read8(byte reg) {
203
+    uint8_t value;
204
+    
205
+    if (_cs == -1) {
206
+        _wire -> beginTransmission((uint8_t)_i2caddr);
207
+        _wire -> write((uint8_t)reg);
208
+        _wire -> endTransmission();
209
+        _wire -> requestFrom((uint8_t)_i2caddr, (byte)1);
210
+        value = _wire -> read();
211
+    } else {
212
+        if (_sck == -1)
213
+            SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
214
+        digitalWrite(_cs, LOW);
215
+        spixfer(reg | 0x80); // read, bit 7 high
216
+        value = spixfer(0);
217
+        digitalWrite(_cs, HIGH);
218
+        if (_sck == -1)
219
+            SPI.endTransaction(); // release the SPI bus
220
+    }
221
+    return value;
222
+}
223
+
224
+
225
+/**************************************************************************/
226
+/*!
227
+    @brief  Reads a 16 bit value over I2C or SPI
228
+*/
229
+/**************************************************************************/
230
+uint16_t Adafruit_BME280::read16(byte reg)
231
+{
232
+    uint16_t value;
233
+
234
+    if (_cs == -1) {
235
+        _wire -> beginTransmission((uint8_t)_i2caddr);
236
+        _wire -> write((uint8_t)reg);
237
+        _wire -> endTransmission();
238
+        _wire -> requestFrom((uint8_t)_i2caddr, (byte)2);
239
+        value = (_wire -> read() << 8) | _wire -> read();
240
+    } else {
241
+        if (_sck == -1)
242
+            SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
243
+        digitalWrite(_cs, LOW);
244
+        spixfer(reg | 0x80); // read, bit 7 high
245
+        value = (spixfer(0) << 8) | spixfer(0);
246
+        digitalWrite(_cs, HIGH);
247
+        if (_sck == -1)
248
+            SPI.endTransaction(); // release the SPI bus
249
+    }
250
+
251
+    return value;
252
+}
253
+
254
+
255
+/**************************************************************************/
256
+/*!
257
+    
258
+*/
259
+/**************************************************************************/
260
+uint16_t Adafruit_BME280::read16_LE(byte reg) {
261
+    uint16_t temp = read16(reg);
262
+    return (temp >> 8) | (temp << 8);
263
+}
264
+
265
+
266
+/**************************************************************************/
267
+/*!
268
+    @brief  Reads a signed 16 bit value over I2C or SPI
269
+*/
270
+/**************************************************************************/
271
+int16_t Adafruit_BME280::readS16(byte reg)
272
+{
273
+    return (int16_t)read16(reg);
274
+}
275
+
276
+
277
+/**************************************************************************/
278
+/*!
279
+   
280
+*/
281
+/**************************************************************************/
282
+int16_t Adafruit_BME280::readS16_LE(byte reg)
283
+{
284
+    return (int16_t)read16_LE(reg);
285
+}
286
+
287
+
288
+/**************************************************************************/
289
+/*!
290
+    @brief  Reads a 24 bit value over I2C
291
+*/
292
+/**************************************************************************/
293
+uint32_t Adafruit_BME280::read24(byte reg)
294
+{
295
+    uint32_t value;
296
+
297
+    if (_cs == -1) {
298
+        _wire -> beginTransmission((uint8_t)_i2caddr);
299
+        _wire -> write((uint8_t)reg);
300
+        _wire -> endTransmission();
301
+        _wire -> requestFrom((uint8_t)_i2caddr, (byte)3);
302
+
303
+        value = _wire -> read();
304
+        value <<= 8;
305
+        value |= _wire -> read();
306
+        value <<= 8;
307
+        value |= _wire -> read();
308
+    } else {
309
+        if (_sck == -1)
310
+            SPI.beginTransaction(SPISettings(500000, MSBFIRST, SPI_MODE0));
311
+        digitalWrite(_cs, LOW);
312
+        spixfer(reg | 0x80); // read, bit 7 high
313
+
314
+        value = spixfer(0);
315
+        value <<= 8;
316
+        value |= spixfer(0);
317
+        value <<= 8;
318
+        value |= spixfer(0);
319
+
320
+        digitalWrite(_cs, HIGH);
321
+        if (_sck == -1)
322
+            SPI.endTransaction(); // release the SPI bus
323
+    }
324
+
325
+    return value;
326
+}
327
+
328
+
329
+/**************************************************************************/
330
+/*!
331
+    @brief  Take a new measurement (only possible in forced mode)
332
+*/
333
+/**************************************************************************/
334
+void Adafruit_BME280::takeForcedMeasurement()
335
+{   
336
+    // If we are in forced mode, the BME sensor goes back to sleep after each
337
+    // measurement and we need to set it to forced mode once at this point, so
338
+    // it will take the next measurement and then return to sleep again.
339
+    // In normal mode simply does new measurements periodically.
340
+    if (_measReg.mode == MODE_FORCED) {
341
+        // set to forced mode, i.e. "take next measurement"
342
+        write8(BME280_REGISTER_CONTROL, _measReg.get());
343
+        // wait until measurement has been completed, otherwise we would read
344
+        // the values from the last measurement
345
+        while (read8(BME280_REGISTER_STATUS) & 0x08)
346
+		delay(1);
347
+    }
348
+}
349
+
350
+
351
+/**************************************************************************/
352
+/*!
353
+    @brief  Reads the factory-set coefficients
354
+*/
355
+/**************************************************************************/
356
+void Adafruit_BME280::readCoefficients(void)
357
+{
358
+    _bme280_calib.dig_T1 = read16_LE(BME280_REGISTER_DIG_T1);
359
+    _bme280_calib.dig_T2 = readS16_LE(BME280_REGISTER_DIG_T2);
360
+    _bme280_calib.dig_T3 = readS16_LE(BME280_REGISTER_DIG_T3);
361
+
362
+    _bme280_calib.dig_P1 = read16_LE(BME280_REGISTER_DIG_P1);
363
+    _bme280_calib.dig_P2 = readS16_LE(BME280_REGISTER_DIG_P2);
364
+    _bme280_calib.dig_P3 = readS16_LE(BME280_REGISTER_DIG_P3);
365
+    _bme280_calib.dig_P4 = readS16_LE(BME280_REGISTER_DIG_P4);
366
+    _bme280_calib.dig_P5 = readS16_LE(BME280_REGISTER_DIG_P5);
367
+    _bme280_calib.dig_P6 = readS16_LE(BME280_REGISTER_DIG_P6);
368
+    _bme280_calib.dig_P7 = readS16_LE(BME280_REGISTER_DIG_P7);
369
+    _bme280_calib.dig_P8 = readS16_LE(BME280_REGISTER_DIG_P8);
370
+    _bme280_calib.dig_P9 = readS16_LE(BME280_REGISTER_DIG_P9);
371
+
372
+    _bme280_calib.dig_H1 = read8(BME280_REGISTER_DIG_H1);
373
+    _bme280_calib.dig_H2 = readS16_LE(BME280_REGISTER_DIG_H2);
374
+    _bme280_calib.dig_H3 = read8(BME280_REGISTER_DIG_H3);
375
+    _bme280_calib.dig_H4 = (read8(BME280_REGISTER_DIG_H4) << 4) | (read8(BME280_REGISTER_DIG_H4+1) & 0xF);
376
+    _bme280_calib.dig_H5 = (read8(BME280_REGISTER_DIG_H5+1) << 4) | (read8(BME280_REGISTER_DIG_H5) >> 4);
377
+    _bme280_calib.dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6);
378
+}
379
+
380
+/**************************************************************************/
381
+/*!
382
+    @brief return true if chip is busy reading cal data
383
+*/
384
+/**************************************************************************/
385
+bool Adafruit_BME280::isReadingCalibration(void)
386
+{
387
+  uint8_t const rStatus = read8(BME280_REGISTER_STATUS);
388
+
389
+  return (rStatus & (1 << 0)) != 0;
390
+}
391
+
392
+
393
+/**************************************************************************/
394
+/*!
395
+    @brief  Returns the temperature from the sensor
396
+*/
397
+/**************************************************************************/
398
+float Adafruit_BME280::readTemperature(void)
399
+{
400
+    int32_t var1, var2;
401
+
402
+    int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
403
+    if (adc_T == 0x800000) // value in case temp measurement was disabled
404
+        return NAN;
405
+    adc_T >>= 4;
406
+
407
+    var1 = ((((adc_T>>3) - ((int32_t)_bme280_calib.dig_T1 <<1))) *
408
+            ((int32_t)_bme280_calib.dig_T2)) >> 11;
409
+             
410
+    var2 = (((((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1)) *
411
+              ((adc_T>>4) - ((int32_t)_bme280_calib.dig_T1))) >> 12) *
412
+            ((int32_t)_bme280_calib.dig_T3)) >> 14;
413
+
414
+    t_fine = var1 + var2;
415
+
416
+    float T = (t_fine * 5 + 128) >> 8;
417
+    return T/100;
418
+}
419
+
420
+
421
+/**************************************************************************/
422
+/*!
423
+    @brief  Returns the temperature from the sensor
424
+*/
425
+/**************************************************************************/
426
+float Adafruit_BME280::readPressure(void) {
427
+    int64_t var1, var2, p;
428
+
429
+    readTemperature(); // must be done first to get t_fine
430
+
431
+    int32_t adc_P = read24(BME280_REGISTER_PRESSUREDATA);
432
+    if (adc_P == 0x800000) // value in case pressure measurement was disabled
433
+        return NAN;
434
+    adc_P >>= 4;
435
+
436
+    var1 = ((int64_t)t_fine) - 128000;
437
+    var2 = var1 * var1 * (int64_t)_bme280_calib.dig_P6;
438
+    var2 = var2 + ((var1*(int64_t)_bme280_calib.dig_P5)<<17);
439
+    var2 = var2 + (((int64_t)_bme280_calib.dig_P4)<<35);
440
+    var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3)>>8) +
441
+           ((var1 * (int64_t)_bme280_calib.dig_P2)<<12);
442
+    var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bme280_calib.dig_P1)>>33;
443
+
444
+    if (var1 == 0) {
445
+        return 0; // avoid exception caused by division by zero
446
+    }
447
+    p = 1048576 - adc_P;
448
+    p = (((p<<31) - var2)*3125) / var1;
449
+    var1 = (((int64_t)_bme280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
450
+    var2 = (((int64_t)_bme280_calib.dig_P8) * p) >> 19;
451
+
452
+    p = ((p + var1 + var2) >> 8) + (((int64_t)_bme280_calib.dig_P7)<<4);
453
+    return (float)p/256;
454
+}
455
+
456
+
457
+/**************************************************************************/
458
+/*!
459
+    @brief  Returns the humidity from the sensor
460
+*/
461
+/**************************************************************************/
462
+float Adafruit_BME280::readHumidity(void) {
463
+    readTemperature(); // must be done first to get t_fine
464
+
465
+    int32_t adc_H = read16(BME280_REGISTER_HUMIDDATA);
466
+    if (adc_H == 0x8000) // value in case humidity measurement was disabled
467
+        return NAN;
468
+        
469
+    int32_t v_x1_u32r;
470
+
471
+    v_x1_u32r = (t_fine - ((int32_t)76800));
472
+
473
+    v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bme280_calib.dig_H4) << 20) -
474
+                    (((int32_t)_bme280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
475
+                 (((((((v_x1_u32r * ((int32_t)_bme280_calib.dig_H6)) >> 10) *
476
+                      (((v_x1_u32r * ((int32_t)_bme280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
477
+                    ((int32_t)2097152)) * ((int32_t)_bme280_calib.dig_H2) + 8192) >> 14));
478
+
479
+    v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
480
+                               ((int32_t)_bme280_calib.dig_H1)) >> 4));
481
+
482
+    v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
483
+    v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
484
+    float h = (v_x1_u32r>>12);
485
+    return  h / 1024.0;
486
+}
487
+
488
+
489
+/**************************************************************************/
490
+/*!
491
+    Calculates the altitude (in meters) from the specified atmospheric
492
+    pressure (in hPa), and sea-level pressure (in hPa).
493
+
494
+    @param  seaLevel      Sea-level pressure in hPa
495
+    @param  atmospheric   Atmospheric pressure in hPa
496
+*/
497
+/**************************************************************************/
498
+float Adafruit_BME280::readAltitude(float seaLevel)
499
+{
500
+    // Equation taken from BMP180 datasheet (page 16):
501
+    //  http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
502
+
503
+    // Note that using the equation from wikipedia can give bad results
504
+    // at high altitude. See this thread for more information:
505
+    //  http://forums.adafruit.com/viewtopic.php?f=22&t=58064
506
+
507
+    float atmospheric = readPressure() / 100.0F;
508
+    return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903));
509
+}
510
+
511
+
512
+/**************************************************************************/
513
+/*!
514
+    Calculates the pressure at sea level (in hPa) from the specified altitude 
515
+    (in meters), and atmospheric pressure (in hPa).  
516
+    @param  altitude      Altitude in meters
517
+    @param  atmospheric   Atmospheric pressure in hPa
518
+*/
519
+/**************************************************************************/
520
+float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric)
521
+{
522
+    // Equation taken from BMP180 datasheet (page 17):
523
+    //  http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
524
+
525
+    // Note that using the equation from wikipedia can give bad results
526
+    // at high altitude. See this thread for more information:
527
+    //  http://forums.adafruit.com/viewtopic.php?f=22&t=58064
528
+
529
+    return atmospheric / pow(1.0 - (altitude/44330.0), 5.255);
530
+}

+ 300
- 0
firmware/libraries/Adafruit_BME280_Library/Adafruit_BME280.h View File

@@ -0,0 +1,300 @@
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!
13
+
14
+  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15
+  BSD license, all text above must be included in any redistribution
16
+ ***************************************************************************/
17
+#ifndef __BME280_H__
18
+#define __BME280_H__
19
+
20
+#if (ARDUINO >= 100)
21
+ #include "Arduino.h"
22
+#else
23
+ #include "WProgram.h"
24
+#endif
25
+
26
+#include <Adafruit_Sensor.h>
27
+#include <Wire.h>
28
+
29
+/*=========================================================================
30
+    I2C ADDRESS/BITS
31
+    -----------------------------------------------------------------------*/
32
+    #define BME280_ADDRESS                (0x77)
33
+/*=========================================================================*/
34
+
35
+/*=========================================================================
36
+    REGISTERS
37
+    -----------------------------------------------------------------------*/
38
+    enum
39
+    {
40
+        BME280_REGISTER_DIG_T1              = 0x88,
41
+        BME280_REGISTER_DIG_T2              = 0x8A,
42
+        BME280_REGISTER_DIG_T3              = 0x8C,
43
+
44
+        BME280_REGISTER_DIG_P1              = 0x8E,
45
+        BME280_REGISTER_DIG_P2              = 0x90,
46
+        BME280_REGISTER_DIG_P3              = 0x92,
47
+        BME280_REGISTER_DIG_P4              = 0x94,
48
+        BME280_REGISTER_DIG_P5              = 0x96,
49
+        BME280_REGISTER_DIG_P6              = 0x98,
50
+        BME280_REGISTER_DIG_P7              = 0x9A,
51
+        BME280_REGISTER_DIG_P8              = 0x9C,
52
+        BME280_REGISTER_DIG_P9              = 0x9E,
53
+
54
+        BME280_REGISTER_DIG_H1              = 0xA1,
55
+        BME280_REGISTER_DIG_H2              = 0xE1,
56
+        BME280_REGISTER_DIG_H3              = 0xE3,
57
+        BME280_REGISTER_DIG_H4              = 0xE4,
58
+        BME280_REGISTER_DIG_H5              = 0xE5,
59
+        BME280_REGISTER_DIG_H6              = 0xE7,
60
+
61
+        BME280_REGISTER_CHIPID             = 0xD0,
62
+        BME280_REGISTER_VERSION            = 0xD1,
63
+        BME280_REGISTER_SOFTRESET          = 0xE0,
64
+
65
+        BME280_REGISTER_CAL26              = 0xE1,  // R calibration stored in 0xE1-0xF0
66
+
67
+        BME280_REGISTER_CONTROLHUMID       = 0xF2,
68
+        BME280_REGISTER_STATUS             = 0XF3,
69
+        BME280_REGISTER_CONTROL            = 0xF4,
70
+        BME280_REGISTER_CONFIG             = 0xF5,
71
+        BME280_REGISTER_PRESSUREDATA       = 0xF7,
72
+        BME280_REGISTER_TEMPDATA           = 0xFA,
73
+        BME280_REGISTER_HUMIDDATA          = 0xFD
74
+    };
75
+
76
+/*=========================================================================*/
77
+
78
+/*=========================================================================
79
+    CALIBRATION DATA
80
+    -----------------------------------------------------------------------*/
81
+    typedef struct
82
+    {
83
+        uint16_t dig_T1;
84
+        int16_t  dig_T2;
85
+        int16_t  dig_T3;
86
+
87
+        uint16_t dig_P1;
88
+        int16_t  dig_P2;
89
+        int16_t  dig_P3;
90
+        int16_t  dig_P4;
91
+        int16_t  dig_P5;
92
+        int16_t  dig_P6;
93
+        int16_t  dig_P7;
94
+        int16_t  dig_P8;
95
+        int16_t  dig_P9;
96
+
97
+        uint8_t  dig_H1;
98
+        int16_t  dig_H2;
99
+        uint8_t  dig_H3;
100
+        int16_t  dig_H4;
101
+        int16_t  dig_H5;
102
+        int8_t   dig_H6;
103
+    } bme280_calib_data;
104
+/*=========================================================================*/
105
+
106
+/*
107
+class Adafruit_BME280_Unified : public Adafruit_Sensor
108
+{
109
+  public:
110
+    Adafruit_BME280_Unified(int32_t sensorID = -1);
111
+
112
+    bool  begin(uint8_t addr = BME280_ADDRESS);
113
+    void  getTemperature(float *temp);
114
+    void  getPressure(float *pressure);
115
+    float pressureToAltitude(float seaLevel, float atmospheric, float temp);
116
+    float seaLevelForAltitude(float altitude, float atmospheric, float temp);
117
+    void  getEvent(sensors_event_t*);
118
+    void  getSensor(sensor_t*);
119
+
120
+  private:
121
+    uint8_t   _i2c_addr;
122
+    int32_t   _sensorID;
123
+};
124
+
125
+*/
126
+
127
+class Adafruit_BME280 {
128
+    public:
129
+        enum sensor_sampling {
130
+            SAMPLING_NONE = 0b000,
131
+            SAMPLING_X1   = 0b001,
132
+            SAMPLING_X2   = 0b010,
133
+            SAMPLING_X4   = 0b011,
134
+            SAMPLING_X8   = 0b100,
135
+            SAMPLING_X16  = 0b101
136
+        };
137
+
138
+        enum sensor_mode {
139
+            MODE_SLEEP  = 0b00,
140
+            MODE_FORCED = 0b01,
141
+            MODE_NORMAL = 0b11
142
+        };
143
+
144
+        enum sensor_filter {
145
+            FILTER_OFF = 0b000,
146
+            FILTER_X2  = 0b001,
147
+            FILTER_X4  = 0b010,
148
+            FILTER_X8  = 0b011,
149
+            FILTER_X16 = 0b100
150
+        };
151
+
152
+        // standby durations in ms 
153
+        enum standby_duration {
154
+            STANDBY_MS_0_5  = 0b000,
155
+            STANDBY_MS_10   = 0b110,
156
+            STANDBY_MS_20   = 0b111,
157
+            STANDBY_MS_62_5 = 0b001,
158
+            STANDBY_MS_125  = 0b010,
159
+            STANDBY_MS_250  = 0b011,
160
+            STANDBY_MS_500  = 0b100,
161
+            STANDBY_MS_1000 = 0b101
162
+        };
163
+    
164
+        // constructors
165
+        Adafruit_BME280(void);
166
+        Adafruit_BME280(int8_t cspin);
167
+        Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin, int8_t sckpin);
168
+		
169
+		bool begin(void);
170
+		bool begin(TwoWire *theWire);
171
+		bool begin(uint8_t addr);
172
+        bool begin(uint8_t addr, TwoWire *theWire);
173
+		bool init();
174
+
175
+	void setSampling(sensor_mode mode              = MODE_NORMAL,
176
+			 sensor_sampling tempSampling  = SAMPLING_X16,
177
+			 sensor_sampling pressSampling = SAMPLING_X16,
178
+			 sensor_sampling humSampling   = SAMPLING_X16,
179
+			 sensor_filter filter          = FILTER_OFF,
180
+			 standby_duration duration     = STANDBY_MS_0_5
181
+			 );
182
+                   
183
+        void takeForcedMeasurement();
184
+        float readTemperature(void);
185
+        float readPressure(void);
186
+        float readHumidity(void);
187
+        
188
+        float readAltitude(float seaLevel);
189
+        float seaLevelForAltitude(float altitude, float pressure);
190
+
191
+        
192
+    private:
193
+		TwoWire *_wire;
194
+        void readCoefficients(void);
195
+        bool isReadingCalibration(void);
196
+        uint8_t spixfer(uint8_t x);
197
+
198
+        void      write8(byte reg, byte value);
199
+        uint8_t   read8(byte reg);
200
+        uint16_t  read16(byte reg);
201
+        uint32_t  read24(byte reg);
202
+        int16_t   readS16(byte reg);
203
+        uint16_t  read16_LE(byte reg); // little endian
204
+        int16_t   readS16_LE(byte reg); // little endian
205
+
206
+        uint8_t   _i2caddr;
207
+        int32_t   _sensorID;
208
+        int32_t   t_fine;
209
+
210
+        int8_t _cs, _mosi, _miso, _sck;
211
+
212
+        bme280_calib_data _bme280_calib;
213
+
214
+        // The config register
215
+        struct config {
216
+            // inactive duration (standby time) in normal mode
217
+            // 000 = 0.5 ms
218
+            // 001 = 62.5 ms
219
+            // 010 = 125 ms
220
+            // 011 = 250 ms
221
+            // 100 = 500 ms
222
+            // 101 = 1000 ms
223
+            // 110 = 10 ms
224
+            // 111 = 20 ms
225
+            unsigned int t_sb : 3;
226
+
227
+            // filter settings
228
+            // 000 = filter off
229
+            // 001 = 2x filter
230
+            // 010 = 4x filter
231
+            // 011 = 8x filter
232
+            // 100 and above = 16x filter
233
+            unsigned int filter : 3;
234
+
235
+            // unused - don't set
236
+            unsigned int none : 1;
237
+            unsigned int spi3w_en : 1;
238
+
239
+            unsigned int get() {
240
+                return (t_sb << 5) | (filter << 3) | spi3w_en;
241
+            }
242
+        };
243
+        config _configReg;
244
+
245
+        
246
+        // The ctrl_meas register
247
+        struct ctrl_meas {
248
+            // temperature oversampling
249
+            // 000 = skipped
250
+            // 001 = x1
251
+            // 010 = x2
252
+            // 011 = x4
253
+            // 100 = x8
254
+            // 101 and above = x16
255
+            unsigned int osrs_t : 3;
256
+
257
+            // pressure oversampling
258
+            // 000 = skipped
259
+            // 001 = x1
260
+            // 010 = x2
261
+            // 011 = x4
262
+            // 100 = x8
263
+            // 101 and above = x16
264
+            unsigned int osrs_p : 3;
265
+
266
+            // device mode
267
+            // 00       = sleep
268
+            // 01 or 10 = forced
269
+            // 11       = normal
270
+            unsigned int mode : 2;
271
+
272
+            unsigned int get() {
273
+                return (osrs_t << 5) | (osrs_p << 3) | mode;
274
+            }
275
+        };
276
+        ctrl_meas _measReg;
277
+
278
+        
279
+        // The ctrl_hum register
280
+        struct ctrl_hum {
281
+            // unused - don't set
282
+            unsigned int none : 5;
283
+
284
+            // pressure oversampling
285
+            // 000 = skipped
286
+            // 001 = x1
287
+            // 010 = x2
288
+            // 011 = x4
289
+            // 100 = x8
290
+            // 101 and above = x16
291
+            unsigned int osrs_h : 3;
292
+
293
+            unsigned int get() {
294
+                return (osrs_h);
295
+            }
296
+        };
297
+        ctrl_hum _humReg;
298
+};
299
+
300
+#endif

+ 59
- 0
firmware/libraries/Adafruit_BME280_Library/README.md View File

@@ -0,0 +1,59 @@
1
+This is a library for the Adafruit BME280 Humidity, Barometric Pressure + Temp sensor
2
+
3
+Designed specifically to work with the Adafruit BME280 Breakout 
4
+ * http://www.adafruit.com/products/2652
5
+
6
+These sensors use I2C or SPI to communicate, up to 4 pins are required to interface
7
+
8
+Use of this library also requires [Adafruit_Sensor](https://github.com/adafruit/Adafruit_Sensor)
9
+to be installed on your local system.
10
+
11
+Adafruit invests time and resources providing this open source code, 
12
+please support Adafruit and open-source hardware by purchasing 
13
+products from Adafruit!
14
+
15
+Check out the links above for our tutorials and wiring diagrams 
16
+
17
+Written by Limor Fried/Ladyada for Adafruit Industries.  
18
+BSD license, all text above must be included in any redistribution
19
+
20
+To download. click the DOWNLOAD ZIP button, rename the uncompressed folder Adafruit_BME280. 
21
+Check that the Adafruit_BME280 folder contains Adafruit_BME280.cpp and Adafruit_BME280.h
22
+
23
+Place the Adafruit_BME280 library folder your arduinosketchfolder/libraries/ folder. 
24
+You may need to create the libraries subfolder if its your first library. Restart the IDE.
25
+
26
+We also have a great tutorial on Arduino library installation at:
27
+http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use
28
+<!-- START COMPATIBILITY TABLE -->
29
+
30
+## Compatibility
31
+
32
+MCU                | Tested Works | Doesn't Work | Not Tested  | Notes
33
+------------------ | :----------: | :----------: | :---------: | -----
34
+Atmega328 @ 16MHz  |      X       |             |            | 
35
+Atmega328 @ 12MHz  |      X       |             |            | 
36
+Atmega32u4 @ 16MHz |      X       |             |            | Use SDA/SCL on pins D2 &amp; D3
37
+Atmega32u4 @ 8MHz  |      X       |             |            | Use SDA/SCL on pins D2 &amp; D3
38
+ESP8266            |      X       |             |            | I2C: just works, SPI: SDA/SCL default to pins 4 &amp; 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL)
39
+ESP32              |      X       |             |            | I2C: just works, SPI: SDA/SCL default to pins 4 &amp; 5 but any two pins can be assigned as SDA/SCL using Wire.begin(SDA,SCL) 
40
+Atmega2560 @ 16MHz |      X       |             |            | Use SDA/SCL on pins 20 &amp; 21
41
+ATSAM3X8E          |      X       |             |            | Use SDA/SCL on pins 20 &amp; 21
42
+ATSAM21D           |      X       |             |            | 
43
+ATtiny85 @ 16MHz   |              |     X       |            | 
44
+ATtiny85 @ 8MHz    |              |     X       |            | 
45
+Intel Curie @ 32MHz |             |             |     X      | 
46
+STM32F2            |              |             |     X      | 
47
+
48
+  * ATmega328 @ 16MHz : Arduino UNO, Adafruit Pro Trinket 5V, Adafruit Metro 328, Adafruit Metro Mini
49
+  * ATmega328 @ 12MHz : Adafruit Pro Trinket 3V
50
+  * ATmega32u4 @ 16MHz : Arduino Leonardo, Arduino Micro, Arduino Yun, Teensy 2.0
51
+  * ATmega32u4 @ 8MHz : Adafruit Flora, Bluefruit Micro
52
+  * ESP8266 : Adafruit Huzzah
53
+  * ATmega2560 @ 16MHz : Arduino Mega
54
+  * ATSAM3X8E : Arduino Due
55
+  * ATSAM21D : Arduino Zero, M0 Pro
56
+  * ATtiny85 @ 16MHz : Adafruit Trinket 5V
57
+  * ATtiny85 @ 8MHz : Adafruit Gemma, Arduino Gemma, Adafruit Trinket 3V
58
+
59
+<!-- END COMPATIBILITY TABLE -->

+ 157
- 0
firmware/libraries/Adafruit_BME280_Library/examples/advancedsettings/advancedsettings.ino View File

@@ -0,0 +1,157 @@
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. The device's I2C address is either 0x76 or 0x77.
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!
13
+
14
+  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15
+  BSD license, all text above must be included in any redistribution
16
+ ***************************************************************************/
17
+
18
+#include <Wire.h>
19
+#include <SPI.h>
20
+#include <Adafruit_Sensor.h>
21
+#include <Adafruit_BME280.h>
22
+
23
+#define BME_SCK 13
24
+#define BME_MISO 12
25
+#define BME_MOSI 11
26
+#define BME_CS 10
27
+
28
+#define SEALEVELPRESSURE_HPA (1013.25)
29
+
30
+Adafruit_BME280 bme; // I2C
31
+//Adafruit_BME280 bme(BME_CS); // hardware SPI
32
+//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
33
+
34
+unsigned long delayTime;
35
+
36
+void setup() {
37
+    Serial.begin(9600);
38
+    Serial.println(F("BME280 test"));
39
+
40
+    if (! bme.begin(&Wire1)) {
41
+        Serial.println("Could not find a valid BME280 sensor, check wiring!");
42
+        while (1);
43
+    }
44
+
45
+    Serial.println("-- Default Test --");
46
+    Serial.println("normal mode, 16x oversampling for all, filter off,");
47
+    Serial.println("0.5ms standby period");
48
+    delayTime = 5000;
49
+    
50
+    
51
+    // For more details on the following scenarious, see chapter
52
+    // 3.5 "Recommended modes of operation" in the datasheet
53
+    
54
+/*
55
+    // weather monitoring
56
+    Serial.println("-- Weather Station Scenario --");
57
+    Serial.println("forced mode, 1x temperature / 1x humidity / 1x pressure oversampling,");
58
+    Serial.println("filter off");
59
+    bme.setSampling(Adafruit_BME280::MODE_FORCED,
60
+                    Adafruit_BME280::SAMPLING_X1, // temperature
61
+                    Adafruit_BME280::SAMPLING_X1, // pressure
62
+                    Adafruit_BME280::SAMPLING_X1, // humidity
63
+                    Adafruit_BME280::FILTER_OFF   );
64
+                      
65
+    // suggested rate is 1/60Hz (1m)
66
+    delayTime = 60000; // in milliseconds
67
+*/
68
+
69
+/*    
70
+    // humidity sensing
71
+    Serial.println("-- Humidity Sensing Scenario --");
72
+    Serial.println("forced mode, 1x temperature / 1x humidity / 0x pressure oversampling");
73
+    Serial.println("= pressure off, filter off");
74
+    bme.setSampling(Adafruit_BME280::MODE_FORCED,
75
+                    Adafruit_BME280::SAMPLING_X1,   // temperature
76
+                    Adafruit_BME280::SAMPLING_NONE, // pressure
77
+                    Adafruit_BME280::SAMPLING_X1,   // humidity
78
+                    Adafruit_BME280::FILTER_OFF );
79
+                      
80
+    // suggested rate is 1Hz (1s)
81
+    delayTime = 1000;  // in milliseconds
82
+*/
83
+
84
+/*    
85
+    // indoor navigation
86
+    Serial.println("-- Indoor Navigation Scenario --");
87
+    Serial.println("normal mode, 16x pressure / 2x temperature / 1x humidity oversampling,");
88
+    Serial.println("0.5ms standby period, filter 16x");
89
+    bme.setSampling(Adafruit_BME280::MODE_NORMAL,
90
+                    Adafruit_BME280::SAMPLING_X2,  // temperature
91
+                    Adafruit_BME280::SAMPLING_X16, // pressure
92
+                    Adafruit_BME280::SAMPLING_X1,  // humidity
93
+                    Adafruit_BME280::FILTER_X16,
94
+                    Adafruit_BME280::STANDBY_MS_0_5 );
95
+    
96
+    // suggested rate is 25Hz
97
+    // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5) + (2 * H_ovs + 0.5)
98
+    // T_ovs = 2
99
+    // P_ovs = 16
100
+    // H_ovs = 1
101
+    // = 40ms (25Hz)
102
+    // with standby time that should really be 24.16913... Hz
103
+    delayTime = 41;
104
+    
105
+    /*
106
+    // gaming
107
+    Serial.println("-- Gaming Scenario --");
108
+    Serial.println("normal mode, 4x pressure / 1x temperature / 0x humidity oversampling,");
109
+    Serial.println("= humidity off, 0.5ms standby period, filter 16x");
110
+    bme.setSampling(Adafruit_BME280::MODE_NORMAL,
111
+                    Adafruit_BME280::SAMPLING_X1,   // temperature
112
+                    Adafruit_BME280::SAMPLING_X4,   // pressure
113
+                    Adafruit_BME280::SAMPLING_NONE, // humidity
114
+                    Adafruit_BME280::FILTER_X16,
115
+                    Adafruit_BME280::STANDBY_MS_0_5 );
116
+                      
117
+    // Suggested rate is 83Hz
118
+    // 1 + (2 * T_ovs) + (2 * P_ovs + 0.5)
119
+    // T_ovs = 1
120
+    // P_ovs = 4
121
+    // = 11.5ms + 0.5ms standby
122
+    delayTime = 12;
123
+*/
124
+
125
+    Serial.println();
126
+}
127
+
128
+
129
+void loop() {
130
+    // Only needed in forced mode! In normal mode, you can remove the next line.
131
+    bme.takeForcedMeasurement(); // has no effect in normal mode
132
+    
133
+    printValues();
134
+    delay(delayTime);
135
+}
136
+
137
+
138
+void printValues() {
139
+    Serial.print("Temperature = ");
140
+    Serial.print(bme.readTemperature());
141
+    Serial.println(" *C");
142
+
143
+    Serial.print("Pressure = ");
144
+
145
+    Serial.print(bme.readPressure() / 100.0F);
146
+    Serial.println(" hPa");
147
+
148
+    Serial.print("Approx. Altitude = ");
149
+    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
150
+    Serial.println(" m");
151
+
152
+    Serial.print("Humidity = ");
153
+    Serial.print(bme.readHumidity());
154
+    Serial.println(" %");
155
+
156
+    Serial.println();
157
+}

+ 82
- 0
firmware/libraries/Adafruit_BME280_Library/examples/bme280test/bme280test.ino View File

@@ -0,0 +1,82 @@
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. The device's I2C address is either 0x76 or 0x77.
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!
13
+
14
+  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15
+  BSD license, all text above must be included in any redistribution
16
+ ***************************************************************************/
17
+
18
+#include <Wire.h>
19
+#include <SPI.h>
20
+#include <Adafruit_Sensor.h>
21
+#include <Adafruit_BME280.h>
22
+
23
+#define BME_SCK 13
24
+#define BME_MISO 12
25
+#define BME_MOSI 11
26
+#define BME_CS 10
27
+
28
+#define SEALEVELPRESSURE_HPA (1013.25)
29
+
30
+Adafruit_BME280 bme; // I2C
31
+//Adafruit_BME280 bme(BME_CS); // hardware SPI
32
+//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
33
+
34
+unsigned long delayTime;
35
+
36
+void setup() {
37
+    Serial.begin(9600);
38
+    Serial.println(F("BME280 test"));
39
+
40
+    bool status;
41
+    
42
+    // default settings
43
+    // (you can also pass in a Wire library object like &Wire2)
44
+    status = bme.begin();  
45
+    if (!status) {
46
+        Serial.println("Could not find a valid BME280 sensor, check wiring!");
47
+        while (1);
48
+    }
49
+    
50
+    Serial.println("-- Default Test --");
51
+    delayTime = 1000;
52
+
53
+    Serial.println();
54
+}
55
+
56
+
57
+void loop() { 
58
+    printValues();
59
+    delay(delayTime);
60
+}
61
+
62
+
63
+void printValues() {
64
+    Serial.print("Temperature = ");
65
+    Serial.print(bme.readTemperature());
66
+    Serial.println(" *C");
67
+
68
+    Serial.print("Pressure = ");
69
+
70
+    Serial.print(bme.readPressure() / 100.0F);
71
+    Serial.println(" hPa");
72
+
73
+    Serial.print("Approx. Altitude = ");
74
+    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
75
+    Serial.println(" m");
76
+
77
+    Serial.print("Humidity = ");
78
+    Serial.print(bme.readHumidity());
79
+    Serial.println(" %");
80
+
81
+    Serial.println();
82
+}

+ 9
- 0
firmware/libraries/Adafruit_BME280_Library/library.properties View File

@@ -0,0 +1,9 @@
1
+name=Adafruit BME280 Library
2
+version=1.0.7
3
+author=Adafruit