#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;
}
string WheelSourcePlugin::uuid()
{
- return "6dd4268a-c605-4a06-9034-59c1e8344c8e";
+ return "c0ffee8a-c605-4a06-9034-59c1deadbeef";
}
boost::any WheelSourcePlugin::getProperty(VehicleProperty::Property property)
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;
}
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);
}