Library which provides functions to control a TAOS TSL2561 Light-To-Digital Converter via I2C.

Dependents:   TweetTest NetworkThermometer GR-PEACH_TAMORI mDot_LoRa_Connect_ABPA_Lux ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TSL2561_I2C.h Source File

TSL2561_I2C.h

00001 #ifndef TSL2561_I2C_H
00002 #define TSL2561_I2C_H
00003 #include "mbed.h"
00004 
00005 //Defines 
00006 #define TSL_SLAVE_ADDRESS       0x39
00007 
00008 #define TSL_CONTROL             0x00
00009 #define TSL_TIMING              0x01
00010 #define TSL_THRESHLOWLOW        0x02
00011 #define TSL_THRESHHIGHLOW       0x04
00012 #define TSL_INTERRUPT           0x06
00013 #define TSL_ID                  0x0A
00014 #define TSL_DATA0LOW            0x0C
00015 #define TSL_DATA1LOW            0x0E
00016 
00017 /** TSL2561_I2C class.
00018  *  Abstraction for TAOS TSL2561 Light-To-Digital Converter.
00019  *
00020  * Example:
00021  * @code
00022  * #include "mbed.h"
00023  * #include "TSL2561_I2C.h"
00024  *
00025  * TSL2561_I2C lum_sensor( p9, p10 );
00026  * 
00027  * int main() {
00028  *     lum_sensor.enablePower();
00029  *
00030  *     int rgb_readings[4];
00031  *     while(1) {
00032  *         printf( "Luminosity: %4.2f\n", lum_sensor.getLux() );
00033  *         wait_ms( 100 );
00034  *     }
00035  * }
00036  * @endcode
00037  */
00038 class TSL2561_I2C {
00039 public:
00040     /** Create TSL2561_I2C instance
00041      *
00042      * @param sda sda pin for I2C
00043      * @param scl scl pin for I2C
00044      */
00045     TSL2561_I2C( PinName sda, PinName scl );
00046     
00047     /** Read broadband photodiode (visible plus infrared)
00048      *
00049      * @returns
00050      *     Irradiance measured
00051      */
00052     int getVisibleAndIR();
00053     
00054     /** Read infrared-responding photodiode
00055      *
00056      * @returns
00057      *     Irradiance measured
00058      */
00059     int getIROnly();
00060     
00061     /** Read sensors and calculate Illuminance in lux
00062      *
00063      * @returns
00064      *     Illuminance (lux)
00065      */
00066     float getLux();
00067     
00068     /** Power up the device.
00069      *
00070      * @returns
00071      *     1 if successful
00072      *     0 if otherwise
00073      */
00074     int enablePower();
00075     
00076     /** Disable power to the device.
00077      *
00078      * @returns
00079      *     1 if successful
00080      *     0 if otherwise
00081      */
00082     int disablePower();
00083     
00084     /** Check if power to the device is enabled.
00085      *
00086      * @returns
00087      *     1 if power ON
00088      *     0 if power OFF
00089      */
00090     bool isPowerEnabled();
00091  
00092     /** Return present gain value
00093      *
00094      * @returns
00095      *     1 (low gain mode)
00096      *     16 (high gain mode)
00097      */
00098     int readGain();
00099     
00100     /** Set gain
00101      *
00102      * @param gain Gain must be either 1 or 16  
00103      *
00104      * @returns
00105      *     1 if successful
00106      *     0 if otherwise
00107      */
00108     int setGain( const int gain );
00109     
00110     /** Read the current integration time.
00111      *
00112      * @returns
00113      *     Integration time in milliseconds
00114      */
00115     float readIntegrationTime();
00116     
00117     /** Set integration time.
00118      *
00119      * @param itime Integration time to set in milliseconds. Should be 13.7, 101 or 402.
00120      *
00121      * @returns
00122      *     1 if successful
00123      *     0 if otherwise
00124      */
00125     int setIntegrationTime( const float itime );
00126     
00127     /** Read the low trigger point for the comparison function for interrupt generation.
00128      *
00129      * @returns
00130      *     Low threshold value
00131      */
00132     int readLowInterruptThreshold();
00133     
00134     /** Read the high trigger point for the comparison function for interrupt generation.
00135      *
00136      * @returns
00137      *     High threshold value
00138      */
00139     int readHighInterruptThreshold();
00140     
00141     /** Set the low trigger point for the comparison function for interrupt generation.
00142      *
00143      * @param threshold Low threshold value
00144      *
00145      * @returns
00146      *     1 if successful
00147      *     0 if otherwise
00148      */
00149     int setLowInterruptThreshold( const int threshold );
00150     
00151     /** Set the high trigger point for the comparison function for interrupt generation.
00152      *
00153      * @param threshold High threshold value
00154      *
00155      * @returns
00156      *     1 if successful
00157      *     0 if otherwise
00158      */
00159     int setHighInterruptThreshold( const int threshold );
00160     
00161     /** Return the number of consecutive values out of range (set by the low and high thresholds) required to trigger an interrupt
00162      *
00163      * @returns
00164      *     interrput persistence
00165      */
00166     int readInterruptPersistence();
00167     
00168     /** Set the number of consecutive values out of range (set by the low and high thresholds) required to trigger an interrupt
00169      *
00170      * @param persistence Value to set. 0: interrupt every ADC cycle, 1-15: corresponding number of cycles until interrupt
00171      *
00172      * @returns
00173      *     1 if successful
00174      *     0 if otherwise
00175      */
00176     int setInterruptPersistence( const int persistence );
00177     
00178     /** Check the interrupt function's operation mode
00179      *
00180      * @returns
00181      *     0: Interrupt output disabled
00182      *     1: Level Interrupt
00183      *     2: SMBAlert compliant
00184      *     3: Test Mode
00185      */
00186     int readInterruptControl();
00187     
00188     /** Set the interrupt function's operation mode
00189      *
00190      * @param control Value to set. 0: interrupt every ADC cycle, 1-15: corresponding number of cycles until interrupt
00191      *
00192      * @returns
00193      *     1 if successful
00194      *     0 if otherwise
00195      */
00196     int setInterruptControl( const int control );
00197     
00198     /** Clear the interrupt, allowing normal operation to resume.
00199      *  (writes 0b11000000 to command register to clear interrupt)
00200      *
00201      * @returns
00202      *     1 if successful
00203      *     0 if otherwise
00204      */
00205     int clearInterrupt();
00206     
00207     /** Get integer indicating part number
00208      *
00209      * @returns
00210      *     0: TSL2560
00211      *     1: TSL2561
00212      */
00213     int getPartNumber();
00214     
00215     /** Get revision number
00216      *
00217      * @returns
00218      *     Revision number
00219      */
00220     int getRevisionNumber();
00221 
00222 private:
00223     I2C i2c;
00224     
00225     int writeSingleRegister( char address, char data );
00226     int writeMultipleRegisters( char address, char* data, int quantity );
00227     char readSingleRegister( char address );
00228     int readMultipleRegisters( char address, char* output, int quantity );
00229 };
00230 
00231 #endif