The Cayenne MQTT mbed Library provides functions to easily connect to the Cayenne IoT project builder.
Fork of Cayenne-MQTT-mbed by
src/Platform/mbed/MQTTTimer.h@0:09ef59d2d0f7, 2016-10-07 (annotated)
- Committer:
- jburhenn
- Date:
- Fri Oct 07 17:21:45 2016 +0000
- Revision:
- 0:09ef59d2d0f7
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jburhenn | 0:09ef59d2d0f7 | 1 | /* |
jburhenn | 0:09ef59d2d0f7 | 2 | The MIT License(MIT) |
jburhenn | 0:09ef59d2d0f7 | 3 | |
jburhenn | 0:09ef59d2d0f7 | 4 | Cayenne MQTT Client Library |
jburhenn | 0:09ef59d2d0f7 | 5 | Copyright (c) 2016 myDevices |
jburhenn | 0:09ef59d2d0f7 | 6 | |
jburhenn | 0:09ef59d2d0f7 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
jburhenn | 0:09ef59d2d0f7 | 8 | documentation files(the "Software"), to deal in the Software without restriction, including without limitation |
jburhenn | 0:09ef59d2d0f7 | 9 | the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, |
jburhenn | 0:09ef59d2d0f7 | 10 | and to permit persons to whom the Software is furnished to do so, subject to the following conditions : |
jburhenn | 0:09ef59d2d0f7 | 11 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
jburhenn | 0:09ef59d2d0f7 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
jburhenn | 0:09ef59d2d0f7 | 13 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR |
jburhenn | 0:09ef59d2d0f7 | 14 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
jburhenn | 0:09ef59d2d0f7 | 15 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jburhenn | 0:09ef59d2d0f7 | 16 | */ |
jburhenn | 0:09ef59d2d0f7 | 17 | |
jburhenn | 0:09ef59d2d0f7 | 18 | #ifndef _MQTTTIMER_h |
jburhenn | 0:09ef59d2d0f7 | 19 | #define _MQTTTIMER_h |
jburhenn | 0:09ef59d2d0f7 | 20 | |
jburhenn | 0:09ef59d2d0f7 | 21 | #include "mbed.h" |
jburhenn | 0:09ef59d2d0f7 | 22 | |
jburhenn | 0:09ef59d2d0f7 | 23 | /** |
jburhenn | 0:09ef59d2d0f7 | 24 | * Timer class for use with MQTTClient. |
jburhenn | 0:09ef59d2d0f7 | 25 | */ |
jburhenn | 0:09ef59d2d0f7 | 26 | class MQTTTimer : private Timer |
jburhenn | 0:09ef59d2d0f7 | 27 | { |
jburhenn | 0:09ef59d2d0f7 | 28 | public: |
jburhenn | 0:09ef59d2d0f7 | 29 | /** |
jburhenn | 0:09ef59d2d0f7 | 30 | * Construct a timer. |
jburhenn | 0:09ef59d2d0f7 | 31 | */ |
jburhenn | 0:09ef59d2d0f7 | 32 | MQTTTimer() : interval_end_ms(0) {}; |
jburhenn | 0:09ef59d2d0f7 | 33 | |
jburhenn | 0:09ef59d2d0f7 | 34 | /** |
jburhenn | 0:09ef59d2d0f7 | 35 | * Construct a timer and start it. |
jburhenn | 0:09ef59d2d0f7 | 36 | * @param[in] milliseconds Number of milliseconds to count down. |
jburhenn | 0:09ef59d2d0f7 | 37 | */ |
jburhenn | 0:09ef59d2d0f7 | 38 | MQTTTimer(int milliseconds) { |
jburhenn | 0:09ef59d2d0f7 | 39 | countdown_ms(milliseconds); |
jburhenn | 0:09ef59d2d0f7 | 40 | } |
jburhenn | 0:09ef59d2d0f7 | 41 | |
jburhenn | 0:09ef59d2d0f7 | 42 | /** |
jburhenn | 0:09ef59d2d0f7 | 43 | * Start countdown. |
jburhenn | 0:09ef59d2d0f7 | 44 | * @param[in] milliseconds Number of milliseconds to count down. |
jburhenn | 0:09ef59d2d0f7 | 45 | */ |
jburhenn | 0:09ef59d2d0f7 | 46 | void countdown_ms(int milliseconds) { |
jburhenn | 0:09ef59d2d0f7 | 47 | interval_end_ms = milliseconds; |
jburhenn | 0:09ef59d2d0f7 | 48 | reset(); |
jburhenn | 0:09ef59d2d0f7 | 49 | start(); |
jburhenn | 0:09ef59d2d0f7 | 50 | } |
jburhenn | 0:09ef59d2d0f7 | 51 | |
jburhenn | 0:09ef59d2d0f7 | 52 | /** |
jburhenn | 0:09ef59d2d0f7 | 53 | * Start countdown. |
jburhenn | 0:09ef59d2d0f7 | 54 | * @param[in] seconds Number of seconds to count down. |
jburhenn | 0:09ef59d2d0f7 | 55 | */ |
jburhenn | 0:09ef59d2d0f7 | 56 | void countdown(int seconds) { |
jburhenn | 0:09ef59d2d0f7 | 57 | countdown_ms(seconds * 1000); |
jburhenn | 0:09ef59d2d0f7 | 58 | } |
jburhenn | 0:09ef59d2d0f7 | 59 | |
jburhenn | 0:09ef59d2d0f7 | 60 | /** |
jburhenn | 0:09ef59d2d0f7 | 61 | * Get the number of milliseconds left in countdown. |
jburhenn | 0:09ef59d2d0f7 | 62 | * @return Number of milliseconds left. |
jburhenn | 0:09ef59d2d0f7 | 63 | */ |
jburhenn | 0:09ef59d2d0f7 | 64 | int left_ms() { |
jburhenn | 0:09ef59d2d0f7 | 65 | return interval_end_ms - read_ms(); |
jburhenn | 0:09ef59d2d0f7 | 66 | } |
jburhenn | 0:09ef59d2d0f7 | 67 | |
jburhenn | 0:09ef59d2d0f7 | 68 | /** |
jburhenn | 0:09ef59d2d0f7 | 69 | * The countdown timer has expired. |
jburhenn | 0:09ef59d2d0f7 | 70 | * @return true if countdown has expired, false otherwise. |
jburhenn | 0:09ef59d2d0f7 | 71 | */ |
jburhenn | 0:09ef59d2d0f7 | 72 | bool expired() { |
jburhenn | 0:09ef59d2d0f7 | 73 | return (interval_end_ms > 0L) && (read_ms() >= interval_end_ms); |
jburhenn | 0:09ef59d2d0f7 | 74 | } |
jburhenn | 0:09ef59d2d0f7 | 75 | |
jburhenn | 0:09ef59d2d0f7 | 76 | private: |
jburhenn | 0:09ef59d2d0f7 | 77 | unsigned long interval_end_ms; |
jburhenn | 0:09ef59d2d0f7 | 78 | }; |
jburhenn | 0:09ef59d2d0f7 | 79 | |
jburhenn | 0:09ef59d2d0f7 | 80 | #endif |