Add opening of joystick dev file, and reading of various JS info
authorJames Ausmus <james.ausmus@intel.com>
Wed, 22 Aug 2012 00:10:33 +0000 (17:10 -0700)
committerMichael Carpenter <malcom2073@gmail.com>
Thu, 23 Aug 2012 09:51:23 +0000 (05:51 -0400)
Signed-off-by: James Ausmus <james.ausmus@intel.com>
plugins/wheel/wheelplugin.cpp
plugins/wheel/wheelplugin.h

index fcec888..601824b 100644 (file)
@@ -19,30 +19,60 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #include "wheelplugin.h"
 
 #include <iostream>
+#include <stdexcept>
 #include <boost/assert.hpp>
 #include <glib.h>
 
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <linux/input.h>
+#include <linux/joystick.h>
+
 using namespace std;
 
 #include "debugout.h"
 
+
+#define JSNAMELEN 128
+
+
+class WheelPrivate
+{
+
+public:
+       WheelPrivate();
+       ~WheelPrivate();
+
+       int fd;
+       char doShutdown;
+       char isShutdown;
+
+private:
+//     boost::thread pollThread;
+
+};
+
+
 static gboolean timeoutCallback(gpointer data)
 {
        WheelSourcePlugin* src = (WheelSourcePlugin*)data;
        
-       src->randomizeProperties();
+       //src->randomizeProperties();
        
        return true;
 }
 
 WheelSourcePlugin::WheelSourcePlugin(AbstractRoutingEngine* re)
-:AbstractSource(re), velocity(0), engineSpeed(0)
+:AbstractSource(re), velocity(0), engineSpeed(0), wheel(new WheelPrivate())
 {
        re->setSupported(supported(), this);
-       
-       debugOut("setting timeout");
-       g_timeout_add(1000, timeoutCallback, this );
-       
+}
+
+WheelSourcePlugin::~WheelSourcePlugin()
+{
+       delete this->wheel;
 }
 
 
@@ -55,7 +85,7 @@ extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine)
 
 string WheelSourcePlugin::uuid()
 {
-       return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
+       return "c0ffee8a-c605-4a06-9034-59c1deadbeef";
 }
 
 boost::any WheelSourcePlugin::getProperty(VehicleProperty::Property property)
@@ -99,7 +129,16 @@ PropertyList WheelSourcePlugin::supported()
        PropertyList props;
        props.push_back(VehicleProperty::EngineSpeed);
        props.push_back(VehicleProperty::VehicleSpeed);
-       
+       props.push_back(VehicleProperty::TransmissionShiftPosition);
+       props.push_back(VehicleProperty::ThrottlePosition);
+       props.push_back(VehicleProperty::WheelBrake);
+       props.push_back(VehicleProperty::SteeringWheelAngle);
+       props.push_back(VehicleProperty::TurnSignal);
+       props.push_back(VehicleProperty::ClutchStatus);
+       props.push_back(VehicleProperty::EngineOilPressure);
+       props.push_back(VehicleProperty::EngineCoolantTemperature);
+       props.push_back(VehicleProperty::MachineGunTurretStatus);
+
        return props;
 }
 
@@ -108,14 +147,51 @@ void WheelSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property p
        mRequests.remove(property);
 }
 
-void WheelSourcePlugin::randomizeProperties()
+//PIMPL:
+
+
+void pollJS(WheelPrivate *wp)
 {
-       velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
-       engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
-       
-       DebugOut()<<"setting velocity to: "<<velocity<<endl;
-       DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
+       if (!wp) {
+               throw std::runtime_error("NULL WheelPrivate in pollJS!");
+               return;
+       }
+       while (!wp->doShutdown) {
+               
+       }
+       wp->isShutdown = 1;
+}
+
+WheelPrivate::WheelPrivate()
+:fd(-1), isShutdown(0), doShutdown(0)
+{
+
+       unsigned char axes = 0;
+       unsigned char buttons = 0;
+       int version = 0;
+       char name[JSNAMELEN] = "Unknown";
+
+
+       //FIXME: Support config file with joystick device mapping, button/axis mappings, etc.
+       if ((this->fd = open("/dev/input/js0", O_RDONLY)) < 0) {
+               throw std::runtime_error("Could not find a joystick class device!");    //FIXME: Later, don't throw, watch input devices, and jump on to any JS devices that appear
+               return;
+       }
+
+       ioctl(fd, JSIOCGVERSION, &version);
+       ioctl(fd, JSIOCGAXES, &axes);
+       ioctl(fd, JSIOCGBUTTONS, &buttons);
+       ioctl(fd, JSIOCGNAME(JSNAMELEN), name);
+
+       cout << "Driver version: " << (version >> 16) << "." << ((version >> 8) & 0xFF) << "." << (version & 0xFF) << "\n";
+       cout << "JS Name: " << name << "\n";
+       cout << "JS Axes/Buttons: " << (int)axes << "/" << (int)buttons << "\n";
+       cout << "Starting polling thread...\n";
        
-       routingEngine->updateProperty(VehicleProperty::VehicleSpeed, velocity);
-       routingEngine->updateProperty(VehicleProperty::EngineSpeed, engineSpeed);
+}
+
+WheelPrivate::~WheelPrivate()
+{
+       if (this->fd != -1)
+               close(fd);
 }
index dddbb1b..b766817 100644 (file)
@@ -24,11 +24,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 using namespace std;
 
+class WheelPrivate;
+
 class WheelSourcePlugin: public AbstractSource
 {
 
 public:
        WheelSourcePlugin(AbstractRoutingEngine* re);
+       ~WheelSourcePlugin();
        
        string uuid();
        boost::any getProperty(VehicleProperty::Property property);
@@ -41,9 +44,8 @@ public:
        void propertyChanged(VehicleProperty::Property property, boost::any value, string uuid) {}
        void supportedChanged(PropertyList) {}
        
-       void randomizeProperties();
-       
 private:
+       WheelPrivate *wheel;
        PropertyList mRequests;
        uint16_t velocity;
        uint16_t engineSpeed;