Adafruit Library
WipperSnapper_I2C_Driver_AS7331.h
Go to the documentation of this file.
1 
15 #ifndef WipperSnapper_I2C_Driver_AS7331_H
16 #define WipperSnapper_I2C_Driver_AS7331_H
17 
19 #include <Adafruit_AS7331.h>
20 
21 #define AS7331_BREAKTIME_200US 25
22 
23 /**************************************************************************/
27 /**************************************************************************/
29 public:
30  /*******************************************************************************/
38  /*******************************************************************************/
39  WipperSnapper_I2C_Driver_AS7331(TwoWire *i2c, uint16_t sensorAddress)
40  : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41  _i2c = i2c;
42  _sensorAddress = sensorAddress;
43  }
44 
45  /*******************************************************************************/
49  /*******************************************************************************/
51 
52  /*******************************************************************************/
57  /*******************************************************************************/
58  bool begin() {
59  if (_as7331)
60  delete _as7331;
61  _as7331 = new Adafruit_AS7331();
62  if (!_as7331->begin(_i2c, (uint8_t)_sensorAddress))
63  return false;
64 
65  bool setupSuccess = true;
66  // Configure sensor — must power down before changing settings
67  setupSuccess &= _as7331->powerDown(true);
68  if (!setupSuccess)
69  return false;
70  // Explicitly set defaults to pin behavior against library changes
71  setupSuccess &= _as7331->setGain(AS7331_GAIN_4X);
72  setupSuccess &= _as7331->setIntegrationTime(AS7331_TIME_64MS);
73  setupSuccess &= _as7331->setMeasurementMode(AS7331_MODE_CONT);
74  setupSuccess &= _as7331->setClockFrequency(AS7331_CLOCK_1024MHZ);
75  setupSuccess &= _as7331->setBreakTime(AS7331_BREAKTIME_200US);
76  setupSuccess &= _as7331->setStandby(false);
77 
78  // Start continuous measurements
79  setupSuccess &= _as7331->powerDown(false);
80 
81  return setupSuccess;
82  }
83 
84  /*******************************************************************************/
92  /*******************************************************************************/
93  bool getEventRaw(sensors_event_t *rawEvent) {
94  if (!_readSensor())
95  return false;
96 
97  rawEvent->data[0] = _cachedUVB;
98  return true;
99  }
100 
101 protected:
102  Adafruit_AS7331 *_as7331 = nullptr;
103  unsigned long _lastRead = 0;
104  float _cachedUVB = 0;
105 
106  /*******************************************************************************/
112  /*******************************************************************************/
113  bool _readSensor() {
114  unsigned long now = millis();
115  if (_lastRead != 0 && (now - _lastRead < ONE_SECOND_IN_MILLIS))
116  return true;
117 
118  float uva, uvb, uvc;
119  if (!_as7331->readAllUV_uWcm2(&uva, &uvb, &uvc))
120  return false;
121 
122  _cachedUVB = uvb;
123  _lastRead = now;
124 
125  WS_DEBUG_PRINT("AS7331 UVA: ");
126  WS_DEBUG_PRINTVAR(uva);
127  WS_DEBUG_PRINT(" UVB: ");
128  WS_DEBUG_PRINTVAR(uvb);
129  WS_DEBUG_PRINT(" UVC: ");
130  WS_DEBUG_PRINTLNVAR(uvc);
131 
132  return true;
133  }
134 };
135 
136 #endif // WipperSnapper_I2C_Driver_AS7331_H
#define AS7331_BREAKTIME_200US
Recommended break time (val * 8µs)
Definition: WipperSnapper_I2C_Driver_AS7331.h:21
#define WS_DEBUG_PRINT(...)
Prints debug output.
Definition: Wippersnapper.h:63
~WipperSnapper_I2C_Driver_AS7331()
Destructor for an AS7331 sensor.
Definition: WipperSnapper_I2C_Driver_AS7331.h:50
bool _readSensor()
Reads all UV channels from the AS7331, caches UVB, and prints UVA/UVB/UVC as debug output...
Definition: WipperSnapper_I2C_Driver_AS7331.h:113
#define WS_DEBUG_PRINTVAR(...)
Prints variable (any type)
Definition: Wippersnapper.h:72
#define WS_DEBUG_PRINTLNVAR(...)
Prints variable with newline.
Definition: Wippersnapper.h:75
WipperSnapper_I2C_Driver_AS7331(TwoWire *i2c, uint16_t sensorAddress)
Constructor for an AS7331 sensor.
Definition: WipperSnapper_I2C_Driver_AS7331.h:39
Base class for I2C Drivers.
Definition: WipperSnapper_I2C_Driver.h:33
Adafruit_AS7331 * _as7331
Pointer to AS7331 sensor object.
Definition: WipperSnapper_I2C_Driver_AS7331.h:102
bool begin()
Initializes the AS7331 sensor and begins I2C.
Definition: WipperSnapper_I2C_Driver_AS7331.h:58
Class that provides a driver interface for an AS7331 UV sensor.
Definition: WipperSnapper_I2C_Driver_AS7331.h:28
uint16_t _sensorAddress
The I2C driver&#39;s unique I2C address.
Definition: WipperSnapper_I2C_Driver.h:1340
bool getEventRaw(sensors_event_t *rawEvent)
Gets the AS7331&#39;s current UVB reading in uW/cm2 as raw event.
Definition: WipperSnapper_I2C_Driver_AS7331.h:93
unsigned long _lastRead
Last sensor read time.
Definition: WipperSnapper_I2C_Driver_AS7331.h:103
#define ONE_SECOND_IN_MILLIS
Used for period checks.
Definition: WipperSnapper_I2C_Driver.h:26
float _cachedUVB
Cached UVB reading in uW/cm2.
Definition: WipperSnapper_I2C_Driver_AS7331.h:104
TwoWire * _i2c
Pointer to the I2C driver&#39;s Wire object.
Definition: WipperSnapper_I2C_Driver.h:1339