Wiselib
wiselib.testing/external_interface/pc/roomba_event_sensor.h
Go to the documentation of this file.
00001 
00002 #ifndef ROOMBA_EVENT_SENSOR_H
00003 #define ROOMBA_EVENT_SENSOR_H
00004 
00005 #include "util/delegates/delegate.hpp"
00006 
00007 #include <iostream>
00008 
00009 namespace wiselib {
00010    
00011    template<typename OsModel_P,
00012       typename Roomba_P
00013    >
00014    class RoombaEventSensor {
00015       public:
00016          typedef OsModel_P OsModel;
00017          typedef Roomba_P Roomba;
00018          typedef delegate1<void, uint8_t> event_callback_t;
00019          typedef RoombaEventSensor<OsModel, Roomba> self_type;
00020          typedef self_type* self_pointer_t;
00021          
00022          enum Event {
00023             EVENT_NONE = 0x0, EVENT_WALL = 0x1
00024          };
00025          
00026          enum ReturnValues {
00027             SUCCESS = OsModel::SUCCESS, ERR_UNSPEC = OsModel::ERR_UNSPEC
00028          };
00029          
00030          int init(Roomba& roomba);
00031          int init();
00032          int destruct();
00033          
00034          template<typename T, void (T::*TMethod)(uint8_t)>
00035          int reg_event_callback(T* obj);
00036          
00037          void on_new_data();
00038          
00039       private:
00040          typename Roomba::self_pointer_t roomba_;
00041          event_callback_t event_callback_;
00042          uint8_t bumps_;
00043    };
00044    
00045    template<typename OsModel_P, typename Roomba_P>
00046    int RoombaEventSensor<OsModel_P, Roomba_P>::
00047 	init(Roomba_P& roomba) {
00048       roomba_ = &roomba;
00049       roomba_->template reg_new_data_callback<
00050          self_type, &self_type::on_new_data
00051       >(this);
00052       return SUCCESS;
00053    }
00054    
00055    template<typename OsModel_P, typename Roomba_P>
00056    void RoombaEventSensor<OsModel_P, Roomba_P>::
00057 	on_new_data() {
00058       typename Roomba::value_t& v = (*roomba_)();
00059       int events = 0;
00060       if(v.bumps != bumps_) {
00061          //std::cout << "bumps before: " << bumps_ << " bumps now: " << v.bumps << "\n";
00062       }
00063       if(v.bumps & ~bumps_) { // if bumps has any 1s it didnt have before
00064          events |= EVENT_WALL;
00065       }
00066       bumps_ = v.bumps;
00067       
00068       if(events && event_callback_) {
00069          event_callback_(events);
00070       }
00071    }
00072    
00073    template<typename OsModel_P, typename Roomba_P>
00074    int RoombaEventSensor<OsModel_P, Roomba_P>::
00075 	init() {
00076       return SUCCESS;
00077    }
00078    
00079    template<typename OsModel_P, typename Roomba_P>
00080    int RoombaEventSensor<OsModel_P, Roomba_P>::
00081 	destruct() {
00082       return SUCCESS;
00083    }
00084    
00085    template<typename OsModel_P, typename Roomba_P>
00086    template<typename T, void (T::*TMethod)(uint8_t)>
00087    int RoombaEventSensor<OsModel_P, Roomba_P>::
00088 	reg_event_callback(T* obj) {
00089       event_callback_ = event_callback_t::from_method<T, TMethod>(obj);
00090       return 1;
00091    }
00092    
00093 } // namespace wiselib
00094 
00095 #endif // ROOMBA_EVENT_SENSOR_H
00096 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines