1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/io1_xplained: add support for light sensor

This commit is contained in:
Alexandre Abadie 2018-04-17 10:10:25 +02:00
parent 367190c08d
commit 734256aab6
4 changed files with 45 additions and 4 deletions

View File

@ -152,6 +152,7 @@ endif
ifneq (,$(filter io1_xplained,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio
FEATURES_REQUIRED += periph_adc
USEMODULE += at30tse75x
USEMODULE += sdcard_spi
endif

View File

@ -53,11 +53,12 @@ enum {
IO1_XPLAINED_OK = 0, /**< Initialization successful */
IO1_XPLAINED_NOTEMP, /**< Error during temperature sensor initialization */
IO1_XPLAINED_NOSDCARD, /**< Error during sdcard initialization */
IO1_XPLAINED_NOLIGHT, /**< Error during light sensor (ADC) initialization */
IO1_XPLAINED_NOLED, /**< Error during extension LED initialization */
IO1_XPLAINED_NOGPIO1, /**< Error during extension GPIO1 initialization */
IO1_XPLAINED_NOGPIO2, /**< Error during extension GPIO2 initialization */
IO1_XPLAINED_READ_OK, /**< Temperature read successful */
IO1_XPLAINED_READ_ERR /**< Error when reading temperature sensor */
IO1_XPLAINED_READ_OK, /**< Light sensor read successful */
IO1_XPLAINED_READ_ERR /**< Error when reading light sensor */
};
/**
@ -91,6 +92,16 @@ typedef struct {
*/
int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params);
/**
* @brief Read light sensor level on the IO1 Xplained extension
*
* @param[out] light Light level value (between 0 and 1023)
*
* @return IO1_XPLAINED_READ_OK on success
* @return -IO1_XPLAINED_READ_ERR when the value cannot be read
*/
int io1_xplained_read_light_level(uint16_t *light);
#ifdef __cplusplus
}
#endif

View File

@ -22,7 +22,9 @@
#include "cpu.h"
#include "periph_cpu.h"
#include "periph/adc.h"
#include "periph/gpio.h"
#include "periph/spi.h"
#ifdef __cplusplus
extern "C" {
@ -50,6 +52,14 @@ extern "C" {
#define IO1_SDCARD_SPI_PARAM_DETECT GPIO_PIN(0,23)
/** @} */
/**
* @name IO1 Xplained Light sensor ADC configuration
* @{
*/
#define IO1_LIGHT_ADC_LINE ADC_LINE(0)
#define IO1_LIGHT_ADC_RES ADC_RES_10BIT
/** @} */
/**
* @name IO1 Xplained LED pin
* @{

View File

@ -24,7 +24,7 @@
#include "at30tse75x.h"
#include "sdcard_spi.h"
#include "periph/adc.h"
#include "periph/i2c.h"
#include "periph/gpio.h"
#define ENABLE_DEBUG (0)
@ -73,6 +73,11 @@ int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params)
}
}
if (adc_init(IO1_LIGHT_ADC_LINE) < 0) {
DEBUG("[io1_xplained] Light sensor (ADC) initialization failed\n");
return -IO1_XPLAINED_NOLIGHT;
}
if (gpio_init(IO1_LED_PIN, GPIO_OUT) < 0) {
DEBUG("[io1_xplained] LED initialization failed\n");
return -IO1_XPLAINED_NOLED;
@ -92,3 +97,17 @@ int io1_xplained_init(io1_xplained_t *dev, const io1_xplained_params_t *params)
return IO1_XPLAINED_OK;
}
int io1_xplained_read_light_level(uint16_t *light)
{
int sample = adc_sample(IO1_LIGHT_ADC_LINE, IO1_LIGHT_ADC_RES);
if (sample < 0) {
DEBUG("[io1_xplained] Light sensor read failed\n");
return -IO1_XPLAINED_READ_ERR;
}
*light = (1023 - sample);
return IO1_XPLAINED_READ_OK;
}