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

Files at this revision

API Documentation at this revision

Comitter:
hlipka
Date:
Thu Feb 24 14:06:24 2011 +0000
Commit message:
initial version

Changed in this revision

ASyncTicker.cpp Show annotated file Show diff for this revision Revisions of this file
ASyncTicker.h Show annotated file Show diff for this revision Revisions of this file
Updateable.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ASyncTicker.cpp	Thu Feb 24 14:06:24 2011 +0000
@@ -0,0 +1,71 @@
+/*
+ * mbed ASyncTicker library
+* Copyright (c) 2010 Hendrik Lipka
+* 
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+* 
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+* 
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+
+#include "ASyncTicker.h"
+
+class TickerEntry
+{
+    public:
+        Timer timer;
+        Updateable *target;
+        int period;
+};
+
+ASyncTicker::ASyncTicker()
+{
+}
+
+void ASyncTicker::addTarget(Updateable *target, int period)
+{
+    TickerEntry *te=new TickerEntry();
+    te->target=target;
+    te->period=period;
+    _entries.push_back(te);
+}
+
+void ASyncTicker::run()
+{
+    list<TickerEntry*>::iterator it;
+    for (it=_entries.begin();it!=_entries.end();it++)
+    {
+        TickerEntry *te=*it;
+        te->timer.reset();
+        te->timer.start();
+    }
+    while (true)
+    {
+        list<TickerEntry*>::iterator it;
+        for (it=_entries.begin();it!=_entries.end();it++)
+        {
+            TickerEntry *te=*it;
+            if (te->timer.read()>te->period)
+            {
+                te->target->update();
+                te->timer.reset();
+            }
+        }
+        wait(1);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ASyncTicker.h	Thu Feb 24 14:06:24 2011 +0000
@@ -0,0 +1,65 @@
+/*
+ * mbed ASyncTicker library
+* Copyright (c) 2010 Hendrik Lipka
+* 
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+* 
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+* 
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+#ifndef __ASYNC_TICKER_H__
+#define __ASYNC_TICKER_H__
+
+#include "Updateable.h"
+#include <list>
+#include "mbed.h"
+
+using namespace std;
+
+class TickerEntry;
+
+/**
+    an asynchronous ticker which allows calling methods in a regular fashion, but not from an interrupt handler.
+    This allows for slower methods (e.g. printing to the serial console, doing network requests or complex 
+    calculation), without affecting other parts of the application.
+    
+    Note that the run method of this class never returns!
+    
+    Since it is using the Timer class, the period between each call cannot be longer than approx. 30 minutes.
+*/
+class ASyncTicker
+{
+    public:
+        ASyncTicker();
+        /**
+            add an object to be called
+            @params target the target object whose 'update' method is called
+            @params period the number of seconds between each call (not more then 30 minutes)
+        */
+        void addTarget(Updateable *target, int period);
+        
+        /**
+            starts the ticker
+            NOTW: this method never returns!
+        */
+        void run();
+    private:
+        list<TickerEntry*> _entries;
+};
+
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Updateable.h	Thu Feb 24 14:06:24 2011 +0000
@@ -0,0 +1,40 @@
+/*
+ * mbed ASyncTicker library
+* Copyright (c) 2010 Hendrik Lipka
+* 
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+* 
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+* 
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*/
+
+#ifndef __UPDATEABLE_H__
+#define __UPDATEABLE_H__
+
+/**
+    callback interface for classes which are subject to be called from the ASync ticker
+*/
+class Updateable
+{
+    public:
+        /**
+            virtual method which is called by the ticker
+        */
+        virtual void update()=0;
+};
+
+
+#endif