Blame view

RIOT/sys/include/analog_util.h 2.58 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  /*
   * 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.
   */
  
  /**
   * @defgroup    sys_analog_util Analog data conversion utilities
   * @ingroup     sys
   * @brief       Utility functions for converting analog data samples
   *
   * @{
   * @file
   * @brief       Analog utility function interfaces
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   */
  
  #ifndef ANALOG_UTIL_H
  #define ANALOG_UTIL_H
  
  #include "periph/adc.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @brief   Map a sampled ADC value to a given range
   *
   * This function is useful for converting sampled ADC values into their physical
   * representation.
   *
   * The min value is asserted to be smaller than the max value.
   *
   * @param[in] sample        sampled ADC value
   * @param[in] res           ADC resolution
   * @param[in] min           the lower bound of the target interval
   * @param[in] max           the upper bound of the target interval
   *
   * @return                  the mapped value
   */
  int adc_util_map(int sample, adc_res_t res, int min, int max);
  
  /**
   * @brief   Map a sampled ADC value to a given range (using floating point
   *          arithmetics)
   *
   * @see adc_util_map
   *
   * @param[in] sample        sampled ADC value
   * @param[in] res           ADC resolution
   * @param[in] min           the lower bound of the target interval
   * @param[in] max           the upper bound of the target interval
   *
   * @return                  the mapped value
   */
  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 /* ANALOG_UTIL_H */
  /** @} */