1 /******************************************************************************* 2 * Copyright (c) 2014, 2017 IBM Corp. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License v1.0 6 * and Eclipse Distribution License v1.0 which accompany this distribution. 7 * 8 * The Eclipse Public License is available at 9 * http://www.eclipse.org/legal/epl-v10.html 10 * and the Eclipse Distribution License is available at 11 * http://www.eclipse.org/org/documents/edl-v10.php. 12 * 13 * Contributors: 14 * Ian Craggs - initial API and implementation and/or initial documentation 15 * Ian Craggs - change Timer member initialization to avoid copy constructor 16 *******************************************************************************/ 17 18 #if !defined(MQTT_MBED_H) 19 #define MQTT_MBED_H 20 21 #include "mbed.h" 22 23 class Countdown 24 { 25 public: Countdown()26 Countdown() : t() 27 { 28 29 } 30 Countdown(int ms)31 Countdown(int ms) : t() 32 { 33 countdown_ms(ms); 34 } 35 36 expired()37 bool expired() 38 { 39 return t.read_ms() >= interval_end_ms; 40 } 41 countdown_ms(unsigned long ms)42 void countdown_ms(unsigned long ms) 43 { 44 t.stop(); 45 interval_end_ms = ms; 46 t.reset(); 47 t.start(); 48 } 49 countdown(int seconds)50 void countdown(int seconds) 51 { 52 countdown_ms((unsigned long)seconds * 1000L); 53 } 54 left_ms()55 int left_ms() 56 { 57 return interval_end_ms - t.read_ms(); 58 } 59 60 private: 61 Timer t; 62 unsigned long interval_end_ms; 63 }; 64 65 #endif 66