Changeset 467 in flair-src for trunk/tools/Controller/DualShock3/src/DualShock3.cpp
- Timestamp:
- Mar 9, 2022, 5:44:06 PM (3 years ago)
- File:
-
- 1 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
Note:
See TracChangeset
for help on using the changeset viewer.