Skip to content
Snippets Groups Projects
Commit 6fcb9ad5 authored by danpetry's avatar danpetry
Browse files

tests/tsl4531x: Add tests to cover API changes.

The changes to the test correspond to the change in the API and design
of the driver, as described in the previous commit and in tsl4531x.h.
parent a1a834a7
No related branches found
No related tags found
No related merge requests found
BOARD ?= stm32f4discovery
include ../Makefile.tests_common include ../Makefile.tests_common
USEMODULE += tsl4531x USEMODULE += tsl4531x
USEMODULE += xtimer USEMODULE += xtimer
I2C_PORT ?= 0
CFLAGS += -DTSL4531_I2C_PORT=$(I2C_PORT)
include $(RIOTBASE)/Makefile.include include $(RIOTBASE)/Makefile.include
## About
This is a test application for the TSL4531x Lux sensor series.
## Usage
The application first initializes the TSL2561x sensor.
After initialization, the test application runs through a series of tests to
allow the user to manually verify the correct operation of the sensor driver.
/* /*
* Copyright (C) 2016 Inria * Copyright (C) 2016 Inria
* Copyright (C) 2018 Freie Universität Berlin
* *
* This file is subject to the terms and conditions of the GNU Lesser * This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level * General Public License v2.1. See the file LICENSE in the top level
...@@ -11,9 +12,9 @@ ...@@ -11,9 +12,9 @@
* @{ * @{
* *
* @file * @file
* @brief Test application for the TSL2561 Lux sensor * @brief Test application for the TSL4531x Lux sensor
* *
* @author Alexandre Abadie <alexandre.abadie@inria.fr> * @author Daniel Petry <daniel.petry@fu-berlin.de>
* *
* @} * @}
*/ */
...@@ -26,6 +27,7 @@ ...@@ -26,6 +27,7 @@
#include "periph/i2c.h" #include "periph/i2c.h"
#include "tsl4531x.h" #include "tsl4531x.h"
#include "tsl4531x_params.h"
#define _100ms_in_us (100 * 1000u) /* 1 second delay between printf */ #define _100ms_in_us (100 * 1000u) /* 1 second delay between printf */
...@@ -36,22 +38,79 @@ int main(void) ...@@ -36,22 +38,79 @@ int main(void)
puts("TSL4531x test application. Initializing..."); puts("TSL4531x test application. Initializing...");
if((err = tsl4531x_init(&dev, TSL4531_I2C_PORT, TSL4531x_INTEGRATE_200ms)) < 0) { if ((err = tsl4531x_init(&dev, tsl4531x_params)) < 0) {
printf("Error setting up device. %d (%s)\n", err, strerror(err)); printf("[Error] Device not initialised. Error code: %d\n", err);
return 1; return 1;
} }
puts("Initialized, will begin measurements."); puts("[Info] Initialized, beginning test.");
while (1) { while (1) {
int err;
uint16_t lux; uint16_t lux;
if ((err = tsl4531x_read(&dev, &lux)) < 0) { printf("-------------------------------------------------------------");
printf("Error reading from device. %d (%s)\n", err, strerror(err)); printf("-------------------------\n");
} else {
printf("Illuminance [lx]: %u\n", lux); /* Set into high power mode */
tsl4531x_set_low_power_mode(&dev, false);
/* Test simple read - high power mode */
lux = tsl4531x_simple_read(&dev);
printf("Illuminance | High power mode | Synchronous read |");
printf(" [lx] | %u\n", lux);
/* Determine the actual integration time - how long does it take for a
value to change?
Note that if the sensor value doesn't change between integration
cycles, this will sum the previous integration times. This mostly
won't happen, but it's best to let this run for a few cycles and take
the minimum. */
uint16_t lux_last = lux;
uint8_t changes = 0;
uint32_t change_times[2];
while (changes < 2) {
lux = tsl4531x_get_sample(&dev);
if (lux != lux_last) {
lux_last = lux;
change_times[changes] = xtimer_now_usec();
changes++;
}
} }
printf("Sample ready time | High power mode | From device |");
printf(" [us] | %lu\n", (unsigned long)(change_times[1] - change_times[0]));
/* Set into low power mode */
tsl4531x_set_low_power_mode(&dev, true);
/* This tests what happens when you read without asking for a sample in
low power mode. */
lux = tsl4531x_get_sample(&dev);
printf("Illuminance | Low power mode | Immediate read after mode change |");
printf(" [lx] | %u\n", lux);
xtimer_usleep(tsl4531x_time_until_sample_ready(&dev));
lux = tsl4531x_get_sample(&dev);
printf("Illuminance | Low power mode | One cycle time after mode change |");
printf(" [lx] | %u\n", lux);
/* Test synchronous read - low power mode */
lux = tsl4531x_simple_read(&dev);
printf("Illuminance | Low power mode | Synchronous read |");
printf(" [lx] | %u\n", lux);
/* Test asynchronous read - low power mode */
tsl4531x_start_sample(&dev);
/* Verify that the stated time until sample ready is reasonable. */
uint32_t t = tsl4531x_time_until_sample_ready(&dev);
xtimer_usleep(t);
lux = tsl4531x_get_sample(&dev);
printf("Illuminance | Low power mode | Asynchronous read |");
printf(" [lx] | %u\n", lux);
printf("Sample ready time | Low power mode | From driver |");
printf(" [us] | %lu\n", (unsigned long)t);
xtimer_usleep(_100ms_in_us); xtimer_usleep(_100ms_in_us);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment