Difference between revisions of "Sensors: TSL2591"

From HiveTool
Jump to: navigation, search
(Code)
(Spec Sheet)
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
[[File:Wiring_TSL2591_Lux.jpg|thumb|right|400px]]
 +
==Summary==
 +
*Lux sensor made by ams AG. 
 +
*Both visual and IR ranges.  Wide dynamic range. 
 +
*Accumulation time and sensitivity are programmable.
 +
*I2C interface
 +
 +
==Spec Sheet==
 +
https://ams.com/chi/content/download/389383%2F1251117/file/TSL2591_DS000338_3-00.pdf<br>
 +
https://www.adafruit.com/datasheets/TSL25911_Datasheet_EN_v1.pdf<br>
 +
Adafruit Application Guide<br>
 +
https://learn.adafruit.com/adafruit-tsl2591
 +
 +
==Sensor Wiring==
 +
 +
[[Hardware:_Sensor_Wiring#TSL2591_Lux_Sensor| Hive Interface Sensor Wiring]]
 +
 
==TSL2591 library conversion==
 
==TSL2591 library conversion==
  
 
===Procedure===
 
===Procedure===
#Stared with Adafruit's Adafruit_TSL2591_Library
+
#Started with Adafruit's Adafruit_TSL2591_Library
 
#: https://github.com/adafruit/Adafruit_TSL2591_Library
 
#: https://github.com/adafruit/Adafruit_TSL2591_Library
 
#Replaced the Adrino's reads and writes with call to the wiringPi library
 
#Replaced the Adrino's reads and writes with call to the wiringPi library
 
#Sanity checked the code against this JS program:
 
#Sanity checked the code against this JS program:
 
#: https://github.com/bbx10/tsl2591-node/blob/master/tsl2591.js
 
#: https://github.com/bbx10/tsl2591-node/blob/master/tsl2591.js
#Tested it and fixed a "bug".
 
  
 
===Code===
 
===Code===
 
+
To compile the program:
2591_pi_2.cpp
+
gcc 2591_pi_2.cpp 2591a.cpp -lwiringPi -o 2591
 +
====2591_pi_2.cpp====
  
 
  <nowiki>
 
  <nowiki>
Line 63: Line 80:
  
 
*/ </nowiki>
 
*/ </nowiki>
 +
 +
 +
====2591a.cpp====
 +
 +
This started as Adafruit's TSL2591 library.  The calls to Adrino funtions were commented out
 +
and replaced with calls to similar functions in the wiringPi library.
 +
 +
 +
<nowiki>
 +
/**************************************************************************/
 +
/*!
 +
    @file    Adafruit_TSL2591.cpp
 +
    @author  KT0WN (adafruit.com)
 +
    This is a library for the Adafruit TSL2591 breakout board
 +
    This library works with the Adafruit TSL2591 breakout
 +
    ----> https://www.adafruit.com/products/1980
 +
 +
    Check out the links above for our tutorials and wiring diagrams
 +
    These chips use I2C to communicate
 +
 +
    Adafruit invests time and resources providing this open source code,
 +
    please support Adafruit and open-source hardware by purchasing
 +
    products from Adafruit!
 +
    @section LICENSE
 +
    Software License Agreement (BSD License)
 +
    Copyright (c) 2014 Adafruit Industries
 +
    All rights reserved.
 +
    Redistribution and use in source and binary forms, with or without
 +
    modification, are permitted provided that the following conditions are met:
 +
    1. Redistributions of source code must retain the above copyright
 +
    notice, this list of conditions and the following disclaimer.
 +
    2. Redistributions in binary form must reproduce the above copyright
 +
    notice, this list of conditions and the following disclaimer in the
 +
    documentation and/or other materials provided with the distribution.
 +
    3. Neither the name of the copyright holders nor the
 +
    names of its contributors may be used to endorse or promote products
 +
    derived from this software without specific prior written permission.
 +
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
 +
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 +
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 +
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
 +
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 +
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 +
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 +
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 +
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 +
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 +
*/
 +
/*!
 +
    @file    2591a.cpp
 +
    @author  hivetool.org
 +
    This started as Adafruit's TSL2591 library.  The calls to Adrino funtions were commented out
 +
    and replaced with calls to similar functions in the wiringPi library.
 +
 +
*/
 +
 +
/**************************************************************************/
 +
 +
#include "2591a.h"
 +
 +
Adafruit_TSL2591::Adafruit_TSL2591(int32_t sensorID)
 +
{
 +
  _initialized = false;
 +
  _integration = TSL2591_INTEGRATIONTIME_100MS;
 +
//  _gain        = TSL2591_GAIN_MED;
 +
  _gain        = TSL2591_GAIN_LOW;
 +
  _sensorID    = sensorID;
 +
 +
  // we cant do wire initialization till later, because we havent loaded Wire yet
 +
}
 +
 +
bool Adafruit_TSL2591::begin(int fd)
 +
{
 +
//  Wire.begin();
 +
  uint8_t d;
 +
 +
//  for (uint8_t i=0; i<0x20; i++)
 +
//  {
 +
//    wiringPiI2CReadReg8(fd,012);
 +
//    uint8_t id = read8(0x12);
 +
//    Serial.print("$"); Serial.print(i, HEX);
 +
//    Serial.print(" = 0x"); Serial.println(read8(i), HEX);
 +
//    d = read8(i);
 +
//    printf ("%x=%x\n",i,d);
 +
//  }
 +
 +
wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
 +
 +
// TSL2591_CONFIG_RW, [TSL2591_DEVICE_RESET_VALUE],
 +
//  uint8_t id = read8(0x12);
 +
 +
  uint8_t id  = wiringPiI2CReadReg8(fd,0x80 | 0x20 | 0x12);
 +
//  printf("id=%x\n",id);
 +
 +
  if (id == 0x50 )
 +
  {
 +
    //Serial.println("Found Adafruit_TSL2591");
 +
//    printf("Found Adafruit_TSL2591\n");
 +
  }
 +
  else
 +
  {
 +
    printf("ERROR: Could not find Adafruit_TSL2591 (id 0x50). Found: %x\n", id);
 +
    return false;
 +
  }
 +
 
 +
  _initialized = true;
 +
 +
  // Set default integration time and gain
 +
  setTiming(fd, _integration);
 +
  setGain(fd,  _gain);
 +
 
 +
  // Note: by default, the device is in power down mode on bootup
 +
  disable(fd);
 +
 +
  return true;
 +
}
 +
 +
void Adafruit_TSL2591::enable(int fd)
 +
{
 +
  if (!_initialized)
 +
  {
 +
    if (!begin(fd))
 +
    {
 +
      return;
 +
    }
 +
  }
 +
 +
  // Enable the device by setting the control bit to 0x01
 +
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
 +
//  wiringPiI2CWriteReg8(fd, TSL2561_COMMAND_BIT, TSL2561_CONTROL_POWERON); //enable the device
 +
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
 +
 +
 
 +
}
 +
 +
void Adafruit_TSL2591::disable(int fd)
 +
{
 +
  if (!_initialized)
 +
  {
 +
    if (!begin(fd))
 +
    {
 +
      return;
 +
    }
 +
  }
 +
 +
  // Disable the device by setting the control bit to 0x00
 +
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
 +
//  wiringPiI2CWriteReg8(fd, TSL2561_COMMAND_BIT, TSL2561_CONTROL_POWEROFF); //disable the device
 +
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
 +
 +
 
 +
}
 +
 +
void Adafruit_TSL2591::setGain(int fd, tsl2591Gain_t gain)
 +
{
 +
  if (!_initialized)
 +
  {
 +
    if (!begin(fd))
 +
    {
 +
      return;
 +
    }
 +
  }
 +
 +
  enable(fd);
 +
  _gain = gain;
 +
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain); 
 +
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain); 
 +
  disable(fd);
 +
}
 +
 +
tsl2591Gain_t Adafruit_TSL2591::getGain()
 +
{
 +
  return _gain;
 +
}
 +
 +
void Adafruit_TSL2591::setTiming(int fd, tsl2591IntegrationTime_t integration)
 +
{
 +
  if (!_initialized)
 +
  {
 +
    if (!begin(fd))
 +
    {
 +
      return;
 +
    }
 +
  }
 +
 +
  enable(fd);
 +
  _integration = integration;
 +
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain); 
 +
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain); 
 +
  disable(fd);
 +
}
 +
 +
tsl2591IntegrationTime_t Adafruit_TSL2591::getTiming()
 +
{
 +
  return _integration;
 +
}
 +
 +
uint32_t Adafruit_TSL2591::calculateLux(uint16_t ch0, uint16_t ch1)
 +
{
 +
  uint16_t atime, again;
 +
  float    cpl, lux1, lux2, lux;
 +
  uint32_t chan0, chan1;
 +
 +
  // Check for overflow conditions first
 +
  if ((ch0 == 0xFFFF) | (ch1 == 0xFFFF))
 +
  {
 +
    // Signal an overflow
 +
    return 0;
 +
  }
 +
 +
  // Note: This algorithm is based on preliminary coefficients
 +
  // provided by AMS and may need to be updated in the future
 +
 
 +
  switch (_integration)
 +
  {
 +
    case TSL2591_INTEGRATIONTIME_100MS :
 +
      atime = 100.0F;
 +
      break;
 +
    case TSL2591_INTEGRATIONTIME_200MS :
 +
      atime = 200.0F;
 +
      break;
 +
    case TSL2591_INTEGRATIONTIME_300MS :
 +
      atime = 300.0F;
 +
      break;
 +
    case TSL2591_INTEGRATIONTIME_400MS :
 +
      atime = 400.0F;
 +
      break;
 +
    case TSL2591_INTEGRATIONTIME_500MS :
 +
      atime = 500.0F;
 +
      break;
 +
    case TSL2591_INTEGRATIONTIME_600MS :
 +
      atime = 600.0F;
 +
      break;
 +
    default: // 100ms
 +
      atime = 100.0F;
 +
      break;
 +
  }
 +
 
 +
  switch (_gain)
 +
  {
 +
    case TSL2591_GAIN_LOW :
 +
      again = 1.0F;
 +
      break;
 +
    case TSL2591_GAIN_MED :
 +
      again = 25.0F;
 +
      break;
 +
    case TSL2591_GAIN_HIGH :
 +
      again = 428.0F;
 +
      break;
 +
    case TSL2591_GAIN_MAX :
 +
      again = 9876.0F;
 +
      break;
 +
    default:
 +
      again = 1.0F;
 +
      break;
 +
  }
 +
 +
  // cpl = (ATIME * AGAIN) / DF
 +
  cpl = (atime * again) / TSL2591_LUX_DF;
 +
 
 +
//printf("ch0= %zu  ch1=%zu\n",ch0,ch1);
 +
 +
  lux1 = ( (float)ch0 - (TSL2591_LUX_COEFB * (float)ch1) ) / cpl;
 +
  lux2 = ( ( TSL2591_LUX_COEFC * (float)ch0 ) - ( TSL2591_LUX_COEFD * (float)ch1 ) ) / cpl;
 +
 +
//  printf("Lux1= %f  Lux2=%f\n",lux1,lux2);
 +
  // The highest value is the approximate lux equivalent
 +
  lux = lux1 > lux2 ? lux1 : lux2;
 +
 +
  // Signal I2C had no errors
 +
  return (uint32_t)lux;
 +
}
 +
 +
uint32_t Adafruit_TSL2591::getFullLuminosity (int fd)
 +
{
 +
//printf("getFullLuminosity\n");
 +
  if (!_initialized)
 +
  {
 +
    if (!begin(fd))
 +
    {
 +
      return 0;
 +
    }
 +
  }
 +
 +
  // Enable the device
 +
  enable(fd);
 +
//printf ("getFullLuminosity: device enabled\n");
 +
  // Wait x ms for ADC to complete
 +
  for (uint8_t d=0; d<=_integration; d++)
 +
  {
 +
    delay(120);
 +
  }
 +
 +
  uint32_t x,y;
 +
//  x = read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW);
 +
  x = wiringPiI2CReadReg16(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW);
 +
//  printf("Chan1=%u\n",x);
 +
  x <<= 16;
 +
//  x |= read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
 +
  y = wiringPiI2CReadReg16(fd,  TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
 +
// printf("Chan0=%u\n",y);
 +
 +
  x |= y;
 +
 +
  disable(fd);
 +
 +
  return x;
 +
}
 +
 +
uint16_t Adafruit_TSL2591::getLuminosity (int fd, uint8_t channel)
 +
{
 +
  uint32_t x = getFullLuminosity(fd);
 +
 +
  if (channel == TSL2591_FULLSPECTRUM)
 +
  {
 +
    // Reads two byte value from channel 0 (visible + infrared)
 +
    return (x & 0xFFFF);
 +
  }
 +
  else if (channel == TSL2591_INFRARED)
 +
  {
 +
    // Reads two byte value from channel 1 (infrared)
 +
    return (x >> 16);
 +
  }
 +
  else if (channel == TSL2591_VISIBLE)
 +
  {
 +
    // Reads all and subtracts out just the visible!
 +
    return ( (x & 0xFFFF) - (x >> 16));
 +
  }
 +
 
 +
  // unknown channel!
 +
  return 0;
 +
}
 +
 +
 +
 +
/**************************************************************************/
 +
/*!
 +
    @brief  Gets the most recent sensor event
 +
*/
 +
/**************************************************************************/
 +
//bool Adafruit_TSL2591::getEvent(sensors_event_t *event)
 +
//{
 +
//  uint16_t ir, full;
 +
//  uint32_t lum = getFullLuminosity();
 +
  /* Early silicon seems to have issues when there is a sudden jump in */
 +
  /* light levels. :( To work around this for now sample the sensor 2x */
 +
//  lum = getFullLuminosity();
 +
//  ir = lum >> 16;
 +
//  full = lum & 0xFFFF; 
 +
 
 +
  /* Clear the event */
 +
//  memset(event, 0, sizeof(sensors_event_t));
 +
 
 +
//  event->version  = sizeof(sensors_event_t);
 +
//  event->sensor_id = _sensorID;
 +
//  event->type      = SENSOR_TYPE_LIGHT;
 +
//  event->timestamp = millis();
 +
 +
  /* Calculate the actual lux value */
 +
  /* 0 = sensor overflow (too much light) */
 +
//  event->light = calculateLux(full, ir);
 +
 
 +
//  return true;
 +
//}
 +
 +
/**************************************************************************/
 +
/*!
 +
    @brief  Gets the sensor_t data
 +
*/
 +
/**************************************************************************/
 +
/*
 +
void Adafruit_TSL2591::getSensor(sensor_t *sensor)
 +
{
 +
  // Clear the sensor_t object
 +
  memset(sensor, 0, sizeof(sensor_t));
 +
 +
  // Insert the sensor name in the fixed length char array
 +
  strncpy (sensor->name, "TSL2591", sizeof(sensor->name) - 1);
 +
  sensor->name[sizeof(sensor->name)- 1] = 0;
 +
  sensor->version    = 1;
 +
  sensor->sensor_id  = _sensorID;
 +
  sensor->type        = SENSOR_TYPE_LIGHT;
 +
  sensor->min_delay  = 0;
 +
  sensor->max_value  = 88000.0;
 +
  sensor->min_value  = 0.0;
 +
  sensor->resolution  = 1.0;
 +
}
 +
*/
 +
 +
</nowiki>
 +
 +
 +
====2591a.h====
 +
 +
<nowiki>
 +
#ifndef _TSL2591_H_
 +
#define _TSL2591_H_
 +
 +
#include <stdio.h>
 +
#include <inttypes.h>
 +
#include <wiringPi.h>
 +
#include <wiringPiI2C.h>
 +
#include <errno.h>
 +
 +
 +
#define TSL2591_VISIBLE          (2)      // channel 0 - channel 1
 +
#define TSL2591_INFRARED          (1)      // channel 1
 +
#define TSL2591_FULLSPECTRUM      (0)      // channel 0
 +
 +
#define TSL2591_ADDR              (0x29)
 +
#define TSL2591_READBIT          (0x01)
 +
 +
#define TSL2591_COMMAND_BIT      (0xA0)    // bits 7 and 5 for 'command normal'
 +
#define TSL2591_CLEAR_BIT        (0x40)    // Clears any pending interrupt (write 1 to clear)
 +
#define TSL2591_WORD_BIT          (0x20)    // 1 = read/write word (rather than byte)
 +
#define TSL2591_BLOCK_BIT        (0x10)    // 1 = using block read/write
 +
 +
#define TSL2591_ENABLE_POWERON    (0x01)
 +
#define TSL2591_ENABLE_POWEROFF  (0x00)
 +
#define TSL2591_ENABLE_AEN        (0x02)
 +
#define TSL2591_ENABLE_AIEN      (0x10)
 +
 +
#define TSL2591_CONTROL_RESET    (0x80)
 +
 +
#define TSL2591_LUX_DF            (408.0F)
 +
#define TSL2591_LUX_COEFB        (1.64F)  // CH0 coefficient
 +
#define TSL2591_LUX_COEFC        (0.59F)  // CH1 coefficient A
 +
#define TSL2591_LUX_COEFD        (0.86F)  // CH2 coefficient B
 +
 +
typedef enum
 +
{
 +
  TSL2591_REGISTER_ENABLE          = 0x00,
 +
  TSL2591_REGISTER_CONTROL          = 0x01,
 +
  TSL2591_REGISTER_THRESHHOLDL_LOW  = 0x02,
 +
  TSL2591_REGISTER_THRESHHOLDL_HIGH = 0x03,
 +
  TSL2591_REGISTER_THRESHHOLDH_LOW  = 0x04,
 +
  TSL2591_REGISTER_THRESHHOLDH_HIGH = 0x05,
 +
  TSL2591_REGISTER_INTERRUPT        = 0x06,
 +
  TSL2591_REGISTER_CRC              = 0x08,
 +
  TSL2591_REGISTER_ID              = 0x0A,
 +
  TSL2591_REGISTER_CHAN0_LOW        = 0x14,
 +
  TSL2591_REGISTER_CHAN0_HIGH      = 0x15,
 +
  TSL2591_REGISTER_CHAN1_LOW        = 0x16,
 +
  TSL2591_REGISTER_CHAN1_HIGH      = 0x17
 +
};
 +
 +
typedef enum
 +
{
 +
  TSL2591_INTEGRATIONTIME_100MS    = 0x00,
 +
  TSL2591_INTEGRATIONTIME_200MS    = 0x01,
 +
  TSL2591_INTEGRATIONTIME_300MS    = 0x02,
 +
  TSL2591_INTEGRATIONTIME_400MS    = 0x03,
 +
  TSL2591_INTEGRATIONTIME_500MS    = 0x04,
 +
  TSL2591_INTEGRATIONTIME_600MS    = 0x05,
 +
}
 +
tsl2591IntegrationTime_t;
 +
 +
typedef enum
 +
{
 +
  TSL2591_GAIN_LOW                  = 0x00,    // low gain (1x)
 +
  TSL2591_GAIN_MED                  = 0x10,    // medium gain (25x)
 +
  TSL2591_GAIN_HIGH                = 0x20,    // medium gain (428x)
 +
  TSL2591_GAIN_MAX                  = 0x30,    // max gain (9876x)
 +
}
 +
tsl2591Gain_t;
 +
 +
class Adafruit_TSL2591
 +
{
 +
public:
 +
  Adafruit_TSL2591(int32_t sensorID = -1);
 +
 
 +
  bool      begin  ( int fd);
 +
  void      enable  ( int fd);
 +
  void      disable ( int fd);
 +
  void      write8  ( uint8_t r, uint8_t v );
 +
  uint16_t  read16  ( uint8_t reg );
 +
  uint8_t  read8  ( uint8_t reg );
 +
 +
  uint32_t  calculateLux  ( uint16_t ch0, uint16_t ch1 );
 +
  void      setGain      ( int fd, tsl2591Gain_t gain );
 +
  void      setTiming    ( int fd, tsl2591IntegrationTime_t integration );
 +
  uint16_t  getLuminosity (int fd, uint8_t channel );
 +
  uint32_t  getFullLuminosity (int fd );
 +
 +
  tsl2591IntegrationTime_t getTiming();
 +
  tsl2591Gain_t            getGain();
 +
 
 +
  /* Unified Sensor API Functions */ 
 +
//  bool getEvent  ( sensors_event_t* );
 +
//  void getSensor ( sensor_t* );
 +
 +
private:
 +
  tsl2591IntegrationTime_t _integration;
 +
  tsl2591Gain_t _gain;
 +
  int32_t _sensorID;
 +
 +
  bool    _initialized;
 +
};
 +
#endif
 +
</nowiki>

Latest revision as of 04:09, 17 December 2017

Error creating thumbnail: File missing

Summary

  • Lux sensor made by ams AG.
  • Both visual and IR ranges. Wide dynamic range.
  • Accumulation time and sensitivity are programmable.
  • I2C interface

Spec Sheet

https://ams.com/chi/content/download/389383%2F1251117/file/TSL2591_DS000338_3-00.pdf
https://www.adafruit.com/datasheets/TSL25911_Datasheet_EN_v1.pdf
Adafruit Application Guide
https://learn.adafruit.com/adafruit-tsl2591

Sensor Wiring

Hive Interface Sensor Wiring

TSL2591 library conversion

Procedure

  1. Started with Adafruit's Adafruit_TSL2591_Library
    https://github.com/adafruit/Adafruit_TSL2591_Library
  2. Replaced the Adrino's reads and writes with call to the wiringPi library
  3. Sanity checked the code against this JS program:
    https://github.com/bbx10/tsl2591-node/blob/master/tsl2591.js

Code

To compile the program:

gcc 2591_pi_2.cpp 2591a.cpp -lwiringPi -o 2591

2591_pi_2.cpp

//http://www.raspberrypi.org/forums/viewtopic.php?t=67979&p=496252

// ALL COMMAND TSL2561
// Default I2C RPI address in (0x39) = FLOAT ADDR (Slave) Other [(0x49) = VCC ADDR / (0x29) = GROUND ADDR]
#define TSL2561_ADDR_LOW                   (0x29)
#define TSL2561_ADDR_FLOAT                 (0x39)    
#define TSL2561_ADDR_HIGH                   (0x49)


#include "2591a.h"
#include <unistd.h> 

int main(){

    Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); // pass in a number for the sensor identifier (for your use later)

    int fd = 0;
    fd = wiringPiI2CSetup(0x29);
    uint16_t ch0,ch1;
    uint32_t visible_and_ir, lux;

    visible_and_ir = tsl.Adafruit_TSL2591::getFullLuminosity (fd);
    sleep(1);
    visible_and_ir = tsl.Adafruit_TSL2591::getFullLuminosity (fd);

    // Reads two byte value from channel 0 (visible + infrared)
    ch0 =  (visible_and_ir & 0xFFFF);
    // Reads two byte value from channel 1 (infrared)
    ch1 = (visible_and_ir >> 16);

    lux = tsl.Adafruit_TSL2591::calculateLux(ch0, ch1);

    printf("TSL2591 Full Spectrum: %zu\n",ch0);
    printf("TSL2591 Infrared: %zu\n",ch1);
    if ( ch0 >= ch1 )  printf("TSL2591 Visible: %zu\n",ch0-ch1);
    printf("TSL2591 Lux: %zu\n", lux);
}

/*
NOTE: To compile programs with wiringPi, you need to add:
    -lwiringPi
  to your compile line(s) To use the Gertboard, MaxDetect, etc.
  code (the devLib), you need to also add:
    -lwiringPiDev
  to your compile line(s).

gcc 2591_pi_2.cpp 2591a.cpp -lwiringPi -o 2591

*/ 


2591a.cpp

This started as Adafruit's TSL2591 library. The calls to Adrino funtions were commented out and replaced with calls to similar functions in the wiringPi library.


/**************************************************************************/
/*! 
    @file     Adafruit_TSL2591.cpp
    @author   KT0WN (adafruit.com)
    This is a library for the Adafruit TSL2591 breakout board
    This library works with the Adafruit TSL2591 breakout 
    ----> https://www.adafruit.com/products/1980
	
    Check out the links above for our tutorials and wiring diagrams 
    These chips use I2C to communicate
	
    Adafruit invests time and resources providing this open source code, 
    please support Adafruit and open-source hardware by purchasing 
    products from Adafruit!
    @section LICENSE
    Software License Agreement (BSD License)
    Copyright (c) 2014 Adafruit Industries
    All rights reserved.
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
    1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holders nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
    EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*! 
    @file     2591a.cpp
    @author   hivetool.org
    This started as Adafruit's TSL2591 library.  The calls to Adrino funtions were commented out 
    and replaced with calls to similar functions in the wiringPi library.

*/

/**************************************************************************/

#include "2591a.h"

Adafruit_TSL2591::Adafruit_TSL2591(int32_t sensorID) 
{
  _initialized = false;
  _integration = TSL2591_INTEGRATIONTIME_100MS;
//  _gain        = TSL2591_GAIN_MED;
  _gain        = TSL2591_GAIN_LOW;
  _sensorID    = sensorID;

  // we cant do wire initialization till later, because we havent loaded Wire yet
}

bool Adafruit_TSL2591::begin(int fd) 
{
//  Wire.begin();
   uint8_t d; 

//  for (uint8_t i=0; i<0x20; i++) 
//  {
//    wiringPiI2CReadReg8(fd,012);
//    uint8_t id = read8(0x12);
//    Serial.print("$"); Serial.print(i, HEX); 
//    Serial.print(" = 0x"); Serial.println(read8(i), HEX);
//    d = read8(i);
//    printf ("%x=%x\n",i,d);
//  }

 wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);

// TSL2591_CONFIG_RW, [TSL2591_DEVICE_RESET_VALUE],
//  uint8_t id = read8(0x12);

  uint8_t id  = wiringPiI2CReadReg8(fd,0x80 | 0x20 | 0x12);
//  printf("id=%x\n",id);

  if (id == 0x50 ) 
  {
    //Serial.println("Found Adafruit_TSL2591");
//     printf("Found Adafruit_TSL2591\n");
  } 
  else 
  {
    printf("ERROR: Could not find Adafruit_TSL2591 (id 0x50). Found: %x\n", id);
    return false;
  }
  
  _initialized = true;

  // Set default integration time and gain
  setTiming(fd, _integration);
  setGain(fd,  _gain);
  
  // Note: by default, the device is in power down mode on bootup
  disable(fd);

  return true;
}

void Adafruit_TSL2591::enable(int fd)
{
  if (!_initialized)
  {
    if (!begin(fd))
    {
      return;
    }
  }

  // Enable the device by setting the control bit to 0x01
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);
//  wiringPiI2CWriteReg8(fd, TSL2561_COMMAND_BIT, TSL2561_CONTROL_POWERON); //enable the device
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWERON | TSL2591_ENABLE_AEN | TSL2591_ENABLE_AIEN);

  
}

void Adafruit_TSL2591::disable(int fd)
{
  if (!_initialized)
  {
    if (!begin(fd))
    {
      return;
    }
  }

  // Disable the device by setting the control bit to 0x00
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);
//  wiringPiI2CWriteReg8(fd, TSL2561_COMMAND_BIT, TSL2561_CONTROL_POWEROFF); //disable the device
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_ENABLE, TSL2591_ENABLE_POWEROFF);

  
}

void Adafruit_TSL2591::setGain(int fd, tsl2591Gain_t gain) 
{
  if (!_initialized)
  {
    if (!begin(fd))
    {
      return;
    }
  }

  enable(fd);
  _gain = gain;
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
  disable(fd);
}

tsl2591Gain_t Adafruit_TSL2591::getGain()
{
  return _gain;
}

void Adafruit_TSL2591::setTiming(int fd, tsl2591IntegrationTime_t integration)
{
  if (!_initialized)
  {
    if (!begin(fd))
    {
      return;
    }
  }

  enable(fd);
  _integration = integration;
//  write8(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
  wiringPiI2CWriteReg8(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL, _integration | _gain);  
  disable(fd);
}

tsl2591IntegrationTime_t Adafruit_TSL2591::getTiming()
{
  return _integration;
}

uint32_t Adafruit_TSL2591::calculateLux(uint16_t ch0, uint16_t ch1)
{
  uint16_t atime, again;
  float    cpl, lux1, lux2, lux;
  uint32_t chan0, chan1;

  // Check for overflow conditions first
  if ((ch0 == 0xFFFF) | (ch1 == 0xFFFF))
  {
    // Signal an overflow
    return 0;
  }

  // Note: This algorithm is based on preliminary coefficients
  // provided by AMS and may need to be updated in the future
  
  switch (_integration)
  {
    case TSL2591_INTEGRATIONTIME_100MS :
      atime = 100.0F;
      break;
    case TSL2591_INTEGRATIONTIME_200MS :
      atime = 200.0F;
      break;
    case TSL2591_INTEGRATIONTIME_300MS :
      atime = 300.0F;
      break;
    case TSL2591_INTEGRATIONTIME_400MS :
      atime = 400.0F;
      break;
    case TSL2591_INTEGRATIONTIME_500MS :
      atime = 500.0F;
      break;
    case TSL2591_INTEGRATIONTIME_600MS :
      atime = 600.0F;
      break;
    default: // 100ms
      atime = 100.0F;
      break;
  }
  
  switch (_gain)
  {
    case TSL2591_GAIN_LOW :
      again = 1.0F;
      break;
    case TSL2591_GAIN_MED :
      again = 25.0F;
      break;
    case TSL2591_GAIN_HIGH :
      again = 428.0F;
      break;
    case TSL2591_GAIN_MAX :
      again = 9876.0F;
      break;
    default:
      again = 1.0F;
      break;
  }

  // cpl = (ATIME * AGAIN) / DF
  cpl = (atime * again) / TSL2591_LUX_DF;
  
//printf("ch0= %zu   ch1=%zu\n",ch0,ch1);

  lux1 = ( (float)ch0 - (TSL2591_LUX_COEFB * (float)ch1) ) / cpl;
  lux2 = ( ( TSL2591_LUX_COEFC * (float)ch0 ) - ( TSL2591_LUX_COEFD * (float)ch1 ) ) / cpl;

//  printf("Lux1= %f   Lux2=%f\n",lux1,lux2); 
  // The highest value is the approximate lux equivalent
  lux = lux1 > lux2 ? lux1 : lux2;

  // Signal I2C had no errors
  return (uint32_t)lux;
}

uint32_t Adafruit_TSL2591::getFullLuminosity (int fd)
{
//printf("getFullLuminosity\n");
  if (!_initialized)
  {
    if (!begin(fd))
    {
      return 0;
    }
  }

  // Enable the device
  enable(fd);
//printf ("getFullLuminosity: device enabled\n");
  // Wait x ms for ADC to complete
  for (uint8_t d=0; d<=_integration; d++) 
  {
    delay(120);
  }

  uint32_t x,y;
//  x = read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW);
  x = wiringPiI2CReadReg16(fd, TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN1_LOW);
//  printf("Chan1=%u\n",x);
  x <<= 16;
//  x |= read16(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
  y = wiringPiI2CReadReg16(fd,  TSL2591_COMMAND_BIT | TSL2591_REGISTER_CHAN0_LOW);
// printf("Chan0=%u\n",y);

  x |= y;
 
  disable(fd);

  return x;
}

uint16_t Adafruit_TSL2591::getLuminosity (int fd, uint8_t channel) 
{
  uint32_t x = getFullLuminosity(fd);

  if (channel == TSL2591_FULLSPECTRUM) 
  {
    // Reads two byte value from channel 0 (visible + infrared)
    return (x & 0xFFFF);
  } 
  else if (channel == TSL2591_INFRARED) 
  {
    // Reads two byte value from channel 1 (infrared)
    return (x >> 16);
  } 
  else if (channel == TSL2591_VISIBLE) 
  {
    // Reads all and subtracts out just the visible!
    return ( (x & 0xFFFF) - (x >> 16));
  }
  
  // unknown channel!
  return 0;
}



/**************************************************************************/
/*!
    @brief  Gets the most recent sensor event
*/
/**************************************************************************/
//bool Adafruit_TSL2591::getEvent(sensors_event_t *event)
//{
//  uint16_t ir, full;
//  uint32_t lum = getFullLuminosity();
  /* Early silicon seems to have issues when there is a sudden jump in */
  /* light levels. :( To work around this for now sample the sensor 2x */
//  lum = getFullLuminosity();
//  ir = lum >> 16;
//  full = lum & 0xFFFF;  
  
  /* Clear the event */
//  memset(event, 0, sizeof(sensors_event_t));
  
//  event->version   = sizeof(sensors_event_t);
//  event->sensor_id = _sensorID;
//  event->type      = SENSOR_TYPE_LIGHT;
//  event->timestamp = millis();

  /* Calculate the actual lux value */
  /* 0 = sensor overflow (too much light) */
//  event->light = calculateLux(full, ir);
  
//  return true;
//}

/**************************************************************************/
/*!
    @brief  Gets the sensor_t data
*/
/**************************************************************************/
/*
void Adafruit_TSL2591::getSensor(sensor_t *sensor)
{
  // Clear the sensor_t object
  memset(sensor, 0, sizeof(sensor_t));

  // Insert the sensor name in the fixed length char array 
  strncpy (sensor->name, "TSL2591", sizeof(sensor->name) - 1);
  sensor->name[sizeof(sensor->name)- 1] = 0;
  sensor->version     = 1;
  sensor->sensor_id   = _sensorID;
  sensor->type        = SENSOR_TYPE_LIGHT;
  sensor->min_delay   = 0;
  sensor->max_value   = 88000.0;
  sensor->min_value   = 0.0;
  sensor->resolution  = 1.0;
}
*/



2591a.h

#ifndef _TSL2591_H_
#define _TSL2591_H_

#include <stdio.h>
#include <inttypes.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <errno.h>


#define TSL2591_VISIBLE           (2)       // channel 0 - channel 1
#define TSL2591_INFRARED          (1)       // channel 1
#define TSL2591_FULLSPECTRUM      (0)       // channel 0

#define TSL2591_ADDR              (0x29)
#define TSL2591_READBIT           (0x01)

#define TSL2591_COMMAND_BIT       (0xA0)    // bits 7 and 5 for 'command normal'
#define TSL2591_CLEAR_BIT         (0x40)    // Clears any pending interrupt (write 1 to clear)
#define TSL2591_WORD_BIT          (0x20)    // 1 = read/write word (rather than byte)
#define TSL2591_BLOCK_BIT         (0x10)    // 1 = using block read/write

#define TSL2591_ENABLE_POWERON    (0x01)
#define TSL2591_ENABLE_POWEROFF   (0x00)
#define TSL2591_ENABLE_AEN        (0x02)
#define TSL2591_ENABLE_AIEN       (0x10)

#define TSL2591_CONTROL_RESET     (0x80)

#define TSL2591_LUX_DF            (408.0F)
#define TSL2591_LUX_COEFB         (1.64F)  // CH0 coefficient 
#define TSL2591_LUX_COEFC         (0.59F)  // CH1 coefficient A
#define TSL2591_LUX_COEFD         (0.86F)  // CH2 coefficient B

typedef enum
{
  TSL2591_REGISTER_ENABLE           = 0x00,
  TSL2591_REGISTER_CONTROL          = 0x01,
  TSL2591_REGISTER_THRESHHOLDL_LOW  = 0x02,
  TSL2591_REGISTER_THRESHHOLDL_HIGH = 0x03,
  TSL2591_REGISTER_THRESHHOLDH_LOW  = 0x04,
  TSL2591_REGISTER_THRESHHOLDH_HIGH = 0x05,
  TSL2591_REGISTER_INTERRUPT        = 0x06,
  TSL2591_REGISTER_CRC              = 0x08,
  TSL2591_REGISTER_ID               = 0x0A,
  TSL2591_REGISTER_CHAN0_LOW        = 0x14,
  TSL2591_REGISTER_CHAN0_HIGH       = 0x15,
  TSL2591_REGISTER_CHAN1_LOW        = 0x16,
  TSL2591_REGISTER_CHAN1_HIGH       = 0x17
};

typedef enum
{
  TSL2591_INTEGRATIONTIME_100MS     = 0x00,
  TSL2591_INTEGRATIONTIME_200MS     = 0x01,
  TSL2591_INTEGRATIONTIME_300MS     = 0x02,
  TSL2591_INTEGRATIONTIME_400MS     = 0x03,
  TSL2591_INTEGRATIONTIME_500MS     = 0x04,
  TSL2591_INTEGRATIONTIME_600MS     = 0x05,
}
tsl2591IntegrationTime_t;

typedef enum
{
  TSL2591_GAIN_LOW                  = 0x00,    // low gain (1x)
  TSL2591_GAIN_MED                  = 0x10,    // medium gain (25x)
  TSL2591_GAIN_HIGH                 = 0x20,    // medium gain (428x)
  TSL2591_GAIN_MAX                  = 0x30,    // max gain (9876x)
}
tsl2591Gain_t;

class Adafruit_TSL2591
{
 public:
  Adafruit_TSL2591(int32_t sensorID = -1);
  
  bool      begin   ( int fd);
  void      enable  ( int fd);
  void      disable ( int fd);
  void      write8  ( uint8_t r, uint8_t v );
  uint16_t  read16  ( uint8_t reg );
  uint8_t   read8   ( uint8_t reg );

  uint32_t  calculateLux  ( uint16_t ch0, uint16_t ch1 );
  void      setGain       ( int fd, tsl2591Gain_t gain );
  void      setTiming     ( int fd, tsl2591IntegrationTime_t integration );
  uint16_t  getLuminosity (int fd, uint8_t channel );
  uint32_t  getFullLuminosity (int fd );

  tsl2591IntegrationTime_t getTiming();
  tsl2591Gain_t            getGain();
  
  /* Unified Sensor API Functions */  
//  bool getEvent  ( sensors_event_t* );
//  void getSensor ( sensor_t* );

 private:
  tsl2591IntegrationTime_t _integration;
  tsl2591Gain_t _gain;
  int32_t _sensorID;

  bool    _initialized;
};
#endif