From 0ce62b1a2fee1ff6b13b1d0557e5573ce829d709 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Fri, 05 Jul 2019 02:45:43 +0800
Subject: [PATCH] raspberrypi/modules/c source code for infrared, relay #pin set for Monitor RPi, and add tsl2561 lux sensor code

---
 modules/c/makefile   |    1 
 modules/c/tsl2561.c  |  130 +++++++++++++++++++++++++++++++++++++++++++
 modules/c/infrared.c |    4 
 modules/c/sht20.c    |    3 
 modules/c/tsl2561.h  |   38 ++++++++++++
 modules/c/relay.h    |    4 
 6 files changed, 174 insertions(+), 6 deletions(-)

diff --git a/modules/c/infrared.c b/modules/c/infrared.c
index 6475fc3..3ebbb99 100644
--- a/modules/c/infrared.c
+++ b/modules/c/infrared.c
@@ -15,8 +15,8 @@
 #include <unistd.h>
 #include <wiringPi.h>
 
-/* I/O Pin connected to PIN#16, BCM code pin number is 23 and wPi pin number is 4 */
-#define INFRARED_PIN           4
+/*Monitor RPi I/O Pin connected to PIN#38, and wPi pin number is 28 */
+#define INFRARED_PIN           28
 
 int main (int argc, char **argv)
 {
diff --git a/modules/c/makefile b/modules/c/makefile
index c4d4d6e..9eaad45 100644
--- a/modules/c/makefile
+++ b/modules/c/makefile
@@ -20,6 +20,7 @@
 
 LDFLAGS+=-lwiringPi
 LDFLAGS+=-lpthread
+LDFLAGS+=-lm
 
 VPATH= .
 SRCS = $(wildcard ${VPATH}/*.c)
diff --git a/modules/c/relay.h b/modules/c/relay.h
index af47779..c2d238c 100644
--- a/modules/c/relay.h
+++ b/modules/c/relay.h
@@ -19,8 +19,8 @@
 #define OFF                 0
 #endif
 
-/*   I/O Pin connected to PIN#18, BCM code pin number is 24 and wPi pin number is 5 */
-#define RELAY_PIN           5
+/* Moniter RPi I/O Pin connected to PIN#40, and wPi pin number is 29 */
+#define RELAY_PIN           29
 
 void relay_init(void);
 void turn_relay(int cmd);
diff --git a/modules/c/sht20.c b/modules/c/sht20.c
index 5395bbe..04a7219 100644
--- a/modules/c/sht20.c
+++ b/modules/c/sht20.c
@@ -17,8 +17,7 @@
 #include <sys/ioctl.h>
 #include <linux/types.h>
 #include <sys/stat.h>
-#include <linux/i2c.h>
-#include <linux/i2c-dev.h> 
+#include <linux/i2c-dev.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
diff --git a/modules/c/tsl2561.c b/modules/c/tsl2561.c
new file mode 100644
index 0000000..e88cd4b
--- /dev/null
+++ b/modules/c/tsl2561.c
@@ -0,0 +1,130 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2019 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
+ *
+ *        Version:  1.0.0(04/07/19)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "04/07/19 17:39:38"
+ *                 
+ ********************************************************************************/
+
+#include <wiringPi.h>
+#include <wiringPiI2C.h> 
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <math.h>
+#include <errno.h>
+
+#include "tsl2561.h"
+
+
+int tsl_fd = -1;
+
+static const int  regs_addr[REG_COUNT]={DATA0LOW, DATA0HIGH, DATA1LOW, DATA1HIGH};
+
+int tsl2561_init(void)
+{
+    if(tsl_fd > 0)
+        return 0;
+
+    tsl_fd = wiringPiI2CSetup(TSL2561_I2C_ADDR);
+    if(tsl_fd < 0)
+    {
+        printf("TSL2561 I2C setup failure: %s\n", strerror(errno));
+    }
+    else
+    {
+        printf("TSL2561 initialise ok, tsl_fd=%d\n", tsl_fd);
+    }
+
+
+    return tsl_fd;
+}
+
+
+int tsl2561_get_lux(void)
+{
+    int                 i;
+    int                 reg_data[REG_COUNT];
+
+    int                 chn0_data = 0;
+    int                 chn1_data = 0;
+
+    double              div = 0.0;
+    double              lux = 0.0;
+ 
+    /* Power up TSL2561 */
+    wiringPiI2CWrite(tsl_fd, CONTROL_REG);
+    wiringPiI2CWrite(tsl_fd, POWER_UP);
+
+    sleep(1);
+
+    /* Read register Channel0 and channel1 data from register */
+    for(i=0; i<REG_COUNT; i++)
+    {
+        wiringPiI2CWrite(tsl_fd, regs_addr[i]);
+        reg_data[i] = wiringPiI2CRead(tsl_fd);
+        //printf("write and read register: 0x%0x->%d\n", 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 )
+    {
+        //printf("TSL2561 get date chn0_data[%d] chn1_data[%d]\n", chn0_data, chn1_data);
+        lux = 0.0;
+        goto OUT;
+    }
+
+    div = (double)chn1_data / (double)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:
+    printf("TSLl2561 get lux: %.3f\n", lux);
+
+    wiringPiI2CWrite(tsl_fd, CONTROL_REG);
+    wiringPiI2CWrite(tsl_fd, POWER_DOWN);
+}
+
+
+int main(int argc, char **argv)
+{
+    int            interval = 1;
+
+    if( argc >= 2 )
+    {
+        interval = atoi(argv[1]);
+    }
+
+    tsl2561_init();
+
+    while(1)
+    {
+        tsl2561_get_lux();
+        sleep(interval);
+    }
+
+}
+
+
diff --git a/modules/c/tsl2561.h b/modules/c/tsl2561.h
new file mode 100644
index 0000000..ea85025
--- /dev/null
+++ b/modules/c/tsl2561.h
@@ -0,0 +1,38 @@
+/********************************************************************************
+ *      Copyright:  (C) 2019 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  tsl2561.h
+ *    Description:  This head file is the Lux sensor TSL2561 API functions on RaspberryPi
+ *
+ *        Version:  1.0.0(04/07/19)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "04/07/19 17:42:35"
+ *                 
+ ********************************************************************************/
+
+#ifndef  _TSL2561_H_
+#define  _TSL2561_H_
+
+#define TSL2561_I2C_ADDR                0x39
+
+#define CONTROL_REG                     0x80
+#define REG_COUNT                       4
+
+#define POWER_UP                        0x03
+#define POWER_DOWN                      0x00
+
+/* Register Address  */
+enum 
+{
+    /* Channel_0 = DATA0HIGH<<8 + DATA0LOW */
+    DATA0LOW = 0x8C, 
+    DATA0HIGH,
+
+    /* Channel_1 = DATA1HIGH<<8 + DATA1LOW */
+    DATA1LOW,
+    DATA1HIGH,
+};              
+
+#endif   /* ----- #ifndef _TSL2561_H_  ----- */
+

--
Gitblit v1.9.1