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

📄 powerswitch.c

📁 软USB核的电力开关PowerSwitch
💻 C
字号:
/* Name: powerSwitch.c * Project: PowerSwitch based on AVR USB driver * Author: Christian Starkjohann * Creation Date: 2005-01-16 * Tabsize: 4 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH * License: Proprietary, free under certain conditions. See Documentation. * This Revision: $Id: powerSwitch.c 20 2005-02-20 16:39:43Z cs $ *//*General Description:This program controls the PowerSwitch USB device from the command line.It must be linked with libusb, a library for accessing the USB bus fromLinux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can beobtained from http://libusb.sourceforge.net/.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <usb.h>	/* this is libusb, see http://libusb.sourceforge.net/ */#define	USBDEV_VENDOR	0x03eb	/* ATMEL */#define	USBDEV_PRODUCT	0x6a53	/* PowerSwitch *//* Our USB device identifies with the numbers above */#define	PSCMD_ECHO	0#define	PSCMD_GET	1#define	PSCMD_ON	2#define	PSCMD_OFF	3/* These are the vendor specific SETUP commands implemented by our USB device */static void	usage(char *name){	fprintf(stderr, "usage:\n");	fprintf(stderr, "  %s status\n", name);	fprintf(stderr, "  %s on <port> [<duration>]\n", name);	fprintf(stderr, "  %s off <port> [<duration>]\n", name);	fprintf(stderr, "  %s test\n\n", name);	fprintf(stderr, "Ports are single digits in the range 0...7\n");	fprintf(stderr, "The pulse duration for switching temporarily is given in seconds.\n");}int	main(int argc, char **argv){struct usb_bus		*bus;struct usb_device	*dev = 0;usb_dev_handle		*handle;unsigned char		buffer[8];int					nBytes;	if(argc < 2){		usage(argv[0]);		exit(1);	}	usb_init();	usb_find_busses();	usb_find_devices();	for(bus=usb_busses; bus; bus=bus->next){		for(dev=bus->devices; dev; dev=dev->next){			if(dev->descriptor.idVendor == USBDEV_VENDOR && dev->descriptor.idProduct == USBDEV_PRODUCT)				break;		}		if(dev)			break;	}	if(!dev){		fprintf(stderr, "Could not find USB device vendor=0x%x product=0x%x\n", USBDEV_VENDOR, USBDEV_PRODUCT);		exit(1);	}/* We have searched all devices on all busses for our USB device above. Now * try to open it and perform the vendor specific control operations for the * function requested by the user. */	handle = usb_open(dev);	if(!handle){		fprintf(stderr, "Error opening USB device PowerSwitch: %s\n", usb_strerror());		exit(1);	}	if(strcmp(argv[1], "test") == 0){		int i, v, r;/* The test consists of writing 1000 random numbers to the device and checking * the echo. This should discover systematic bit errors (e.g. in bit stuffing). */		for(i=0;i<1000;i++){			v = random() & 0xffff;			nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | 0x80, PSCMD_ECHO, v, 0, buffer, sizeof(buffer), 5000);			if(nBytes < 2){				if(nBytes < 0)					fprintf(stderr, "USB error: %s\n", usb_strerror());				fprintf(stderr, "only %d bytes received in iteration %d\n", nBytes, i);				exit(1);			}			r = buffer[0] | (buffer[1] << 8);			if(r != v){				fprintf(stderr, "data error: received 0x%x instead of 0x%x in iteration %d\n", r, v, i);				exit(1);			}		}		printf("test succeeded\n");	}else if(strcmp(argv[1], "status") == 0){		int i;		nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | 0x80, PSCMD_GET, 0, 0, buffer, sizeof(buffer), 5000);		if(nBytes < 2){			if(nBytes < 0)				fprintf(stderr, "USB error: %s\n", usb_strerror());			fprintf(stderr, "only %d bytes status received\n", nBytes);			exit(1);		}		for(i=0;i<8;i++){			int isOn = buffer[0] & (1 << i);			int isInv = buffer[1] & (1 << i);			printf("port %d: %s%s\n", i, isOn ? "on" : "off", isInv ? (isOn ? " / pulse off" : " / pulse on") : "");		}	}else{		int port, duration = 0;		if(argc < 3){			usage(argv[0]);			exit(1);		}		port = atoi(argv[2]);		if(argc > 3){			if((duration = (int)(atof(argv[3]) / 0.2 + 0.5)) > 255)				duration = 255;		}		if(strcmp(argv[1], "on") == 0){			nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | 0x80, PSCMD_ON, duration, port, buffer, sizeof(buffer), 5000);		}else if(strcmp(argv[1], "off") == 0){			nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | 0x80, PSCMD_OFF, duration, port, buffer, sizeof(buffer), 5000);		}else{			nBytes = 0;			usage(argv[0]);			exit(1);		}		if(nBytes < 0)			fprintf(stderr, "USB error: %s\n", usb_strerror());	}	usb_close(handle);	return 0;}

⌨️ 快捷键说明

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