Wiselib
wiselib.testing/external_interface/isense/isense_light_sensor.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_LIGHT_SENSOR_H
00020 #define CONNECTOR_ISENSE_LIGHT_SENSOR_H
00021 
00022 #include "config_testing.h"
00023 #include "external_interface/isense/isense_types.h"
00024 #include "util/base_classes/sensor_callback_base.h"
00025 #include <isense/os.h>
00026 #include <isense/modules/environment_module/environment_module.h>
00027 #include <isense/modules/environment_module/light_sensor.h>
00028 
00029 namespace wiselib
00030 {
00036    template<typename OsModel_P,
00037             int LIGHT_THRESHOLD = 30>
00038    class iSenseLightSensor
00039       : public SensorCallbackBase<OsModel_P, bool>,
00040          public isense::Uint32DataHandler,
00041          public isense::Task
00042    {
00043    public:
00044       typedef OsModel_P OsModel;
00045 
00046       typedef iSenseLightSensor<OsModel, LIGHT_THRESHOLD> self_t;
00047       typedef self_t* self_pointer_t;
00048 
00049       typedef bool value_t;
00050       // --------------------------------------------------------------------
00051       enum ErrorCodes
00052       {
00053          SUCCESS = OsModel::SUCCESS,
00054          ERR_UNSPEC = OsModel::ERR_UNSPEC
00055       };
00056       // --------------------------------------------------------------------
00057       enum States
00058       {
00059          READY = OsModel::READY,
00060          NO_VALUE = OsModel::NO_VALUE,
00061          INACTIVE = OsModel::INACTIVE
00062       };
00063       // --------------------------------------------------------------------
00064       iSenseLightSensor( isense::Os& os )
00065          : os_     ( os ),
00066             state_ ( INACTIVE )
00067       {
00068          em_ = new isense::EnvironmentModule( os );
00069 
00070          if (em_ != 0 )
00071          {
00072             if (em_->light_sensor() != 0)
00073             {
00074                em_->light_sensor()->set_data_handler(this);
00075                // register a task to be called in a second. Then, the
00076                // threshold mode is set one second after boot, to give the
00077                // sensor time for its first conversion
00078                os.add_task_in( isense::Time(1,0), this, 0 );
00079             }
00080             else
00081                os.fatal("Could not allocate light sensor");
00082 
00083             // enable the Environmental Sensor Module,
00084             // including its sensors
00085             em_->enable(true);
00086          }
00087          else
00088             os.fatal("Could not allocate env module");
00089       }
00090       // --------------------------------------------------------------------
00091       int state()
00092       {
00093          return state_;
00094       }
00095       // --------------------------------------------------------------------
00096       value_t operator()()
00097       {
00098          uint32 lux = em_->light_sensor()->luminance();
00099          return lux >= LIGHT_THRESHOLD;
00100       }
00101 
00102       uint32_t get_v() {
00103           return em_->light_sensor()->luminance();
00104       }
00105       // --------------------------------------------------------------------
00109       void handle_uint32_data( uint32 value )
00110       {
00111          uint32 lux = em_->light_sensor()->luminance();
00112 #ifdef DEBUG_ISENSE_LIGHT_SENSOR
00113          os().debug("Callback with light value: %d", lux);
00114 #endif
00115 
00116          if ( lux > LIGHT_THRESHOLD )
00117             this->notify_receivers(true);
00118          else
00119             this->notify_receivers(false);
00120       }
00121       // --------------------------------------------------------------------
00122       void execute( void* )
00123       {
00124          // enable the sensor threshold mode, and configure it
00125          // to call the handler upon changes of 60% and more
00126          em_->light_sensor()->enable_threshold_interrupt(true, 60);
00127          state_ = READY;
00128       }
00129 
00130 
00131    private:
00132       // --------------------------------------------------------------------
00133       isense::Os& os()
00134       { return os_; }
00135       // --------------------------------------------------------------------
00136       isense::Os& os_;
00137       isense::EnvironmentModule* em_;
00138       States state_;
00139    };
00140 }
00141 
00142 #endif
00143 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines