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

ArJoyHandler_LIN.cpp

00001 /*
00002 ActivMedia Robotics Interface for Applications (ARIA)
00003 Copyright (C) 2004,2005 ActivMedia Robotics, LLC
00004 
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 ActivMedia Robotics for information about a commercial version of ARIA at 
00022 robots@activmedia.com or 
00023 ActivMedia Robotics, 19 Columbia Drive, Amherst, NH 03031; 800-639-9481
00024 
00025 */
00026 
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArJoyHandler.h"
00030 #include "ArLog.h"
00031 #include <errno.h>
00032 #include "ariaUtil.h"
00033 
00034 bool ArJoyHandler::init(void)
00035 {
00036   int i;
00037 
00038   myLastOpenTry.setToNow();
00039   myJoyNumber = 0;
00040 
00041   if (myUseOld)
00042   {
00043     myOldJoyDesc = fopen("/dev/js0", "r");
00044   }
00045   else
00046   {
00047     for (i = 0; i < 32; i++)
00048     {
00049       sprintf(myJoyNameTemp, "/dev/input/js%d", i);
00050       if ((myJoyDesc = open(myJoyNameTemp, O_RDWR | O_NONBLOCK)) > 0)
00051       {
00052         ArLog::log(ArLog::Verbose, "ArJoyHandler: Opened %s", myJoyNameTemp);
00053         break;
00054       }
00055     }
00056   }
00057   
00058   if ((myUseOld && myOldJoyDesc != NULL) || (!myUseOld && myJoyDesc > 0))
00059   {
00060     myPhysMax = 255;
00061     myInitialized = true;
00062     startCal();
00063     endCal();
00064     getData();
00065     return true;
00066   } 
00067   else 
00068   {
00069     myJoyNumber = -1;
00070     myPhysMax = 255;
00071     myInitialized = false;
00072     getData();
00073     return false;
00074   }
00075 }
00076 
00077 void ArJoyHandler::getData(void)
00078 {
00079   if (myUseOld && !myInitialized)
00080     return;
00081 
00082   if (!myFirstData && myLastDataGathered.mSecSince() < 5)
00083     return;
00084   myFirstData = false;
00085   myLastDataGathered.setToNow();
00086   if (myUseOld)
00087     getOldData();
00088   else
00089     getNewData();
00090 }
00091 
00092 void ArJoyHandler::getOldData(void)
00093 {
00094 #ifdef linux
00095   int x, y;
00096   if (myOldJoyDesc == NULL || !myInitialized || 
00097       fread(&myJoyData, 1, JS_RETURN, myOldJoyDesc) != JS_RETURN) 
00098   {
00099     myAxes[1] = 0;
00100     myAxes[2] = 0;
00101     myAxes[3] = 0;
00102     myButtons[1] = false;
00103     myButtons[2] = false;
00104     myButtons[3] = false;
00105     myButtons[4] = false;
00106   } 
00107   else 
00108   {
00109     x = myJoyData.x - 128; 
00110     y =  - (myJoyData.y - 128);
00111     if (x > myMaxX)
00112       myMaxX = x;
00113     if (x < myMinX)
00114       myMinX = x;
00115     if (y > myMaxY)
00116       myMaxY = y;
00117     if (y < myMinY)
00118       myMinY = y;
00119     myAxes[1] = x;
00120     myAxes[2] = y;
00121     myAxes[3] = 0;
00122     myButtons[1] = myJoyData.buttons & 1;
00123     myButtons[2] = myJoyData.buttons & 2;
00124     myButtons[3] = myJoyData.buttons & 4;
00125     myButtons[4] = myJoyData.buttons & 8;
00126   }
00127 #endif // ifdef linux
00128 }
00129 
00131 void ArJoyHandler::getNewData(void)
00132 {
00133 #ifdef linux
00134   if (myLastOpenTry.mSecSince() > 125)
00135   {
00136     int tempDesc;
00137     myLastOpenTry.setToNow();
00138     sprintf(myJoyNameTemp, "/dev/input/js%d", myJoyNumber + 1);
00139     if ((tempDesc = open(myJoyNameTemp, O_RDWR | O_NONBLOCK)) > 0)
00140     {
00141       ArLog::log(ArLog::Verbose, "ArJoyHandler: Opened next joydev %s", myJoyNameTemp);
00142       close(myJoyDesc);
00143       myInitialized = true;
00144       myJoyDesc = tempDesc;
00145       myJoyNumber++;
00146     }
00147     else if (myJoyNumber > 0)
00148     {
00149       if ((tempDesc = open("/dev/input/js0", O_RDWR | O_NONBLOCK)) > 0)
00150       {
00151         myInitialized = true;
00152         ArLog::log(ArLog::Verbose, "ArJoyHandler: Opened first joydev /dev/input/js0");
00153         close(myJoyDesc);
00154         myJoyDesc = tempDesc;
00155         myJoyNumber = 0;
00156       }
00157     }
00158   }
00159 
00160   struct js_event e;
00161   while (read (myJoyDesc, &e, sizeof(struct js_event)) > 0)
00162   {
00163     // see if its a button even
00164     if ((e.type & JS_EVENT_BUTTON))
00165     {
00166       // if its one of the buttons we want set it
00167       myButtons[e.number+1] = (bool)e.value;
00168     }
00169     // see if its an axis
00170     if ((e.type & JS_EVENT_AXIS))
00171     {
00172       // if its one of the buttons we want set it
00173       if (e.number == 0)
00174         myAxes[e.number+1] = ArMath::roundInt(e.value * 128.0 / 32767.0);
00175       else
00176         myAxes[e.number+1] = ArMath::roundInt(-e.value * 128.0 / 32767.0);
00177       if (e.number == 2)
00178         myHaveZ = true;
00179     }
00180     //printf("%d 0x%x 0x%x\n", e.value, e.type, e.number);
00181   }
00182   if (errno != EAGAIN)
00183   {
00184     //ArLog::log(ArLog::Terse, "ArJoyHandler::getUnfiltered: Trouble reading data.");
00185   }
00186 #endif // ifdef linux 
00187 }
00188 

Generated on Wed Oct 19 12:56:35 2005 for Aria by  doxygen 1.4.0