Wiselib
wiselib.testing/external_interface/feuerwhere/feuerwhere_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_FEUERWHERE_TIMER_H
00020 #define CONNECTOR_FEUERWHERE_TIMER_H
00021 
00022 //#include "external_interface/contiki/contiki_types.h"
00023 #include "external_interface/feuerwhere/feuerwhere_types.h"
00024 #include "util/delegates/delegate.hpp"
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 
00028 extern "C" {
00029 //TODO:
00030 // #include "sys/ctimer.h"
00031 #include "ktimer.h"
00032 //#include "timerdef.h"
00033 //#include "kernel.h"
00034 #include "utimer.h"
00035 #include "timelib.h"
00036 #include "flags.h"
00037 #include "msg.h"
00038 #include "thread.h"
00039 #include "clock.h"
00040 #include "callback-demon.h"
00041 #include "cfg-feuerware.h"
00042 //#include "powermon.h"
00043 #include "mutex.h"
00044 //#include "kernel.h"
00045 //#include "clock.h"
00046 //static mutex_t list_mutex;
00047 #define UTIMER_STACK_SIZE  400
00048 #define PRIORITY_UTIMER 22
00049 // drivers
00050 //#include "hal-board.h"
00051 
00052 }
00053 
00054 
00055 namespace wiselib
00056 {
00057 
00058    typedef delegate1<void, void*> feuerwhere_timer_delegate_t;
00059    // -----------------------------------------------------------------------
00060    struct FeuerwhereTimerItem
00061    {
00062       feuerwhere_timer_delegate_t cb;
00063       void *ptr;
00064       struct utimer ut;
00065 //    bool used;
00066    };
00067    // -----------------------------------------------------------------------
00068    void init_timer( void );
00069    FeuerwhereTimerItem* get_feuerwhere_timer_item( void );
00070    void feuerwhere_timer_thread(void);
00071 
00072    // -----------------------------------------------------------------------
00078    template<typename OsModel_P>
00079    class FeuerwhereTimerModel
00080    {
00081    public:
00082       typedef OsModel_P OsModel;
00083 
00084       typedef FeuerwhereTimerModel<OsModel> self_type;
00085       typedef self_type* self_pointer_t;
00086       typedef struct utimer *ut;
00087       typedef uint32_t millis_t;
00088       // --------------------------------------------------------------------
00089       enum ErrorCodes
00090       {
00091          SUCCESS = OsModel::SUCCESS,
00092          ERR_UNSPEC = OsModel::ERR_UNSPEC,
00093          ERR_NOTIMPL = OsModel::ERR_NOTIMPL
00094       };
00095 
00096       // -----------------------------------------------------------------
00097       template<typename T, void (T::*TMethod)(void*)>
00098       int set_timer( millis_t millis, T *obj_pnt, void *userdata )
00099       {
00100              FeuerwhereTimerItem *fti = get_feuerwhere_timer_item();
00101              // no free timer item left
00102 
00103              int secs= ((int)millis/1000) + 1;
00104 
00105               if ( !fti ){
00106                  printf ("ERORR configuring fti.....RETURNING.....\n");
00107                  return ERR_UNSPEC;
00108               }
00109 
00110              fti->cb = feuerwhere_timer_delegate_t::from_method<T, TMethod>( obj_pnt );
00111              utimer_set_wpid(&(fti->ut), (time_t)secs, pid , (void*)fti);
00112 
00113              printf ("Setting up a timer for %d\n", (int)userdata);
00114 
00115          return SUCCESS;
00116    };
00117 
00118 
00119    FeuerwhereTimerModel(void)
00120    {
00121     pid=0;
00122     printf("FeuerwhereTimerModel constructor\n");
00123     pid = thread_create(4000, 10, CREATE_STACKTEST, feuerwhere_timer_thread, "fw_timer_thread");
00124    };
00125 
00126    private:
00127       int pid;
00128    };
00129    // -----------------------------------------------------------------------
00130 
00131 }
00132 
00133 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines