RaspberrPi project source code
guowenxue
8 hours ago 72fea370ff20ecb2494ab985c4431b4bd691e7cd
project/lightd/hal/tsl2561.c
@@ -1,14 +1,23 @@
/*********************************************************************************
 *      Copyright:  (C) 2019 LingYun IoT System Studio
 *      Copyright:  (C) 2023 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  tsl2561.c
 *    Description:  This file is the Lux sensor TSL2561 API functions on RaspberryPi,
 *                  which connected to I2C-1
 *    Description:  This file is the Lux sensor TSL2561 code
 *
 *        Version:  1.0.0(04/07/19)
 *        Version:  1.0.0(10/08/23)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "04/07/19 17:39:38"
 *      ChangeLog:  1, Release initial version on "10/08/23 17:52:00"
 *
 * Pin connection:
 *              TSL2561 Module           Raspberry Pi Board
 *                   VCC      <----->      #Pin1(3.3V)
 *                   SDA0     <----->      #Pin27(SDA, BCM GPIO0)
 *                   SCL0     <----->      #Pin28(SCL, BCM GPIO1)
 *                   GND      <----->      GND
 *
 * /boot/config.txt:
 *                  dtoverlay=i2c0,pins_0_1
 *
 ********************************************************************************/
@@ -17,57 +26,52 @@
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <time.h>
#include <errno.h>
#include <libgen.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "logger.h"
#include "util_proc.h"
#include "logger.h"
#include "tsl2561.h"
int s_tsl_fd = -1;
#define CONTROL_REG                     0x80
#define REG_COUNT                       4
#define POWER_UP                        0x03
#define POWER_DOWN                      0x00
#define OFF                             0
#define ON                              1
/* Register Address  */
enum
{
    /* Channel_0 = DATA0HIGH<<8 + DATA0LOW */
    DATA0LOW = 0x8C,
    DATA0HIGH,
    /* Channel_1 = DATA1HIGH<<8 + DATA1LOW */
    DATA1LOW,
    DATA1HIGH,
};
static const int  regs_addr[REG_COUNT]={DATA0LOW, DATA0HIGH, DATA1LOW, DATA1HIGH};
int tsl2561_init(void)
{
    if(s_tsl_fd > 0)
        return 0;
    if( (s_tsl_fd=open("/dev/i2c-1", O_RDWR)) < 0)
    {
        log_error("TSL2561 I2C device setup failure: %s\n", strerror(errno));
        return -1;
    }
    log_debug("TSL2561 initialise ok, s_tsl_fd=%d\n", s_tsl_fd);
    return s_tsl_fd;
}
void tsl2561_term(void)
{
    close(s_tsl_fd);
    s_tsl_fd = -1;
    log_warn("Terminate TSL2561.\n");
}
#define ON          1
#define OFF         0
int tsl2561_power(int cmd)
void tsl2561_power(int fd, int cmd)
{
    struct i2c_msg               msg;
    struct i2c_rdwr_ioctl_data   data;
    unsigned char                buf[2];
    msg.addr= TSL2561_I2C_ADDR;
    msg.addr= TSL2561_I2CADDR;
    msg.flags=0;  /* write */
    msg.len= 1;
    msg.buf= buf;
@@ -76,10 +80,10 @@
    data.msgs= &msg;
    msg.buf[0]=CONTROL_REG;
    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    if( ioctl(fd, I2C_RDWR, &data) < 0 )
    {
        log_error("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
        return ;
    }
@@ -88,22 +92,22 @@
    else
        msg.buf[0]=POWER_DOWN;
    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    if( ioctl(fd, I2C_RDWR, &data) < 0 )
    {
        log_error("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
        return ;
    }
    return 0;
    return ;
}
int tsl2561_readreg(unsigned char regaddr, unsigned char *regval)
int tsl2561_readreg(int fd, unsigned char regaddr, unsigned char *regval)
{
    struct i2c_msg               msg;
    struct i2c_rdwr_ioctl_data   data;
    unsigned char                buf[2];
    msg.addr= TSL2561_I2C_ADDR;
    msg.addr= TSL2561_I2CADDR;
    msg.flags=0;  /* write */
    msg.len= 1;
    msg.buf= buf;
@@ -112,7 +116,7 @@
    data.nmsgs= 1;
    data.msgs= &msg;
    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    if( ioctl(fd, I2C_RDWR, &data) < 0 )
    {
        log_error("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
@@ -120,7 +124,7 @@
    memset(buf, 0, sizeof(buf));
    msg.addr= TSL2561_I2C_ADDR;
    msg.addr= TSL2561_I2CADDR;
    msg.flags=I2C_M_RD;  /* read */
    msg.len= 1;
    msg.buf= buf;
@@ -128,7 +132,7 @@
    data.nmsgs= 1;
    data.msgs= &msg;
    if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 )
    if( ioctl(fd, I2C_RDWR, &data) < 0 )
    {
        log_error("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
@@ -138,28 +142,39 @@
    return 0;
}
float tsl2561_get_lux(void)
int tsl2561_get_lux(float *lux)
{
    int                 i;
    unsigned char       reg_data[REG_COUNT];
    int                 i, fd;
    int                 rv = 0;
    char               *dev = TSL2561_I2CDEV;
    float               div = 0.0;
    unsigned char       reg_data[REG_COUNT];
    int                 chn0_data = 0;
    int                 chn1_data = 0;
    float               div = 0.0;
    float               lux = 0.0;
    if( !lux )
    {
        log_error("Invalid input arguments\n");
        return -1;
    }
    if( (fd=open(dev, O_RDWR)) < 0)
    {
        log_error("i2c device '%s' open failed: %s\n", dev, strerror(errno));
        return -2;
    }
    tsl2561_power(ON);
    tsl2561_power(fd, ON);
    msleep(410);  /* t(CONV) MAX 400ms */
    /* Read register Channel0 and channel1 data from register */
    for(i=0; i<REG_COUNT; i++)
    {
        tsl2561_readreg(regs_addr[i], &reg_data[i]);
        rv = tsl2561_readreg(fd, regs_addr[i], &reg_data[i]);
        if( rv < 0)
            goto failed;
    }
    chn0_data = reg_data[1]*256 + reg_data[0]; /* Channel0 = DATA0HIGH<<8 + DATA0LOW  */
@@ -167,32 +182,31 @@
    if( chn0_data<=0 || chn1_data<0 )
    {
        lux = 0.0;
        goto OUT;
        rv = -2;
        goto cleanup;
    }
    div = (float)chn1_data / (float)chn0_data;
    if( div>0 && div<=0.5 )
        lux = 0.304*chn0_data-0.062*chn0_data*pow(div,1.4);
        *lux = 0.304*chn0_data-0.062*chn0_data*pow(div,1.4);
    else if( div>0.5 && div<=0.61 )
        lux = 0.0224*chn0_data-0.031*chn1_data;
        *lux = 0.0224*chn0_data-0.031*chn1_data;
    else if( div>0.61 && div<=0.8 )
        lux = 0.0128*chn0_data-0.0153*chn1_data;
        *lux = 0.0128*chn0_data-0.0153*chn1_data;
    else if( div>0.8 && div<=1.3 )
        lux = 0.00146*chn0_data-0.00112*chn1_data;
        *lux = 0.00146*chn0_data-0.00112*chn1_data;
    else if( div>1.3 )
        lux = 0.0;
        *lux = 0.0;
    log_debug("TSLl2561 get lux: %.3f\n", lux);
cleanup:
    tsl2561_power(fd, OFF);
OUT:
    tsl2561_power(OFF);
    return lux;
failed:
    close(fd);
    return rv;
}