Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 265 lines (227 sloc) 7.137 kb
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
1 /*
2 * blink1-mini-tool -- minimal command-line tool for controlling blink(1)s
3 *
4 * Will work on small unix-based systems that have just libusb-0.1.4
5 * No need for pthread & iconv, which is needed for hidapi-based tools
6 *
7 * Known to work on:
8 * - Ubuntu Linux
9 * - Mac OS X
10 * - TomatoUSB WRT / OpenWrt / DD-WRT
11 *
12 * 2012, Tod E. Kurt, http://todbot.com/blog/ , http://thingm.com/
13 *
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
b80ed2c @todbot c lib: minor updates; moved webservice to its own repo
authored
18 #include <string.h> // for memset() et al
19 #include <stdint.h> // for uint8_t
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
20 #include <unistd.h> // for usleep()
51b9003 make random more random
root authored
21 #include <time.h> // for time()
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
22
23 #include "hiddata.h"
24
25 // taken from blink1/hardware/firmware/usbconfig.h
26 #define IDENT_VENDOR_NUM 0x27B8
27 #define IDENT_PRODUCT_NUM 0x01ED
28 #define IDENT_VENDOR_STRING "ThingM"
29 #define IDENT_PRODUCT_STRING "blink(1)"
30
31
32 int millis = 100;
33
34 int delayMillis = 1000;
35
36 const int blink1_debug = 2;
37
38
39 int blink1_open(usbDevice_t **dev);
40 char *blink1_error_msg(int errCode);
41 void blink1_close(usbDevice_t *dev);
42 int blink1_fadeToRGB(usbDevice_t *dev, int fadeMillis,
43 uint8_t r, uint8_t g, uint8_t b );
44 int blink1_setRGB(usbDevice_t *dev, uint8_t r, uint8_t g, uint8_t b );
45 static int hexread(char *buffer, char *string, int buflen);
46
47 //
48 static void usage(char *myName)
49 {
50 fprintf(stderr, "usage:\n");
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
51 fprintf(stderr, " %s blink [<num>]\n", myName);
52 fprintf(stderr, " %s random [<num>]\n", myName);
53 fprintf(stderr, " %s rgb <red>,<green>,<blue> \n", myName);
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
54 //fprintf(stderr, " %s read\n", myName);
55 //fprintf(stderr, " %s write <listofbytes>\n", myName);
56 }
57
58 //
59 int main(int argc, char **argv)
60 {
61 usbDevice_t *dev;
62 int rc;
63
c778742 @todbot blink1-mini-tool: updated hiddata to work with mk2
authored
64 char argbuf[16];
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
65
66 if(argc < 2) {
67 usage(argv[0]);
68 exit(1);
69 }
70 char* cmd = argv[1];
71
72 if( blink1_open(&dev) ) {
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
73 fprintf(stderr, "error: couldn't open blink1\n");
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
74 exit(1);
75 }
76
77 if( strcasecmp(cmd, "rgb") == 0 ) {
78 hexread(argbuf, argv[2], sizeof(argbuf)); // cmd w/ hexlist arg
79 uint8_t r = argbuf[0];
80 uint8_t g = argbuf[1];
81 uint8_t b = argbuf[2];
996dc1a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
82 printf("cmd:%s rgb:%2.2x,%2.2x,%2.2x in %d ms\n", cmd, r,g,b, millis );
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
83 rc = blink1_fadeToRGB(dev, millis, r,g,b);
bbf8c5a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
84 if( rc ) { // on error, do something
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
85 printf("error on fadeToRGB\n");
86 }
87 }
88 else if( strcasecmp(cmd, "blink") == 0 ) {
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
89 if( argc < 3 ) {
90 argbuf[0] = 3; // blink 3 times if none specified
91 } else {
92 hexread(argbuf, argv[2], sizeof(argbuf));
93 }
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
94
996dc1a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
95 uint8_t v = 0;
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
96 for( int i=0; i< argbuf[0]*2; i++ ) {
996dc1a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
97 uint8_t r = v;
98 uint8_t g = v;
99 uint8_t b = v;
c778742 @todbot blink1-mini-tool: updated hiddata to work with mk2
authored
100 printf("cmd:%s rgb:%2.2x,%2.2x,%2.2x in %d ms\n",cmd, r,g,b,millis);
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
101 rc = blink1_fadeToRGB( dev, millis, v,v,v );
bbf8c5a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
102 if( rc ) { // on error, do something
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
103 printf("error on fadeToRGB\n");
104 }
105 v = (v) ? 0 : 255;
106 usleep(millis * 1000 ); // sleep milliseconds
107 }
108 }
109 else if( strcasecmp(cmd, "random") == 0 ) {
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
110 if( argc < 3 ) {
bbf8c5a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
111 argbuf[0] = 5; // random 10 times if none specified
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
112 } else {
113 hexread(argbuf, argv[2], sizeof(argbuf));
114 }
51b9003 make random more random
root authored
115 srand(time(NULL) * getpid()); // feed the random number generator
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
116 for( int i=0; i<argbuf[0]; i++ ) {
117 uint8_t r = rand()%255;
118 uint8_t g = rand()%255;
119 uint8_t b = rand()%255 ;
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
120 printf("cmd:%s rgb:%2.2x,%2.2x,%2.2x in %d ms\n",cmd, r,g,b,millis);
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
121 rc = blink1_fadeToRGB(dev, millis, r,g,b);
bbf8c5a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
122 if( rc ) // on error, do something
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
123 printf("error on fadeToRGB\n");
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
124 usleep(millis * 1000 ); // sleep milliseconds
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
125 }
126 }
996dc1a @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
127 else {
128 usage(argv[0]);
129 exit(1);
130 }
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
131 /*
132 else if(strcasecmp(cmd, "read") == 0){
133 int len = sizeof(buffer);
134 if((rc = usbhidGetReport(dev, 0, buffer, &len)) != 0){
135 fprintf(stderr,"error reading data: %s\n",blink1_error_msg(rc));
136 }else{
137 hexdump(buffer + 1, sizeof(buffer) - 1);
138 }
139 }
140 else if(strcasecmp(cmd, "write") == 0){
141 int i, pos;
142 memset(buffer, 0, sizeof(buffer));
143 for(pos = 1, i = 2; i < argc && pos < sizeof(buffer); i++){
144 pos += hexread(buffer + pos, argv[i], sizeof(buffer) - pos);
145 }
146
147 // add a dummy report ID
148 if((rc = usbhidSetReport(dev, buffer, sizeof(buffer))) != 0)
149 fprintf(stderr,"error writing data: %s\n",blink1_error_msg(rc));
150
151 }
152 else
153 */
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
154
155 }
156
157
158 /**
159 * Open up a blink(1) for transactions.
160 * returns 0 on success, and opened device in "dev"
161 * or returns non-zero error that can be decoded with blink1_error_msg()
162 * FIXME: what happens when multiple are plugged in?
163 */
164 int blink1_open(usbDevice_t **dev)
165 {
166 return usbhidOpenDevice(dev,
ae18974 @todbot blink1-mini-tool: works on tomatousb on Asus RT-N16. cleaned up and fixe...
authored
167 IDENT_VENDOR_NUM, NULL,
168 IDENT_PRODUCT_NUM, NULL,
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
169 1); // NOTE: '0' means "not using report IDs"
170 }
171
172 /**
173 * Close a Blink1
174 */
175 void blink1_close(usbDevice_t *dev)
176 {
177 usbhidCloseDevice(dev);
178 }
179
180 /**
181 *
182 */
183 int blink1_fadeToRGB(usbDevice_t *dev, int fadeMillis,
184 uint8_t r, uint8_t g, uint8_t b )
185 {
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
186 char buf[9];
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
187 int err;
188
189 if( dev==NULL ) {
190 return -1; // BLINK1_ERR_NOTOPEN;
191 }
192
193 int dms = fadeMillis/10; // millis_divided_by_10
194
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
195 buf[0] = 1;
196 buf[1] = 'c';
197 buf[2] = r;
198 buf[3] = g;
199 buf[4] = b;
200 buf[5] = (dms >> 8);
201 buf[6] = dms % 0xff;
202 buf[7] = 0; // ledn
203 //buf[8] = 0; // unused
204
205 if( (err = usbhidSetReport(dev, buf, sizeof(buf))) != 0) {
206 fprintf(stderr,"fadeToRGB: error writing: %s\n",blink1_error_msg(err));
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
207 }
208 return err; // FIXME: remove fprintf
209 }
210
211 /**
212 *
213 */
214 int blink1_setRGB(usbDevice_t *dev, uint8_t r, uint8_t g, uint8_t b )
215 {
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
216 char buf[9];
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
217 int err;
218
219 if( dev==NULL ) {
220 return -1; // BLINK1_ERR_NOTOPEN;
221 }
222
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
223 buf[0] = 1;
224 buf[1] = 'n';
225 buf[2] = r;
226 buf[3] = g;
227 buf[4] = b;
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
228
32c7176 @todbot blink1-mini-tool mods to make work better on openwrt
authored
229 if( (err = usbhidSetReport(dev, buf, sizeof(buf))) != 0) {
230 fprintf(stderr,"setRGB: error writing: %s\n",blink1_error_msg(err));
621ef76 @todbot blink1-mini-tool: added small commandline tool for uClib / WRT tiny Linu...
authored
231 }
232 return err; // FIXME: remove fprintf
233 }
234
235
236 //
237 char *blink1_error_msg(int errCode)
238 {
239 static char buffer[80];
240
241 switch(errCode){
242 case USBOPEN_ERR_ACCESS: return "Access to device denied";
243 case USBOPEN_ERR_NOTFOUND: return "The specified device was not found";
244 case USBOPEN_ERR_IO: return "Communication error with device";
245 default:
246 sprintf(buffer, "Unknown USB error %d", errCode);
247 return buffer;
248 }
249 return NULL; /* not reached */
250 }
251
252 //
253 static int hexread(char *buffer, char *string, int buflen)
254 {
255 char *s;
256 int pos = 0;
257
258 while((s = strtok(string, ", ")) != NULL && pos < buflen){
259 string = NULL;
260 buffer[pos++] = (char)strtol(s, NULL, 0);
261 }
262 return pos;
263 }
264
Something went wrong with that request. Please try again.