- experimental signal network works now

This commit is contained in:
Ingo Ruhnke 2008-04-24 20:01:10 +02:00
parent cc912338d3
commit 1c8790b5c6
5 changed files with 113 additions and 14 deletions

View file

@ -2,6 +2,7 @@
env = Environment(CPPFLAGS=["-g", "-O2", "-Wall"], LIBS=["usb"])
env.Program("xboxdrv", ["xboxdrv.cpp", "uinput.cpp"])
env.Program("inputdrv", ["inputdrv.cpp", "xbox360_driver.cpp"], LIBS=['boost_signals', 'usb'])
env.Program("inputdrv", ["inputdrv.cpp", "xbox360_driver.cpp", "control.cpp"],
LIBS=['boost_signals', 'usb'])
# EOF #

View file

@ -27,8 +27,9 @@
#define HEADER_CONTROL_HPP
#include <boost/signal.hpp>
class BtnPortIn;
class BtnPortOut
{
public:
@ -45,7 +46,10 @@ public:
std::string get_label() { return label; }
bool get_state() { return state; }
void set_state(bool s) { state = s; sig_change(this); }
void set_state(bool s) { if (state != s) { state = s; sig_change(this); } }
void connect(BtnPortIn* in);
void connect(boost::function<void(BtnPortOut*)> func);
};
struct BtnPortIn
@ -93,8 +97,8 @@ protected:
public:
UInputButton(UInput* uinput, uint16_t code)
: uinput(uinput),
code(code)
: uinput(uinput),
code(code)
{
btn_port_in.push_back(new BtnPortIn((boost::format("UInput-Button %hd") % code).str(),
boost::bind(this, &UInputButton::on_button)));

View file

@ -24,10 +24,17 @@
*/
#include <usb.h>
//#include "xbox360_driver.hpp"
#include <iostream>
#include <boost/bind.hpp>
#include "xbox360_driver.hpp"
#include "control.hpp"
#include "inputdrv.hpp"
void btn_change(BtnPortOut* port)
{
std::cout << "Button" << port->get_label() << ": " << port->get_state() << std::endl;
}
int main()
{
// Init USB
@ -35,7 +42,18 @@ int main()
usb_find_busses();
usb_find_devices();
Xbox360Driver xbox360(0);
BtnPortOut* btn_a = xbox360.get_btn_port_out(0);
BtnPortOut* btn_b = xbox360.get_btn_port_out(1);
btn_a->connect(btn_change);
btn_b->connect(btn_change);
btn_a->connect(xbox360.get_btn_port_in(0));
btn_b->connect(xbox360.get_btn_port_in(1));
xbox360.run();
return 0;
}

View file

@ -29,8 +29,51 @@
#include <boost/bind.hpp>
#include "xbox360_driver.hpp"
struct usb_device* find_usb_device_by_path(const std::string& busid, const std::string& devid) { return 0; }
struct usb_device* find_usb_device_by_ids(uint16_t idVendor, uint16_t idProduct) { return 0; }
struct usb_device* find_usb_device_by_path(const std::string& busid, const std::string& devid)
{
struct usb_bus* busses = usb_get_busses();
for (struct usb_bus* bus = busses; bus; bus = bus->next)
{
if (bus->dirname == busid)
{
for (struct usb_device* dev = bus->devices; dev; dev = dev->next)
{
if (dev->filename == devid)
{
return dev;
}
}
}
}
return 0;
}
struct usb_device* find_usb_device_by_ids(int id, uint16_t idVendor, uint16_t idProduct)
{
struct usb_bus* busses = usb_get_busses();
int id_count = 0;
for (struct usb_bus* bus = busses; bus; bus = bus->next)
{
for (struct usb_device* dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == idVendor &&
dev->descriptor.idProduct == idProduct)
{
if (id_count == id)
{
return dev;
}
else
{
id_count += 1;
}
}
}
}
return 0;
}
XPadDevice xbox360_devices[] = {
{ GAMEPAD_XBOX360, 0x045e, 0x028e, "Microsoft Xbox 360 Controller" },
@ -51,8 +94,10 @@ Xbox360Driver::init()
// This should really be abs ports so that one can select different
// rumble strength an LED status
btn_port_in.push_back(new BtnPortIn("Xbox360Driver LED", boost::function<void(BtnPortOut*)>()));
btn_port_in.push_back(new BtnPortIn("Xbox360Driver Rumble", boost::function<void(BtnPortOut*)>()));
btn_port_in.push_back(new BtnPortIn("Xbox360Driver LED",
boost::bind(&Xbox360Driver::on_led_btn, this, _1)));
btn_port_in.push_back(new BtnPortIn("Xbox360Driver Rumble",
boost::bind(&Xbox360Driver::on_rumble_btn, this, _1)));
}
Xbox360Driver::Xbox360Driver(const std::string& busid, const std::string& devid)
@ -67,12 +112,14 @@ Xbox360Driver::Xbox360Driver(int id)
{
init();
// FIXME: This loop can't work
for(int i = 0; i < xbox360_devices_count && !dev; ++i)
{
if (xbox360_devices[i].type == GAMEPAD_XBOX360 ||
xbox360_devices[i].type == GAMEPAD_XBOX360_GUITAR)
{
dev = find_usb_device_by_ids(xbox360_devices[i].idVendor,
dev = find_usb_device_by_ids(id,
xbox360_devices[i].idVendor,
xbox360_devices[i].idProduct);
}
}
@ -194,8 +241,34 @@ Xbox360Driver::set_led(uint8_t led_status)
void
Xbox360Driver::set_rumble(uint8_t big, uint8_t small)
{
char rumblecmd[] = { 0x00, 0x06, 0x00, small/*right*/, 0x00, big/*left*/};
usb_interrupt_write(handle, 2, rumblecmd, 6, 0);
char rumblecmd[] = { 0x00, 0x08, 0x00, big, small, 0x00, 0x00, 0x00 };
usb_interrupt_write(handle, 2, rumblecmd, 8, 0);
}
void
Xbox360Driver::on_led_btn(BtnPortOut* btn)
{
if (btn->get_state())
{
set_led(10);
}
else
{
set_led(0);
}
}
void
Xbox360Driver::on_rumble_btn(BtnPortOut* btn)
{
if (btn->get_state())
{
set_rumble(155, 155);
}
else
{
set_rumble(0, 0);
}
}
/* EOF */

View file

@ -65,6 +65,9 @@ public:
void set_led(uint8_t led_status);
void set_rumble(uint8_t big, uint8_t small);
void on_led_btn(BtnPortOut* btn);
void on_rumble_btn(BtnPortOut* btn);
void run();
private: