Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: util.cpp
- Revision:
- 0:15d3cdafd311
- Child:
- 1:9ebe670d8a19
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util.cpp Thu Dec 11 21:54:03 2014 +0000 @@ -0,0 +1,34 @@ +#include "mbed.h" +#include "util.h" + +uint16_t mvgAvg(uint16_t val, int reset) { + const uint32_t len = 25; + static uint16_t vals[len] = {0}; + static uint32_t ind = 0; + static uint32_t full = 0; + static uint32_t sum = 0; + + if (reset) { + ind = full = sum = 0; + return 0; + } + if (full) { + sum -= vals[ind]; + } + sum += val; + vals[ind] = val; + + ind++; + if (ind >= len) { + ind = 0; + full = 1; + } + if (full) { + return (uint16_t) (sum / len); + } + return (uint16_t) (sum / ind); +} + +int round(float val) { + return val > 0 ? ((int) (val + 0.5)) : ((int) (val - 0.5)); +}