<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forums.obdev.at/app.php/feed/topic/3924" />

	<title>Objective Development Forums</title>
	
	<link href="https://forums.obdev.at/index.php" />
	<updated>2011-11-24T19:49:29+02:00</updated>

	<author><name><![CDATA[Objective Development Forums]]></name></author>
	<id>https://forums.obdev.at/app.php/feed/topic/3924</id>

		<entry>
		<author><name><![CDATA[wismith]]></name></author>
		<updated>2011-11-24T19:49:29+02:00</updated>

		<published>2011-11-24T19:49:29+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=3924&amp;p=20281#p20281</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=3924&amp;p=20281#p20281"/>
		<title type="html"><![CDATA[Re: PowerSwitch toggle feature for the command line.]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=3924&amp;p=20281#p20281"><![CDATA[
Do you need a code?<p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=6055">wismith</a> — Thu Nov 24, 2011 7:49 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[firewalker]]></name></author>
		<updated>2010-02-23T16:39:43+02:00</updated>

		<published>2010-02-23T16:39:43+02:00</published>
		<id>https://forums.obdev.at/viewtopic.php?t=3924&amp;p=13358#p13358</id>
		<link href="https://forums.obdev.at/viewtopic.php?t=3924&amp;p=13358#p13358"/>
		<title type="html"><![CDATA[PowerSwitch toggle feature for the command line.]]></title>

		
		<content type="html" xml:base="https://forums.obdev.at/viewtopic.php?t=3924&amp;p=13358#p13358"><![CDATA[
Hello everyone!<br /><br />I needed a toggle function for powerswitch. So I played around with the code. I don't know if this is 100% correct but it Works. Any recommendations is more than welcome.<br /><br />I added the option <strong class="text-strong">./powerSwitch toggle &lt;port&gt;</strong> or <strong class="text-strong">./powerSwitch toggle all</strong>.<br /><br /><strong class="text-strong">The patch:</strong><br /><div class="codebox"><p>Code: </p><pre><code>--- powerSwitch-orig.c    2010-02-23 14:14:34.000000000 +0200<br />+++ powerSwitch.c    2010-02-23 15:59:34.000000000 +0200<br />@@ -39,7 +39,7 @@<br />     fprintf(stderr, &quot;  %s status\n&quot;, name);<br />     fprintf(stderr, &quot;  %s on &lt;port&gt; &#91;&lt;duration&gt;&#93;\n&quot;, name);<br />     fprintf(stderr, &quot;  %s off &lt;port&gt; &#91;&lt;duration&gt;&#93;\n&quot;, name);<br />-    fprintf(stderr, &quot;  %s toggle &lt;port&gt; &#91;&lt;duration&gt;&#93;\n&quot;, name);<br />+    fprintf(stderr, &quot;  %s toggle &lt;port&gt; or %s toggle all\n&quot;, name, name);<br />     fprintf(stderr, &quot;  %s test\n\n&quot;, name);<br />     fprintf(stderr, &quot;Ports are single digits in the range 0...7\n&quot;);<br />     fprintf(stderr, &quot;The pulse duration for switching temporarily is given in seconds.\n&quot;);<br />@@ -214,6 +214,55 @@<br />             nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ON, duration, port, (char *)buffer, sizeof(buffer), 5000);<br />         }else if(strcmp(argv&#91;1&#93;, &quot;off&quot;) == 0){<br />             nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_OFF, duration, port, (char *)buffer, sizeof(buffer), 5000);<br /><br />+<br />+/*<br />+############### My try to add a toggle switch ###############<br />+ * Author: Alexander Kaltsas<br />+ * Creation Date: 2010-02-23<br />+*/<br />+        }else if(strcmp(argv&#91;1&#93;, &quot;toggle&quot;) == 0){<br />+            int i;<br />+<br />+            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, 0, (char *)buffer, sizeof(buffer), 5000);<br />+<br />+            if(nBytes &lt; 2){<br />+                if(nBytes &lt; 0)<br />+                fprintf(stderr, &quot;USB error: %s\n&quot;, usb_strerror());<br />+                fprintf(stderr, &quot;only %d bytes status received\n&quot;, nBytes);<br />+                exit(1);<br />+                }<br />+<br />+            if (strcmp(argv&#91;2&#93;, &quot;all&quot;)== 0){<br />+<br />+                for(i=0;i&lt;8;i++){<br />+                    printf  (&quot;%d \n&quot;,i);<br />+                    nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, i, (char *)buffer, sizeof(buffer), 5000);<br />+                    int isOn = buffer&#91;0&#93; &amp; (1 &lt;&lt; i);;<br />+                    if (isOn != 0) {<br />+                        nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_OFF, 0, i, (char *)buffer, sizeof(buffer), 5000);<br />+                    }else{<br />+                        nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ON, 0, i, (char *)buffer, sizeof(buffer), 5000);<br />+                        }<br />+                    }<br />+                    exit (0);<br />+            }<br />+<br />+            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, port, (char *)buffer, sizeof(buffer), 5000);<br />+<br />+            int isOn = buffer&#91;0&#93; &amp; (1 &lt;&lt; port);;<br />+<br />+            if (isOn != 0) {<br />+                nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_OFF, 0, port, (char *)buffer, sizeof(buffer), 5000);<br />+            }else{<br />+                nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ON, 0, port, (char *)buffer, sizeof(buffer), 5000);<br />+            }<br />+<br />+/*<br />+############### End of the toggle section ###############<br />+*/<br />+<br />         }else{<br />             nBytes = 0;<br />             usage(argv&#91;0&#93;);<br /><br /> </code></pre></div><br /><br /><br /><strong class="text-strong"><br />powerSwitch.c</strong><br /><div class="codebox"><p>Code: </p><pre><code>/* Name: powerSwitch.c<br /> * Project: PowerSwitch based on AVR USB driver<br /> * Author: Christian Starkjohann<br /> * Creation Date: 2005-01-16<br /> * Tabsize: 4<br /> * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH<br /> * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)<br /> * This Revision: $Id: powerSwitch.c 472 2008-01-21 18:21:59Z cs $<br /> */<br /><br />/*<br />General Description:<br />This program controls the PowerSwitch USB device from the command line.<br />It must be linked with libusb, a library for accessing the USB bus from<br />Linux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can be<br />obtained from http://libusb.sourceforge.net/.<br />*/<br /><br />#include &lt;stdio.h&gt;<br />#include &lt;stdlib.h&gt;<br />#include &lt;string.h&gt;<br />#include &lt;usb.h&gt;    /* this is libusb, see http://libusb.sourceforge.net/ */<br /><br />#define USBDEV_SHARED_VENDOR    0x16C0  /* VOTI */<br />#define USBDEV_SHARED_PRODUCT   0x05DC  /* Obdev's free shared PID */<br />/* Use obdev's generic shared VID/PID pair and follow the rules outlined<br /> * in firmware/usbdrv/USBID-License.txt.<br /> */<br /><br />#define PSCMD_ECHO  0<br />#define PSCMD_GET   1<br />#define PSCMD_ON    2<br />#define PSCMD_OFF   3<br />/* These are the vendor specific SETUP commands implemented by our USB device */<br /><br />static void usage(char *name)<br />{<br />    fprintf(stderr, &quot;usage:\n&quot;);<br />    fprintf(stderr, &quot;  %s status\n&quot;, name);<br />    fprintf(stderr, &quot;  %s on &lt;port&gt; &#91;&lt;duration&gt;&#93;\n&quot;, name);<br />    fprintf(stderr, &quot;  %s off &lt;port&gt; &#91;&lt;duration&gt;&#93;\n&quot;, name);<br />    fprintf(stderr, &quot;  %s toggle &lt;port&gt; or %s toggle all\n&quot;, name, name);<br />    fprintf(stderr, &quot;  %s test\n\n&quot;, name);<br />    fprintf(stderr, &quot;Ports are single digits in the range 0...7\n&quot;);<br />    fprintf(stderr, &quot;The pulse duration for switching temporarily is given in seconds.\n&quot;);<br />}<br /><br /><br />static int  usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen)<br />{<br />char    buffer&#91;256&#93;;<br />int     rval, i;<br /><br />    if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING &lt;&lt; 8) + index, langid, buffer, sizeof(buffer), 1000)) &lt; 0)<br />        return rval;<br />    if(buffer&#91;1&#93; != USB_DT_STRING)<br />        return 0;<br />    if((unsigned char)buffer&#91;0&#93; &lt; rval)<br />        rval = (unsigned char)buffer&#91;0&#93;;<br />    rval /= 2;<br />    /* lossy conversion to ISO Latin1 */<br />    for(i=1;i&lt;rval;i++){<br />        if(i &gt; buflen)  /* destination buffer overflow */<br />            break;<br />        buf&#91;i-1&#93; = buffer&#91;2 * i&#93;;<br />        if(buffer&#91;2 * i + 1&#93; != 0)  /* outside of ISO Latin1 range */<br />            buf&#91;i-1&#93; = '?';<br />    }<br />    buf&#91;i-1&#93; = 0;<br />    return i-1;<br />}<br /><br /><br />/* PowerSwitch uses the free shared default VID/PID. If you want to see an<br /> * example device lookup where an individually reserved PID is used, see our<br /> * RemoteSensor reference implementation.<br /> */<br /><br />#define USB_ERROR_NOTFOUND  1<br />#define USB_ERROR_ACCESS    2<br />#define USB_ERROR_IO        3<br /><br />static int usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName)<br />{<br />struct usb_bus      *bus;<br />struct usb_device   *dev;<br />usb_dev_handle      *handle = NULL;<br />int                 errorCode = USB_ERROR_NOTFOUND;<br />static int          didUsbInit = 0;<br /><br />    if(!didUsbInit){<br />        didUsbInit = 1;<br />        usb_init();<br />    }<br />    usb_find_busses();<br />    usb_find_devices();<br />    for(bus=usb_get_busses(); bus; bus=bus-&gt;next){<br />        for(dev=bus-&gt;devices; dev; dev=dev-&gt;next){<br />            if(dev-&gt;descriptor.idVendor == vendor &amp;&amp; dev-&gt;descriptor.idProduct == product){<br />                char    string&#91;256&#93;;<br />                int     len;<br />                handle = usb_open(dev); /* we need to open the device in order to query strings */<br />                if(!handle){<br />                    errorCode = USB_ERROR_ACCESS;<br />                    fprintf(stderr, &quot;Warning: cannot open USB device: %s\n&quot;, usb_strerror());<br />                    continue;<br />                }<br />                if(vendorName == NULL &amp;&amp; productName == NULL){  /* name does not matter */<br />                    break;<br />                }<br />                /* now check whether the names match: */<br />                len = usbGetStringAscii(handle, dev-&gt;descriptor.iManufacturer, 0x0409, string, sizeof(string));<br />                if(len &lt; 0){<br />                    errorCode = USB_ERROR_IO;<br />                    fprintf(stderr, &quot;Warning: cannot query manufacturer for device: %s\n&quot;, usb_strerror());<br />                }else{<br />                    errorCode = USB_ERROR_NOTFOUND;<br />                    /* fprintf(stderr, &quot;seen device from vendor -&gt;%s&lt;-\n&quot;, string); */<br />                    if(strcmp(string, vendorName) == 0){<br />                        len = usbGetStringAscii(handle, dev-&gt;descriptor.iProduct, 0x0409, string, sizeof(string));<br />                        if(len &lt; 0){<br />                            errorCode = USB_ERROR_IO;<br />                            fprintf(stderr, &quot;Warning: cannot query product for device: %s\n&quot;, usb_strerror());<br />                        }else{<br />                            errorCode = USB_ERROR_NOTFOUND;<br />                            /* fprintf(stderr, &quot;seen product -&gt;%s&lt;-\n&quot;, string); */<br />                            if(strcmp(string, productName) == 0)<br />                                break;<br />                        }<br />                    }<br />                }<br />                usb_close(handle);<br />                handle = NULL;<br />            }<br />        }<br />        if(handle)<br />            break;<br />    }<br />    if(handle != NULL){<br />        errorCode = 0;<br />        *device = handle;<br />    }<br />    return errorCode;<br />}<br /><br /><br />int main(int argc, char **argv)<br />{<br />usb_dev_handle      *handle = NULL;<br />unsigned char       buffer&#91;8&#93;;<br />int                 nBytes;<br /><br />    if(argc &lt; 2){<br />        usage(argv&#91;0&#93;);<br />        exit(1);<br />    }<br />    usb_init();<br />    if(usbOpenDevice(&amp;handle, USBDEV_SHARED_VENDOR, &quot;www.obdev.at&quot;, USBDEV_SHARED_PRODUCT, &quot;PowerSwitch&quot;) != 0){<br />        fprintf(stderr, &quot;Could not find USB device \&quot;PowerSwitch\&quot; with vid=0x%x pid=0x%x\n&quot;, USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT);<br />        exit(1);<br />    }<br />/* We have searched all devices on all busses for our USB device above. Now<br /> * try to open it and perform the vendor specific control operations for the<br /> * function requested by the user.<br /> */<br />    if(strcmp(argv&#91;1&#93;, &quot;test&quot;) == 0){<br />        int i, v, r;<br />/* The test consists of writing 1000 random numbers to the device and checking<br /> * the echo. This should discover systematic bit errors (e.g. in bit stuffing).<br /> */<br />        for(i=0;i&lt;1000;i++){<br />            v = rand() &amp; 0xffff;<br />            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ECHO, v, 0, (char *)buffer, sizeof(buffer), 5000);<br />            if(nBytes &lt; 2){<br />                if(nBytes &lt; 0)<br />                    fprintf(stderr, &quot;USB error: %s\n&quot;, usb_strerror());<br />                fprintf(stderr, &quot;only %d bytes received in iteration %d\n&quot;, nBytes, i);<br />                fprintf(stderr, &quot;value sent = 0x%x\n&quot;, v);<br />                exit(1);<br />            }<br />            r = buffer&#91;0&#93; | (buffer&#91;1&#93; &lt;&lt; 8);<br />            if(r != v){<br />                fprintf(stderr, &quot;data error: received 0x%x instead of 0x%x in iteration %d\n&quot;, r, v, i);<br />                exit(1);<br />            }<br />        }<br />        printf(&quot;test succeeded\n&quot;);<br />    }else if(strcmp(argv&#91;1&#93;, &quot;status&quot;) == 0){<br />        int i;<br />        nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, 0, (char *)buffer, sizeof(buffer), 5000);<br />        if(nBytes &lt; 2){<br />            if(nBytes &lt; 0)<br />                fprintf(stderr, &quot;USB error: %s\n&quot;, usb_strerror());<br />            fprintf(stderr, &quot;only %d bytes status received\n&quot;, nBytes);<br />            exit(1);<br />        }<br />        for(i=0;i&lt;8;i++){<br />            int isOn = buffer&#91;0&#93; &amp; (1 &lt;&lt; i);<br />            int isInv = buffer&#91;1&#93; &amp; (1 &lt;&lt; i);<br />            printf(&quot;port %d: %s%s\n&quot;, i, isOn ? &quot;on&quot; : &quot;off&quot;, isInv ? (isOn ? &quot; / pulse off&quot; : &quot; / pulse on&quot;) : &quot;&quot;);<br />        }<br />    }else{<br />        int port, duration = 0;<br />        if(argc &lt; 3){<br />            usage(argv&#91;0&#93;);<br />            exit(1);<br />        }<br />        port = atoi(argv&#91;2&#93;);<br />        if(argc &gt; 3){<br />            if((duration = (int)(atof(argv&#91;3&#93;) / 0.2 + 0.5)) &gt; 255)<br />                duration = 255;<br />        }<br />        if(strcmp(argv&#91;1&#93;, &quot;on&quot;) == 0){<br />            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ON, duration, port, (char *)buffer, sizeof(buffer), 5000);<br />        }else if(strcmp(argv&#91;1&#93;, &quot;off&quot;) == 0){<br />            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_OFF, duration, port, (char *)buffer, sizeof(buffer), 5000);<br /><br /><br />/*<br />############### My try to add a toggle switch ###############<br /> * Author: Alexander Kaltsas<br /> * Creation Date: 2010-02-23<br />*/<br />        }else if(strcmp(argv&#91;1&#93;, &quot;toggle&quot;) == 0){<br />            int i;<br /><br />            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, 0, (char *)buffer, sizeof(buffer), 5000);<br /><br />            if(nBytes &lt; 2){<br />                if(nBytes &lt; 0)<br />                fprintf(stderr, &quot;USB error: %s\n&quot;, usb_strerror());<br />                fprintf(stderr, &quot;only %d bytes status received\n&quot;, nBytes);<br />                exit(1);<br />                }<br /><br />            if (strcmp(argv&#91;2&#93;, &quot;all&quot;)== 0){<br /><br />                for(i=0;i&lt;8;i++){<br />                    printf  (&quot;%d \n&quot;,i);<br />                    nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, i, (char *)buffer, sizeof(buffer), 5000);<br />                    int isOn = buffer&#91;0&#93; &amp; (1 &lt;&lt; i);;<br />                    if (isOn != 0) {<br />                        nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_OFF, 0, i, (char *)buffer, sizeof(buffer), 5000);<br />                    }else{<br />                        nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ON, 0, i, (char *)buffer, sizeof(buffer), 5000);<br />                        }<br />                    }<br />                    exit (0);<br />            }<br /><br />            nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0, port, (char *)buffer, sizeof(buffer), 5000);<br /><br />            int isOn = buffer&#91;0&#93; &amp; (1 &lt;&lt; port);;<br /><br />            if (isOn != 0) {<br />                nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_OFF, 0, port, (char *)buffer, sizeof(buffer), 5000);<br />            }else{<br />                nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_ON, 0, port, (char *)buffer, sizeof(buffer), 5000);<br />            }<br /><br />/*<br />############### End of the toggle section ###############<br />*/<br /><br /><br />        }else{<br />            nBytes = 0;<br />            usage(argv&#91;0&#93;);<br />            exit(1);<br />        }<br />        if(nBytes &lt; 0)<br />            fprintf(stderr, &quot;USB error: %s\n&quot;, usb_strerror());<br />    }<br />    usb_close(handle);<br />    return 0;<br />}<br /></code></pre></div><p>Statistics: Posted by <a href="https://forums.obdev.at/memberlist.php?mode=viewprofile&amp;u=3572">firewalker</a> — Tue Feb 23, 2010 4:39 pm</p><hr />
]]></content>
	</entry>
	</feed>
