Doc fixes.
[bertos.git] / mware / mean.h
1 #warning revise me!
2
3
4 /**
5  *  DECLARE_SMEAN(temperature, uint8_t, uint16_t);
6  *  for (i = 0; i < TEMP_MEANS; ++i)
7  *    SMEAN_ADD(temperature, adc_get(), TEMP_MEANS);
8  *  printf("mean temperature = %d\n", SMEAN_GET(temperature));
9  */
10
11 /**
12  * Instantiate a mean instance
13  */
14 #define DECLARE_SMEAN(name, Type, SumType) \
15         struct { \
16                 SumType sum; \
17                 Type result; \
18                 int count; \
19         } name = { 0, 0, 0 }
20
21 /**
22  * Insert a new sample into the mean.
23  *
24  * \note \a mean and \a max_samples are evaluated multiple times
25  */
26 #define SMEAN_ADD(mean, sample, max_samples) \
27         do { \
28                 (mean).sum += (sample); \
29                 if ((mean).count++ >= (max_samples)) \
30                 { \
31                         (mean).result = (mean).sum / (max_samples); \
32                         (mean).sum = 0; \
33                         (mean).count = 0; \
34                 } \
35         } while (0)
36
37 /**
38  * Return current mean value.
39  */
40 #define SMEAN_GET(mean)  ((mean).result)
41