Changeset 467 in flair-src for trunk/tools/Controller/DualShock3/src
- Timestamp:
- Mar 9, 2022, 5:44:06 PM (3 years ago)
- Location:
- trunk/tools/Controller/DualShock3/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/Controller/DualShock3/src/DualShock3.cpp
r318 r467 39 39 #include <bluetooth/hci_lib.h> 40 40 #include <sys/file.h> 41 #include <linux/input.h> 41 42 42 43 #define USB_DIR_IN 0x80 … … 113 114 dataSize = 6; 114 115 datas = new int8_t[dataSize]; 116 for(int i=0;i<dataSize;i++) datas[i]=0; 115 117 116 118 GroupBox *settingsGroupBox = … … 148 150 setup_device(dev); 149 151 } 150 } else if (connectionType == Usb ) {152 } else if (connectionType == UsbHidRaw) { 151 153 int nr, i; 152 154 unsigned char buf[128]; … … 180 182 } 181 183 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"); 183 185 return; 184 186 } … … 186 188 // block until PS button is pressed 187 189 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"); 189 191 } 190 192 191 193 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"); 193 236 } 194 237 } … … 197 240 198 241 DualShock3::~DualShock3() { 199 if (connectionType == Usb ) {242 if (connectionType == UsbHidRaw || connectionType == UsbEvent) { 200 243 flock(usb_fd, LOCK_UN); 201 244 close(usb_fd); … … 335 378 return false; 336 379 337 } else if (connectionType == Usb ) {380 } else if (connectionType == UsbHidRaw) { 338 381 int nr = read(usb_fd, report, sizeof(report)); 339 382 return parse_report_sixaxis_ds3(report, nr); 383 } else if (connectionType == UsbEvent) { 384 return parse_input_event(); 340 385 } 341 386 return false; 387 } 388 389 uint8_t DualShock3::SetBitValue(int8_t ®,uint8_t mask,uint8_t value) { 390 reg=(reg&~mask) | value*mask; 391 } 392 393 bool 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; 342 439 } 343 440 … … 357 454 batteryChargeLevel->SetText("battery: %i/5", r[30]); 358 455 } 359 if (connectionType == Usb ) {456 if (connectionType == UsbHidRaw) { 360 457 batteryChargeLevel->SetText("battery: usb connected"); 361 458 } … … 388 485 389 486 void DualShock3::GetButtonData() { 390 // static uint8_t old_start_button=0;391 487 button->GetMutex(); 392 488 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 */404 489 button->SetValueNoMutex(1, 0, (datas[0] & 0x01) == 0 ? 0 : 1); // select 405 490 button->SetValueNoMutex(2, 0, (datas[1] & 0x80) == 0 ? 0 : 1); // square -
trunk/tools/Controller/DualShock3/src/DualShock3.h
r137 r467 50 50 class DualShock3 : public HostEthController { 51 51 public: 52 typedef enum { Usb ,Bluetooth } ConnectionType_t;52 typedef enum { UsbHidRaw, UsbEvent,Bluetooth } ConnectionType_t; 53 53 54 54 DualShock3(std::string name, … … 79 79 void setup_device(struct motion_dev *dev); 80 80 bool parse_report_sixaxis_ds3(unsigned char *r, int len); 81 bool parse_input_event(void); 81 82 int mystr2ba(const char *s, bdaddr_t *ba); 82 83 char *myba2str(const bdaddr_t *ba); 83 84 int8_t compute_dead_zone(int axis, unsigned char value); 85 uint8_t SetBitValue(int8_t ®,uint8_t mask,uint8_t value); 84 86 struct motion_dev *dev; 85 87 int usb_fd; -
trunk/tools/Controller/DualShock3/src/main.cpp
r433 r467 47 47 if (connection == "usb") { 48 48 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") { 51 54 joystick = new DualShock3("dualshock3", receiverAddress, 52 55 receiverPort, DualShock3::Bluetooth, period, 6); 56 } else { 57 manager->Err("unknown connection type\n"); 58 delete manager; 59 return -1; 53 60 } 54 61 … … 73 80 74 81 ValueArg<string> connectionArg("c", "connection", 75 "connection type (usb or bluetooth)", false,82 "connection type (usb, usb_hidraw or bluetooth)", false, 76 83 "bluetooth", "string"); 77 84 cmd.add(connectionArg);
Note:
See TracChangeset
for help on using the changeset viewer.