RaspberrPi project source code
guowenxue
6 days ago c87957cc22bf01c03635f23de15031f182c87bf8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*********************************************************************************
 *      Copyright:  (C) 2023 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  tsl2561.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:
 *              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
 *
 ********************************************************************************/
 
#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>
 
#include "logger.h"
#include "utils.h"
#include "tsl2561.h"
 
 
#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};
 
void tsl2561_power(int fd, int cmd)
{
    struct i2c_msg               msg;
    struct i2c_rdwr_ioctl_data   data;
    unsigned char                buf[2];
 
    msg.addr= TSL2561_I2CADDR;
    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 )
    {
        log_error("%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 )
    {
        log_error("%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_I2CADDR;
    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 )
    {
        log_error("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
    }
 
    memset(buf, 0, sizeof(buf));
 
    msg.addr= TSL2561_I2CADDR;
    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 )
    {
        log_error("%s() ioctl failure: %s\n", __func__, strerror(errno));
        return -1;
    }
 
    *regval = msg.buf[0];
    return 0;
}
 
int tsl2561_sample_lux(float *lux)
{
    int                 i, fd;
    int                 rv = 0;
    char               *dev = TSL2561_I2CDEV;
 
    unsigned char       reg_data[REG_COUNT];
    int                 ch0_data = 0;
    int                 ch1_data = 0;
    float               ratio = 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(fd, ON);
 
    msleep(410);  /* t(CONV) MAX 400ms */
 
    /* Read register Channel0 and channel1 data from register */
    for(i=0; i<REG_COUNT; i++)
    {
        rv = tsl2561_readreg(fd, regs_addr[i], &reg_data[i]);
        if( rv < 0)
        {
            log_error("TSL2561 read register failed\n");
            goto cleanup;
        }
    }
 
    rv = 0;
    ch0_data = (reg_data[1]<<8) + reg_data[0]; /* 通道0:可见光+红外 */
    ch1_data = (reg_data[3]<<8) + reg_data[2]; /* 通道1:仅红外 */
 
 
    /* 根据datasheet,饱和时光照强度很大 */
    if (ch0_data == 0xFFFF || ch1_data == 0xFFFF) {
        *lux = 50000.0;
        goto cleanup;
    }
 
    if (ch0_data == 0) { /* 无光照 */
        *lux = 0.0;
        goto cleanup;
    }
 
    ratio = (float)ch1_data / (float)ch0_data;
 
    if (ratio >= 0 && ratio <= 0.50) {
        *lux = 0.0304f * ch0_data - 0.062f * ch0_data * powf(ratio, 1.4f);
    }
    else if (ratio <= 0.61) {
        *lux = 0.0224f * ch0_data - 0.031f * ch1_data;
    }
    else if (ratio <= 0.80) {
        *lux = 0.0128f * ch0_data - 0.0153f * ch1_data;
    }
    else if (ratio <= 1.30) {
        *lux = 0.00146f * ch0_data - 0.00112f * ch1_data;
    }
    else { /* 超出有效范围 */
        *lux = 0.0;
    }
 
    if (*lux < 0) { /* 确保结果非负 */
        *lux = 0.0;
    }
 
cleanup:
    tsl2561_power(fd, OFF);
    close(fd);
    return rv;
}
 
/**
 * @brief 采样TSL2561光照强度数据
 * @param lux 输出参数,存储计算后的光照强度值
 * @return int 0成功,-1失败
 */
#define SAMPLE_COUNT 5
int tsl2561_get_lux(float *lux)
{
    float       samples[SAMPLE_COUNT];
    float       current_lux, temp;
    int         valid_samples = 0;
    int         i,j;
 
    if (!lux) {
        log_error("Invalid input argument\n");
        return -1;
    }
 
    /* 采样N次数据 */
    for (i=0; i<SAMPLE_COUNT; i++)
    {
        if (tsl2561_sample_lux(&current_lux) == 0) {
            samples[valid_samples++] = current_lux;
        }
 
        if (i < SAMPLE_COUNT-1) {
            msleep(100);
        }
    }
 
    if (valid_samples == 0)
    {
        log_error("All TSL2561 samples failed\n");
        return -2;
    }
 
    /* 只有一个有效采样,直接使用 */
    if (valid_samples == 1)
    {
        *lux = samples[0];
        return 0;
    }
 
    /* 对采样值进行排序 */
    for (i=0; i<valid_samples-1; i++) {
        for (j = i + 1; j < valid_samples; j++) {
            if (samples[i] > samples[j]) {
                temp = samples[i];
                samples[i] = samples[j];
                samples[j] = temp;
            }
        }
    }
 
    /* 取中位数 */
    *lux = samples[valid_samples / 2];
    return 0;
}