Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phJoystickData.cpp

Go to the documentation of this file.
00001 #include <phJoystickData.h>
00002 #include <phbase.h>
00003 
00004 /* ------------------------------------------------------------------------ */
00005 phJoystickData::phJoystickData()
00006 {
00007     phFUNCTION("phJoystickData::phJoystickData")
00008     int locked = 0;
00009     int rwlocked = 0;
00010     
00011     phTHIS_LOOSE_LOCK(locked);
00012     phTHIS_WRITELOCK_LOOSE(rwlocked);
00013 
00014     this->setName("phJoystickData");
00015     
00016     this->m_button_count    = 0;
00017     this->m_button          = NULL;
00018     this->m_button_size     = 0;
00019 
00020     this->m_axis_count      = 0;
00021     this->m_axis            = NULL;
00022     this->m_axis_size       = 0;
00023 
00024     this->m_keys            = NULL;
00025     this->m_key_count       = 0;
00026 
00027     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00028     phTHIS_LOOSE_UNLOCK(locked);
00029 }
00030 
00031 /* ------------------------------------------------------------------------ */
00032 phJoystickData::~phJoystickData()
00033 {
00034     phFUNCTION("phJoystickData::~phJoystickData")
00035     int locked = 0;
00036     int rwlocked = 0;
00037     
00038     phTHIS_LOOSE_LOCK(locked);
00039     phTHIS_WRITELOCK_LOOSE(rwlocked);
00040 
00041     phFree(this->m_button);
00042     phFree(this->m_axis);
00043     this->m_button_size     = 0;
00044     this->m_button_count    = 0;
00045     this->m_axis_size       = 0;
00046     this->m_axis_count      = 0;
00047 
00048     this->m_key_count       = 0;
00049     phFree(this->m_keys);
00050 
00051     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00052     phTHIS_LOOSE_UNLOCK(locked);
00053 }
00054 
00055 /* ---------------------------------------------------------------------- */
00056 int phJoystickData::setKeys(ph_joystick_key *keys, uint32_t key_count )
00057 {
00058     phFUNCTION("phJoystickData::setKeys")
00059     int locked = 0;
00060     int rwlocked = 0;
00061  
00062     phTHIS_LOOSE_LOCK(locked);
00063     phTHIS_READLOCK(rwlocked);
00064 
00065     if (key_count > 0)
00066     {
00067         phFree(this->m_keys);
00068 
00069         this->m_keys = (ph_joystick_key *)phCalloc(key_count,
00070                                     sizeof(struct ph_joystick_key_t));
00071         phCHECK_PTR(this->m_keys,"phCalloc","phCalloc failed allocating key table");
00072 
00073         phMemcpy(this->m_keys,keys,key_count * sizeof(struct ph_joystick_key_t));
00074         this->m_key_count = key_count;
00075     }
00076 
00077     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00078     phTHIS_LOOSE_UNLOCK(locked);
00079     
00080     return phSUCCESS;
00081 error:
00082     return phFAIL;
00083 }
00084 
00085 /* ---------------------------------------------------------------------- */
00086 int phJoystickData::copy( phObject *copyto_object )
00087 {
00088     phFUNCTION("phJoystickData::copy")
00089     
00090     if (this->isNamed(((phObject *)copyto_object)->getName()))
00091     {
00092         phJoystickData *copyto_data = (phJoystickData *)copyto_object;
00093         return copyto_data->copyData(*this);
00094     }
00095     /* Otherwise I'll assume they are incompatible */
00096     else
00097     {
00098         phPRINT_RC(-1,NULL, "Invalid object pairing in update."
00099                           "[ this:%s != target:%s ]",
00100                  this->getName(), ((phObject *)copyto_object)->getName());
00101         return phFAIL;
00102     }
00103 }
00104 
00105 /* ---------------------------------------------------------------------- */
00106 int phJoystickData::swap( phObject *object )
00107 {
00108     phFUNCTION("phJoystickData::swap")
00109 
00110     if (this->isNamed(((phObject *)object)->getName()))
00111     {
00112         phJoystickData *data = (phJoystickData *)object;
00113 
00114         return data->swapData(*this);
00115     }
00116     /* Otherwise I'll assume they are incompatible */
00117     else
00118     {
00119         phPRINT_RC(-1,NULL, "Invalid object pairing in update."
00120                           "[ this:%s != target:%s ]",
00121                  this->getName(), ((phObject *)object)->getName());
00122         return phFAIL;
00123     }    
00124 }
00125 
00126 /* ---------------------------------------------------------------------- */
00127 int phJoystickData::copyData( phJoystickData &copyfrom )
00128 {
00129     phFUNCTION("phJoystickData::copyData")
00130     int rwlocked = 0;
00131     int other_rwlocked = 0;
00132    
00133     phTHIS_WRITELOCK(rwlocked);
00134     phREADLOCK(copyfrom,other_rwlocked);
00135 
00136     /*phDALLOC_RESIZE(ptr,cursize,nelems,cast)*/
00137     phDALLOC_RESIZE(this->m_button,
00138                     this->m_button_size,
00139                     copyfrom.m_button_count,
00140                     int32_t);
00141     phDALLOC_RESIZE(this->m_axis,
00142                     this->m_axis_size,
00143                     copyfrom.m_axis_count,
00144                     int32_t);
00145 
00146     phMemcpy(this->m_button,  
00147              copyfrom.m_button, 
00148              sizeof(int32_t) * copyfrom.m_button_count);
00149     phMemcpy(this->m_axis,  
00150              copyfrom.m_axis, 
00151              sizeof(int32_t) * copyfrom.m_axis_count);
00152 
00153     this->m_axis_count = copyfrom.m_axis_count;
00154     this->m_button_count = copyfrom.m_button_count;
00155     
00156     rc = this->notify();
00157     phPRINT_RC(rc,NULL,"this->notify()");
00158 
00159     phRWUNLOCK(copyfrom,other_rwlocked);
00160     phTHIS_RWUNLOCK(rwlocked);
00161 
00162     return phSUCCESS;
00163 error:
00164     phRWUNLOCK_ERROR(copyfrom,other_rwlocked);
00165     phTHIS_RWUNLOCK_ERROR(rwlocked);
00166 
00167     return phFAIL;    
00168 }
00169 
00170 /* ---------------------------------------------------------------------- */
00171 int phJoystickData::swapData( phJoystickData &data )
00172 {
00173     phFUNCTION("phJoystickData::swapData")
00174     int rwlocked = 0;
00175     int other_rwlocked = 0;
00176     int32_t *t_button;
00177     uint32_t t_button_size;
00178     uint32_t t_button_count;
00179     int32_t *t_axis;
00180     uint32_t t_axis_size;
00181     uint32_t t_axis_count;
00182 
00183     phTHIS_WRITELOCK(rwlocked);
00184         
00185     if (this->isSwapEnabled())
00186     {
00187         phWRITELOCK(data,other_rwlocked);
00188 
00189         t_button        = this->m_button;
00190         t_button_size   = this->m_button_size;
00191         t_button_count  = this->m_button_count;
00192         t_axis          = this->m_axis;
00193         t_axis_size     = this->m_axis_size;
00194         t_axis_count    = this->m_axis_count;
00195     
00196         this->m_button        = data.m_button;
00197         this->m_button_size   = data.m_button_size;
00198         this->m_button_count  = data.m_button_count;
00199         this->m_axis          = data.m_axis;
00200         this->m_axis_size     = data.m_axis_size;
00201         this->m_axis_count    = data.m_axis_count;
00202 
00203         data.m_button        = t_button;
00204         data.m_button_size   = t_button_size;
00205         data.m_button_count  = t_button_count;
00206         data.m_axis          = t_axis;
00207         data.m_axis_size     = t_axis_size;
00208         data.m_axis_count    = t_axis_count;
00209     
00210         rc = this->notify();
00211         phPRINT_RC(rc,NULL,"this->notify()");
00212 
00213         rc = data.notify();
00214         phPRINT_RC(rc,NULL,"data.notify()");
00215     
00216         phRWUNLOCK(data,other_rwlocked);
00217     }
00218     else
00219     {
00220         rc = this->setData(data);
00221         phCHECK_RC(rc,NULL,"this->setData(data)");
00222     }
00223 
00224     phTHIS_RWUNLOCK(rwlocked);
00225 
00226     return phSUCCESS;
00227 error:
00228     phRWUNLOCK_ERROR(data,other_rwlocked);
00229     phTHIS_RWUNLOCK_ERROR(rwlocked);
00230 
00231     return phFAIL;    
00232 }
00233 
00234 /* ---------------------------------------------------------------------- */
00235 int phJoystickData::reset()
00236 {
00237     phFUNCTION("phJoystickData::reset")
00238     int rwlocked = 0;
00239     
00240     phTHIS_WRITELOCK(rwlocked);
00241 
00242     phFree(this->m_button);
00243     phFree(this->m_axis);
00244     this->m_button_size     = 0;
00245     this->m_button_count    = 0;
00246     this->m_axis_size       = 0;
00247     this->m_axis_count      = 0;
00248    
00249     phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL);
00250 }
00251 
00252 /* ---------------------------------------------------------------------- */
00253 int phJoystickData::set (   uint32_t naxis,
00254                             int32_t *axis,
00255                             uint32_t nbuttons,
00256                             int32_t *button    )
00257 {
00258     phFUNCTION("phJoystickData::set")
00259     int rwlocked = 0;
00260     
00261     phTHIS_WRITELOCK(rwlocked);
00262 
00263     phDALLOC_RESIZE(this->m_button,
00264                     this->m_button_size,
00265                     nbuttons,
00266                     int32_t);
00267     phDALLOC_RESIZE(this->m_axis,
00268                     this->m_axis_size,
00269                     naxis,
00270                     int32_t);
00271 
00272     if (button != NULL)
00273     {
00274         phMemcpy(this->m_button,button, sizeof(int32_t) * nbuttons );
00275     }
00276     if (axis != NULL)
00277     {
00278         phMemcpy(this->m_axis,  axis,   sizeof(int32_t) * naxis    );
00279     }
00280     this->m_button_count    = nbuttons;
00281     this->m_axis_count      = naxis;
00282     
00283     if ((button != NULL) || (axis != NULL))
00284     {
00285         rc = this->notify();
00286         phPRINT_RC(rc,NULL,"this->notify()");
00287     }
00288     
00289     phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL);
00290 }
00291 
00292 /* ---------------------------------------------------------------------- */
00293 int phJoystickData::setAxis(uint32_t axis_id,
00294                             int32_t axis)
00295 {
00296     phFUNCTION("phJoystickData::setAxis")
00297     int rwlocked = 0;
00298     
00299     phTHIS_WRITELOCK(rwlocked);
00300 
00301     if (this->m_axis_count < (axis_id+1))
00302     {
00303         phDALLOC_RESIZE(this->m_axis,
00304                         this->m_axis_size,
00305                         (axis_id+1),
00306                         int32_t);
00307         this->m_axis_count = axis_id+1;
00308     }
00309 
00310     this->m_axis[axis_id] = axis;
00311 
00312     rc = this->notify();
00313     phPRINT_RC(rc,NULL,"this->notify()");
00314 
00315     phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL);
00316 }
00317 
00318 /* ---------------------------------------------------------------------- */
00319 int phJoystickData::setButton(uint32_t button_id,
00320                               int32_t button)
00321 {
00322     phFUNCTION("phJoystickData::setButton")
00323     int rwlocked = 0;
00324     
00325     phTHIS_WRITELOCK(rwlocked);
00326 
00327     if (this->m_button_count < (button_id+1))
00328     {
00329         phDALLOC_RESIZE(this->m_button,
00330                         this->m_button_size,
00331                         (button_id+1),
00332                         int32_t);
00333         this->m_button_count = button_id+1;
00334     }
00335 
00336     this->m_button[button_id] = button;
00337 
00338     rc = this->notify();
00339     phPRINT_RC(rc,NULL,"this->notify()");
00340 
00341     phTHIS_RWUNLOCK_RET(rwlocked,phSUCCESS,phFAIL);
00342 }
00343 
00344 /* ---------------------------------------------------------------------- */
00345 int32_t phJoystickData::getAxis(uint32_t axis_id)
00346 {
00347     phFUNCTION("phJoystickData::getAxis")
00348     int         rwlocked    = 0;
00349     int32_t    value       = 0;
00350     
00351     phTHIS_READLOCK(rwlocked);
00352 
00353     if (this->m_axis_count > axis_id)
00354     {
00355         value = this->m_axis[axis_id];
00356     }
00357 
00358     phTHIS_RWUNLOCK_RET(rwlocked,value,0);
00359 }
00360 
00361 /* ---------------------------------------------------------------------- */
00362 uint32_t phJoystickData::getAxisCount()
00363 {
00364     phFUNCTION("phJoystickData::getAxisCount")
00365     int         rwlocked    = 0;
00366     uint32_t    value       = 0;
00367     
00368     phTHIS_READLOCK(rwlocked);
00369 
00370     value = this->m_axis_count;
00371 
00372     phTHIS_RWUNLOCK_RET(rwlocked,value,0);
00373 }
00374 
00375 /* ---------------------------------------------------------------------- */
00376 int32_t phJoystickData::getButton( uint32_t button_id )
00377 {
00378     phFUNCTION("phJoystickData::getButton")
00379     int         rwlocked    = 0;
00380     int32_t    value       = 0;
00381     
00382     phTHIS_READLOCK(rwlocked);
00383 
00384     if (this->m_button_count > button_id)
00385     {
00386         value = this->m_button[button_id];
00387     }
00388 
00389     phTHIS_RWUNLOCK_RET(rwlocked,value,0);
00390 }
00391 
00392 /* ---------------------------------------------------------------------- */
00393 uint32_t phJoystickData::getButtonCount()
00394 {
00395     phFUNCTION("phJoystickData::getButtonCount")
00396     int         rwlocked    = 0;
00397     uint32_t    value       = 0;
00398     
00399     phTHIS_READLOCK(rwlocked);
00400 
00401     value = this->m_button_count;
00402 
00403     phTHIS_RWUNLOCK_RET(rwlocked,value,0);
00404 }
00405 
00406 /* ---------------------------------------------------------------------- */
00407 ph_joystick_key *phJoystickData::getKeys( )
00408 {
00409     phFUNCTION("phJoystickData::getKeys")
00410     int                 rwlocked    = 0;
00411     ph_joystick_key     *value      = NULL;
00412     
00413     phTHIS_READLOCK(rwlocked);
00414 
00415     value = this->m_keys;
00416 
00417     phTHIS_RWUNLOCK_RET(rwlocked,value,NULL);
00418 }
00419 
00420 /* ---------------------------------------------------------------------- */
00421 uint32_t phJoystickData::getKeyCount()
00422 {
00423     phFUNCTION("phJoystickData::getKeyCount")
00424     int         rwlocked    = 0;
00425     uint32_t    value       = 0;
00426     
00427     phTHIS_READLOCK(rwlocked);
00428 
00429     value = this->m_button_count;
00430 
00431     phTHIS_RWUNLOCK_RET(rwlocked,value,0);
00432 }
00433 
00434 /* ------------------------------------------------------------------------- */
00435 int32_t phJoystickData::valueMax()
00436 {
00437     return 32767;
00438 }
00439 
00440 /* ------------------------------------------------------------------------- */
00441 int32_t phJoystickData::valueMin()
00442 {
00443     return -32767;
00444 }
00445 
00446 /* ------------------------------------------------------------------------- */
00447 int32_t phJoystickData::x()
00448 {
00449     return this->get("x");
00450 }
00451 
00452 /* ------------------------------------------------------------------------- */
00453 int32_t phJoystickData::roll()
00454 {
00455     return this->get("roll");
00456 }
00457 
00458 /* ------------------------------------------------------------------------- */
00459 int32_t phJoystickData::y()
00460 {
00461     return this->get("y");
00462 }
00463 
00464 /* ------------------------------------------------------------------------- */
00465 int32_t phJoystickData::pitch()
00466 {
00467     return this->get("pitch");
00468 }
00469 
00470 /* ------------------------------------------------------------------------- */
00471 int32_t phJoystickData::z()
00472 {
00473     return this->get("z");
00474 }
00475 
00476 /* ------------------------------------------------------------------------- */
00477 int32_t phJoystickData::yaw()
00478 {
00479     return this->get("yaw");
00480 }
00481 
00482 /* ------------------------------------------------------------------------- */
00483 int32_t phJoystickData::get(const char *name)
00484 {
00485     phFUNCTION("phJoystickData::get")
00486     uint32_t    i           = 0;
00487     int         locked      = 0;
00488     int32_t     val         = 0;
00489     int32_t     aindex      = -1;
00490     int32_t     bindex      = -1;
00491 
00492     phTHIS_LOCK(locked);
00493 
00494     for (i = 0; i < this->m_key_count; i++ )
00495     {
00496         /*
00497         phPRINT("i:%d name:%s type:%d index:%d\n",
00498               i, this->m_keys[i].name,
00499               this->m_keys[i].type,
00500               this->m_keys[i].index);
00501         */
00502         if (strcmp(this->m_keys[i].name,name) == 0) 
00503         {
00504             switch(this->m_keys[i].type)
00505             {                
00506                 case phJOYSTICK_KEY_AXIS:
00507                     aindex = this->m_keys[i].index;
00508                     break;
00509                 case phJOYSTICK_KEY_BUTTON:
00510                     bindex = this->m_keys[i].index;
00511                     break;
00512             }
00513         }
00514     }
00515     /*
00516     phPRINT("name:%s aindex:%d bindex:%d\n",
00517           name,aindex,bindex);
00518     */
00519     if ((bindex >= 0) && (this->getButtonCount() > (uint32_t)bindex))
00520         val = this->getButton(bindex);
00521     if ((aindex >= 0) && (this->getAxisCount() > (uint32_t)aindex))
00522         val = this->getAxis(aindex);
00523 
00524     phTHIS_UNLOCK(locked);
00525 
00526     return val;
00527 error:
00528     phTHIS_ERROR_UNLOCK(locked);
00529     
00530     return 0;
00531 }
00532 
00533 /* ---------------------------------------------------------------------- */
00534 void phJoystickData::print_data( )
00535 {
00536     phFUNCTION("phJoystickData::print_data")
00537     uint32_t    i           = 0;
00538     int         rwlocked    = 0;
00539     int32_t     val         = 0;
00540 
00541     phTHIS_READLOCK_LOOSE(rwlocked);
00542 
00543     fprintf(stderr,"----------------------------------------------------\n");
00544     fprintf(stderr,"%s\n",this->getName());
00545     fprintf(stderr,"----------------------------------------------------\n");
00546     fprintf(stderr,"Axis Count: %u\n",this->m_axis_count);
00547     fprintf(stderr,"axis = ( ");
00548     for (i = 0; i < this->m_axis_count; i++ )
00549     {
00550         fprintf(stderr,"%d%s",this->m_axis[i],
00551                 (i == (this->m_axis_count - 1)) ? " " : ", ");
00552     }
00553     fprintf(stderr,"); \n");
00554     fprintf(stderr,"Button Count: %u\n",this->m_button_count);
00555     fprintf(stderr,"button = ( ");
00556     for (i = 0; i < this->m_button_count; i++ )
00557     {
00558         fprintf(stderr,"%d%s",this->m_button[i],
00559                 (i == (this->m_button_count - 1)) ? " " : ", ");
00560     }
00561     fprintf(stderr,"); \n");
00562     fprintf(stderr,"Key Count: %u\n",this->m_key_count);
00563     fprintf(stderr,"key = ( ");
00564     for (i = 0; i < this->m_key_count; i++ )
00565     {
00566         char    *type = "Unknown";
00567         switch (this->m_keys[i].type)
00568         {
00569             case phJOYSTICK_KEY_BUTTON:
00570                 type = "button";
00571                 if (this->m_keys[i].index < this->m_button_count)
00572                     val = this->m_button[this->m_keys[i].index];
00573             break;
00574             case phJOYSTICK_KEY_AXIS:
00575                 type = "axis";
00576                 if (this->m_keys[i].index < this->m_axis_count)
00577                     val = this->m_axis[this->m_keys[i].index];
00578             break;
00579         }
00580         fprintf(stderr,"(%s, %s, %d)%s",
00581                 type,
00582                 this->m_keys[i].name,
00583                 val,
00584                 (i == (this->m_key_count - 1)) ? " " : ", ");
00585     }
00586     fprintf(stderr,"); \n");
00587 
00588     fprintf(stderr,"----------------------------------------------------\n");
00589 
00590     fflush(stderr);
00591     
00592     phTHIS_RWUNLOCK_LOOSE(rwlocked);
00593 }




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:07 2007 for phission by  doxygen 1.4.4