Add use of lightsensor
This commit is contained in:
parent
58f49c7403
commit
a2b430f5e1
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
motion
|
||||
*.o
|
8
Makefile
Normal file
8
Makefile
Normal file
@ -0,0 +1,8 @@
|
||||
all:
|
||||
gcc -Wall -O2 -o TSL2561.o -c TSL2561.c
|
||||
gcc -Wall -O2 -o motion.o -c motion.c
|
||||
gcc -Wall -O2 -o motion -l wiringPi -l curl TSL2561.o motion.o
|
||||
make clean
|
||||
|
||||
clean:
|
||||
rm *.o > /dev/null 2>&1 &
|
458
TSL2561.c
Normal file
458
TSL2561.c
Normal file
@ -0,0 +1,458 @@
|
||||
/**
|
||||
* Copyright 2014 Dino Ciuffetti <dino@tuxweb.it>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include "TSL2561.h"
|
||||
|
||||
/**
|
||||
* read two bytes from i2c bus getting a 16 bit unsigned integer
|
||||
*/
|
||||
static inline uint16_t tsl2561_read16(TSL2561 *sensor, uint8_t reg) {
|
||||
uint16_t x;
|
||||
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ask for reading
|
||||
sensor->buf[0] = reg;
|
||||
if(write(sensor->adapter_fd, sensor->buf, 1) != 1) {
|
||||
sensor->lasterr = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(read(sensor->adapter_fd, sensor->buf, 2) != 2) {
|
||||
sensor->lasterr = errno;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//printf("x1: 0x%0x, x2: 0x%0x\n", sensor->buf[1], sensor->buf[0]);
|
||||
x = sensor->buf[1];
|
||||
x <<= 8;
|
||||
x |= sensor->buf[0];
|
||||
|
||||
//printf("test: 0x%02x%02x: 0x%04x\n", sensor->buf[1], sensor->buf[0], x);
|
||||
|
||||
return x;
|
||||
}
|
||||
/**
|
||||
* read one byte from i2c bus getting a 8 bit unsigned integer
|
||||
*/
|
||||
static inline uint8_t tsl2561_read8(TSL2561 *sensor, uint8_t reg) {
|
||||
uint8_t x;
|
||||
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ask for reading
|
||||
sensor->buf[0] = reg;
|
||||
if(write(sensor->adapter_fd, sensor->buf, 1) != 1) {
|
||||
sensor->lasterr = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(read(sensor->adapter_fd, sensor->buf, 1) != 1) {
|
||||
sensor->lasterr = errno;
|
||||
return 0;
|
||||
}
|
||||
x = sensor->buf[0];
|
||||
|
||||
return x;
|
||||
}
|
||||
/**
|
||||
* write one byte to i2c bus getting
|
||||
*/
|
||||
static inline int tsl2561_write8(TSL2561 *sensor, uint8_t reg, uint32_t byte_value) {
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// we mask with (& 0xFF) to get the last 8 bits from a 32 bit unsigned integer
|
||||
sensor->buf[0] = reg;
|
||||
sensor->buf[1] = (byte_value & 0xFF );
|
||||
if(write(sensor->adapter_fd, sensor->buf, 2) != 2) {
|
||||
sensor->lasterr = errno;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
// TSL2561 Functions (inspired on Adafruit_TSL2561_U.cpp at https://github.com/adafruit/Adafruit_TSL2561)
|
||||
// wake up TSL2561 by setting the control bit
|
||||
static inline int TSL2561_ON(TSL2561 *sensor) {
|
||||
int rc;
|
||||
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = tsl2561_write8(sensor, TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);
|
||||
return rc;
|
||||
}
|
||||
// turn TSL2561 into power saving mode
|
||||
static inline int TSL2561_OFF(TSL2561 *sensor) {
|
||||
int rc;
|
||||
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
rc = tsl2561_write8(sensor, TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF);
|
||||
return rc;
|
||||
}
|
||||
// this is a private function that is used to get data from the sensor (infrared + full spectrum including infrared)
|
||||
static inline int tsl2561_getdata(TSL2561 *sensor, uint16_t *full_spectrum, uint16_t *infrared) {
|
||||
TSL2561_ON(sensor);
|
||||
// wait for the internal ADC to complete conversion
|
||||
switch(sensor->integration_time) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
usleep(20000);
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_101MS:
|
||||
usleep(150000);
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_402MS:
|
||||
usleep(450000);
|
||||
break;
|
||||
}
|
||||
//usleep(450000);
|
||||
// reads two bytes from channel 0 (full spectrum + infrared)
|
||||
//*full_spectrum = wiringPiI2CReadReg16(_fd, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
|
||||
*full_spectrum = tsl2561_read16(sensor, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
|
||||
//fprintf(stdout, "got 0x%04X for full spectrum light\n", *full_spectrum);
|
||||
|
||||
// reads two bytes from channel 1 (infrared)
|
||||
//*infrared = wiringPiI2CReadReg16(_fd, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
|
||||
*infrared = tsl2561_read16(sensor, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
|
||||
//fprintf(stdout, "got 0x%04X for ir light\n", *infrared);
|
||||
|
||||
// turn the device off to save power
|
||||
TSL2561_OFF(sensor);
|
||||
|
||||
return 0;
|
||||
}
|
||||
/**
|
||||
* converts the raw sensor values to the standard SI lux equivalent.
|
||||
* returns 0 if the sensor is saturated and the values are unreliable.
|
||||
*/
|
||||
/**************************************************************************/
|
||||
static uint32_t TSL2561_CALCULATE_LUX(TSL2561 *sensor, uint16_t broadband, uint16_t ir) {
|
||||
unsigned long chScale;
|
||||
unsigned long channel1;
|
||||
unsigned long channel0;
|
||||
uint16_t clipThreshold;
|
||||
unsigned long ratio1 = 0;
|
||||
unsigned long ratio;
|
||||
unsigned int b, m;
|
||||
unsigned long temp;
|
||||
uint32_t lux;
|
||||
|
||||
// Make sure the sensor isn't saturated!
|
||||
switch (sensor->integration_time) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
clipThreshold = TSL2561_CLIPPING_13MS;
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_101MS:
|
||||
clipThreshold = TSL2561_CLIPPING_101MS;
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_402MS:
|
||||
clipThreshold = TSL2561_CLIPPING_402MS;
|
||||
break;
|
||||
default:
|
||||
clipThreshold = TSL2561_CLIPPING_402MS;
|
||||
break;
|
||||
}
|
||||
|
||||
// return 0 lux if the sensor is saturated
|
||||
if ((broadband > clipThreshold) || (ir > clipThreshold)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// get the correct scale depending on the intergration time
|
||||
switch (sensor->integration_time) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
chScale = TSL2561_LUX_CHSCALE_TINT0;
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_101MS:
|
||||
chScale = TSL2561_LUX_CHSCALE_TINT1;
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_402MS:
|
||||
chScale = (1 << TSL2561_LUX_CHSCALE);
|
||||
break;
|
||||
default: /* No scaling ... integration time = 402ms */
|
||||
chScale = (1 << TSL2561_LUX_CHSCALE);
|
||||
break;
|
||||
}
|
||||
|
||||
// scale for gain (1x or 16x)
|
||||
if (!sensor->gain) chScale = chScale << 4;
|
||||
|
||||
// scale the channel values
|
||||
channel0 = (broadband * chScale) >> TSL2561_LUX_CHSCALE;
|
||||
channel1 = (ir * chScale) >> TSL2561_LUX_CHSCALE;
|
||||
|
||||
/* find the ratio of the channel values (Channel1/Channel0) */
|
||||
if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0;
|
||||
|
||||
// round the ratio value
|
||||
ratio = (ratio1 + 1) >> 1;
|
||||
|
||||
#ifdef TSL2561_PACKAGE_CS
|
||||
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C))
|
||||
{b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;}
|
||||
else if (ratio <= TSL2561_LUX_K2C)
|
||||
{b=TSL2561_LUX_B2C; m=TSL2561_LUX_M2C;}
|
||||
else if (ratio <= TSL2561_LUX_K3C)
|
||||
{b=TSL2561_LUX_B3C; m=TSL2561_LUX_M3C;}
|
||||
else if (ratio <= TSL2561_LUX_K4C)
|
||||
{b=TSL2561_LUX_B4C; m=TSL2561_LUX_M4C;}
|
||||
else if (ratio <= TSL2561_LUX_K5C)
|
||||
{b=TSL2561_LUX_B5C; m=TSL2561_LUX_M5C;}
|
||||
else if (ratio <= TSL2561_LUX_K6C)
|
||||
{b=TSL2561_LUX_B6C; m=TSL2561_LUX_M6C;}
|
||||
else if (ratio <= TSL2561_LUX_K7C)
|
||||
{b=TSL2561_LUX_B7C; m=TSL2561_LUX_M7C;}
|
||||
else if (ratio > TSL2561_LUX_K8C)
|
||||
{b=TSL2561_LUX_B8C; m=TSL2561_LUX_M8C;}
|
||||
#else
|
||||
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T))
|
||||
{b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;}
|
||||
else if (ratio <= TSL2561_LUX_K2T)
|
||||
{b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;}
|
||||
else if (ratio <= TSL2561_LUX_K3T)
|
||||
{b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;}
|
||||
else if (ratio <= TSL2561_LUX_K4T)
|
||||
{b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;}
|
||||
else if (ratio <= TSL2561_LUX_K5T)
|
||||
{b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;}
|
||||
else if (ratio <= TSL2561_LUX_K6T)
|
||||
{b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;}
|
||||
else if (ratio <= TSL2561_LUX_K7T)
|
||||
{b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;}
|
||||
else if (ratio > TSL2561_LUX_K8T)
|
||||
{b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;}
|
||||
#endif
|
||||
|
||||
temp = ((channel0 * b) - (channel1 * m));
|
||||
|
||||
// do not allow negative lux value
|
||||
if (temp < 0) temp = 0;
|
||||
|
||||
// round lsb (2^(LUX_SCALE-1))
|
||||
temp += (1 << (TSL2561_LUX_LUXSCALE-1));
|
||||
|
||||
// strip off fractional portion
|
||||
lux = temp >> TSL2561_LUX_LUXSCALE;
|
||||
|
||||
// Signal I2C had no errors */
|
||||
return lux;
|
||||
}
|
||||
|
||||
|
||||
int TSL2561_SETINTEGRATIONTIME(TSL2561 *sensor, tsl2561IntegrationTime_t time) {
|
||||
int rc;
|
||||
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
TSL2561_ON(sensor);
|
||||
rc = tsl2561_write8(sensor, TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, time | sensor->gain);
|
||||
TSL2561_OFF(sensor);
|
||||
if(rc == 0) {
|
||||
sensor->integration_time = time;
|
||||
//fprintf(stderr, "setting integration time: 0x%02x to 0x%02x\n", TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, time | sensor->gain);
|
||||
return 0;
|
||||
} else {
|
||||
//fprintf(stderr, "Error setting integration time: 0x%02x to 0x%02x\n", TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, time | sensor->gain);
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int TSL2561_SETGAIN(TSL2561 *sensor, tsl2561Gain_t gain) {
|
||||
int rc;
|
||||
|
||||
if(sensor->adapter_fd == -1) { // not opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
TSL2561_ON(sensor);
|
||||
rc = tsl2561_write8(sensor, TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, sensor->integration_time | gain);
|
||||
TSL2561_OFF(sensor);
|
||||
if(rc == 0) {
|
||||
sensor->gain = gain;
|
||||
//fprintf(stderr, "setting gain: 0x%02x to 0x%02x\n", TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, sensor->integration_time | gain);
|
||||
return 0;
|
||||
} else {
|
||||
//fprintf(stderr, "Error setting gain: 0x%02x to 0x%02x\n", TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, sensor->integration_time | gain);
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
int TSL2561_OPEN(TSL2561 *sensor) {
|
||||
char filename[20];
|
||||
|
||||
if(sensor->adapter_fd != -1) { // already opened
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
snprintf(filename, 20, "/dev/i2c-%d", sensor->adapter_nr);
|
||||
if ((sensor->adapter_fd = open(filename, O_RDWR)) < 0) { // open the device file (requests i2c-dev kernel module loaded)
|
||||
sensor->lasterr = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (ioctl(sensor->adapter_fd, I2C_SLAVE, sensor->sensor_addr) < 0) { // talk to the requested device
|
||||
sensor->lasterr = errno;
|
||||
close(sensor->adapter_fd);
|
||||
sensor->adapter_fd = -1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
TSL2561_SETINTEGRATIONTIME(sensor, TSL2561_INTEGRATIONTIME_402MS);
|
||||
TSL2561_SETGAIN(sensor, TSL2561_GAIN_16X);
|
||||
|
||||
/*
|
||||
TSL2561_ON(sensor);
|
||||
tsl2561_write8(sensor, 0x81, 0x11);
|
||||
TSL2561_OFF(sensor);
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
void TSL2561_CLOSE(TSL2561 *sensor) {
|
||||
if(sensor->adapter_fd != -1) {
|
||||
close(sensor->adapter_fd);
|
||||
sensor->adapter_fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* sense the ambient light. Returns 0 on success, -1 on errors.
|
||||
* the parameter pointer fullspectrum is the quantity og light at full spectrum (including infrared)
|
||||
* the parameter pointer infrared is the quantity of infrared light
|
||||
* if autogain is 0 a single sensor reading is done with the gain and integration time previously selected by invoking
|
||||
* the TSL2561_SETINTEGRATIONTIME() and TSL2561_SETGAIN() functions. It autogain is 1 and automatic gain adjustment alghoritm is used
|
||||
*/
|
||||
int TSL2561_SENSELIGHT(TSL2561 *sensor, uint16_t *full_spectrum, uint16_t *infrared, uint32_t *lux, int autogain) {
|
||||
int rc=1;
|
||||
uint16_t fs, ir, hi, lo;
|
||||
//tsl2561Gain_t old_gain;
|
||||
|
||||
if(sensor->adapter_fd == -1) {
|
||||
// TODO: choose a valid errno error
|
||||
sensor->lasterr = -1;
|
||||
return -1;
|
||||
}
|
||||
if (autogain == 0) { // autogain not requested. Executing a single sensor read
|
||||
rc = tsl2561_getdata(sensor, full_spectrum, infrared);
|
||||
*lux = TSL2561_CALCULATE_LUX(sensor, *full_spectrum, *infrared);
|
||||
return rc;
|
||||
}
|
||||
|
||||
// autogain requested
|
||||
switch(sensor->integration_time) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
hi = TSL2561_AGC_THI_13MS;
|
||||
lo = TSL2561_AGC_TLO_13MS;
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_101MS:
|
||||
hi = TSL2561_AGC_THI_101MS;
|
||||
lo = TSL2561_AGC_TLO_101MS;
|
||||
break;
|
||||
case TSL2561_INTEGRATIONTIME_402MS:
|
||||
hi = TSL2561_AGC_THI_402MS;
|
||||
lo = TSL2561_AGC_TLO_402MS;
|
||||
break;
|
||||
default:
|
||||
hi = TSL2561_AGC_THI_402MS;
|
||||
lo = TSL2561_AGC_TLO_402MS;
|
||||
break;
|
||||
}
|
||||
|
||||
// save the old gain
|
||||
//old_gain = sensor->gain;
|
||||
|
||||
// try to adjust the gain
|
||||
rc = tsl2561_getdata(sensor, &fs, &ir);
|
||||
if(rc != 0) {
|
||||
return -1; // invalid read or sensor error
|
||||
}
|
||||
if ((fs < lo) && (sensor->gain == TSL2561_GAIN_1X)) { // light too low with this gain
|
||||
// raise the gain and redo the reading
|
||||
TSL2561_SETGAIN(sensor, TSL2561_GAIN_16X);
|
||||
//printf("gain raised\n");
|
||||
rc = tsl2561_getdata(sensor, &fs, &ir);
|
||||
// restore the previous gain
|
||||
//TSL2561_SETGAIN(sensor, old_gain);
|
||||
if(rc != 0) { // invalid read or sensor error
|
||||
return -1;
|
||||
} else {
|
||||
// now consider the reading valid after being adjusted
|
||||
*full_spectrum = fs;
|
||||
*infrared = ir;
|
||||
*lux = TSL2561_CALCULATE_LUX(sensor, *full_spectrum, *infrared);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if ((fs > hi) && (sensor->gain == TSL2561_GAIN_16X)) { // light too high with this gain
|
||||
// lower the gain and redo the reading
|
||||
TSL2561_SETGAIN(sensor, TSL2561_GAIN_1X);
|
||||
//printf("gain lowered\n");
|
||||
rc = tsl2561_getdata(sensor, &fs, &ir);
|
||||
// restore the previous gain
|
||||
//TSL2561_SETGAIN(sensor, old_gain);
|
||||
if(rc != 0) { // invalid read or sensor error
|
||||
return -1;
|
||||
} else {
|
||||
// now consider the reading valid after being adjusted
|
||||
*full_spectrum = fs;
|
||||
*infrared = ir;
|
||||
*lux = TSL2561_CALCULATE_LUX(sensor, *full_spectrum, *infrared);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
// the reading was valid without gain adjustment (or chip limits encountered!)
|
||||
*full_spectrum = fs;
|
||||
*infrared = ir;
|
||||
*lux = TSL2561_CALCULATE_LUX(sensor, *full_spectrum, *infrared);
|
||||
return 0;
|
||||
}
|
239
TSL2561.h
Normal file
239
TSL2561.h
Normal file
@ -0,0 +1,239 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* @file TSL2561.h
|
||||
* @oldauthor K. Townsend (Adafruit Industries)
|
||||
* @newauthor Dino Ciuffetti <dino@tuxweb.it>
|
||||
* @date: 13/mar/2014
|
||||
* @modified Modified and redistributed according the license by Dino Ciuffetti <dino@tuxweb.it>
|
||||
* @section OLDLICENSE
|
||||
*
|
||||
* Software License Agreement (BSD License)
|
||||
*
|
||||
* Copyright (c) 2013, Adafruit Industries
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holders nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* @section NEWLICENSE -- This software is redistributed as apache v2 license according to the old license
|
||||
*
|
||||
* Copyright 2014 Dino Ciuffetti <dino@tuxweb.it>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
**************************************************************************/
|
||||
#ifndef _TSL2561_H_
|
||||
#define _TSL2561_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <strings.h>
|
||||
#include <linux/i2c-dev.h>
|
||||
|
||||
#define TSL2561_VISIBLE 2 // channel 0 - channel 1
|
||||
#define TSL2561_INFRARED 1 // channel 1
|
||||
#define TSL2561_FULLSPECTRUM 0 // channel 0
|
||||
|
||||
// I2C address options
|
||||
#define TSL2561_ADDR_LOW (0x29)
|
||||
#define TSL2561_ADDR_FLOAT (0x39) // Default address (pin left floating)
|
||||
#define TSL2561_ADDR_HIGH (0x49)
|
||||
|
||||
// Lux calculations differ slightly for CS package
|
||||
//#define TSL2561_PACKAGE_CS
|
||||
#define TSL2561_PACKAGE_T_FN_CL
|
||||
|
||||
#define TSL2561_COMMAND_BIT (0x80) // Must be 1
|
||||
#define TSL2561_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear)
|
||||
#define TSL2561_WORD_BIT (0x20) // 1 = read/write word (rather than byte)
|
||||
#define TSL2561_BLOCK_BIT (0x10) // 1 = using block read/write
|
||||
|
||||
#define TSL2561_CONTROL_POWERON (0x03)
|
||||
#define TSL2561_CONTROL_POWEROFF (0x00)
|
||||
|
||||
#define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14
|
||||
#define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9
|
||||
#define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10
|
||||
#define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE
|
||||
#define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE
|
||||
|
||||
// T, FN and CL package values
|
||||
#define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE
|
||||
|
||||
// CS package values
|
||||
#define TSL2561_LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE
|
||||
#define TSL2561_LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE
|
||||
#define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE
|
||||
|
||||
// Auto-gain thresholds
|
||||
#define TSL2561_AGC_THI_13MS (4850) // Max value at Ti 13ms = 5047
|
||||
#define TSL2561_AGC_TLO_13MS (100)
|
||||
#define TSL2561_AGC_THI_101MS (36000) // Max value at Ti 101ms = 37177
|
||||
#define TSL2561_AGC_TLO_101MS (200)
|
||||
#define TSL2561_AGC_THI_402MS (63000) // Max value at Ti 402ms = 65535
|
||||
#define TSL2561_AGC_TLO_402MS (500)
|
||||
|
||||
// Clipping thresholds
|
||||
#define TSL2561_CLIPPING_13MS (4900)
|
||||
#define TSL2561_CLIPPING_101MS (37000)
|
||||
#define TSL2561_CLIPPING_402MS (65000)
|
||||
|
||||
enum
|
||||
{
|
||||
TSL2561_REGISTER_CONTROL = 0x00,
|
||||
TSL2561_REGISTER_TIMING = 0x01,
|
||||
TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02,
|
||||
TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03,
|
||||
TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04,
|
||||
TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05,
|
||||
TSL2561_REGISTER_INTERRUPT = 0x06,
|
||||
TSL2561_REGISTER_CRC = 0x08,
|
||||
TSL2561_REGISTER_ID = 0x0A,
|
||||
TSL2561_REGISTER_CHAN0_LOW = 0x0C,
|
||||
TSL2561_REGISTER_CHAN0_HIGH = 0x0D,
|
||||
TSL2561_REGISTER_CHAN1_LOW = 0x0E,
|
||||
TSL2561_REGISTER_CHAN1_HIGH = 0x0F
|
||||
};
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms
|
||||
TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms
|
||||
TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms
|
||||
} tsl2561IntegrationTime_t;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
TSL2561_GAIN_1X = 0x00, // No gain
|
||||
TSL2561_GAIN_16X = 0x10, // 16x gain
|
||||
} tsl2561Gain_t;
|
||||
|
||||
typedef struct TSL2561
|
||||
{
|
||||
int adapter_nr;
|
||||
tsl2561IntegrationTime_t integration_time;
|
||||
tsl2561Gain_t gain;
|
||||
int adapter_fd;
|
||||
uint8_t sensor_addr;
|
||||
int lasterr;
|
||||
uint8_t buf[10];
|
||||
} TSL2561;
|
||||
|
||||
|
||||
/* INITIALIZATION FUNCTIONS AND MACRO */
|
||||
/**
|
||||
* Prepare the sensor.
|
||||
* The first parameter is the raspberry pi i2c master controller attached to the TSL2561, the second is the i2c selection jumper.
|
||||
* The i2c selection address can be one of: TSL2561_ADDR_LOW, TSL2561_ADDR_FLOAT or TSL2561_ADDR_HIGH
|
||||
*/
|
||||
#define TSL2561_INIT(i2cadapter, lightaddr) { \
|
||||
.adapter_nr = i2cadapter, \
|
||||
.integration_time = TSL2561_INTEGRATIONTIME_402MS, \
|
||||
.gain = TSL2561_GAIN_16X, \
|
||||
.adapter_fd = -1, \
|
||||
.sensor_addr = lightaddr, \
|
||||
.lasterr = 0, \
|
||||
.buf = {0} \
|
||||
};
|
||||
/**
|
||||
* Initialize the sensor. Returns 0 on success, -1 on errors.
|
||||
*/
|
||||
int TSL2561_OPEN(TSL2561 *sensor);
|
||||
/**
|
||||
* Close the sensor.
|
||||
*/
|
||||
void TSL2561_CLOSE(TSL2561 *sensor);
|
||||
/**
|
||||
* Set the integration time. Returns 0 on success, -1 on errors.
|
||||
* TSL2561_INTEGRATIONTIME_402MS or TSL2561_INTEGRATIONTIME_101MS or TSL2561_INTEGRATIONTIME_13MS
|
||||
* TSL2561_INTEGRATIONTIME_402MS is slower but more precise, TSL2561_INTEGRATIONTIME_13MS is very fast but not so precise
|
||||
*/
|
||||
int TSL2561_SETINTEGRATIONTIME(TSL2561 *sensor, tsl2561IntegrationTime_t time);
|
||||
/**
|
||||
* Set the gain. Returns 0 on success, -1 on errors.
|
||||
* It can be TSL2561_GAIN_1X or TSL2561_GAIN_16X
|
||||
* Use 16X gain to get more precision in dark ambients, or enable auto gain with TSL2561_SENSELIGHT()
|
||||
*/
|
||||
int TSL2561_SETGAIN(TSL2561 *sensor, tsl2561Gain_t gain);
|
||||
/**
|
||||
* Sense the ambient light. Returns 0 on success, -1 on errors.
|
||||
* the parameter pointer fullspectrum is the quantity og light at full spectrum (including infrared)
|
||||
* the parameter pointer infrared is the quantity of infrared light
|
||||
* if autogain is 0 a single sensor reading is done with the gain and integration time previously selected by invoking
|
||||
* the TSL2561_SETINTEGRATIONTIME() and TSL2561_SETGAIN() functions. It autogain is 1 and automatic gain adjustment alghoritm is used
|
||||
*/
|
||||
int TSL2561_SENSELIGHT(TSL2561 *sensor, uint16_t *full_spectrum, uint16_t *infrared, uint32_t *lux, int autogain);
|
||||
|
||||
#endif
|
35
motion.c
35
motion.c
@ -1,9 +1,11 @@
|
||||
#include <unistd.h>
|
||||
#include <wiringPi.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <wiringPi.h>
|
||||
#include <curl/curl.h>
|
||||
#include "TSL2561.h"
|
||||
|
||||
#define MOTION_PIN 0
|
||||
#define SECONDS_TO_OFF 5*60
|
||||
@ -59,9 +61,32 @@ void light_control_script(int turn_on) {
|
||||
curl_global_cleanup();
|
||||
}
|
||||
|
||||
void setup_light_sensor(TSL2561* tsl) {
|
||||
int rc = TSL2561_OPEN(tsl);
|
||||
if(rc != 0) {
|
||||
fprintf(stderr, "Error initializing TSL2561 sensor (%d). Check your i2c bus (es. i2cdetect)\n", tsl->lasterr);
|
||||
// you don't need to TSL2561_CLOSE() if TSL2561_OPEN() failed, but it's safe doing it.
|
||||
TSL2561_CLOSE(tsl);
|
||||
return;
|
||||
}
|
||||
TSL2561_SETGAIN(tsl, TSL2561_GAIN_1X);
|
||||
TSL2561_SETINTEGRATIONTIME(tsl, TSL2561_INTEGRATIONTIME_101MS);
|
||||
}
|
||||
|
||||
uint32_t read_light_level(TSL2561* tsl) {
|
||||
uint32_t lux=0;
|
||||
uint16_t broadband, ir;
|
||||
TSL2561_SENSELIGHT(tsl, &broadband, &ir, &lux, 0);
|
||||
return lux;
|
||||
}
|
||||
|
||||
int main(){
|
||||
wiringPiSetup();
|
||||
pinMode(MOTION_PIN, INPUT);
|
||||
|
||||
TSL2561 light_sensor = TSL2561_INIT(1, TSL2561_ADDR_FLOAT);
|
||||
setup_light_sensor(&light_sensor);
|
||||
|
||||
while(1){
|
||||
pin_read = digitalRead(MOTION_PIN);
|
||||
|
||||
@ -80,8 +105,12 @@ int main(){
|
||||
|
||||
if(!state && pin_read){
|
||||
printf("Turning on\n");
|
||||
light_control_script(1);
|
||||
system("vcgencmd display_power 1");
|
||||
|
||||
if(read_light_level(&light_sensor) < 10) {
|
||||
light_control_script(1);
|
||||
}
|
||||
|
||||
//system("tvservice -p");
|
||||
//system("fbset -depth 8");
|
||||
//usleep(50*1000);
|
||||
@ -95,5 +124,7 @@ int main(){
|
||||
counter = 0;
|
||||
usleep(500*1000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user