WiiMoteDriver.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_WiiMote_h_
00003 #define INCLUDED_WiiMote_h_
00004
00005 #include "local/DeviceDriver.h"
00006 #include "local/MotionHook.h"
00007 #include "local/DataSource.h"
00008
00009 #include "IPC/Thread.h"
00010
00011 #include <bluetooth/bluetooth.h>
00012 #include <wiimote.h>
00013
00014 using namespace std;
00015
00016
00017 class WiiMoteDriver;
00018 WiiMoteDriver *driver = 0;
00019
00020 class DataCache:public DataSource{
00021 public:
00022 DataCache();
00023 virtual ~DataCache() {}
00024 virtual unsigned int nextTimestamp() { return 0; };
00025 virtual const std::string& nextName() {
00026 static const std::string noneStr="(none)";
00027 return noneStr;
00028 }
00029
00030 virtual unsigned int getData(const char *& payload, unsigned int& size, unsigned int& timestamp, std::string& name);
00031 virtual void setDataSourceThread(LoadDataThread* p);
00032
00033 virtual void updateAccMesg(const wiimote_acc_mesg &mesg);
00034 virtual void updateBtnMesg(const wiimote_btn_mesg &mesg);
00035
00036 protected:
00037 wiimote_acc_mesg acc_mesg;
00038 wiimote_btn_mesg btn_mesg;
00039
00040 unsigned int number;
00041
00042 std::ostringstream state;
00043 std::string states[2];
00044 int cstate;
00045
00046 ThreadNS::Condition updateCond;
00047 ThreadNS::Lock updateLock;
00048
00049 short updated;
00050
00051 virtual void doFreeze() {}
00052 virtual void doUnfreeze() {}
00053
00054 private:
00055 DataCache(const DataCache&);
00056 DataCache& operator=(const DataCache&);
00057 };
00058
00059
00060
00061
00062
00063
00064
00065 class WiiMoteDriver : public virtual DeviceDriver, public MotionHook, public virtual plist::PrimitiveListener {
00066 public:
00067
00068 explicit WiiMoteDriver(const std::string& name)
00069 : DeviceDriver(autoRegisterWiiMoteDriver,name), MotionHook(), data(), wiim(NULL)
00070 {
00071 if(driver)
00072 cerr << "Warning: Createing another WiiMote Driver is not supported!" << endl;
00073
00074 driver = this;
00075 this->init();
00076 }
00077
00078
00079 virtual ~WiiMoteDriver() {
00080 close();
00081
00082 }
00083
00084 virtual std::string getClassName() const { return autoRegisterWiiMoteDriver; }
00085
00086 virtual MotionHook* getMotionSink() { return dynamic_cast<MotionHook*>(this); }
00087 virtual void getSensorSources(std::map<std::string,DataSource*>& sources) {
00088 sources.clear();
00089 sources["Sensors"] = &data;
00090 }
00091
00092 virtual void getImageSources(std::map<std::string,DataSource*>& sources) {
00093 sources.clear();
00094 }
00095
00096 virtual void motionStarting() {}
00097 virtual void motionUpdated(const std::vector<size_t>& changedIndices, const float outputs[][NumOutputs]);
00098 virtual void motionStopping() {}
00099
00100 virtual void plistValueChanged(const plist::PrimitiveBase& pl) { };
00101
00102 virtual void mesg_callback(int id, int mesg_count, union wiimote_mesg* mesg[]);
00103
00104 DataCache data;
00105
00106 protected:
00107
00108 virtual int init();
00109
00110
00111 virtual void connect() { };
00112
00113
00114 virtual void close() {
00115
00116 wiimote_disconnect(wiim);
00117 wiim=NULL;
00118 }
00119
00120 inline int calcLEDValue(unsigned int i,float x) {
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 return 1;
00139 }
00140
00141 wiimote_t *wiim;
00142 private:
00143
00144 static const std::string autoRegisterWiiMoteDriver;
00145
00146 WiiMoteDriver(const WiiMoteDriver&);
00147 WiiMoteDriver& operator=(const WiiMoteDriver&);
00148 };
00149
00150
00151
00152
00153
00154
00155 #endif
00156