Sensors: TSL2591

From HiveTool
Revision as of 16:00, 20 May 2015 by Paul (talk | contribs) (Code)
Jump to: navigation, search

TSL2591 library conversion


  1. Stared with Adafruit's 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:
  4. Tested it and fixed a "bug".



// 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);
    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:
  to your compile line(s) To use the Gertboard, MaxDetect, etc.
  code (the devLib), you need to also add:
  to your compile line(s).

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