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

📄 ptpcanon.c

📁 linux下佳能数码相机使用源码
💻 C
📖 第 1 页 / 共 3 页
字号:
    /* Catch event, attempt  2 */    if (val16!=PTP_RC_OK) {        if (PTP_RC_OK==ptp_usb_event_wait (params, &evc)) {            if (evc.Code==PTP_EC_StorageInfoChanged)                printf("Event: exiting  shooting mode.\n");            else                printf("Event: 0x%X\n", evc.Code);        } else            printf("No expected mode change event.\n");    }    return PTP_RC_OK;}uint16_t download_object(PTPParams *params, uint32_t handle, char *filename){    FILE *f;    char *image;    uint32_t dummy;    uint32_t size;    uint16_t res;    uint32_t bsize=5000;    uint32_t nblocks;     uint32_t tail;  /* remainder */    uint32_t pos;    int i;        CRE2(res=ptp_canon_getobjectsize (params, handle, 0, &size, &dummy));    if (res!=PTP_RC_OK)        return res;    if (size==0) {        fprintf(stderr, "Empty object\n");        return res;    }    printf("Downloading %s...\n", filename);    f = fopen(filename,"w");    if (!f) {        fprintf(stderr, "Cannot open file %s\n", filename);        return res;    }    nblocks=size/bsize; /* full blocks */    tail=size%bsize;    for(i=0; i<nblocks; i++) {        int readnum;        pos = i==0?1:((tail==0)&&(i==nblocks-1))?3:2;        image=NULL;        CRE2(res=ptp_canon_getpartialobject (params, handle, i*bsize, bsize, pos, &image, &readnum));        if (res==PTP_RC_OK) {            if (readnum!=bsize)                printf("Image block read error\n");            fwrite(image,1,readnum,f);        }        free(image);    }    /* the last block */    if (tail!=0) {        uint32_t readnum;        pos=nblocks==0?1:3;        image=NULL;        CRE2(res=ptp_canon_getpartialobject (params, handle, nblocks*bsize, tail, pos, &image, &readnum));        if (res==PTP_RC_OK) {            if (readnum!=tail)                printf("Image remainder read error\n");            fwrite(image,1,readnum,f);        }        free(image);    }    fclose(f);    printf("Image %s saved\n", filename);    return PTP_RC_OK;}void take_shot (int busn, int devn,uint16_t force, char* filename, uint8_t flash, uint16_t zoom, int on_button){    PTP_USB ptp_usb;    struct usb_device *dev;    PTPParams params;    PTPUSBEventContainer event;    PTPContainer evc;    int isevent;    uint32_t entnum;    int i;    uint16_t res;    uint16_t val16;    uint32_t val32;    uint16_t *pval16;    uint32_t *pval32;    uint32_t handle;    CR(connect_camera(busn, devn, force, &ptp_usb, &params, &dev),        "Could not connect camera!\n");    CR(enter_shooting_mode(&params),        "Could not enter shooting mode!\n");    if (on_button) while(1) {        CRE(ptp_canon_checkevent(&params,&event,&isevent));        if (isevent && event.code==0xc00b) {                printf("Event: Shot pressed. \n");                break;        } else if (isevent && event.length>0) {            printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                event.length,event.type,event.code,event.trans_id,                event.param1, event.param2, event.param3);        }        usleep(100000);    }    printf("Setting Flash mode to %d\n",flash);    CRE(ptp_setdevicepropvalue(&params, PTP_DPC_CANON_FlashMode, &flash, PTP_DTC_UINT8));    printf("Setting Zoom value to %d\n",zoom);    CRE(ptp_setdevicepropvalue(&params, PTP_DPC_CANON_Zoom, &zoom, PTP_DTC_UINT16));    /* take a "real" shot */     val16=3;	CRE(ptp_setdevicepropvalue(&params, PTP_DPC_CANON_D029, &val16, PTP_DTC_UINT16));    printf("Initiating capture...\n");    CRE(res=ptp_canon_initiatecaptureinmemory (&params));    if (res==PTP_RC_OK) {        /* Catch event */        if (PTP_RC_OK==(val16=ptp_usb_event_wait (&params, &evc))) {            if (evc.Code==PTP_EC_CaptureComplete)                printf("Event: capture complete. \n");            else                printf("Event: 0x%X\n", evc.Code);                    } else            printf("No event yet, we'll try later.\n");        printf("Checking events in stack.\n");        for (i=0;i<100;i++) {            CRE(res=ptp_canon_checkevent(&params,&event,&isevent));            if (res!=PTP_RC_OK)                continue;            if (isevent  &&                 (event.type==PTP_USB_CONTAINER_EVENT) &&                (event.code==PTP_EC_CANON_RequestObjectTransfer) ) {                    handle=event.param1;                    printf("PTP_EC_CANON_RequestObjectTransfer, object handle=0x%X. \n",event.param1);                    printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                        event.length,event.type,event.code,event.trans_id,                        event.param1, event.param2, event.param3);                    int j;                    for (j=0;j<2;j++) {                        CRE(res=ptp_canon_checkevent(&params,&event,&isevent));                        if ((res==PTP_RC_OK) && isevent)                            printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                                event.length,event.type,event.code,event.trans_id,                                event.param1, event.param2, event.param3);                    }                    break;                }        }        /* Catch event, attempt  2 */        if (val16!=PTP_RC_OK) {            if (PTP_RC_OK==ptp_usb_event_wait (&params, &evc)) {                if (evc.Code==PTP_EC_CaptureComplete)                    printf("Event: capture complete. \n");                else                    printf("Event: 0x%X\n", evc.Code);            } else                printf("No expected capture complete event\n");        }                if (i==100)            fprintf(stderr,"ERROR: Capture timed out!\n");                else            download_object(&params, handle, filename);    }        CR(exit_shooting_mode(&params),        "Could not exit shooting mode!\n");    CR(disconnect_camera(&ptp_usb, &params, dev),        "Disconnecting failed!\n");}voidreset_device (int busn, int devn, short force){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	uint16_t status;	uint16_t devstatus[2] = {0,0};	int ret;#ifdef DEBUG	printf("dev %i\tbus %i\n",devn,busn);#endif	dev=find_device(busn,devn,force);	if (dev==NULL) {		fprintf(stderr,"could not find any device matching given "		"bus/dev numbers\n");		exit(-1);	}	find_endpoints(dev,&ptp_usb.inep,&ptp_usb.outep,&ptp_usb.intep);	init_ptp_usb(&params, &ptp_usb, dev);	// get device status (devices likes that regardless of its result)	usb_ptp_get_device_status(&ptp_usb,devstatus);		// check the in endpoint status	ret = usb_get_endpoint_status(&ptp_usb,ptp_usb.inep,&status);	if (ret<0) perror ("usb_get_endpoint_status()");	// and clear the HALT condition if happend	if (status) {		printf("Resetting input pipe!\n");		ret=usb_clear_stall_feature(&ptp_usb,ptp_usb.inep);		if (ret<0)perror ("usb_clear_stall_feature()");	}	status=0;	// check the out endpoint status	ret = usb_get_endpoint_status(&ptp_usb,ptp_usb.outep,&status);	if (ret<0) perror ("usb_get_endpoint_status()");	// and clear the HALT condition if happend	if (status) {		printf("Resetting output pipe!\n");		ret=usb_clear_stall_feature(&ptp_usb,ptp_usb.outep);		if (ret<0)perror ("usb_clear_stall_feature()");	}	status=0;	// check the interrupt endpoint status	ret = usb_get_endpoint_status(&ptp_usb,ptp_usb.intep,&status);	if (ret<0)perror ("usb_get_endpoint_status()");	// and clear the HALT condition if happend	if (status) {		printf ("Resetting interrupt pipe!\n");		ret=usb_clear_stall_feature(&ptp_usb,ptp_usb.intep);		if (ret<0)perror ("usb_clear_stall_feature()");	}	// get device status (now there should be some results)	ret = usb_ptp_get_device_status(&ptp_usb,devstatus);	if (ret<0) 		perror ("usb_ptp_get_device_status()");	else	{		if (devstatus[1]==PTP_RC_OK) 			printf ("Device status OK\n");		else			printf ("Device status 0x%04x\n",devstatus[1]);	}		// finally reset the device (that clears prevoiusly opened sessions)	ret = usb_ptp_device_reset(&ptp_usb);	if (ret<0)perror ("usb_ptp_device_reset()");	// get device status (devices likes that regardless of its result)	usb_ptp_get_device_status(&ptp_usb,devstatus);	usb_release_interface(ptp_usb.handle,		dev->config->interface->altsetting->bInterfaceNumber);}/* main program  */intmain(int argc, char ** argv){	int busn=0,devn=0;    	int flash=0;	int zoom=0;    int on_button=0;    	int action=0;	short force=0;	uint16_t property=0;	char* value=NULL;    char filename[MAXNAMELEN];	/* parse options */	int option_index = 0,opt;	static struct option loptions[] = {		{"help",0,0,'h'},		{"bus",1,0,'B'},		{"dev",1,0,'D'},		{"reset",0,0,'r'},		{"list-devices",0,0,'l'},		{"list-properties",0,0,'p'},		{"show-property",1,0,'s'},		{"set-property",1,0,'s'},		{"val",1,0,0},		{"force",0,0,'f'},		{"verbose",2,0,'v'},		{"canon-test",0,0,'c'},		{"take-shot",0,0,'t'},		{"name",1,0,'N'},		{"flash",1,0,'F'},		{"zoom",1,0,'Z'},		{"on-button",0,0,'b'},		{0,0,0,0}	};        strcpy(filename,"noname.jpg");		while(1) {		opt = getopt_long (argc, argv, "hlpfrctbs:D:N:F:B:Z:v::", loptions, &option_index);		if (opt==-1) break;			switch (opt) {		case 0:			if (!(strcmp("val",loptions[option_index].name)))				value=strdup(optarg);			break;		case 'h':			help();			break;		case 'B':			busn=strtol(optarg,NULL,10);			break;		case 'D':			devn=strtol(optarg,NULL,10);			break;		case 'f':			force=~force;			break;		case 'v':			if (optarg) 				ptp_usb_verbose=strtol(optarg,NULL,10);			else				ptp_usb_verbose=1;			printf("VERBOSE LEVEL  = %i\n",ptp_usb_verbose);			break;		case 'r':			action=ACT_DEVICE_RESET;			break;		case 'l':			action=ACT_LIST_DEVICES;			break;		case 'p':			action=ACT_LIST_PROPERTIES;			break;		case 's':			action=ACT_GETSET_PROPERTY;			property=strtol(optarg,NULL,16);			break;		case 'c':			action=ACT_CANON_TEST;			break;		case 't':			action=ACT_TAKE_SHOT;			break;		case 'N':			strncpy(filename,optarg,MAXNAMELEN);            filename[MAXNAMELEN-1]='\0';			break;		case 'F':			flash=strtol(optarg,NULL,10);			break;		case 'Z':			zoom=strtol(optarg,NULL,10);			break;		case 'b':			on_button=1;			break;		case '?':			break;		default:			fprintf(stderr,"getopt returned character code 0%o\n",				opt);			break;		}	}	if (argc==1) {		usage();		return 0;	}	switch (action) {		case ACT_DEVICE_RESET:			reset_device(busn,devn,force);			break;		case ACT_LIST_DEVICES:			list_devices(force);			break;		case ACT_LIST_PROPERTIES:			list_properties(busn,devn,force);			break;		case ACT_GETSET_PROPERTY:			getset_property(busn,devn,property,value,force);			break;		case ACT_CANON_TEST:            test_canon (busn,devn,force);			break;		case ACT_TAKE_SHOT:            take_shot (busn,devn,force,filename,flash,zoom,on_button);			break;	}	return 0;}

⌨️ 快捷键说明

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