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
hlipka 0:13db8277ce26 25 #include "ASyncTicker.h"
hlipka 0:13db8277ce26 26
hlipka 0:13db8277ce26 27 class TickerEntry
hlipka 0:13db8277ce26 28 {
hlipka 0:13db8277ce26 29 public:
hlipka 0:13db8277ce26 30 Timer timer;
hlipka 0:13db8277ce26 31 Updateable *target;
hlipka 0:13db8277ce26 32 int period;
hlipka 0:13db8277ce26 33 };
hlipka 0:13db8277ce26 34
hlipka 0:13db8277ce26 35 ASyncTicker::ASyncTicker()
hlipka 0:13db8277ce26 36 {
hlipka 0:13db8277ce26 37 }
hlipka 0:13db8277ce26 38
hlipka 0:13db8277ce26 39 void ASyncTicker::addTarget(Updateable *target, int period)
hlipka 0:13db8277ce26 40 {
hlipka 0:13db8277ce26 41 TickerEntry *te=new TickerEntry();
hlipka 0:13db8277ce26 42 te->target=target;
hlipka 0:13db8277ce26 43 te->period=period;
hlipka 0:13db8277ce26 44 _entries.push_back(te);
hlipka 0:13db8277ce26 45 }
hlipka 0:13db8277ce26 46
hlipka 0:13db8277ce26 47 void ASyncTicker::run()
hlipka 0:13db8277ce26 48 {
hlipka 0:13db8277ce26 49 list<TickerEntry*>::iterator it;
hlipka 0:13db8277ce26 50 for (it=_entries.begin();it!=_entries.end();it++)
hlipka 0:13db8277ce26 51 {
hlipka 0:13db8277ce26 52 TickerEntry *te=*it;
hlipka 0:13db8277ce26 53 te->timer.reset();
hlipka 0:13db8277ce26 54 te->timer.start();
hlipka 0:13db8277ce26 55 }
hlipka 0:13db8277ce26 56 while (true)
hlipka 0:13db8277ce26 57 {
hlipka 0:13db8277ce26 58 list<TickerEntry*>::iterator it;
hlipka 0:13db8277ce26 59 for (it=_entries.begin();it!=_entries.end();it++)
hlipka 0:13db8277ce26 60 {
hlipka 0:13db8277ce26 61 TickerEntry *te=*it;
hlipka 0:13db8277ce26 62 if (te->timer.read()>te->period)
hlipka 0:13db8277ce26 63 {
hlipka 0:13db8277ce26 64 te->target->update();
hlipka 0:13db8277ce26 65 te->timer.reset();
hlipka 0:13db8277ce26 66 }
hlipka 0:13db8277ce26 67 }
hlipka 0:13db8277ce26 68 wait(1);
hlipka 0:13db8277ce26 69 }
hlipka 0:13db8277ce26 70 }
hlipka 0:13db8277ce26 71