Wiselib
wiselib.testing/external_interface/trisos/trisos_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_TRISOS_TIMER_H
00020 #define CONNECTOR_TRISOS_TIMER_H
00021 
00022 #include "external_interface/trisos/trisos_types.h"
00023 #include "util/delegates/delegate.hpp"
00024 
00025 extern "C" {
00026 #include "contiki.h"
00027 #include "sys/etimer.h"
00028 }
00029 
00030 namespace wiselib
00031 {
00032    typedef delegate1<void, void*> trisos_timer_delegate_t;
00033    // -----------------------------------------------------------------------
00034    struct timer_item {
00035       struct timer_item *next;
00036       struct etimer etimer;
00037       struct process *p;
00038       trisos_timer_delegate_t cb;
00039       void *ptr;
00040    };
00041    // -----------------------------------------------------------------------
00042    void init_trisos_timer( void );
00043    extern timer_item* get_timer_item_ref( void );
00044    // -----------------------------------------------------------------------
00045    extern process timer_process;
00046    extern timer_item timer_item_;
00047    // -----------------------------------------------------------------------
00053    template<typename OsModel_P>
00054    class TriSOSTimer
00055    {
00056    public:
00057       typedef OsModel_P OsModel;
00058 
00059       typedef TriSOSTimer<OsModel> self_type;
00060       typedef self_type* self_pointer_t;
00061 
00062       typedef uint32_t millis_t;
00063       // --------------------------------------------------------------------
00064       enum ErrorCodes
00065       {
00066          SUCCESS = OsModel::SUCCESS,
00067          ERR_UNSPEC = OsModel::ERR_UNSPEC
00068       };
00069       // --------------------------------------------------------------------
00070       void init()
00071       {
00072          init_trisos_timer();
00073       }
00074       // --------------------------------------------------------------------
00075       template<typename T, void (T::*TMethod)(void*)>
00076       int set_timer( millis_t millis, T *obj_pnt, void *userdata );
00077 
00078    };
00079    // -----------------------------------------------------------------------
00080    // -----------------------------------------------------------------------
00081    // -----------------------------------------------------------------------
00082    template<typename OsModel_P>
00083    template<typename T, void (T::*TMethod)(void*)>
00084    int
00085    TriSOSTimer<OsModel_P>::
00086 	set_timer( millis_t millis, T *obj_pnt, void *userdata )
00087    {
00088       uint16_t ticks = (millis * CLOCK_SECOND) / 1000;
00089       if (ticks == 0)
00090          ticks = 1;
00091       timer_item *item = get_timer_item_ref();
00092       if ( !item )
00093          return ERR_UNSPEC;
00094       item->p = PROCESS_CURRENT();
00095       item->cb = trisos_timer_delegate_t::from_method<T, TMethod>( obj_pnt );
00096       item->ptr = userdata;
00097 
00098       PROCESS_CONTEXT_BEGIN( &timer_process );
00099       etimer_set( &item->etimer,  ticks );
00100       PROCESS_CONTEXT_END( &timer_process );
00101 
00102       return SUCCESS;
00103    }
00104 
00105 }
00106 
00107 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines