Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

WiiMoteDriver.h

Go to the documentation of this file.
00001 //-*-c++-*-
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 // bleh work around hack :(
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() {} //!< user hook for when #frozen is set to true
00052   virtual void doUnfreeze() {} //!< user hook for when #frozen is set to false
00053 
00054  private:
00055   DataCache(const DataCache&); //!< do not call
00056   DataCache& operator=(const DataCache&); //!< do not call
00057 };
00058 
00059 
00060 //! Provides something
00061 /*!
00062  *
00063  *  To do: write something here
00064  */
00065 class WiiMoteDriver : public virtual DeviceDriver, public MotionHook, public virtual plist::PrimitiveListener {
00066 public:
00067   //! constructor
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   //! destructor
00079   virtual ~WiiMoteDriver() {
00080     close();
00081     // don't need to delete properties, smart pointer
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   //! connect to qwerk
00111   virtual void connect() { };
00112 
00113   //! closes the current proxies (reuses the communicator instance though)
00114   virtual void close() {
00115     // disconnect the wiimote
00116     wiimote_disconnect(wiim);
00117     wiim=NULL;
00118   }
00119   
00120   inline int calcLEDValue(unsigned int i,float x) {
00121     /*
00122     if(x<=0.0) {
00123       ledActivation[i]*=.9; //decay activation... resets to keeps LEDs in sync, looks a little better
00124       return 0;
00125     } else if(x>=1.0) {
00126       return 1;
00127     } else {
00128       x*=x; // squared to "gamma correct" - we can see a single pulse better than a single flicker - after image and all that
00129       ledActivation[i]+=x;
00130       if(ledActivation[i]>=1.0) {
00131         ledActivation[i]-=1.0;
00132         return 0;
00133       } else {
00134         return 1;
00135       }
00136     }
00137     */
00138     return 1;
00139   }
00140 
00141   wiimote_t *wiim;
00142 private:
00143   //! holds the class name, set via registration with the DeviceDriver registry
00144   static const std::string autoRegisterWiiMoteDriver;
00145 
00146   WiiMoteDriver(const WiiMoteDriver&); //!< do not call
00147   WiiMoteDriver& operator=(const WiiMoteDriver&); //!< do not call
00148 };
00149 
00150 /*! @file
00151  * @brief 
00152  * @author Benson Tsai
00153  */
00154 
00155 #endif
00156 

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:39 2016 by Doxygen 1.6.3