Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages | Examples

ArJoyHandler_LIN.cpp

00001 /*
00002 MobileRobots Advanced Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004, 2005 ActivMedia Robotics LLC
00004 Copyright (C) 2006, 2007 MobileRobots Inc.
00005 
00006      This program is free software; you can redistribute it and/or modify
00007      it under the terms of the GNU General Public License as published by
00008      the Free Software Foundation; either version 2 of the License, or
00009      (at your option) any later version.
00010 
00011      This program is distributed in the hope that it will be useful,
00012      but WITHOUT ANY WARRANTY; without even the implied warranty of
00013      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014      GNU General Public License for more details.
00015 
00016      You should have received a copy of the GNU General Public License
00017      along with this program; if not, write to the Free Software
00018      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 If you wish to redistribute ARIA under different terms, contact 
00021 MobileRobots for information about a commercial version of ARIA at 
00022 robots@mobilerobots.com or 
00023 MobileRobots Inc, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 */
00025 
00026 #include "ArExport.h"
00027 #include "ariaOSDef.h"
00028 #include "ArJoyHandler.h"
00029 #include "ArLog.h"
00030 #include <errno.h>
00031 #include "ariaUtil.h"
00032 
00033 bool ArJoyHandler::init(void)
00034 {
00035   int i;
00036 
00037   myLastOpenTry.setToNow();
00038   myJoyNumber = 0;
00039 
00040   if (myUseOld)
00041   {
00042     myOldJoyDesc = fopen("/dev/js0", "r");
00043   }
00044   else
00045   {
00046     for (i = 0; i < 32; i++)
00047     {
00048       sprintf(myJoyNameTemp, "/dev/input/js%d", i);
00049       if ((myJoyDesc = open(myJoyNameTemp, O_RDWR | O_NONBLOCK)) > 0)
00050       {
00051     ArLog::log(ArLog::Verbose, "ArJoyHandler: Opened %s", myJoyNameTemp);
00052     break;
00053       }
00054     }
00055   }
00056   
00057   if ((myUseOld && myOldJoyDesc != NULL) || (!myUseOld && myJoyDesc > 0))
00058   {
00059     myPhysMax = 255;
00060     myInitialized = true;
00061     startCal();
00062     endCal();
00063     getData();
00064     return true;
00065   } 
00066   else 
00067   {
00068     myJoyNumber = -1;
00069     myPhysMax = 255;
00070     myInitialized = false;
00071     getData();
00072     return false;
00073   }
00074 }
00075 
00076 void ArJoyHandler::getData(void)
00077 {
00078   if (myUseOld && !myInitialized)
00079     return;
00080 
00081   if (!myFirstData && myLastDataGathered.mSecSince() < 5)
00082     return;
00083   myFirstData = false;
00084   myLastDataGathered.setToNow();
00085   if (myUseOld)
00086     getOldData();
00087   else
00088     getNewData();
00089 }
00090 
00091 void ArJoyHandler::getOldData(void)
00092 {
00093 #ifdef linux
00094   int x, y;
00095   if (myOldJoyDesc == NULL || !myInitialized || 
00096       fread(&myJoyData, 1, JS_RETURN, myOldJoyDesc) != JS_RETURN) 
00097   {
00098     myAxes[1] = 0;
00099     myAxes[2] = 0;
00100     myAxes[3] = 0;
00101     myButtons[1] = false;
00102     myButtons[2] = false;
00103     myButtons[3] = false;
00104     myButtons[4] = false;
00105   } 
00106   else 
00107   {
00108     x = myJoyData.x - 128; 
00109     y =  - (myJoyData.y - 128);
00110     if (x > myMaxX)
00111       myMaxX = x;
00112     if (x < myMinX)
00113       myMinX = x;
00114     if (y > myMaxY)
00115       myMaxY = y;
00116     if (y < myMinY)
00117       myMinY = y;
00118     myAxes[1] = x;
00119     myAxes[2] = y;
00120     myAxes[3] = 0;
00121     myButtons[1] = myJoyData.buttons & 1;
00122     myButtons[2] = myJoyData.buttons & 2;
00123     myButtons[3] = myJoyData.buttons & 4;
00124     myButtons[4] = myJoyData.buttons & 8;
00125   }
00126 #endif // ifdef linux
00127 }
00128 
00130 void ArJoyHandler::getNewData(void)
00131 {
00132 #ifdef linux
00133   if (myLastOpenTry.mSecSince() > 125)
00134   {
00135     int tempDesc;
00136     myLastOpenTry.setToNow();
00137     sprintf(myJoyNameTemp, "/dev/input/js%d", myJoyNumber + 1);
00138     if ((tempDesc = open(myJoyNameTemp, O_RDWR | O_NONBLOCK)) > 0)
00139     {
00140       ArLog::log(ArLog::Verbose, "ArJoyHandler: Opened next joydev %s", myJoyNameTemp);
00141       close(myJoyDesc);
00142       myInitialized = true;
00143       myJoyDesc = tempDesc;
00144       myJoyNumber++;
00145     }
00146     else if (myJoyNumber > 0)
00147     {
00148       if ((tempDesc = open("/dev/input/js0", O_RDWR | O_NONBLOCK)) > 0)
00149       {
00150     myInitialized = true;
00151     ArLog::log(ArLog::Verbose, "ArJoyHandler: Opened first joydev /dev/input/js0");
00152     close(myJoyDesc);
00153     myJoyDesc = tempDesc;
00154     myJoyNumber = 0;
00155       }
00156     }
00157   }
00158 
00159   struct js_event e;
00160   while (read (myJoyDesc, &e, sizeof(struct js_event)) > 0)
00161   {
00162     // see if its a button even
00163     if ((e.type & JS_EVENT_BUTTON))
00164     {
00165       // if its one of the buttons we want set it
00166       myButtons[e.number+1] = (bool)e.value;
00167     }
00168     // see if its an axis
00169     if ((e.type & JS_EVENT_AXIS))
00170     {
00171       // if its one of the buttons we want set it
00172       if (e.number == 0)
00173         myAxes[e.number+1] = ArMath::roundInt(e.value * 128.0 / 32767.0);
00174       else
00175     myAxes[e.number+1] = ArMath::roundInt(-e.value * 128.0 / 32767.0);
00176       if (e.number == 2)
00177     myHaveZ = true;
00178     }
00179     //printf("%d 0x%x 0x%x\n", e.value, e.type, e.number);
00180   }
00181   if (errno != EAGAIN)
00182   {
00183     //ArLog::log(ArLog::Terse, "ArJoyHandler::getUnfiltered: Trouble reading data.");
00184   }
00185 #endif // ifdef linux 
00186 }
00187 

Generated on Tue Feb 20 10:51:40 2007 for Aria by  doxygen 1.4.0