Scarab  v2.4.2
Project 8 C++ Utility Library
digital.hh
Go to the documentation of this file.
1 /*
2  * digital.h
3  *
4  * written by jared kofron <jared.kofron@gmail.com>
5  *
6  * functions for converting from D2A and A2D. prototypes are declared in
7  * digital.h. versions are included for both floats and doubles. there are
8  * two styles of the functions inspired by the GSL way of doing things - a
9  * normal version which returns the simple type of interest, and an 'error'
10  * version which can return information about bad arguments.
11  */
12 
13 #ifndef SCARAB_DIGITAL_HH_
14 #define SCARAB_DIGITAL_HH_
15 
16 #include "scarab_api.hh"
17 
18 #include <cmath>
19 #include <type_traits>
20 
21 namespace scarab
22 {
23 
25  {
26  unsigned bit_depth;
27  unsigned levels;
28  unsigned data_type_size;
29  double v_range;
30  double v_offset;
31  double inv_levels;
32  double inv_v_range;
33  double dac_gain;
35  };
36 
37 
38 
39  SCARAB_API void get_calib_params( unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, bool bits_r_aligned, dig_calib_params *params );
40  SCARAB_API void get_calib_params2( unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, double dac_gain, bool bits_r_aligned, dig_calib_params *params );
41 
42 
43  namespace detail {
44  enum class is_signed {};
45  enum class is_unsigned {};
46  }
47 
48  template< typename condition >
50 
51  template< typename condition >
53 
54  /*
55  * convert an unsigned digital <=64 bit value to a double or float.
56  */
57  template< typename dig_type, typename an_type,
59  an_type d2a( dig_type dig, const struct dig_calib_params* params )
60  {
61  return params->v_offset + params->dac_gain * ( an_type )dig;
62  }
63 
64  /*
65  * convert a signed digital <=64 bit value to a double or float.
66  */
67  template< typename dig_type, typename an_type,
69  an_type d2a( dig_type dig, const struct dig_calib_params* params )
70  {
71  return params->v_offset + params->dac_gain * (( an_type )dig + 0.5 * params->levels);
72  }
73 
74  /*
75  * convert an analog value to an unsigned digital value.
76  */
77  template< typename an_type, typename dig_type,
79  //typename = typename std::enable_if< std::is_unsigned<dig_type>::value, void >::type >
80  dig_type a2d( an_type analog, const struct dig_calib_params* params )
81  {
82  analog = ( analog - params->v_offset ) * params->inv_v_range * (an_type)(params->levels);
83  if( analog > (an_type)(params->levels - 1) ) analog = params->levels - 1;
84  else if( analog < 0. ) analog = 0.;
85  return (dig_type)std::round( analog );
86  }
87 
88  /*
89  * convert an analog value to a signed digital value.
90  */
91  template< typename an_type, typename dig_type,
93  //typename = typename std::enable_if< std::is_signed<dig_type>::value, void >::type >
94  dig_type a2d( an_type analog, const struct dig_calib_params* params )
95  {
96  analog = ( analog - params->v_offset ) * params->inv_v_range * (an_type)(params->levels);
97  if( analog > (an_type)(params->levels - 1) ) analog = params->levels - 1;
98  else if( analog < 0. ) analog = 0.;
99  return (dig_type)std::round( analog - 0.5 * params->levels );
100  }
101 
102 }
103 
104 #endif // SCARAB_DIGITAL_HH_
void get_calib_params2(unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, double dac_gain, bool bits_r_aligned, dig_calib_params *params)
Definition: digital.cc:32
#define SCARAB_API
Definition: scarab_api.hh:24
std::string type(const x_type &a_param)
Definition: typename.hh:22
typename std::enable_if< condition::value, detail::is_unsigned >::type enable_if_unsigned
Definition: digital.hh:52
typename std::enable_if< condition::value, detail::is_signed >::type enable_if_signed
Definition: digital.hh:49
dig_type a2d(an_type analog, const struct dig_calib_params *params)
Definition: digital.hh:80
void get_calib_params(unsigned n_bits, unsigned data_type_size, double v_offset, double v_range, bool bits_r_aligned, dig_calib_params *params)
Definition: digital.cc:18
an_type d2a(dig_type dig, const struct dig_calib_params *params)
Definition: digital.hh:59
To round(const std::chrono::duration< Rep, Period > &d)
Definition: date.h:1162