⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tusb.c

📁 一种linux下的看摄像头的画面的软件
💻 C
字号:
#include <stdio.h>#include <fcntl.h>#include <usb.h>#include <sys/types.h>#include <sys/stat.h>#include "bayer.h"#include "gamma.h"#define USB_DIR_OUT 0#define USB_DIR_IN  0x80#define USB_TYPE_VENDOR (0x02 << 5)#define PPM_HEADER "\x50\x36\x0a\x33\x35\x32\x20\x32\x38\x38\x0a\x32\x35\x35\x0a"#define PPM_HEADER_SMALL "\x50\x36\x0a\x31\x37\x36\x20\x31\x34\x34\x0a\x32\x35\x35\x0a"void print_endpoint(struct usb_endpoint_descriptor *endpoint){    printf("	  bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);    printf("  	  bmAttributes:     %02xh\n", endpoint->bmAttributes);    printf("	  wMaxPacketSize:   %d\n", endpoint->wMaxPacketSize);    printf("	  bInterval:        %d\n", endpoint->bInterval);    printf("	  bRefresh:         %d\n", endpoint->bRefresh);    printf("	  bSynchAddress:    %d\n", endpoint->bSynchAddress);}void print_altsetting(struct usb_interface_descriptor *interface){    int i;        printf("	bInterfaceNumber:    %d\n", interface->bInterfaceNumber);    printf("	bAlternateSetting:   %d\n", interface->bAlternateSetting);    printf("	bNumEndpoints:       %d\n", interface->bNumEndpoints);    printf("	bInterfaceClass:     %d\n", interface->bInterfaceClass);    printf("	bInterfaceSubClass:  %d\n", interface->bInterfaceSubClass);    printf("	bInterfaceProtocol:  %d\n", interface->bInterfaceProtocol);    printf("	iInterface:          %d\n", interface->iInterface);        for (i = 0; i < interface->bNumEndpoints; i++)	print_endpoint(&interface->endpoint[i]);}void print_interface(struct usb_interface *interface){    int i;        for (i = 0; i < interface->num_altsetting; i++)	print_altsetting(&interface->altsetting[i]);}void print_configuration(struct usb_config_descriptor *config){    int i;        printf("  wTotalLength:        %d\n", config->wTotalLength);    printf("  bNumInterfaces:      %d\n", config->bNumInterfaces);    printf("  bConfigurationValue: %d\n", config->bConfigurationValue);    printf("  iConfiguration:      %d\n", config->iConfiguration);    printf("  bmAttributes:        %02xh\n", config->bmAttributes);    printf("  MaxPower:            %d\n", config->MaxPower);        for (i = 0; i < config->bNumInterfaces; i++)	print_interface(&config->interface[i]);	}unsigned int get_pic_num (char * data){  unsigned int i;  for (i=0; i * 0x10 < 0x4000; i++)    if (!data[i*0x10]) return i;  return 0;}unsigned int get_pic_width (char * data, unsigned int n){  switch (data[n * 0x10]) {  case 0x41:  case 0x61:    return 352;  case 0x42:  case 0x62:    return 176;  default:    return 0;  }}unsigned int get_pic_height (char * data, unsigned int n){  switch (data[n * 0x10]) {  case 0x41:  case 0x61:    return 288;  case 0x42:  case 0x62:    return 144;  default:    return 0;  }}void camera_reset (usb_dev_handle *udevh){  char c;  int ret;  c=0xff;  ret = usb_control_msg(udevh, (int)(USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE), 			0x0c, 0xc0, 0x00, &c, 1, 5000);#ifdef DEBUG  printf("return: ret: %d, %02X		(13)\n",ret,c);   #endif  ret = usb_control_msg(udevh, (int)(USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE), 			0x0c, 0x06, 0xa0, &c, 1, 5000);#ifdef DEBUG  printf("return: ret: %d, %02X		(14)\n",ret,c);   #endif  ret = usb_control_msg(udevh, (int)(USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE), 			0x0c, 0x07, 0x00, &c, 1, 5000);#ifdef DEBUG  printf("return: ret: %d, %02X		(15)\n",ret,c);   #endif}int main(int argc, char** argv){    struct usb_bus *bus;    struct usb_device *dev;    int i,j, ret, fd, pix_num, pix_width, pix_height, read;    char ch[5];    char setup[0x4000];    unsigned char t[256];    unsigned char *d, *buf, b, *filename;    usb_dev_handle *udev = NULL;        usb_init();        usb_find_busses();    usb_find_devices();    #ifdef DEBUG    printf("bus/device	idVendor/idProduct\n");#endif    for (bus = usb_busses; bus; bus = bus->next) {	for (dev = bus->devices; dev; dev = dev->next) {#ifdef DEBUG	    printf("%s/%s	%04X/%04X\n", bus->dirname, dev->filename,	      dev->descriptor.idVendor, dev->descriptor.idProduct);#endif	    if (!dev->config) {		printf("  Couldn't retrieve descriptors\n");		continue;	    }	    if (dev->descriptor.idVendor == 0x2770 && dev->descriptor.idProduct == 0x9120)	    {#ifdef DEBUG		for (i = 0; i < dev->descriptor.bNumConfigurations; i++)		    print_configuration(&dev->config[i]);#endif		udev = usb_open(dev);	    }	} /* for dev */    } /* for bus */    if (udev) {		usb_set_configuration(udev,1);		usb_claim_interface(udev,0);		ch[0]=0;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x06, 0xf0, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(4)\n",ret,ch[0]);   #endif		ret = usb_control_msg(udev, (int)(USB_DIR_IN | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x07, 0x0000, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(5)\n",ret,ch[0]);   #endif		ret = usb_control_msg(udev, (int)(USB_DIR_IN | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x07, 0x0000, ch, 4, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X %02X %02X %02X	(6)\n",		       ret,ch[0],ch[1],ch[2],ch[3]);#endif		ch[0]=0x50;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x06, 0xa0, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %d		(7)\n",ret,ch[0]);#endif		ret = usb_control_msg(udev, (int)(USB_DIR_IN | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x07, 0x00, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %d		(8)\n",ret,ch[0]);   #endif		/* ^^ 8-ig, MyDSC start */		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x06, 0xf0, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(9)\n",ret,ch[0]);   #endif		ret = usb_control_msg(udev, (int)(USB_DIR_IN | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x07, 0x0000, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(10)\n",ret,ch[0]);   #endif		ch[0]=0xff;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x03, 0x04, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(11)\n",ret,ch[0]);   #endif		sleep(1);		ret = usb_bulk_read(udev, 0x81, ch, 4,5000);#ifdef DEBUG		printf("return: ret: %d, %02X %02X %02X %02X	(12)\n",		       ret,ch[0],ch[1],ch[2],ch[3]);#endif		/* 09 05 00 26 kell legyen a visszateres */		if (ret < 0) {		    usb_resetep(udev,0x81);		    sleep(1);		    ret = usb_bulk_read(udev, 0x81, ch, 4,5000);#ifdef DEBUG		    printf("return: ret: %d, %02X %02X %02X %02X	(12)\n",			   ret,ch[0],ch[1],ch[2],ch[3]);#endif		}		camera_reset(udev);		ch[0]=0x00;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x06, 0x20, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d 		(16)\n",ret);   #endif		ch[0]=0x00;		ret = usb_control_msg(udev, (int)(USB_DIR_IN | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x07, 0x00, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(17)\n",ret,ch[0]);   #endif		ch[0]=0xff;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x03, 0x4000, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(18)\n",ret,ch[0]);   #endif		ret = usb_bulk_read(udev, 0x81, setup, 0x4000,5000);		if (ret > 0) pix_num=get_pic_num(setup);		else pix_num = 0;#ifdef DEBUG		printf("return: ret: %d, %02X %02X %02X %02X	(19)\n",		       ret,setup[0],setup[1],setup[2],setup[3]);		if (ret > 0) {		    pix_num=get_pic_num(setup);		    fd = open("setup.dat",O_RDWR | O_CREAT | O_TRUNC, S_IRUSR \			      | S_IWUSR | S_IRGRP | S_IROTH);		    ret = write(fd, &setup, ret);		    close(fd);		}		for (j = 1; j < 65; j++)		{		    printf(" %02X ",setup[j]);		    if (j % 16 == 0) printf("\n");		}#endif		camera_reset(udev);		ch[0]=0x00;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \		      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0xc0, 0x00, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d 		(23)\n",ret);   #endif		ch[0]=0x00;		ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x06, 0x30, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d 		(24)\n",ret);   #endif		ch[0]=0x00;		ret = usb_control_msg(udev, (int)(USB_DIR_IN | \                      USB_TYPE_VENDOR | USB_RECIP_DEVICE), 		      0x0c, 0x07, 0x00, &ch[0], 1, 5000);#ifdef DEBUG		printf("return: ret: %d, %02X		(25)\n",ret,ch[0]);   #endif		printf("No. of PIX: %d\n", pix_num);		for (i=0; i<pix_num; i++) {		  printf("processinc picture: %d\n",i);		  /* read one pic */		  pix_width=get_pic_width(setup,i);		  pix_height=get_pic_height(setup,i);		  read=pix_width * pix_height;		  d = calloc(read,1);		  do {		    ch[0]=0x00;	       		    ret = usb_control_msg(udev, (int)(USB_DIR_OUT | \			  USB_TYPE_VENDOR | USB_RECIP_DEVICE), 			  0x0c, 0x03, 			  (read>0x8000) ? 0x8000 : read, &ch[0], 1, 5000);#ifdef DEBUG		    printf("return: ret: %d	   (26)\n",ret);#endif		    ret = usb_bulk_read(udev, 0x81,			  d + (pix_width*pix_height - read),			  (read>0x8000) ? 0x8000 : read,5000);#ifdef DEBUG		    printf("return: ret: %d        (19)\n",ret);#endif		    read -= ret;		  } while (read);		  if (ret != -1) {		    /* Reverse the data, else the pic is upside down. */		    for (j = 0; j < pix_height*pix_width / 2; j++) {		      b = d[j];		      d[j] = d[pix_height*pix_width - 1 - j];		      d[pix_height*pix_width - 1 - j] = b;		    } 		    /* Decode the data and postprocess. */		    buf = malloc (3*pix_height*pix_width);		    if (!buf) return -1;		    gp_bayer_decode (d, pix_width, pix_height, buf, 2);		    gamma_fill_table (t, .65);		    gamma_correct_single (t, buf, pix_width * pix_height);		    filename=malloc(10);		    snprintf(filename,10,"pix%0d.ppm",i);		    fd = open(filename,O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | \			      S_IWUSR | S_IRGRP | S_IROTH);		    free(filename);		    ret = write(fd, 			  (pix_width == 352)? PPM_HEADER : PPM_HEADER_SMALL, 			  15);		    ret = write(fd, buf, 3*pix_height*pix_width);#ifdef DEBUG		    printf("wrote %d bytes\n",ret + 15);#endif		    close(fd);		    free(buf);		  } /* ret != -1 */		  free(d);   		} /* for (i=0;i<pix_num;i++) */		camera_reset(udev);		usb_release_interface(udev,0);		usb_reset(udev);		//usb_resetep(udev,0x81);		usb_close(udev);    } else {       /* udev == NULL */      printf("No camera found!\n\n");    }        return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -