esp-open-rtos/examples/tsl4531/tsl4531_example.c

58 lines
1.3 KiB
C
Raw Permalink Normal View History

2017-03-20 16:50:46 +00:00
/*
* This sample code is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "i2c/i2c.h"
#include "task.h"
#include "tsl4531/tsl4531.h"
/* An example using the TSL4531 light sensor
* to read and print lux values from a sensor
* attached to GPIO pin 2 (SCL) and GPIO pin 0 (SDA)
* Connect 3.3v from the ESP to Vin and GND to GND
*/
2017-09-01 09:29:32 +00:00
#define I2C_BUS (0)
2017-03-20 16:50:46 +00:00
#define SCL_PIN (2)
#define SDA_PIN (0)
void tsl4531MeasurementTask(void *pvParameters)
{
tsl4531_t lightSensor;
2017-09-01 09:29:32 +00:00
lightSensor.i2c_dev.bus= I2C_BUS;
lightSensor.i2c_dev.addr= TSL4531_I2C_ADDR;
2017-03-20 16:50:46 +00:00
tsl4531_init(&lightSensor);
tsl4531_set_integration_time(&lightSensor, TSL4531_INTEGRATION_400MS);
tsl4531_set_power_save_skip(&lightSensor, true);
uint16_t lux = 0;
while (1)
{
if (tsl4531_read_lux(&lightSensor, &lux))
{
printf("Lux: %u\n", lux);
}
else
{
printf("Could not read data from TSL4531\n");
}
// 0.05 second delay
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
2017-09-01 09:29:32 +00:00
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
2017-03-20 16:50:46 +00:00
xTaskCreate(tsl4531MeasurementTask, "tsl4531MeasurementTask", 256, NULL, 2, NULL);
}