sys: s/adc_util/analog_util/ and added DAC mapping

This commit is contained in:
Hauke Petersen 2015-12-08 01:10:18 +01:00
parent 40ae604c3b
commit a1e3bb1bfc
4 changed files with 69 additions and 9 deletions

View File

@ -7,7 +7,7 @@
*/ */
/** /**
* @ingroup sys_adc_util * @ingroup sys_analog_util
* @{ * @{
* *
* @file * @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 */ /* keep a max value to ADC resolution mapping for quick access in the ROM */
static const int val_max[] = { static const int val_max[] = {
@ -28,7 +28,7 @@ static const int val_max[] = {
[ADC_RES_12BIT] = 0x0fff, [ADC_RES_12BIT] = 0x0fff,
[ADC_RES_14BIT] = 0x3fff, [ADC_RES_14BIT] = 0x3fff,
[ADC_RES_16BIT] = 0xffff [ADC_RES_16BIT] = 0xffff
} };
int adc_util_map(int sample, adc_res_t res, int min, int max) int adc_util_map(int sample, adc_res_t res, int min, int max)
{ {

View File

@ -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 <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <limits.h>
#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));
}

View File

@ -7,19 +7,19 @@
*/ */
/** /**
* @defgroup sys_adc_util ADC utilities * @defgroup sys_analog_util Analog data conversion utilities
* @ingroup sys * @ingroup sys
* @brief Utility functions for handling ADC samples * @brief Utility functions for converting analog data samples
* *
* @{ * @{
* @file * @file
* @brief ADC utility function interfaces * @brief Analog utility function interfaces
* *
* @author Hauke Petersen <hauke.petersen@fu-berlin.de> * @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/ */
#ifndef ADC_UTIL_H #ifndef ANALOG_UTIL_H
#define ADC_UTIL_H #define ANALOG_UTIL_H
#include "periph/adc.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); 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 #ifdef __cplusplus
} }
#endif #endif
#endif /* ADC_UTIL_H */ #endif /* ANALOG_UTIL_H */
/** @} */ /** @} */