Wiselib
|
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 00020 #ifndef __UTIL_WISEBED_NODE_API_MANAGED_SENSOR_H 00021 #define __UTIL_WISEBED_NODE_API_MANAGED_SENSOR_H 00022 00023 #include "util/wisebed_node_api/sensors/sensor_encoding.h" 00024 #include "util/serialization/simple_types.h" 00025 #include "util/pstl/static_string.h" 00026 #include "util/delegates/delegate.hpp" 00027 00028 namespace wiselib 00029 { 00030 template<typename OsModel_P, 00031 typename Sensor_P, 00032 typename String_P = StaticString> 00033 class ManagedSensor 00034 { 00035 public: 00036 typedef OsModel_P OsModel; 00037 typedef typename OsModel::AppMainParameter AppMainParameter; 00038 typedef String_P String; 00039 typedef Sensor_P Sensor; 00040 typedef typename Sensor_P::value_t value_t; 00041 00042 typedef ManagedSensor<OsModel, Sensor, String> self_type; 00043 typedef self_type* self_pointer_t; 00044 00045 typedef delegate0<char*> sensor_delegate_t; 00046 // -------------------------------------------------------------------- 00047 enum ErrorCodes 00048 { 00049 SUCCESS = Sensor::SUCCESS, 00050 ERR_UNSPEC = Sensor::ERR_UNSPEC 00051 }; 00052 // -------------------------------------------------------------------- 00053 enum States 00054 { 00055 READY = Sensor::READY, 00056 NO_VALUE = Sensor::NO_VALUE, 00057 INACTIVE = Sensor::INACTIVE 00058 }; 00059 // -------------------------------------------------------------------- 00060 void init( Sensor& sensor, String name ) 00061 { 00062 sensor_ = &sensor; 00063 name_ = name; 00064 } 00065 // -------------------------------------------------------------------- 00066 void init_with_facetprovider( AppMainParameter& app, String name ) 00067 { 00068 sensor_ = &wiselib::FacetProvider<OsModel, Sensor>::get_facet( app ); 00069 name_ = name; 00070 } 00071 // -------------------------------------------------------------------- 00072 int state() 00073 { 00074 return sensor_->state(); 00075 } 00076 // -------------------------------------------------------------------- 00077 value_t operator()() 00078 { 00079 return (*sensor_)(); 00080 } 00081 00082 uint32_t get_v() 00083 { 00084 return (*sensor_).get_v(); 00085 } 00086 00087 // -------------------------------------------------------------------- 00088 char* encoded_value() 00089 { 00090 // write sensor value with the aid of serialization to buffer, 00091 // starting at pos 2 00092 value_t value = (*sensor_)(); 00093 int len = write<OsModel, uint8_t, value_t>( &buffer_[2], value ); 00094 // write header 00095 buffer_[0] = SensorEncoding<value_t>::encoding(); 00096 buffer_[1] = len; // len of sensor value 00097 00098 return (char*)buffer_; 00099 } 00100 // -------------------------------------------------------------------- 00101 char* name() 00102 { 00103 return name_.c_str(); 00104 } 00105 // -------------------------------------------------------------------- 00106 sensor_delegate_t sensor_delegate() 00107 { 00108 return sensor_delegate_t::template from_method<self_type, &self_type::encoded_value>( this ); 00109 } 00110 00111 private: 00112 typename Sensor::self_pointer_t sensor_; 00113 String name_; 00114 uint8_t buffer_[32]; 00115 }; 00116 } 00117 #endif /* _MANAGED_SENSOR_H */ 00118