movingavg.h 685 B

1234567891011121314151617181920212223242526272829303132
  1. #ifndef MOVINGAVG_H_
  2. #define MOVINGAVG_H_
  3. #include "util.h"
  4. typedef uint16_t movingavg_t;
  5. typedef __uint24 movingavgsum_t;
  6. struct movingavg {
  7. uint8_t size;
  8. uint8_t count;
  9. uint8_t begin;
  10. uint8_t end;
  11. movingavgsum_t avgsum;
  12. movingavg_t buf[0];
  13. };
  14. #define DEFINE_MOVINGAVG(name, maxsize) \
  15. struct { \
  16. struct movingavg m; \
  17. movingavg_t buf[maxsize]; \
  18. } name
  19. movingavg_t _movingavg_calc(struct movingavg *m, movingavg_t newvalue);
  20. #define movingavg_calc(_m, _newvalue) _movingavg_calc(&((_m)->m), (_newvalue))
  21. void _movingavg_init(struct movingavg *m, uint8_t size);
  22. #define movingavg_init(_m, _size) _movingavg_init(&((_m)->m), (_size))
  23. #endif /* MOVINGAVG_H_ */