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