Work in progress MQTT example program on top of mbed OS 5

Dependencies:   C12832 MQTT

Fork of HelloMQTT by MQTT

Committer:
Jan Jongboom
Date:
Tue Jan 10 18:10:17 2017 -0600
Revision:
20:49c9daf2b0ff
Parent:
8:a3e3113054a1
Update to mbed OS 5, use NetworkInterface for multiple connectivity methods - not just ethernet

Who changed what in which revision?

UserRevisionLine numberNew 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