mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
drivers/vl6180x: add SAUL integration
This commit is contained in:
parent
add625aa84
commit
afb5e043be
78
drivers/saul/init_devs/auto_init_vl6180x.c
Normal file
78
drivers/saul/init_devs/auto_init_vl6180x.c
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Gunar Schorcht
|
||||
*
|
||||
* 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
|
||||
* directory for more details.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* @ingroup drivers_vl6180x
|
||||
* @ingroup sys_auto_init_saul
|
||||
* @brief Auto initialization of ST VL6180X Ranging and Ambient Light Sensor
|
||||
* @author Gunar Schorcht <gunar@schorcht.net>
|
||||
* @file
|
||||
*/
|
||||
|
||||
#ifdef MODULE_VL6180X
|
||||
|
||||
#include "assert.h"
|
||||
#include "log.h"
|
||||
#include "saul_reg.h"
|
||||
#include "vl6180x.h"
|
||||
#include "vl6180x_params.h"
|
||||
|
||||
/**
|
||||
* @brief Define the number of configured sensors
|
||||
*/
|
||||
#define VL6180X_NUM ARRAY_SIZE(vl6180x_params)
|
||||
|
||||
/**
|
||||
* @brief Allocate memory for the device descriptors
|
||||
*/
|
||||
static vl6180x_t vl6180x_devs[VL6180X_NUM];
|
||||
|
||||
/**
|
||||
* @brief Memory for the SAUL registry entries
|
||||
*/
|
||||
static saul_reg_t saul_entries[VL6180X_NUM * 2];
|
||||
|
||||
/**
|
||||
* @brief Define the number of saul info
|
||||
*/
|
||||
#define VL6180X_INFO_NUM ARRAY_SIZE(vl6180x_saul_info)
|
||||
|
||||
/**
|
||||
* @brief Reference the driver structs
|
||||
*/
|
||||
extern saul_driver_t vl6180x_saul_als_driver;
|
||||
extern saul_driver_t vl6180x_saul_rng_driver;
|
||||
|
||||
void auto_init_vl6180x(void)
|
||||
{
|
||||
assert(VL6180X_INFO_NUM == VL6180X_NUM);
|
||||
|
||||
for (unsigned i = 0; i < VL6180X_NUM; i++) {
|
||||
LOG_DEBUG("[auto_init_saul] initializing vl6180x #%u\n", i);
|
||||
|
||||
if (vl6180x_init(&vl6180x_devs[i], &vl6180x_params[i]) != VL6180X_OK) {
|
||||
LOG_ERROR("[auto_init_saul] error initializing vl6180x #%u\n", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
saul_entries[(i * 2)].dev = &(vl6180x_devs[i]);
|
||||
saul_entries[(i * 2)].name = vl6180x_saul_info[i].name;
|
||||
saul_entries[(i * 2)].driver = &vl6180x_saul_als_driver;
|
||||
saul_reg_add(&(saul_entries[(i * 2)]));
|
||||
|
||||
saul_entries[(i * 2) + 1].dev = &(vl6180x_devs[i]);
|
||||
saul_entries[(i * 2) + 1].name = vl6180x_saul_info[i].name;
|
||||
saul_entries[(i * 2) + 1].driver = &vl6180x_saul_rng_driver;
|
||||
saul_reg_add(&(saul_entries[(i * 2) + 1]));
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
typedef int dont_be_pedantic;
|
||||
#endif /* MODULE_VL6180X */
|
@ -335,4 +335,8 @@ void saul_init_devs(void)
|
||||
extern void auto_init_veml6070(void);
|
||||
auto_init_veml6070();
|
||||
}
|
||||
if (IS_USED(MODULE_VL6180X)) {
|
||||
extern void auto_init_vl6180x(void);
|
||||
auto_init_vl6180x();
|
||||
}
|
||||
}
|
||||
|
64
drivers/vl6180x/vl6180x_saul.c
Normal file
64
drivers/vl6180x/vl6180x_saul.c
Normal file
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (C) 2021 Gunar Schorcht
|
||||
*
|
||||
* 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
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup drivers_vl6180x
|
||||
* @brief VL6180X adaption to the RIOT actuator/sensor interface
|
||||
* @author Gunar Schorcht <gunar@schorcht.net>
|
||||
* @file
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "saul.h"
|
||||
#include "vl6180x.h"
|
||||
|
||||
#if IS_USED(MODULE_VL6180X_ALS)
|
||||
|
||||
static int read_als(const void *dev, phydat_t *res)
|
||||
{
|
||||
if (vl6180x_als_data_ready((vl6180x_t*)dev) == VL6180X_OK &&
|
||||
vl6180x_als_read((vl6180x_t*)dev, NULL,
|
||||
(uint16_t*)&res->val[0]) == VL6180X_OK) {
|
||||
res->unit = UNIT_LUX;
|
||||
res->scale = 0;
|
||||
return 1;
|
||||
}
|
||||
return -ECANCELED;
|
||||
}
|
||||
|
||||
const saul_driver_t vl6180x_saul_als_driver = {
|
||||
.read = read_als,
|
||||
.write = saul_write_notsup,
|
||||
.type = SAUL_SENSE_LIGHT,
|
||||
};
|
||||
|
||||
#endif /* IS_USED(MODULE_VL6180X_ALS) */
|
||||
|
||||
#if IS_USED(MODULE_VL6180X_RNG)
|
||||
|
||||
static int read_rng(const void *dev, phydat_t *res)
|
||||
{
|
||||
uint8_t mm;
|
||||
if (vl6180x_rng_data_ready((vl6180x_t*)dev) == VL6180X_OK) {
|
||||
vl6180x_rng_read((vl6180x_t*)dev, &mm);
|
||||
res->val[0] = mm;
|
||||
res->unit = UNIT_M;
|
||||
res->scale = -3;
|
||||
return 1;
|
||||
}
|
||||
return -ECANCELED;
|
||||
}
|
||||
|
||||
const saul_driver_t vl6180x_saul_rng_driver = {
|
||||
.read = read_rng,
|
||||
.write = saul_write_notsup,
|
||||
.type = SAUL_SENSE_DISTANCE,
|
||||
};
|
||||
|
||||
#endif /* IS_USED(MODULE_VL6180X_RNG) */
|
Loading…
Reference in New Issue
Block a user