Pulse measurement to know signal occupancy
Diff: PwmReader.h
- Revision:
- 1:ebc39fb22351
- Parent:
- 0:15aa9d3aeb2e
--- a/PwmReader.h Wed Jan 25 08:52:00 2017 +0000 +++ b/PwmReader.h Thu Jan 26 08:26:22 2017 +0000 @@ -23,29 +23,87 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ - - #include "mbed.h" - + +#include "mbed.h" + +/*! + * Class PwmReader lmeasure the time spend in high and low state on a digital pin + * Uses useconds timer and IRQ + */ class PwmReader { public: + /** create a PwmReader object + * + * @param pin Pin numer to be observed + */ PwmReader(PinName pin) : _pin(pin) { init(); }; + /** Start the measurment + * + */ void start(); + /** Stop the measurment + * + */ void stop(); + /** return the time spend in low state + * + * @return the percentage of time spent in low state. value between 0.0 and 1.0 + */ float occupacyLow() { return 100.0 * _down / (_down + _high); }; + /** return the time spend in high state + * + * @return the percentage of time spent in high state. value between 0.0 and 1.0 + */ float occupacyHigh() { return 100.0 * _high / (_down + _high); }; - long down() { + /** return the time spend in low state + * + * @return the percentage of time spent in low state. value in useconds + */ + long down_us() { return _down; }; - float high() { + /** return the time spend in high state + * + * @return the percentage of time spent in high state. value in useconds + */ + long high_us() { return _high; }; + /** return the time spend in low state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in low state. value in mseconds + */ + long down_ms(bool round = false) { + return (_down + (round ? 500 : 0)) / 1000; + }; + /** return the time spend in high state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in high state. value in mseconds + */ + long high_ms(bool round = false) { + return (_high + (round ? 500 : 0)) / 1000; + }; + /** return the time spend in low state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in low state. value in seconds + */ + long down(bool round = false) { + return (_down + (round ? 500000 : 0)) / 1000000; + }; + /** return the time spend in high state + * @param round set round to tue to round the value (closest integer) - false by default + * @return the time spent in high state. value in seconds + */ + long high(bool round = false) { + return (_high + (round ? 500000 : 0)) / 1000000; + }; private: void init(); void pressedInt();