Work in progress MQTT example program on top of mbed OS 5
Fork of HelloMQTT by
Linux-example/LinuxMQTT.h@8:a3e3113054a1, 2014-05-20 (annotated)
- Committer:
- icraggs
- Date:
- Tue May 20 15:08:05 2014 +0000
- Revision:
- 8:a3e3113054a1
Refactoring headers
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icraggs | 8:a3e3113054a1 | 1 | #if !defined(LINUXMQTT_H) |
icraggs | 8:a3e3113054a1 | 2 | #define LINUXMQTT_H |
icraggs | 8:a3e3113054a1 | 3 | |
icraggs | 8:a3e3113054a1 | 4 | class Countdown |
icraggs | 8:a3e3113054a1 | 5 | { |
icraggs | 8:a3e3113054a1 | 6 | public: |
icraggs | 8:a3e3113054a1 | 7 | Countdown() |
icraggs | 8:a3e3113054a1 | 8 | { |
icraggs | 8:a3e3113054a1 | 9 | |
icraggs | 8:a3e3113054a1 | 10 | } |
icraggs | 8:a3e3113054a1 | 11 | |
icraggs | 8:a3e3113054a1 | 12 | Countdown(int ms) |
icraggs | 8:a3e3113054a1 | 13 | { |
icraggs | 8:a3e3113054a1 | 14 | countdown_ms(ms); |
icraggs | 8:a3e3113054a1 | 15 | } |
icraggs | 8:a3e3113054a1 | 16 | |
icraggs | 8:a3e3113054a1 | 17 | |
icraggs | 8:a3e3113054a1 | 18 | bool expired() |
icraggs | 8:a3e3113054a1 | 19 | { |
icraggs | 8:a3e3113054a1 | 20 | struct timeval now, res; |
icraggs | 8:a3e3113054a1 | 21 | gettimeofday(&now, NULL); |
icraggs | 8:a3e3113054a1 | 22 | timersub(&end_time, &now, &res); |
icraggs | 8:a3e3113054a1 | 23 | //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000); |
icraggs | 8:a3e3113054a1 | 24 | //if (res.tv_sec > 0 || res.tv_usec > 0) |
icraggs | 8:a3e3113054a1 | 25 | // printf("expired %d %d\n", res.tv_sec, res.tv_usec); |
icraggs | 8:a3e3113054a1 | 26 | return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0); |
icraggs | 8:a3e3113054a1 | 27 | } |
icraggs | 8:a3e3113054a1 | 28 | |
icraggs | 8:a3e3113054a1 | 29 | |
icraggs | 8:a3e3113054a1 | 30 | void countdown_ms(int ms) |
icraggs | 8:a3e3113054a1 | 31 | { |
icraggs | 8:a3e3113054a1 | 32 | struct timeval now; |
icraggs | 8:a3e3113054a1 | 33 | gettimeofday(&now, NULL); |
icraggs | 8:a3e3113054a1 | 34 | struct timeval interval = {ms / 1000, (ms % 1000) * 1000}; |
icraggs | 8:a3e3113054a1 | 35 | //printf("interval %d %d\n", interval.tv_sec, interval.tv_usec); |
icraggs | 8:a3e3113054a1 | 36 | timeradd(&now, &interval, &end_time); |
icraggs | 8:a3e3113054a1 | 37 | } |
icraggs | 8:a3e3113054a1 | 38 | |
icraggs | 8:a3e3113054a1 | 39 | |
icraggs | 8:a3e3113054a1 | 40 | void countdown(int seconds) |
icraggs | 8:a3e3113054a1 | 41 | { |
icraggs | 8:a3e3113054a1 | 42 | struct timeval now; |
icraggs | 8:a3e3113054a1 | 43 | gettimeofday(&now, NULL); |
icraggs | 8:a3e3113054a1 | 44 | struct timeval interval = {seconds, 0}; |
icraggs | 8:a3e3113054a1 | 45 | timeradd(&now, &interval, &end_time); |
icraggs | 8:a3e3113054a1 | 46 | } |
icraggs | 8:a3e3113054a1 | 47 | |
icraggs | 8:a3e3113054a1 | 48 | |
icraggs | 8:a3e3113054a1 | 49 | int left_ms() |
icraggs | 8:a3e3113054a1 | 50 | { |
icraggs | 8:a3e3113054a1 | 51 | struct timeval now, res; |
icraggs | 8:a3e3113054a1 | 52 | gettimeofday(&now, NULL); |
icraggs | 8:a3e3113054a1 | 53 | timersub(&end_time, &now, &res); |
icraggs | 8:a3e3113054a1 | 54 | //printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000); |
icraggs | 8:a3e3113054a1 | 55 | return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000; |
icraggs | 8:a3e3113054a1 | 56 | } |
icraggs | 8:a3e3113054a1 | 57 | |
icraggs | 8:a3e3113054a1 | 58 | private: |
icraggs | 8:a3e3113054a1 | 59 | |
icraggs | 8:a3e3113054a1 | 60 | struct timeval end_time; |
icraggs | 8:a3e3113054a1 | 61 | }; |
icraggs | 8:a3e3113054a1 | 62 | |
icraggs | 8:a3e3113054a1 | 63 | |
icraggs | 8:a3e3113054a1 | 64 | #endif |