2015-02-04 21:13:44 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Zolertia - http://www.zolertia.com
|
|
|
|
* 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 Institute 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 INSTITUTE AND CONTRIBUTORS ``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 INSTITUTE OR CONTRIBUTORS 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2015-11-12 15:29:41 +00:00
|
|
|
* \addtogroup zoul-examples
|
2015-02-04 21:13:44 +00:00
|
|
|
* @{
|
|
|
|
*
|
2016-10-19 13:47:12 +00:00
|
|
|
* \defgroup zoul-tsl256x-test TSL256X light sensor test (TSL2561/TSL2563)
|
2015-02-04 21:13:44 +00:00
|
|
|
*
|
2016-10-19 13:47:12 +00:00
|
|
|
* Demonstrates the use of the TSL256X digital ambient light sensor
|
2015-02-04 21:13:44 +00:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2016-10-19 13:47:12 +00:00
|
|
|
* Test file for the external TSL256X light sensor
|
2015-02-04 21:13:44 +00:00
|
|
|
*
|
|
|
|
* \author
|
|
|
|
* Antonio Lignan <alinan@zolertia.com>
|
|
|
|
* Toni Lozano <tlozano@zolertia.com>
|
|
|
|
*/
|
2015-11-12 15:29:41 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-02-04 21:13:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "dev/i2c.h"
|
2016-01-06 23:41:14 +00:00
|
|
|
#include "dev/leds.h"
|
2016-10-19 13:47:12 +00:00
|
|
|
#include "dev/tsl256x.h"
|
2015-11-12 15:29:41 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-04 08:30:25 +00:00
|
|
|
/* Default sensor's integration cycle is 402ms */
|
2016-01-06 23:41:14 +00:00
|
|
|
#define SENSOR_READ_INTERVAL (CLOCK_SECOND)
|
2015-11-12 15:29:41 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-10-19 13:47:12 +00:00
|
|
|
PROCESS(remote_tsl256x_process, "TSL256X test process");
|
|
|
|
AUTOSTART_PROCESSES(&remote_tsl256x_process);
|
2015-02-04 21:13:44 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static struct etimer et;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-06 23:41:14 +00:00
|
|
|
void
|
|
|
|
light_interrupt_callback(uint8_t value)
|
|
|
|
{
|
|
|
|
printf("* Light sensor interrupt!\n");
|
2018-02-25 22:24:36 +00:00
|
|
|
leds_toggle(LEDS_RED);
|
2016-01-06 23:41:14 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-10-19 13:47:12 +00:00
|
|
|
PROCESS_THREAD(remote_tsl256x_process, ev, data)
|
2015-02-04 21:13:44 +00:00
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
2016-01-04 08:30:25 +00:00
|
|
|
static uint16_t light;
|
|
|
|
|
2016-10-19 13:47:12 +00:00
|
|
|
/* Print the sensor used, teh default is the TSL2561 (from Grove) */
|
|
|
|
if(TSL256X_REF == TSL2561_SENSOR_REF) {
|
|
|
|
printf("Light sensor test --> TSL2561\n");
|
|
|
|
} else if(TSL256X_REF == TSL2563_SENSOR_REF) {
|
|
|
|
printf("Light sensor test --> TSL2563\n");
|
|
|
|
} else {
|
|
|
|
printf("Unknown light sensor reference, aborting\n");
|
|
|
|
PROCESS_EXIT();
|
|
|
|
}
|
|
|
|
|
2016-01-04 08:30:25 +00:00
|
|
|
/* Use Contiki's sensor macro to enable the sensor */
|
2016-10-19 13:47:12 +00:00
|
|
|
SENSORS_ACTIVATE(tsl256x);
|
2015-02-04 21:13:44 +00:00
|
|
|
|
2016-01-06 23:41:14 +00:00
|
|
|
/* Default integration time is 402ms with 1x gain, use the below call to
|
2016-01-04 08:30:25 +00:00
|
|
|
* change the gain and timming, see tsl2563.h for more options
|
|
|
|
*/
|
2016-10-19 13:47:12 +00:00
|
|
|
/* tsl256x.configure(TSL256X_TIMMING_CFG, TSL256X_G16X_402MS); */
|
2016-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
/* Register the interrupt handler */
|
2016-10-19 13:47:12 +00:00
|
|
|
TSL256X_REGISTER_INT(light_interrupt_callback);
|
2016-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
/* Enable the interrupt source for values over the threshold. The sensor
|
|
|
|
* compares against the value of CH0, one way to find out the required
|
|
|
|
* threshold for a given lux quantity is to enable the DEBUG flag and see
|
|
|
|
* the CH0 value for a given measurement. The other is to reverse the
|
|
|
|
* calculations done in the calculate_lux() function. The below value roughly
|
|
|
|
* represents a 2500 lux threshold, same as pointing a flashlight directly
|
|
|
|
*/
|
2016-10-19 13:47:12 +00:00
|
|
|
tsl256x.configure(TSL256X_INT_OVER, 0x15B8);
|
2016-01-06 23:41:14 +00:00
|
|
|
|
|
|
|
/* And periodically poll the sensor */
|
2016-01-04 08:30:25 +00:00
|
|
|
|
2015-02-04 21:13:44 +00:00
|
|
|
while(1) {
|
|
|
|
etimer_set(&et, SENSOR_READ_INTERVAL);
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
2016-10-19 13:47:12 +00:00
|
|
|
light = tsl256x.value(TSL256X_VAL_READ);
|
|
|
|
if(light != TSL256X_ERROR) {
|
2016-01-06 23:41:14 +00:00
|
|
|
printf("Light = %u\n", (uint16_t)light);
|
|
|
|
} else {
|
2016-10-19 13:47:12 +00:00
|
|
|
printf("Error, enable the DEBUG flag in the tsl256x driver for info, ");
|
2016-01-06 23:41:14 +00:00
|
|
|
printf("or check if the sensor is properly connected\n");
|
2016-01-10 03:09:07 +00:00
|
|
|
PROCESS_EXIT();
|
2016-01-06 23:41:14 +00:00
|
|
|
}
|
2015-02-04 21:13:44 +00:00
|
|
|
}
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|