| | |
| | | #include <sys/types.h> |
| | | #include <sys/stat.h> |
| | | |
| | | #include "util_proc.h" |
| | | #include "logger.h" |
| | | #include "utils.h" |
| | | #include "tsl2561.h" |
| | | |
| | | |
| | |
| | | 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; |
| | | int ch0_data = 0; |
| | | int ch1_data = 0; |
| | | float ratio = 0.0; |
| | | |
| | | if( !lux ) |
| | | { |
| | |
| | | { |
| | | rv = tsl2561_readreg(fd, regs_addr[i], ®_data[i]); |
| | | if( rv < 0) |
| | | goto failed; |
| | | { |
| | | log_error("TSL2561 read register failed\n"); |
| | | goto cleanup; |
| | | } |
| | | } |
| | | |
| | | 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 */ |
| | | rv = 0; |
| | | ch0_data = (reg_data[1]<<8) + reg_data[0]; /* 通道0:可见光+红外 */ |
| | | ch1_data = (reg_data[3]<<8) + reg_data[2]; /* 通道1:仅红外 */ |
| | | |
| | | if( chn0_data<=0 || chn1_data<0 ) |
| | | { |
| | | rv = -2; |
| | | |
| | | /* 根据datasheet,饱和时光照强度很大 */ |
| | | if (ch0_data == 0xFFFF || ch1_data == 0xFFFF) { |
| | | *lux = 50000.0; |
| | | 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); |
| | | |
| | | 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 ) |
| | | 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); |
| | | |
| | | failed: |
| | | close(fd); |
| | | return rv; |
| | | } |