Changeset 467 in flair-src for trunk/tools/Controller


Ignore:
Timestamp:
03/09/22 17:44:06 (2 years ago)
Author:
Sanahuja Guillaume
Message:

change default usb method for ds3, now input event, no need to be root

Location:
trunk/tools/Controller/DualShock3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/Controller/DualShock3/resources/core2-64/dualshock3.sh

    r306 r467  
    11#! /bin/bash
    2 #ARCH_DIR=$(uname -m)
    32ARCH_DIR=core2-64
    43#on kernel >=4.13, ds3 is seen as a mouse
     
    65for id in `xinput --list|grep 'Sony PLAYSTATION(R)3 Controller'|perl -ne 'while (m/id=(\d+)/g){print "$1\n";}'`; do
    76    xinput set-prop $id 'Device Enabled' 0
    8 done
    9 
    10 #This script is the only one that calls itself with sudo.
    11 #As a consequence, only one exception is needed in sudoers (the following 2 lines)
    12 #  Defaults!/opt/flair/flair-bin/tools/scripts/dualshock3.sh setenv
    13 #  uav ALL=(root) /opt/flair/flair-bin/tools/scripts/dualshock3.sh
    14 
    15 #we must run as root
    16 if [ $EUID -ne 0 ]; then
    17   exec sudo -E $0 $*
    18 fi
     7done
    198
    209. ${FLAIR_ROOT}/flair-src/scripts/ubuntu_cgroup_hack.sh
    2110
    22 export LD_LIBRARY_PATH="${OECORE_HOST_SYSROOT}/usr/lib:${OECORE_HOST_SYSROOT}/lib"
    2311${FLAIR_ROOT}/flair-install/bin/tools/$ARCH_DIR/dualshock3 $*
  • trunk/tools/Controller/DualShock3/src/DualShock3.cpp

    r318 r467  
    3939#include <bluetooth/hci_lib.h>
    4040#include <sys/file.h>
     41#include <linux/input.h>
    4142
    4243#define USB_DIR_IN 0x80
     
    113114  dataSize = 6;
    114115  datas = new int8_t[dataSize];
     116  for(int i=0;i<dataSize;i++) datas[i]=0;
    115117
    116118  GroupBox *settingsGroupBox =
     
    148150      setup_device(dev);
    149151    }
    150   } else if (connectionType == Usb) {
     152  } else if (connectionType == UsbHidRaw) {
    151153    int nr, i;
    152154    unsigned char buf[128];
     
    180182    }
    181183    if (i == 255) {
    182       Thread::Err("sixad-raw::open(hidrawX) - failed to open hidraw device\n");
     184      Thread::Err("open(hidrawX) - failed to open hidraw device\n");
    183185      return;
    184186    }
     
    186188    // block until PS button is pressed
    187189    if ((nr = read(usb_fd, buf, sizeof(buf))) < 0) {
    188       Thread::Err("sixad-raw::read(fd) - failed to read from device\n");
     190      Thread::Err("read(fd) - failed to read from device\n");
    189191    }
    190192
    191193    if (nr < 49 || nr > 50) {
    192       Thread::Err("sixad-raw::read(fd) - not a sixaxis (nr = %i )\n", nr);
     194      Thread::Err("read(fd) - not a sixaxis (nr = %i )\n", nr);
     195    }
     196  } else if (connectionType == UsbEvent) {
     197    int nr, i;
     198    unsigned char buf[128];
     199
     200    for (i = 0; i < 255; i++) {
     201      ostringstream dev_name;
     202      dev_name << "/dev/input/event" << i;
     203      if ((usb_fd = open(dev_name.str().c_str(), O_RDONLY)) >= 0) {
     204        int res=flock(usb_fd, LOCK_EX|LOCK_NB);
     205        if(res<0) {
     206          Thread::Warn("%s seems to be locked by another application\n", dev_name.str().c_str());
     207          close(usb_fd);
     208          continue;
     209        }
     210       
     211        unsigned short id[4];
     212        res = ioctl(usb_fd, EVIOCGID, id);
     213        if (res < 0) {
     214          Thread::Err("ioctl error (EVIOCGID) on %s\n",dev_name.str().c_str());
     215        } else {
     216          if (id[ID_VENDOR] == 0x054c && id[ID_PRODUCT] == 0x0268) {
     217            Printf("successfully opened %s\n", dev_name.str().c_str());
     218            Printf("Press PS button to turn the controller on\n");
     219            break;
     220          }
     221        }
     222        flock(usb_fd, LOCK_UN);
     223        close(usb_fd);
     224      }
     225    }
     226    if (i == 255) {
     227      Thread::Err("open(/dev/input/eventX) - failed to open event device\n");
     228      Thread::Err("you can try with the old usb_hidraw method\n");
     229      return;
     230    }
     231   
     232    // block until PS button is pressed
     233    struct input_event tmp;
     234    if ((nr = read(usb_fd, &tmp, sizeof(struct input_event))) < 0) {
     235      Thread::Err("read(fd) - failed to read from device\n");
    193236    }
    194237  }
     
    197240
    198241DualShock3::~DualShock3() {
    199   if (connectionType == Usb) {
     242  if (connectionType == UsbHidRaw || connectionType == UsbEvent) {
    200243    flock(usb_fd, LOCK_UN);
    201244    close(usb_fd);
     
    335378    return false;
    336379
    337   } else if (connectionType == Usb) {
     380  } else if (connectionType == UsbHidRaw) {
    338381    int nr = read(usb_fd, report, sizeof(report));
    339382    return parse_report_sixaxis_ds3(report, nr);
     383  } else if (connectionType == UsbEvent) {
     384    return parse_input_event();
    340385  }
    341386  return false;
     387}
     388
     389uint8_t DualShock3::SetBitValue(int8_t &reg,uint8_t mask,uint8_t value) {
     390  reg=(reg&~mask) | value*mask;
     391}
     392
     393bool DualShock3::parse_input_event(void) {
     394  struct input_event ev[64];
     395        int i, rd;
     396
     397  rd = read(usb_fd, ev, sizeof(struct input_event) * 64);
     398
     399  if (rd < (int) sizeof(struct input_event)) {
     400    printf("expected %d bytes, got %d\n", (int) sizeof(struct input_event), rd);
     401    return false;
     402  }
     403
     404  for (i = 0; i < rd / sizeof(struct input_event); i++) {
     405    if (ev[i].type == EV_ABS) {
     406      //code 2 and 5 are z/rz (l2 and r2)
     407      if(ev[i].code==0) datas[2] = compute_dead_zone(0, ev[i].value);//x
     408      if(ev[i].code==1) datas[3] = compute_dead_zone(1, ev[i].value);//y
     409      if(ev[i].code==3) datas[4] = compute_dead_zone(2, ev[i].value);//rx
     410      if(ev[i].code==4) datas[5] = compute_dead_zone(3, ev[i].value);//ry
     411    }
     412    if (ev[i].type == EV_KEY) {
     413      if(ev[i].code==BTN_START) SetBitValue(datas[0],0x08,ev[i].value);
     414      if(ev[i].code==BTN_SELECT) SetBitValue(datas[0],0x01,ev[i].value);
     415      if(ev[i].code==BTN_WEST) SetBitValue(datas[1],0x80,ev[i].value);// square
     416      if(ev[i].code==BTN_NORTH) SetBitValue(datas[1],0x10,ev[i].value);// triangle
     417      if(ev[i].code==BTN_EAST) SetBitValue(datas[1],0x20,ev[i].value);// circle
     418      if(ev[i].code==BTN_SOUTH) SetBitValue(datas[1],0x40,ev[i].value);// cross
     419      if(ev[i].code==BTN_TL) SetBitValue(datas[1],0x04,ev[i].value);
     420      if(ev[i].code==BTN_TL2) SetBitValue(datas[1],0x01,ev[i].value);
     421      if(ev[i].code==BTN_THUMBL) SetBitValue(datas[0],0x02,ev[i].value);
     422      if(ev[i].code==BTN_TR) SetBitValue(datas[1],0x08,ev[i].value);
     423      if(ev[i].code==BTN_TR2) SetBitValue(datas[1],0x02,ev[i].value);
     424      if(ev[i].code==BTN_THUMBR) SetBitValue(datas[0],0x04,ev[i].value);
     425      if(ev[i].code==BTN_DPAD_UP) SetBitValue(datas[0],0x10,ev[i].value);
     426      if(ev[i].code==BTN_DPAD_DOWN) SetBitValue(datas[0],0x40,ev[i].value);
     427      if(ev[i].code==BTN_DPAD_LEFT) SetBitValue(datas[0],0x80,ev[i].value);
     428      if(ev[i].code==BTN_DPAD_RIGHT) SetBitValue(datas[0],0x20,ev[i].value);
     429    }
     430 
     431    if (GetTime() > (last_voltage_time + 5 * (Time)1000000000)) {
     432      // toute les 5 secondes
     433      // report battery charge level
     434      batteryChargeLevel->SetText("battery: usb connected");
     435      last_voltage_time = GetTime();
     436    }
     437  }
     438  return true;
    342439}
    343440
     
    357454        batteryChargeLevel->SetText("battery: %i/5", r[30]);
    358455      }
    359       if (connectionType == Usb) {
     456      if (connectionType == UsbHidRaw) {
    360457        batteryChargeLevel->SetText("battery: usb connected");
    361458      }
     
    388485
    389486void DualShock3::GetButtonData() {
    390   // static uint8_t old_start_button=0;
    391487  button->GetMutex();
    392488  button->SetValueNoMutex(0, 0, (datas[0] & 0x08) == 0 ? 0 : 1); // start
    393   /*
    394       uint8_t start_button=datas[0]&0x08;
    395       if (start_button!=old_start_button) {
    396           if (start_button==0) {
    397               Thread::Info("Debug: start button released\n");
    398           } else {
    399               Thread::Info("Debug: start button pressed\n");
    400           }
    401           old_start_button=start_button;
    402       }
    403   */
    404489  button->SetValueNoMutex(1, 0, (datas[0] & 0x01) == 0 ? 0 : 1); // select
    405490  button->SetValueNoMutex(2, 0, (datas[1] & 0x80) == 0 ? 0 : 1); // square
  • trunk/tools/Controller/DualShock3/src/DualShock3.h

    r137 r467  
    5050class DualShock3 : public HostEthController {
    5151public:
    52   typedef enum { Usb, Bluetooth } ConnectionType_t;
     52  typedef enum { UsbHidRaw, UsbEvent,Bluetooth } ConnectionType_t;
    5353
    5454  DualShock3(std::string name,
     
    7979  void setup_device(struct motion_dev *dev);
    8080  bool parse_report_sixaxis_ds3(unsigned char *r, int len);
     81  bool parse_input_event(void);
    8182  int mystr2ba(const char *s, bdaddr_t *ba);
    8283  char *myba2str(const bdaddr_t *ba);
    8384  int8_t compute_dead_zone(int axis, unsigned char value);
     85  uint8_t SetBitValue(int8_t &reg,uint8_t mask,uint8_t value);
    8486  struct motion_dev *dev;
    8587  int usb_fd;
  • trunk/tools/Controller/DualShock3/src/main.cpp

    r433 r467  
    4747  if (connection == "usb") {
    4848    joystick = new DualShock3("dualshock3", receiverAddress,
    49                               receiverPort, DualShock3::Usb, period, 6);
    50   } else {
     49                              receiverPort, DualShock3::UsbEvent, period, 6);
     50  } else if (connection == "usb_hidraw") {
     51    joystick = new DualShock3("dualshock3", receiverAddress,
     52                              receiverPort, DualShock3::UsbHidRaw, period, 6);
     53  } else if (connection == "bluetooth") {
    5154    joystick = new DualShock3("dualshock3", receiverAddress,
    5255                              receiverPort, DualShock3::Bluetooth, period, 6);
     56  } else {
     57    manager->Err("unknown connection type\n");
     58    delete manager;
     59    return -1;
    5360  }
    5461
     
    7380
    7481    ValueArg<string> connectionArg("c", "connection",
    75                                    "connection type (usb or bluetooth)", false,
     82                                   "connection type (usb, usb_hidraw or bluetooth)", false,
    7683                                   "bluetooth", "string");
    7784    cmd.add(connectionArg);
Note: See TracChangeset for help on using the changeset viewer.