Wiselib
wiselib.stable/external_interface/isense/isense_timer.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef CONNECTOR_ISENSE_TIMER_H
00020 #define CONNECTOR_ISENSE_TIMER_H
00021 
00022 #include "external_interface/isense/isense_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 #include <isense/os.h>
00025 #include <isense/task.h>
00026 
00027 namespace wiselib
00028 {
00029    namespace
00030    {
00031       enum { MAX_INTERNAL_TIMERS = 10 };
00032       // --------------------------------------------------------------------
00033       typedef delegate1<void, void*> isense_timer_delegate_t;
00034       // --------------------------------------------------------------------
00035       class iSenseTimerCallback
00036          : public isense::Task
00037       {
00038       public:
00039          iSenseTimerCallback()
00040             : del_( isense_timer_delegate_t() )
00041          {}
00042          // -----------------------------------------------------------------
00043          iSenseTimerCallback( isense_timer_delegate_t del )
00044             : del_( del )
00045          {}
00046          // -----------------------------------------------------------------
00047          virtual ~iSenseTimerCallback()
00048          {}
00049          // -----------------------------------------------------------------
00050          virtual void execute( void *data )
00051          {
00052             if ( del_ )
00053                del_( data );
00054             del_ = isense_timer_delegate_t();
00055          }
00056          // -----------------------------------------------------------------
00057          inline bool is_empty()
00058          { return !del_; };
00059          // -----------------------------------------------------------------
00060          inline void clear()
00061          { del_ = isense_timer_delegate_t(); };
00062 
00063       private:
00064          isense_timer_delegate_t del_;
00065       };
00066       // --------------------------------------------------------------------
00067       iSenseTimerCallback isense_timer_callbacks[MAX_INTERNAL_TIMERS];
00068    }
00069    // -----------------------------------------------------------------------
00070    // -----------------------------------------------------------------------
00071    // -----------------------------------------------------------------------
00079    template<typename OsModel_P>
00080    class iSenseTimerModel
00081    {
00082    public:
00083       typedef OsModel_P OsModel;
00084 
00085       typedef iSenseTimerModel<OsModel> self_type;
00086       typedef self_type* self_pointer_t;
00087 
00088       typedef uint32 millis_t;
00089       // --------------------------------------------------------------------
00090       enum ErrorCodes
00091       {
00092          SUCCESS = OsModel::SUCCESS,
00093          ERR_UNSPEC = OsModel::ERR_UNSPEC
00094       };
00095       // --------------------------------------------------------------------
00096       iSenseTimerModel( isense::Os& os )
00097          : os_(os)
00098       {}
00099       // -----------------------------------------------------------------------
00100       template<typename T, void (T::*TMethod)(void*)>
00101       int set_timer( millis_t millis, T *obj_pnt, void *userdata );
00102 
00103    private:
00104       int get_free_timer_item();
00105       // -----------------------------------------------------------------------
00106       // -----------------------------------------------------------------------
00107       // -----------------------------------------------------------------------
00108       isense::Os& os_;
00109    };
00110    // -----------------------------------------------------------------------
00111    // -----------------------------------------------------------------------
00112    // -----------------------------------------------------------------------
00113    template<typename OsModel_P>
00114    template<typename T, void (T::*TMethod)(void*)>
00115    int
00116    iSenseTimerModel<OsModel_P>::
00117    set_timer( millis_t millis, T *obj_pnt, void *userdata )
00118    {
00119       int idx = get_free_timer_item();
00120       if ( idx < 0 )
00121          return ERR_UNSPEC;
00122 
00123       isense_timer_callbacks[idx] = iSenseTimerCallback( isense_timer_delegate_t::from_method<T, TMethod>( obj_pnt ) );
00124       // only return success if task has been added
00125       if ( os_.add_task_in( isense::Time(millis), &isense_timer_callbacks[idx], userdata ) )
00126          return SUCCESS;
00127 
00128       // if not successful, clear unused timer entry
00129       isense_timer_callbacks[idx].clear();
00130       return ERR_UNSPEC;
00131    }
00132    // -----------------------------------------------------------------------
00133    template<typename OsModel_P>
00134    int
00135    iSenseTimerModel<OsModel_P>::
00136    get_free_timer_item()
00137    {
00138       for ( int i = 0; i < MAX_INTERNAL_TIMERS; i++ )
00139       {
00140          if ( isense_timer_callbacks[i].is_empty() )
00141             return i;
00142       }
00143       return -1;
00144    }
00145 }
00146 
00147 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines