[137] | 1 | /**************************************************************************/
|
---|
| 2 | /*!
|
---|
| 3 | @file Adafruit_TSL2591.h
|
---|
| 4 | @author KT0WN (adafruit.com)
|
---|
| 5 |
|
---|
| 6 | This is a library for the Adafruit TSL2591 breakout board
|
---|
| 7 | This library works with the Adafruit TSL2591 breakout
|
---|
| 8 | ----> https://www.adafruit.com/products/1980
|
---|
| 9 |
|
---|
| 10 | Check out the links above for our tutorials and wiring diagrams
|
---|
| 11 | These chips use I2C to communicate
|
---|
| 12 |
|
---|
| 13 | Adafruit invests time and resources providing this open source code,
|
---|
| 14 | please support Adafruit and open-source hardware by purchasing
|
---|
| 15 | products from Adafruit!
|
---|
| 16 |
|
---|
| 17 | @section LICENSE
|
---|
| 18 |
|
---|
| 19 | Software License Agreement (BSD License)
|
---|
| 20 |
|
---|
| 21 | Copyright (c) 2014 Adafruit Industries
|
---|
| 22 | All rights reserved.
|
---|
| 23 |
|
---|
| 24 | Redistribution and use in source and binary forms, with or without
|
---|
| 25 | modification, are permitted provided that the following conditions are met:
|
---|
| 26 | 1. Redistributions of source code must retain the above copyright
|
---|
| 27 | notice, this list of conditions and the following disclaimer.
|
---|
| 28 | 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 29 | notice, this list of conditions and the following disclaimer in the
|
---|
| 30 | documentation and/or other materials provided with the distribution.
|
---|
| 31 | 3. Neither the name of the copyright holders nor the
|
---|
| 32 | names of its contributors may be used to endorse or promote products
|
---|
| 33 | derived from this software without specific prior written permission.
|
---|
| 34 |
|
---|
| 35 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
---|
| 36 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
| 37 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
| 38 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
---|
| 39 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
| 40 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
| 41 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
| 42 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 43 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 44 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 45 | */
|
---|
| 46 | /**************************************************************************/
|
---|
| 47 |
|
---|
| 48 | #ifndef _TSL2591_H_
|
---|
| 49 | #define _TSL2591_H_
|
---|
| 50 |
|
---|
| 51 | #if ARDUINO >= 100
|
---|
| 52 | #include <Arduino.h>
|
---|
| 53 | #else
|
---|
| 54 | #include <WProgram.h>
|
---|
| 55 | #endif
|
---|
| 56 | #include <Adafruit_Sensor.h>
|
---|
| 57 | #include <Wire.h>
|
---|
| 58 |
|
---|
| 59 | #define TSL2591_VISIBLE (2) // channel 0 - channel 1
|
---|
| 60 | #define TSL2591_INFRARED (1) // channel 1
|
---|
| 61 | #define TSL2591_FULLSPECTRUM (0) // channel 0
|
---|
| 62 |
|
---|
| 63 | #define TSL2591_ADDR (0x29)
|
---|
| 64 | #define TSL2591_READBIT (0x01)
|
---|
| 65 |
|
---|
| 66 | #define TSL2591_COMMAND_BIT (0xA0) // bits 7 and 5 for 'command normal'
|
---|
| 67 | #define TSL2591_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear)
|
---|
| 68 | #define TSL2591_WORD_BIT (0x20) // 1 = read/write word (rather than byte)
|
---|
| 69 | #define TSL2591_BLOCK_BIT (0x10) // 1 = using block read/write
|
---|
| 70 |
|
---|
| 71 | #define TSL2591_ENABLE_POWERON (0x01)
|
---|
| 72 | #define TSL2591_ENABLE_POWEROFF (0x00)
|
---|
| 73 | #define TSL2591_ENABLE_AEN (0x02)
|
---|
| 74 | #define TSL2591_ENABLE_AIEN (0x10)
|
---|
| 75 |
|
---|
| 76 | #define TSL2591_CONTROL_RESET (0x80)
|
---|
| 77 |
|
---|
| 78 | #define TSL2591_LUX_DF (408.0F)
|
---|
| 79 | #define TSL2591_LUX_COEFB (1.64F) // CH0 coefficient
|
---|
| 80 | #define TSL2591_LUX_COEFC (0.59F) // CH1 coefficient A
|
---|
| 81 | #define TSL2591_LUX_COEFD (0.86F) // CH2 coefficient B
|
---|
| 82 |
|
---|
| 83 | enum
|
---|
| 84 | {
|
---|
| 85 | TSL2591_REGISTER_ENABLE = 0x00,
|
---|
| 86 | TSL2591_REGISTER_CONTROL = 0x01,
|
---|
| 87 | TSL2591_REGISTER_THRESHHOLDL_LOW = 0x02,
|
---|
| 88 | TSL2591_REGISTER_THRESHHOLDL_HIGH = 0x03,
|
---|
| 89 | TSL2591_REGISTER_THRESHHOLDH_LOW = 0x04,
|
---|
| 90 | TSL2591_REGISTER_THRESHHOLDH_HIGH = 0x05,
|
---|
| 91 | TSL2591_REGISTER_INTERRUPT = 0x06,
|
---|
| 92 | TSL2591_REGISTER_CRC = 0x08,
|
---|
| 93 | TSL2591_REGISTER_ID = 0x0A,
|
---|
| 94 | TSL2591_REGISTER_CHAN0_LOW = 0x14,
|
---|
| 95 | TSL2591_REGISTER_CHAN0_HIGH = 0x15,
|
---|
| 96 | TSL2591_REGISTER_CHAN1_LOW = 0x16,
|
---|
| 97 | TSL2591_REGISTER_CHAN1_HIGH = 0x17
|
---|
| 98 | };
|
---|
| 99 |
|
---|
| 100 | typedef enum
|
---|
| 101 | {
|
---|
| 102 | TSL2591_INTEGRATIONTIME_100MS = 0x00,
|
---|
| 103 | TSL2591_INTEGRATIONTIME_200MS = 0x01,
|
---|
| 104 | TSL2591_INTEGRATIONTIME_300MS = 0x02,
|
---|
| 105 | TSL2591_INTEGRATIONTIME_400MS = 0x03,
|
---|
| 106 | TSL2591_INTEGRATIONTIME_500MS = 0x04,
|
---|
| 107 | TSL2591_INTEGRATIONTIME_600MS = 0x05,
|
---|
| 108 | }
|
---|
| 109 | tsl2591IntegrationTime_t;
|
---|
| 110 |
|
---|
| 111 | typedef enum
|
---|
| 112 | {
|
---|
| 113 | TSL2591_GAIN_LOW = 0x00, // low gain (1x)
|
---|
| 114 | TSL2591_GAIN_MED = 0x10, // medium gain (25x)
|
---|
| 115 | TSL2591_GAIN_HIGH = 0x20, // medium gain (428x)
|
---|
| 116 | TSL2591_GAIN_MAX = 0x30, // max gain (9876x)
|
---|
| 117 | }
|
---|
| 118 | tsl2591Gain_t;
|
---|
| 119 |
|
---|
| 120 | class Adafruit_TSL2591 : public Adafruit_Sensor
|
---|
| 121 | {
|
---|
| 122 | public:
|
---|
| 123 | Adafruit_TSL2591(int32_t sensorID = -1);
|
---|
| 124 |
|
---|
| 125 | boolean begin ( void );
|
---|
| 126 | void enable ( void );
|
---|
| 127 | void disable ( void );
|
---|
| 128 | void write8 ( uint8_t r, uint8_t v );
|
---|
| 129 | uint16_t read16 ( uint8_t reg );
|
---|
| 130 | uint8_t read8 ( uint8_t reg );
|
---|
| 131 |
|
---|
| 132 | uint32_t calculateLux ( uint16_t ch0, uint16_t ch1 );
|
---|
| 133 | void setGain ( tsl2591Gain_t gain );
|
---|
| 134 | void setTiming ( tsl2591IntegrationTime_t integration );
|
---|
| 135 | uint16_t getLuminosity (uint8_t channel );
|
---|
| 136 | uint32_t getFullLuminosity ( );
|
---|
| 137 |
|
---|
| 138 | tsl2591IntegrationTime_t getTiming();
|
---|
| 139 | tsl2591Gain_t getGain();
|
---|
| 140 |
|
---|
| 141 | /* Unified Sensor API Functions */
|
---|
| 142 | bool getEvent ( sensors_event_t* );
|
---|
| 143 | void getSensor ( sensor_t* );
|
---|
| 144 |
|
---|
| 145 | private:
|
---|
| 146 | tsl2591IntegrationTime_t _integration;
|
---|
| 147 | tsl2591Gain_t _gain;
|
---|
| 148 | int32_t _sensorID;
|
---|
| 149 |
|
---|
| 150 | boolean _initialized;
|
---|
| 151 | };
|
---|
| 152 | #endif
|
---|