Better timing

Dependencies:   FP MQTTPacket

Fork of MQTT by MQTT

Committer:
Jan Jongboom
Date:
Mon Sep 11 16:43:10 2017 +0200
Revision:
50:c37c8236e84a
Parent:
48:187c6d4d7812
Child:
52:3f9919941b86
Roll back f313853d22a6 and 08571008b958 - Usage of free'd memory. Needs investigation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 29:833386b16f3e 1 #if !defined(MQTT_MBED_H)
icraggs 29:833386b16f3e 2 #define MQTT_MBED_H
icraggs 29:833386b16f3e 3
icraggs 29:833386b16f3e 4 #include "mbed.h"
icraggs 29:833386b16f3e 5
icraggs 29:833386b16f3e 6 class Countdown
icraggs 29:833386b16f3e 7 {
icraggs 29:833386b16f3e 8 public:
icraggs 29:833386b16f3e 9 Countdown()
icraggs 29:833386b16f3e 10 {
Jan Jongboom 50:c37c8236e84a 11 t = Timer();
icraggs 29:833386b16f3e 12 }
Jan Jongboom 50:c37c8236e84a 13
icraggs 29:833386b16f3e 14 Countdown(int ms)
icraggs 29:833386b16f3e 15 {
Jan Jongboom 50:c37c8236e84a 16 t = Timer();
Jan Jongboom 50:c37c8236e84a 17 countdown_ms(ms);
icraggs 29:833386b16f3e 18 }
Jan Jongboom 50:c37c8236e84a 19
Jan Jongboom 50:c37c8236e84a 20
icraggs 29:833386b16f3e 21 bool expired()
icraggs 29:833386b16f3e 22 {
Jan Jongboom 50:c37c8236e84a 23 return t.read_ms() >= interval_end_ms;
icraggs 29:833386b16f3e 24 }
Jan Jongboom 50:c37c8236e84a 25
Jan Jongboom 50:c37c8236e84a 26 void countdown_ms(unsigned long ms)
icraggs 29:833386b16f3e 27 {
Jan Jongboom 50:c37c8236e84a 28 t.stop();
icraggs 29:833386b16f3e 29 interval_end_ms = ms;
Jan Jongboom 50:c37c8236e84a 30 t.reset();
Jan Jongboom 50:c37c8236e84a 31 t.start();
icraggs 29:833386b16f3e 32 }
Jan Jongboom 50:c37c8236e84a 33
icraggs 29:833386b16f3e 34 void countdown(int seconds)
icraggs 29:833386b16f3e 35 {
icraggs 43:21da1f744243 36 countdown_ms((unsigned long)seconds * 1000L);
icraggs 29:833386b16f3e 37 }
Jan Jongboom 50:c37c8236e84a 38
icraggs 29:833386b16f3e 39 int left_ms()
icraggs 29:833386b16f3e 40 {
Jan Jongboom 50:c37c8236e84a 41 return interval_end_ms - t.read_ms();
icraggs 29:833386b16f3e 42 }
Jan Jongboom 50:c37c8236e84a 43
icraggs 29:833386b16f3e 44 private:
Jan Jongboom 50:c37c8236e84a 45 Timer t;
Jan Jongboom 50:c37c8236e84a 46 unsigned long interval_end_ms;
icraggs 29:833386b16f3e 47 };
icraggs 29:833386b16f3e 48
icraggs 29:833386b16f3e 49 #endif