hsk-libs-dev  270
High Speed Karlsruhe XC878 library collection
hsk_filter.h
Go to the documentation of this file.
1 /** \file
2  * HSK Filter generator
3  *
4  * This file offers preprocessor macros to filter analogue values, by
5  * calculating the average of a set of a given length.
6  *
7  * The buffer for the filter is stored in xdata memory.
8  *
9  * @author kami
10  */
11 
12 #ifndef _HSK_FILTER_H_
13 #define _HSK_FILTER_H_
14 
15 #include <Infineon/XC878.h>
16 
17 #include <string.h> /* memset() */
18 
19 /**
20  * Generates a filter.
21  *
22  * The filter can be accessed with:
23  * - void <prefix>_init(void)
24  * - Initializes the filter with 0
25  * - <valueType> <prefix>_update(const <valueType> value)
26  * - Update the filter and return the current average
27  *
28  * @param prefix
29  * A prefix for the generated internals and functions
30  * @param valueType
31  * The data type of the stored values
32  * @param sumType
33  * A data type that can contain the sum of all buffered values
34  * @param sizeType
35  * A data type that can hold the length of the buffer
36  * @param size
37  * The length of the buffer
38  */
39 #define FILTER_FACTORY(prefix, valueType, sumType, sizeType, size) \
40  \
41  /**
42  * Holds the buffer and its current state.
43  */ \
44  struct { \
45  /**
46  * The value buffer.
47  */ \
48  valueType values[size]; \
49  \
50  /**
51  * The sum of the buffered values.
52  */ \
53  sumType sum; \
54  \
55  /**
56  * The index of the oldest buffered value.
57  */ \
58  sizeType current; \
59  } xdata prefix; \
60  \
61  /**
62  * Initializes the buffer with 0.
63  */ \
64  void prefix##_init(void) { \
65  memset(&prefix, 0, sizeof(prefix)); \
66  } \
67  \
68  /**
69  * Updates the filter and returns the current sliding average of
70  * buffered values.
71  *
72  * @param value
73  * The value to add to the buffer
74  * @return
75  * The average of the buffed values
76  */ \
77  valueType prefix##_update(const valueType value) { \
78  prefix.sum -= prefix.values[prefix.current]; \
79  prefix.values[prefix.current++] = value; \
80  prefix.sum += value; \
81  prefix.current %= size; \
82  return prefix.sum / size; \
83  } \
84 
85 
86 /**
87  * Generates a group of filters.
88  *
89  * The filters can be accessed with:
90  * - void <prefix>_init(void)
91  * - Initializes all filters with 0
92  * - <valueType> <prefix>_update(const ubyte filter, const <valueType> value)
93  * - Update the given filter and return the current average
94  *
95  * @param prefix
96  * A prefix for the generated internals and functions
97  * @param filters
98  * The number of filters
99  * @param valueType
100  * The data type of the stored values
101  * @param sumType
102  * A data type that can contain the sum of all buffered values
103  * @param sizeType
104  * A data type that can hold the length of the buffer
105  * @param size
106  * The length of the buffer
107  */
108 #define FILTER_GROUP_FACTORY(prefix, filters, valueType, sumType, sizeType, size) \
109  \
110  /**
111  * Holds the buffers and their current states.
112  */ \
113  struct { \
114  /**
115  * The value buffer.
116  */ \
117  valueType values[size]; \
118  \
119  /**
120  * The sum of the buffered values.
121  */ \
122  sumType sum; \
123  \
124  /**
125  * The index of the oldest buffered value.
126  */ \
127  sizeType current; \
128  } xdata prefix[filters]; \
129  \
130  /**
131  * Initializes all buffers with 0.
132  */ \
133  void prefix##_init(void) { \
134  memset(&prefix, 0, sizeof(prefix)); \
135  } \
136  \
137  /**
138  * Updates the given filter and returns the current sliding average of
139  * buffered values.
140  *
141  * @param filter
142  * The filter to update
143  * @param value
144  * The value to add to the buffer
145  * @return
146  * The average of the buffed values
147  */ \
148  valueType prefix##_update(const ubyte filter, const valueType value) { \
149  prefix[filter].sum -= prefix[filter].values[prefix[filter].current]; \
150  prefix[filter].values[prefix[filter].current++] = value; \
151  prefix[filter].sum += value; \
152  prefix[filter].current %= size; \
153  return prefix[filter].sum / size; \
154  } \
155 
156 
157 #endif /* _HSK_FILTER_H_ */
158