From 80686e6bc4cb17e9d856647084725203fa63ebe6 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 19 Aug 2024 16:47:42 +0800
Subject: [PATCH] Add LCD API examples

---
 hal/modules/tsl2561.c |  337 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 337 insertions(+), 0 deletions(-)

diff --git a/hal/modules/tsl2561.c b/hal/modules/tsl2561.c
new file mode 100644
index 0000000..20bd0c2
--- /dev/null
+++ b/hal/modules/tsl2561.c
@@ -0,0 +1,337 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2023 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  sht20.c
+ *    Description:  This file is the Lux sensor TSL2561 code
+ *
+ *        Version:  1.0.0(10/08/23)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "10/08/23 17:52:00"
+ *
+ * Pin connection:
+ *               STH20 Module            Raspberry Pi Board
+ *                   VCC      <----->      #Pin1(3.3V)
+ *                   SDA      <----->      #Pin27(SDA, BCM GPIO0)
+ *                   SCL      <----->      #Pin28(SCL, BCM GPIO1)
+ *                   GND      <----->      GND
+ *
+ * /boot/config.txt:
+ *                  dtoverlay=i2c0,pins_0_1
+ *
+ ********************************************************************************/
+
+
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <math.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>
+
+#define TSL2561_I2C_ADDR                0x39
+
+#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};
+
+float tsl2561_get_lux(int fd);
+static inline void print_datime(void);
+
+static inline void banner(const char *progname)
+{
+    printf("%s program Version v1.0.0\n", progname);
+    printf("Copyright (C) 2023 LingYun IoT System Studio.\n");
+}
+
+static void program_usage(const char *progname)
+{
+
+    printf("Usage: %s [OPTION]...\n", progname);
+    printf(" %s is TSL2561 Lux sensor program.\n", progname);
+
+    printf(" -d[device  ]  Specify I2C device, such as /dev/i2c-0\n");
+    printf(" -h[help    ]  Display this help information\n");
+    printf(" -v[version ]  Display the program version\n");
+
+    printf("\n");
+    banner(progname);
+    return;
+}
+
+int main(int argc, char **argv)
+{
+    int             fd, rv;
+    float           lux;
+    char           *dev = "/dev/i2c-0";
+    char           *progname=NULL;
+
+    struct option long_options[] = {
+        {"device", required_argument, NULL, 'd'},
+        {"version", no_argument, NULL, 'v'},
+        {"help", no_argument, NULL, 'h'},
+        {NULL, 0, NULL, 0}
+    };
+
+    progname = basename(argv[0]);
+
+    /* Parser the command line parameters */
+    while ((rv = getopt_long(argc, argv, "d:vh", long_options, NULL)) != -1)
+    {
+        switch (rv)
+        {
+            case 'd': /*  Set I2C device path: /dev/i2c-1 */
+                dev = optarg;
+                break;
+
+            case 'v':  /*  Get software version */
+                banner(progname);
+                return EXIT_SUCCESS;
+
+            case 'h':  /*  Get help information */
+                program_usage(progname);
+                return 0;
+
+            default:
+                break;
+        }
+    }
+
+    /*+--------------------------------+
+     *|     open /dev/i2c-x device     |
+     *+--------------------------------+*/
+    if( (fd=open(dev, O_RDWR)) < 0)
+    {
+        printf("i2c device '%s' open failed: %s\n", dev, strerror(errno));
+        return -1;
+    }
+
+    while(1)
+    {
+        lux = tsl2561_get_lux(fd);
+
+        print_datime();
+        printf("TSLl2561 get lux: %.3f\n", lux);
+
+        sleep(1);
+    }
+
+    close(fd);
+    return 0;
+}
+
+static inline void print_datime(void)
+{
+    time_t         tmp;
+    struct tm     *p;
+
+    time(&tmp);
+
+    p=localtime(&tmp);
+
+
+    printf("%d-%02d-%02d %02d:%02d:%02d\t", (p->tm_year+1900),(p->tm_mon+1),
+            p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
+
+}
+
+static inline void msleep(unsigned long ms)
+{
+    struct timespec cSleep;
+    unsigned long ulTmp;
+
+    cSleep.tv_sec = ms / 1000;
+    if (cSleep.tv_sec == 0)
+    {
+        ulTmp = ms * 10000;
+        cSleep.tv_nsec = ulTmp * 100;
+    }
+    else
+    {
+        cSleep.tv_nsec = 0;
+    }
+
+    nanosleep(&cSleep, 0);
+    return ;
+}
+
+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.flags=0;  /* write */
+    msg.len= 1;
+    msg.buf= buf;
+
+    data.nmsgs= 1;
+    data.msgs= &msg;
+
+    msg.buf[0]=CONTROL_REG;
+    if( ioctl(fd, I2C_RDWR, &data) < 0 )
+    {
+        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+        return ;
+    }
+
+
+    if( cmd )
+        msg.buf[0]=POWER_UP;
+    else
+        msg.buf[0]=POWER_DOWN;
+
+    if( ioctl(fd, I2C_RDWR, &data) < 0 )
+    {
+        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+        return ;
+    }
+
+    return ;
+}
+
+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.flags=0;  /* write */
+    msg.len= 1;
+    msg.buf= buf;
+    msg.buf[0] = regaddr;
+
+    data.nmsgs= 1;
+    data.msgs= &msg;
+
+    if( ioctl(fd, I2C_RDWR, &data) < 0 )
+    {
+        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+        return -1;
+    }
+
+    memset(buf, 0, sizeof(buf));
+
+    msg.addr= TSL2561_I2C_ADDR;
+    msg.flags=I2C_M_RD;  /* read */
+    msg.len= 1;
+    msg.buf= buf;
+
+    data.nmsgs= 1;
+    data.msgs= &msg;
+
+    if( ioctl(fd, I2C_RDWR, &data) < 0 )
+    {
+        printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+        return -1;
+    }
+
+    *regval = msg.buf[0];
+    return 0;
+}
+
+float tsl2561_get_lux(int fd)
+{
+    int                 i;
+    unsigned char       reg_data[REG_COUNT];
+    unsigned char       buf;
+
+    int                 chn0_data = 0;
+    int                 chn1_data = 0;
+
+    float               div = 0.0;
+    float               lux = 0.0;
+
+    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(fd, regs_addr[i], &reg_data[i]);
+    }
+
+    chn0_data = reg_data[1]*256 + reg_data[0]; /* Channel0 = DATA0HIGH<<8 + DATA0LOW  */
+    chn1_data = reg_data[3]*256 + reg_data[2]; /* channel1 = DATA1HIGH<<8 +  DATA1LOW */
+
+    if( chn0_data<=0 || chn1_data<0 )
+    {
+        lux = 0.0;
+        goto OUT;
+    }
+
+    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);
+
+    else if( div>0.5 && div<=0.61 )
+        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;
+
+    else if( div>0.8 && div<=1.3 )
+        lux = 0.00146*chn0_data-0.00112*chn1_data;
+
+    else if( div>1.3 )
+        lux = 0.0;
+
+OUT:
+    tsl2561_power(fd, OFF);
+    return lux;
+}
+
+static inline void dump_buf(const char *prompt, char *buf, int size)
+{
+    int          i;
+
+    if( !buf )
+    {
+        return ;
+    }
+
+    if( prompt )
+    {
+        printf("%-32s ", prompt);
+    }
+
+    for(i=0; i<size; i++)
+    {
+        printf("%02x ", buf[i]);
+    }
+    printf("\n");
+
+    return ;
+}

--
Gitblit v1.9.1