#include //bibliothèque USB #include #include #include #include #include #include #define LED_ON 0x21 #define LED_OFF 0x22 #define MOVE_RIGHT 0x04 #define MOVE_LEFT 0x08 #define MOVE_STOP 0x10 #define ACM_CTRL_DTR 0x01 #define ACM_CTRL_RTS 0x02 static int ep_in_addr = 0x83; static int ep_out_addr = 0x04; libusb_device **list; libusb_device_handle *handle = NULL; libusb_device *device; void configuration_periph(libusb_device *device) //Configuration de la tourelle { //Ouverture du pepherique int status=libusb_open(device,&handle); if(status!=0) { perror("libusb_open"); exit(-1); } //Recuperation de la configuration d'indice 0 du périphérique struct libusb_config_descriptor *config; status=libusb_get_active_config_descriptor(device,&config); if(status!=0) { perror("libusb_get_active_config_descriptor"); exit(-1); } //Interfaces int i; int interface; //On détache le noyau de notre périphérique for(i=0;i<(config->bNumInterfaces);i++) { interface=config->interface[i].altsetting[0].bInterfaceNumber; if(libusb_kernel_driver_active(handle,interface)) { status=libusb_detach_kernel_driver(handle,interface); if(status!=0) { perror("libusb_detach_kernel_driver"); exit(-1); } } } //Utilisation d'une configuration du périphérique int configuration=config->bConfigurationValue; status=libusb_set_configuration(handle,configuration); if(status!=0) { perror("libusb_set_configuration"); exit(-1); } //Claim interfaces for(i=0;i<(config->bNumInterfaces);i++) { interface=config->interface[i].altsetting[0].bInterfaceNumber; printf("Numero d'interface : %d\n", interface); status=libusb_claim_interface(handle,interface); if(status!=0) { perror("libusb_claim_interface"); exit(-1); } } } void fermeture(libusb_device_handle *handle) //Fermeture du périphérique { int i, interface, status; libusb_device *device = libusb_get_device(handle); struct libusb_config_descriptor *config; status = libusb_get_active_config_descriptor(device, &config); if(status != 0) { perror("libusb_get_active_config_descriptor");exit(-1); } for(i=0;i<(config->bNumInterfaces);i++) { interface=config->interface[i].altsetting[0].bInterfaceNumber; status=libusb_release_interface(handle,interface); //On libère l'interface if(status!=0) { perror("libusb_release_interface"); exit(-1); } printf("Interface liberee : %d\n",interface); } libusb_close(handle); } void enumeration(libusb_context *context) //Enumération des périphériques USB { int t=0; libusb_device **list; ssize_t count=libusb_get_device_list(context,&list); if(count<0) { perror("libusb_get_device_list"); exit(-1); } ssize_t i=0; for(i=0;i 0x80, 0x25 in little endian */ //Configuration du port série. unsigned char encoding[] = { 0x80, 0x25, 0x00, 0x00, 0x00, 0x00, 0x08 }; status = libusb_control_transfer(handle, 0x21, 0x20, 0, 0, encoding, sizeof(encoding), 0); if (status < 0) { fprintf(stderr, "Error during control transfer: %s\n", libusb_error_name(status)); } //Menu de commande char action; for(;;) { printf("s=fermer / d=allumer led / g=eteindre led / h=moteur gauche / b=moteur droit / t=moteur éteint \n"); scanf("%c",&action); switch (action) { case 's': fermeture(handle); libusb_free_device_list(list,1); libusb_exit(context); return 0; break; case 'd': envoie(LED_ON); sleep(1); //len = read_chars(buf, 1); break; case 'g': envoie(LED_OFF); sleep(1); break; case 'h': envoie(MOVE_LEFT); sleep(1); break; case 'b': envoie(MOVE_RIGHT); sleep(1); break; case 't' : envoie(MOVE_STOP); sleep(1); break; default : printf("Commande invalide ! Retaper :\n"); break; } } return 0; }