Wiselib
wiselib.testing/util/wisebed_node_api/sensors/sensor_controller.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 __UTIL_WISEBED_NODE_API_SENSOR_CONTROLLER_H
00020 #define __UTIL_WISEBED_NODE_API_SENSOR_CONTROLLER_H
00021 
00022 #include "util/wisebed_node_api/response_types.h"
00023 #include "util/wisebed_node_api/command_types.h"
00024 #include "util/delegates/delegate.hpp"
00025 #include "util/serialization/simple_types.h"
00026 #include "util/pstl/map_static_vector.h"
00027 #include "util/pstl/static_string.h"
00028 #include "util/pstl/pair.h"
00029 
00030 namespace wiselib
00031 {
00032 
00038    template<typename OsModel_P,
00039             typename SensorMap_P, // = typename MapStaticVector<typename OsModel_P, uint8_t, pair<typename String_P, delegate0<char*>, 10>,
00040             typename String_P = StaticString,
00041             typename Debug_P = typename OsModel_P::Debug>
00042    class SensorController
00043    {
00044    public:
00045       typedef OsModel_P OsModel;
00046       typedef SensorMap_P SensorMap;
00047       typedef String_P String;
00048       typedef Debug_P Debug;
00049 
00050       typedef SensorController<OsModel, SensorMap, String, Debug> self_type;
00051       typedef self_type* self_pointer_t;
00052 
00053       typedef delegate0<char*> sensor_delegate_t;
00054       // --------------------------------------------------------------------
00055       void init( Debug& debug )
00056       {
00057          debug_ = &debug;
00058       }
00059       // --------------------------------------------------------------------
00060       void add_sensor( uint8_t id, String name, sensor_delegate_t delegate )
00061       {
00062          pair<String, sensor_delegate_t> p( name, delegate );
00063          sensor_map_[id] = p;
00064       }
00065       // --------------------------------------------------------------------
00066       void delete_sensor( uint8_t id )
00067       {
00068          sensor_map_.erase( id );
00069       }
00070       // --------------------------------------------------------------------
00071       char* value( uint8_t id )
00072       {
00073          typename SensorMap::iterator it = sensor_map_.find( id );
00074          if ( it == sensor_map_.end() )
00075             return 0;
00076 
00077          return (it->second.second)();
00078       };
00079       // --------------------------------------------------------------------
00080       bool has_sensor( uint8_t id )
00081       {
00082          return sensor_map_.find( id ) != sensor_map_.end();
00083       };
00084       // --------------------------------------------------------------------
00085       String sensors( void )
00086       {
00087          String all_sensors;
00088 
00089          typename SensorMap::iterator it;
00090          for ( it =  sensor_map_.begin(); it !=  sensor_map_.end(); ++it )
00091          {
00092             all_sensors.append( it->second.first );
00093             all_sensors.append( "=" );
00094             char buf[4]; // max value is 255 (3 chars), plus '\0' = 4 chars
00095             sprintf( buf, "%d", (uint8_t)it->first );
00096             all_sensors.append( buf );
00097             all_sensors.append( "\n" );
00098          }
00099 
00100          return all_sensors;
00101       };
00102       // --------------------------------------------------------------------
00103       template<class T, char* (T::*TMethod)()>
00104       sensor_delegate_t create_sensor_delegate( T *obj_pnt )
00105       {
00106          return sensor_delegate_t::template from_method<T, TMethod>( obj_pnt );
00107       }
00108 
00109 
00110    private:
00111       Debug& debug()
00112       { return *debug_; }
00113       // --------------------------------------------------------------------
00114       typename Debug::self_pointer_t debug_;
00115       SensorMap sensor_map_;
00116    };
00117 }
00118 
00119 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines