Skip to content

Commit

Permalink
sys: s/adc_util/analog_util/ and added DAC mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
haukepetersen committed Mar 14, 2016
1 parent 40ae604 commit a1e3bb1
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions sys/adc_util/adc_util.c → sys/analog_util/adc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

/**
* @ingroup sys_adc_util
* @ingroup sys_analog_util
* @{
*
* @file
Expand All @@ -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[] = {
Expand All @@ -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)
{
Expand Down
33 changes: 33 additions & 0 deletions sys/analog_util/dac_util.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* @}
*/

#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));
}
39 changes: 33 additions & 6 deletions sys/include/adc_util.h → sys/include/analog_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/

#ifndef ADC_UTIL_H
#define ADC_UTIL_H
#ifndef ANALOG_UTIL_H
#define ANALOG_UTIL_H

#include "periph/adc.h"

Expand Down Expand Up @@ -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 */
/** @} */

0 comments on commit a1e3bb1

Please sign in to comment.