From a1e3bb1bfc598a57a13e74b994910812c2d01741 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Tue, 8 Dec 2015 01:10:18 +0100 Subject: [PATCH] sys: s/adc_util/analog_util/ and added DAC mapping --- sys/{adc_util => analog_util}/Makefile | 0 sys/{adc_util => analog_util}/adc_util.c | 6 ++-- sys/analog_util/dac_util.c | 33 +++++++++++++++++++ sys/include/{adc_util.h => analog_util.h} | 39 +++++++++++++++++++---- 4 files changed, 69 insertions(+), 9 deletions(-) rename sys/{adc_util => analog_util}/Makefile (100%) rename sys/{adc_util => analog_util}/adc_util.c (94%) create mode 100644 sys/analog_util/dac_util.c rename sys/include/{adc_util.h => analog_util.h} (56%) diff --git a/sys/adc_util/Makefile b/sys/analog_util/Makefile similarity index 100% rename from sys/adc_util/Makefile rename to sys/analog_util/Makefile diff --git a/sys/adc_util/adc_util.c b/sys/analog_util/adc_util.c similarity index 94% rename from sys/adc_util/adc_util.c rename to sys/analog_util/adc_util.c index 1cd3293c3..af4f48023 100644 --- a/sys/adc_util/adc_util.c +++ b/sys/analog_util/adc_util.c @@ -7,7 +7,7 @@ */ /** - * @ingroup sys_adc_util + * @ingroup sys_analog_util * @{ * * @file @@ -18,7 +18,7 @@ * @} */ -#include "adc_util.h" +#include "analog_util.h" /* keep a max value to ADC resolution mapping for quick access in the ROM */ static const int val_max[] = { @@ -28,7 +28,7 @@ static const int val_max[] = { [ADC_RES_12BIT] = 0x0fff, [ADC_RES_14BIT] = 0x3fff, [ADC_RES_16BIT] = 0xffff -} +}; int adc_util_map(int sample, adc_res_t res, int min, int max) { diff --git a/sys/analog_util/dac_util.c b/sys/analog_util/dac_util.c new file mode 100644 index 000000000..fe934c153 --- /dev/null +++ b/sys/analog_util/dac_util.c @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2015 Freie Universität Berlin + * + * 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 sys_analog_util + * @{ + * + * @file + * @brief DAC utility function implementation + * + * @author Hauke Petersen + * + * @} + */ + +#include + +#include "analog_util.h" + +uint16_t dac_util_map(int value, int min, int max) +{ + return (uint16_t)((value - min) * UINT16_MAX) / (max - min); +} + +uint16_t dac_util_mapf(float value, float min, float max) +{ + return (uint16_t)(((value - min) * UINT16_MAX) / (max - min)); +} diff --git a/sys/include/adc_util.h b/sys/include/analog_util.h similarity index 56% rename from sys/include/adc_util.h rename to sys/include/analog_util.h index fc9d568b8..a1cd53fa6 100644 --- a/sys/include/adc_util.h +++ b/sys/include/analog_util.h @@ -7,19 +7,19 @@ */ /** - * @defgroup sys_adc_util ADC utilities + * @defgroup sys_analog_util Analog data conversion utilities * @ingroup sys - * @brief Utility functions for handling ADC samples + * @brief Utility functions for converting analog data samples * * @{ * @file - * @brief ADC utility function interfaces + * @brief Analog utility function interfaces * * @author Hauke Petersen */ -#ifndef ADC_UTIL_H -#define ADC_UTIL_H +#ifndef ANALOG_UTIL_H +#define ANALOG_UTIL_H #include "periph/adc.h" @@ -59,9 +59,36 @@ int adc_util_map(int sample, adc_res_t res, int min, int max); */ float adc_util_mapf(int sample, adc_res_t res, float min, float max); +/** + * @brief Map a value out of the given range to a 16-bit unsigned int + * + * The min value is assumed to be smaller than max value and value is assumed + * to be between min and max. + * + * @param[in] value value to map to a DAC set value + * @param[in] min the lower bound of the source interval + * @param[in] max the upper bound of the source interval + * + * @return the mapped value + */ +uint16_t dac_util_map(int value, int min, int max); + +/** + * @brief Helper function to map a given float value range to a valid DAC value. + * + * @see dac_util_map + * + * @param[in] value value to map to a DAC set value + * @param[in] min the lower bound of the source interval + * @param[in] max the upper bound of the source interval + * + * @return the mapped value + */ +uint16_t dac_util_mapf(float value, float min, float max); + #ifdef __cplusplus } #endif -#endif /* ADC_UTIL_H */ +#endif /* ANALOG_UTIL_H */ /** @} */