an asynchronous ticker which calls methods in a regular fashion, but not from an interrupt handler. This allows the called methods to do complex and more time-consuming stuff (e.g. network requests, calculation, or printing to the serial console)

Dependents:   IOT_Sockets

Committer:
hlipka
Date:
Thu Feb 24 14:06:24 2011 +0000
Revision:
0:13db8277ce26
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hlipka 0:13db8277ce26 1 /*
hlipka 0:13db8277ce26 2 * mbed ASyncTicker library
hlipka 0:13db8277ce26 3 * Copyright (c) 2010 Hendrik Lipka
hlipka 0:13db8277ce26 4 *
hlipka 0:13db8277ce26 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
hlipka 0:13db8277ce26 6 * of this software and associated documentation files (the "Software"), to deal
hlipka 0:13db8277ce26 7 * in the Software without restriction, including without limitation the rights
hlipka 0:13db8277ce26 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hlipka 0:13db8277ce26 9 * copies of the Software, and to permit persons to whom the Software is
hlipka 0:13db8277ce26 10 * furnished to do so, subject to the following conditions:
hlipka 0:13db8277ce26 11 *
hlipka 0:13db8277ce26 12 * The above copyright notice and this permission notice shall be included in
hlipka 0:13db8277ce26 13 * all copies or substantial portions of the Software.
hlipka 0:13db8277ce26 14 *
hlipka 0:13db8277ce26 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hlipka 0:13db8277ce26 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hlipka 0:13db8277ce26 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hlipka 0:13db8277ce26 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hlipka 0:13db8277ce26 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hlipka 0:13db8277ce26 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hlipka 0:13db8277ce26 21 * THE SOFTWARE.
hlipka 0:13db8277ce26 22 */
hlipka 0:13db8277ce26 23
hlipka 0:13db8277ce26 24 #ifndef __ASYNC_TICKER_H__
hlipka 0:13db8277ce26 25 #define __ASYNC_TICKER_H__
hlipka 0:13db8277ce26 26
hlipka 0:13db8277ce26 27 #include "Updateable.h"
hlipka 0:13db8277ce26 28 #include <list>
hlipka 0:13db8277ce26 29 #include "mbed.h"
hlipka 0:13db8277ce26 30
hlipka 0:13db8277ce26 31 using namespace std;
hlipka 0:13db8277ce26 32
hlipka 0:13db8277ce26 33 class TickerEntry;
hlipka 0:13db8277ce26 34
hlipka 0:13db8277ce26 35 /**
hlipka 0:13db8277ce26 36 an asynchronous ticker which allows calling methods in a regular fashion, but not from an interrupt handler.
hlipka 0:13db8277ce26 37 This allows for slower methods (e.g. printing to the serial console, doing network requests or complex
hlipka 0:13db8277ce26 38 calculation), without affecting other parts of the application.
hlipka 0:13db8277ce26 39
hlipka 0:13db8277ce26 40 Note that the run method of this class never returns!
hlipka 0:13db8277ce26 41
hlipka 0:13db8277ce26 42 Since it is using the Timer class, the period between each call cannot be longer than approx. 30 minutes.
hlipka 0:13db8277ce26 43 */
hlipka 0:13db8277ce26 44 class ASyncTicker
hlipka 0:13db8277ce26 45 {
hlipka 0:13db8277ce26 46 public:
hlipka 0:13db8277ce26 47 ASyncTicker();
hlipka 0:13db8277ce26 48 /**
hlipka 0:13db8277ce26 49 add an object to be called
hlipka 0:13db8277ce26 50 @params target the target object whose 'update' method is called
hlipka 0:13db8277ce26 51 @params period the number of seconds between each call (not more then 30 minutes)
hlipka 0:13db8277ce26 52 */
hlipka 0:13db8277ce26 53 void addTarget(Updateable *target, int period);
hlipka 0:13db8277ce26 54
hlipka 0:13db8277ce26 55 /**
hlipka 0:13db8277ce26 56 starts the ticker
hlipka 0:13db8277ce26 57 NOTW: this method never returns!
hlipka 0:13db8277ce26 58 */
hlipka 0:13db8277ce26 59 void run();
hlipka 0:13db8277ce26 60 private:
hlipka 0:13db8277ce26 61 list<TickerEntry*> _entries;
hlipka 0:13db8277ce26 62 };
hlipka 0:13db8277ce26 63
hlipka 0:13db8277ce26 64
hlipka 0:13db8277ce26 65 #endif