Joined different XboxMsg types into a union

This commit is contained in:
Ingo Ruhnke 2008-05-01 10:16:49 +02:00
parent ea7511ffcf
commit c2d807735e
2 changed files with 122 additions and 0 deletions

73
xbox_controller.cpp Normal file
View file

@ -0,0 +1,73 @@
/*
** Xbox/Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2008 Ingo Ruhnke <grumbel@gmx.de>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include <sstream>
#include "xboxmsg.hpp"
#include "xbox360_controller.hpp"
XboxController::XboxController(struct usb_device* dev,
XPadDevice* dev_type)
{
struct usb_dev_handle* handle = usb_open(dev);
if (!handle)
{
throw std::runtime_error("Error opening Xbox360 controller");
}
else
{
if (usb_claim_interface(handle, 0) != 0) // FIXME: bInterfaceNumber shouldn't be hardcoded
{
throw std::runtime_error("Error couldn't claim the USB interface");
}
}
}
void
XboxController::set_rumble(uint8_t left, uint8_t right)
{
char rumblecmd[] = { 0x00, 0x06, 0x00, r, 0x00, l };
usb_interrupt_write(handle, 2, rumblecmd, sizeof(rumblecmd), 0);
}
void
XboxController::set_led(uint8_t status)
{
// Controller doesn't have a LED
}
void
XboxController::read(XboxGenericMsg& msg);
{
uint8_t data[32];
int ret = usb_interrupt_read(handle, 1 /*EndPoint*/, (char*)data, sizeof(data), 0 /*Timeout*/);
if (ret < 0)
{ // Error
std::ostringstream str;
str << "USBError: " << ret << "\n" << usb_strerror();
throw std::runtime_error(str.str());
}
else if (ret == 20 && data[0] == 0x00 && data[1] == 0x14)
{
msg.type = GAMEPAD_XBOX;
msg.xbox = *reinterpret_cast<XboxMsg*>(data);
}
}
/* EOF */

49
xbox_controller.hpp Normal file
View file

@ -0,0 +1,49 @@
/*
** Xbox/Xbox360 USB Gamepad Userspace Driver
** Copyright (C) 2008 Ingo Ruhnke <grumbel@gmx.de>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef HEADER_XBOX_CONTROLLER_HPP
#define HEADER_XBOX_CONTROLLER_HPP
#include <usb.h>
struct XPadDevice;
/** */
class XboxController
{
private:
struct usb_device* dev;
XPadDevice* dev_type;
struct usb_dev_handle* handle;
public:
XboxController(struct usb_device* dev,
XPadDevice* dev_type);
void set_rumble(uint8_t left, uint8_t right);
void set_led(uint8_t status);
void read(XboxGenericMsg& msg);
private:
XboxController (const XboxController&);
XboxController& operator= (const XboxController&);
};
#endif
/* EOF */