rb-blink1 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /*
2
- * blink(1) C library --
2
+ * blink(1) C library --
3
3
  *
4
4
  *
5
5
  * 2012, Tod E. Kurt, http://todbot.com/blog/ , http://thingm.com/
@@ -26,9 +26,11 @@
26
26
  #define LOG(...) do {} while (0)
27
27
  #endif
28
28
 
29
- #define blink1_report_id 1
29
+ #define blink1_report_id 1
30
+ #define blink1_report_size 8
31
+ #define blink1_buf_size (blink1_report_size+1)
30
32
 
31
- // addresses in EEPROM
33
+ // addresses in EEPROM
32
34
  #define blink1_eeaddr_osccal 0
33
35
  #define blink1_eeaddr_bootmode 1
34
36
  #define blink1_eeaddr_serialnum 2
@@ -37,11 +39,11 @@
37
39
 
38
40
  #define pathmax 16
39
41
  #define pathstrmax 128
40
- #define serialmax (8 + 1)
42
+ #define serialmax (8 + 1)
41
43
 
42
44
 
43
45
  // FIXME: use hid_device_info instead with custom sorter on serial or path
44
- static char blink1_cached_paths[pathmax][pathstrmax];
46
+ static char blink1_cached_paths[pathmax][pathstrmax];
45
47
  static int blink1_cached_count = 0;
46
48
  static wchar_t blink1_cached_serials[pathmax][serialmax];
47
49
 
@@ -62,20 +64,20 @@ int blink1_enumerateByVidPid(int vid, int pid)
62
64
 
63
65
  int p = 0;
64
66
  devs = hid_enumerate(vid, pid);
65
- cur_dev = devs;
67
+ cur_dev = devs;
66
68
  while (cur_dev) {
67
- if( (cur_dev->vendor_id != 0 && cur_dev->product_id != 0) &&
68
- (cur_dev->vendor_id == vid && cur_dev->product_id == pid) ) {
69
+ if( (cur_dev->vendor_id != 0 && cur_dev->product_id != 0) &&
70
+ (cur_dev->vendor_id == vid && cur_dev->product_id == pid) ) {
69
71
  if( cur_dev->serial_number != NULL ) { // can happen if not root
70
- strcpy( blink1_cached_paths[p], cur_dev->path );
71
- wcscpy( blink1_cached_serials[p], cur_dev->serial_number );
72
- p++;
73
- }
74
- }
72
+ strcpy( blink1_cached_paths[p], cur_dev->path );
73
+ wcscpy( blink1_cached_serials[p], cur_dev->serial_number );
74
+ p++;
75
+ }
76
+ }
75
77
  cur_dev = cur_dev->next;
76
78
  }
77
79
  hid_free_enumeration(devs);
78
-
80
+
79
81
  blink1_cached_count = p;
80
82
 
81
83
  blink1_sortSerials();
@@ -92,7 +94,7 @@ int blink1_getCachedCount(void)
92
94
  //
93
95
  const char* blink1_getCachedPath(int i)
94
96
  {
95
- return blink1_cached_paths[i];
97
+ return blink1_cached_paths[i];
96
98
  }
97
99
  //
98
100
  const wchar_t* blink1_getCachedSerial(int i)
@@ -104,7 +106,7 @@ const wchar_t* blink1_getCachedSerial(int i)
104
106
  hid_device* blink1_openByPath(const char* path)
105
107
  {
106
108
  if( path == NULL || strlen(path) == 0 ) return NULL;
107
- hid_device* handle = hid_open_path( path );
109
+ hid_device* handle = hid_open_path( path );
108
110
  return handle;
109
111
  }
110
112
 
@@ -114,15 +116,15 @@ hid_device* blink1_openBySerial(const wchar_t* serial)
114
116
  if( serial == NULL || wcslen(serial) == 0 ) return NULL;
115
117
  int vid = blink1_vid();
116
118
  int pid = blink1_pid();
117
-
119
+
118
120
  LOG("opening %ls at vid/pid %x/%x\n", serial, vid,pid);
119
- hid_device* handle = hid_open(vid,pid, serial );
121
+ hid_device* handle = hid_open(vid,pid, serial );
120
122
  return handle;
121
123
  }
122
124
 
123
125
  //
124
- hid_device* blink1_openById( int i )
125
- {
126
+ hid_device* blink1_openById( int i )
127
+ {
126
128
  //return blink1_openByPath( blink1_getCachedPath(i) );
127
129
  return blink1_openBySerial( blink1_getCachedSerial(i) );
128
130
  }
@@ -142,7 +144,7 @@ hid_device* blink1_open(void)
142
144
  // FIXME: search through blink1s list to zot it too?
143
145
  void blink1_close( hid_device* dev )
144
146
  {
145
- if( dev != NULL )
147
+ if( dev != NULL )
146
148
  hid_close(dev);
147
149
  dev = NULL;
148
150
  hid_exit(); // FIXME: this cleans up libusb in a way that hid_close doesn't
@@ -155,6 +157,10 @@ int blink1_write( hid_device* dev, void* buf, int len)
155
157
  return -1; // BLINK1_ERR_NOTOPEN;
156
158
  }
157
159
  int rc = hid_send_feature_report( dev, buf, len );
160
+ // FIXME: put this in an ifdef?
161
+ if( rc==-1 ) {
162
+ fprintf(stderr, "blink1_write error: %ls\n", hid_error(dev));
163
+ }
158
164
  return rc;
159
165
  }
160
166
 
@@ -188,7 +194,7 @@ int blink1_getSerialNumber(hid_device *dev, char* buf)
188
194
  /*
189
195
  wchar_t* wbuf = dev->serial_number;
190
196
  int i=0;
191
- while( wbuf ) {
197
+ while( wbuf ) {
192
198
  buf[i++] = *wbuf;
193
199
  }
194
200
  return i;
@@ -199,7 +205,7 @@ int blink1_getSerialNumber(hid_device *dev, char* buf)
199
205
  //
200
206
  int blink1_getVersion(hid_device *dev)
201
207
  {
202
- char buf[9] = {blink1_report_id, 'v' };
208
+ char buf[blink1_buf_size] = {blink1_report_id, 'v' };
203
209
  int len = sizeof(buf);
204
210
 
205
211
  //hid_set_nonblocking(dev, 0);
@@ -208,8 +214,8 @@ int blink1_getVersion(hid_device *dev)
208
214
  if( rc != -1 ) // no error
209
215
  rc = blink1_read(dev, buf, len);
210
216
  if( rc != -1 ) // also no error
211
- rc = ((buf[3]-'0') * 100) + (buf[4]-'0');
212
- // rc is now version number or error
217
+ rc = ((buf[3]-'0') * 100) + (buf[4]-'0');
218
+ // rc is now version number or error
213
219
  // FIXME: we don't know vals of errcodes
214
220
  return rc;
215
221
  }
@@ -217,14 +223,14 @@ int blink1_getVersion(hid_device *dev)
217
223
  //
218
224
  int blink1_eeread(hid_device *dev, uint16_t addr, uint8_t* val)
219
225
  {
220
- char buf[9] = {blink1_report_id, 'e', addr };
226
+ char buf[blink1_buf_size] = {blink1_report_id, 'e', addr };
221
227
  int len = sizeof(buf);
222
228
 
223
229
  int rc = blink1_write(dev, buf, len );
224
230
  blink1_sleep( 50 ); // FIXME:
225
231
  if( rc != -1 ) // no error
226
232
  rc = blink1_read(dev, buf, len );
227
- if( rc != -1 )
233
+ if( rc != -1 )
228
234
  *val = buf[3];
229
235
  return rc;
230
236
  }
@@ -232,10 +238,10 @@ int blink1_eeread(hid_device *dev, uint16_t addr, uint8_t* val)
232
238
  //
233
239
  int blink1_eewrite(hid_device *dev, uint16_t addr, uint8_t val)
234
240
  {
235
- char buf[9] = {blink1_report_id, 'E', addr, val };
241
+ char buf[blink1_buf_size] = {blink1_report_id, 'E', addr, val };
236
242
 
237
243
  int rc = blink1_write(dev, buf, sizeof(buf) );
238
-
244
+
239
245
  return rc;
240
246
  }
241
247
 
@@ -250,7 +256,7 @@ int blink1_serialnumread(hid_device *dev, uint8_t** serialnum)
250
256
  }
251
257
 
252
258
  //
253
- static uint8_t parseHex(char c)
259
+ static uint8_t parseHex(char c)
254
260
  {
255
261
  c = toupper(c);
256
262
  if (c >= '0' && c <= '9') return (c - '0');
@@ -259,7 +265,7 @@ static uint8_t parseHex(char c)
259
265
  }
260
266
 
261
267
  // serialnum comes in as an ascii set of 8 characters representing
262
- // 4-bytes
268
+ // 4-bytes
263
269
  int blink1_serialnumwrite(hid_device *dev, uint8_t* serialnumstr)
264
270
  {
265
271
  uint8_t serialnum[4];
@@ -270,13 +276,13 @@ int blink1_serialnumwrite(hid_device *dev, uint8_t* serialnumstr)
270
276
 
271
277
  int rc = 0;
272
278
  for( int i=0; i<blink1_serialnum_len; i++ ) { // serialnum is 4 chars long
273
- blink1_sleep(50); //FIXME:
279
+ blink1_sleep(50); //FIXME:
274
280
  uint8_t v = serialnum[i];
275
281
  int rc = blink1_eewrite( dev, blink1_eeaddr_serialnum+i, v);
276
282
  if( rc == -1 ) { // try again
277
283
  LOG("blink1_serialwrite: oops, trying again on char %d\n",i);
278
284
  rc = blink1_eewrite(dev,blink1_eeaddr_serialnum+i, v);
279
- if( rc == -1 ) {
285
+ if( rc == -1 ) {
280
286
  LOG("blink1_serialwrite: error on try again\n");
281
287
  break;
282
288
  }
@@ -286,6 +292,27 @@ int blink1_serialnumwrite(hid_device *dev, uint8_t* serialnumstr)
286
292
  return rc;
287
293
  }
288
294
 
295
+ int blink1_fadeToRGBN(hid_device *dev, uint16_t fadeMillis,
296
+ uint8_t r, uint8_t g, uint8_t b, uint8_t n)
297
+ {
298
+ int dms = fadeMillis/10; // millis_divided_by_10
299
+
300
+ char buf[blink1_buf_size];
301
+
302
+ buf[0] = blink1_report_id; // report id
303
+ buf[1] = 'c'; // command code for 'fade to rgb'
304
+ buf[2] = ((blink1_enable_degamma) ? blink1_degamma(r) : r );
305
+ buf[3] = ((blink1_enable_degamma) ? blink1_degamma(g) : g );
306
+ buf[4] = ((blink1_enable_degamma) ? blink1_degamma(b) : b );
307
+ buf[5] = (dms >> 8);
308
+ buf[6] = dms % 0xff;
309
+ buf[7] = n;
310
+
311
+ int rc = blink1_write(dev, buf, sizeof(buf) );
312
+
313
+ return rc;
314
+ }
315
+
289
316
 
290
317
  //
291
318
  int blink1_fadeToRGB(hid_device *dev, uint16_t fadeMillis,
@@ -293,7 +320,7 @@ int blink1_fadeToRGB(hid_device *dev, uint16_t fadeMillis,
293
320
  {
294
321
  int dms = fadeMillis/10; // millis_divided_by_10
295
322
 
296
- char buf[9];
323
+ uint8_t buf[9];
297
324
 
298
325
  buf[0] = blink1_report_id; // report id
299
326
  buf[1] = 'c'; // command code for 'fade to rgb'
@@ -302,26 +329,30 @@ int blink1_fadeToRGB(hid_device *dev, uint16_t fadeMillis,
302
329
  buf[4] = ((blink1_enable_degamma) ? blink1_degamma(b) : b );
303
330
  buf[5] = (dms >> 8);
304
331
  buf[6] = dms % 0xff;
332
+ buf[7] = 0;
305
333
 
306
334
  int rc = blink1_write(dev, buf, sizeof(buf) );
307
335
 
308
- return rc;
336
+ return rc;
309
337
  }
310
338
 
311
339
  //
312
340
  int blink1_setRGB(hid_device *dev, uint8_t r, uint8_t g, uint8_t b )
313
341
  {
314
- char buf[9];
342
+ uint8_t buf[blink1_buf_size];
315
343
 
316
344
  buf[0] = blink1_report_id; // report id
317
345
  buf[1] = 'n'; // command code for "set rgb now"
318
346
  buf[2] = ((blink1_enable_degamma) ? blink1_degamma(r) : r ); // red
319
347
  buf[3] = ((blink1_enable_degamma) ? blink1_degamma(g) : g ); // grn
320
348
  buf[4] = ((blink1_enable_degamma) ? blink1_degamma(b) : b ); // blu
321
-
349
+ buf[5] = 0;
350
+ buf[6] = 0;
351
+ buf[7] = 0;
352
+
322
353
  int rc = blink1_write(dev, buf, sizeof(buf) );
323
354
 
324
- return rc;
355
+ return rc;
325
356
  }
326
357
 
327
358
 
@@ -330,7 +361,7 @@ int blink1_serverdown(hid_device *dev, uint8_t on, uint16_t millis)
330
361
  {
331
362
  int dms = millis/10; // millis_divided_by_10
332
363
 
333
- char buf[9] = {blink1_report_id, 'D', on, (dms>>8), (dms % 0xff) };
364
+ char buf[blink1_buf_size] = {blink1_report_id, 'D', on, (dms>>8), (dms % 0xff) };
334
365
 
335
366
  int rc = blink1_write(dev, buf, sizeof(buf) );
336
367
  return rc;
@@ -339,31 +370,32 @@ int blink1_serverdown(hid_device *dev, uint8_t on, uint16_t millis)
339
370
  //
340
371
  int blink1_play(hid_device *dev, uint8_t play, uint8_t pos)
341
372
  {
342
- char buf[9] = {blink1_report_id, 'p', play, pos };
373
+ char buf[blink1_buf_size] = {blink1_report_id, 'p', play, pos };
343
374
  int rc = blink1_write(dev, buf, sizeof(buf) );
344
375
  return rc;
345
376
  }
346
-
377
+
347
378
  //
348
- int blink1_writePatternLine(hid_device *dev, uint16_t fadeMillis,
349
- uint8_t r, uint8_t g, uint8_t b,
379
+ int blink1_writePatternLine(hid_device *dev, uint16_t fadeMillis,
380
+ uint8_t r, uint8_t g, uint8_t b,
350
381
  uint8_t pos)
351
382
  {
352
383
  int dms = fadeMillis/10; // millis_divided_by_10
353
384
  r = (blink1_enable_degamma) ? blink1_degamma(r) : r ;
354
385
  g = (blink1_enable_degamma) ? blink1_degamma(g) : g ;
355
386
  b = (blink1_enable_degamma) ? blink1_degamma(b) : b ;
356
- char buf[9] = {blink1_report_id, 'P', r,g,b, (dms>>8), (dms % 0xff), pos };
387
+
388
+ char buf[blink1_buf_size] = {blink1_report_id, 'P', r,g,b, (dms>>8), (dms % 0xff), pos };
357
389
  int rc = blink1_write(dev, buf, sizeof(buf) );
358
390
  return rc;
359
391
  }
360
392
 
361
393
  //
362
- int blink1_readPatternLine(hid_device *dev, uint16_t* fadeMillis,
363
- uint8_t* r, uint8_t* g, uint8_t* b,
394
+ int blink1_readPatternLine(hid_device *dev, uint16_t* fadeMillis,
395
+ uint8_t* r, uint8_t* g, uint8_t* b,
364
396
  uint8_t pos)
365
397
  {
366
- char buf[9] = {blink1_report_id, 'R', 0,0,0, 0,0, pos };
398
+ char buf[blink1_buf_size] = {blink1_report_id, 'R', 0,0,0, 0,0, pos };
367
399
  int rc = blink1_write(dev, buf, sizeof(buf) );
368
400
  blink1_sleep( 50 ); // FIXME:
369
401
  if( rc != -1 ) // no error
@@ -378,7 +410,7 @@ int blink1_readPatternLine(hid_device *dev, uint16_t* fadeMillis,
378
410
  }
379
411
 
380
412
 
381
- // FIXME:
413
+ // FIXME:
382
414
  int readUUID( hid_device* dev, uint8_t** uuid )
383
415
  {
384
416
  return -1;
@@ -394,7 +426,7 @@ int setUUID( hid_device* dev, uint8_t* uuid )
394
426
 
395
427
  // FIXME: this is wrong
396
428
  // FIXME: provide function that generated this
397
- uint8_t degamma_lookup[256] = {
429
+ uint8_t degamma_lookup[256] = {
398
430
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
399
431
  1,1,1,1,1,1,2,2,2,2,2,3,3,3,3,4,
400
432
  4,4,4,5,5,5,5,6,6,6,7,7,7,8,8,9,
@@ -424,49 +456,49 @@ void blink1_disableDegamma()
424
456
 
425
457
  // a simple logarithmic -> linear mapping as a sort of gamma correction
426
458
  // maps from 0-255 to 0-255
427
- static int blink1_degamma_log2lin( int n )
459
+ static int blink1_degamma_log2lin( int n )
428
460
  {
429
461
  //return (int)(1.0* (n * 0.707 )); // 1/sqrt(2)
430
462
  return (((1<<(n/32))-1) + ((1<<(n/32))*((n%32)+1)+15)/32);
431
463
  }
432
464
 
433
465
  //
434
- int blink1_degamma( int n )
435
- {
466
+ int blink1_degamma( int n )
467
+ {
436
468
  //return degamma_lookup[n];
437
469
  return blink1_degamma_log2lin(n);
438
470
  }
439
471
 
440
472
 
441
- // qsort C-string comparison function
442
- int cmp_path(const void *a, const void *b)
443
- {
473
+ // qsort C-string comparison function
474
+ int cmp_path(const void *a, const void *b)
475
+ {
444
476
  return strncmp( (const char *)a, (const char *)b, pathstrmax);
445
- }
446
- // qsort wchar_t string comparison function
447
- int cmp_serial(const void *a, const void *b)
448
- {
477
+ }
478
+ // qsort wchar_t string comparison function
479
+ int cmp_serial(const void *a, const void *b)
480
+ {
449
481
  return wcsncmp( (const wchar_t *)a, (const wchar_t *)b, serialmax);
450
- }
482
+ }
451
483
 
452
484
  //
453
485
  void blink1_sortPaths(void)
454
486
  {
455
- size_t elemsize = sizeof( blink1_cached_paths[0] ); // 128
487
+ size_t elemsize = sizeof( blink1_cached_paths[0] ); // 128
456
488
  //size_t count = sizeof(blink1_cached_paths) / elemsize; // 16
457
-
489
+
458
490
  return qsort( blink1_cached_paths, blink1_cached_count,elemsize,cmp_path);
459
491
  }
460
492
 
461
493
  //
462
494
  void blink1_sortSerials(void)
463
495
  {
464
- size_t elemsize = sizeof( blink1_cached_serials[0] ); //
465
- //size_t count = sizeof(blink1_cached_serials) / elemsize; //
466
-
467
- qsort( blink1_cached_serials,
468
- blink1_cached_count,
469
- elemsize,
496
+ size_t elemsize = sizeof( blink1_cached_serials[0] ); //
497
+ //size_t count = sizeof(blink1_cached_serials) / elemsize; //
498
+
499
+ qsort( blink1_cached_serials,
500
+ blink1_cached_count,
501
+ elemsize,
470
502
  cmp_serial);
471
503
  }
472
504
 
@@ -490,7 +522,7 @@ void blink1_sleep(uint16_t millis)
490
522
  {
491
523
  #ifdef WIN32
492
524
  Sleep(millis);
493
- #else
525
+ #else
494
526
  usleep( millis * 1000);
495
527
  #endif
496
528
  }
@@ -519,10 +551,10 @@ char *blink1_error_msg(int errCode)
519
551
  //
520
552
  int blink1_nightlight(hid_device *dev, uint8_t on)
521
553
  {
522
- char buf[9] = { blink1_report_id, 'N', on };
554
+ char buf[8] = { blink1_report_id, 'N', on };
523
555
 
524
556
  int rc = blink1_write(dev, buf, sizeof(buf) );
525
-
557
+
526
558
  return rc;
527
559
  }
528
560
 
@@ -539,8 +571,8 @@ int blink1_command(hid_device* dev, int num_send, int num_recv,
539
571
  fprintf(stderr,"error writing data: %s\n",blink1_error_msg(err));
540
572
  return err;
541
573
  }
542
-
543
- if( num_recv > 0 ) {
574
+
575
+ if( num_recv > 0 ) {
544
576
  int len = num_recv;
545
577
  if((err = usbhidGetReport(dev, 0, (char*)buf_recv, &len)) != 0) {
546
578
  fprintf(stderr,"error reading data: %s\n",blink1_error_msg(err));
@@ -549,4 +581,4 @@ int blink1_command(hid_device* dev, int num_send, int num_recv,
549
581
  }
550
582
  return err;
551
583
  }
552
- */
584
+ */
@@ -1,5 +1,5 @@
1
1
  /*
2
- * blink(1) C library --
2
+ * blink(1) C library --
3
3
  *
4
4
  * 2012, Tod E. Kurt, http://todbot.com/blog/ , http://thingm.com/
5
5
  *
@@ -12,7 +12,7 @@
12
12
  #include <stdint.h>
13
13
 
14
14
  #include "hidapi.h"
15
- #include "usbconfig.h" // from firmware, for VID,PID,vendor name & product name
15
+ #include "usbconfig.h" // from firmware, for VID,PID,vendor name & product name
16
16
 
17
17
  #ifdef __cplusplus
18
18
  extern "C" {
@@ -47,6 +47,8 @@ int blink1_getVersion(hid_device *dev);
47
47
 
48
48
  int blink1_fadeToRGB(hid_device *dev, uint16_t fadeMillis,
49
49
  uint8_t r, uint8_t g, uint8_t b );
50
+ int blink1_fadeToRGBN(hid_device *dev, uint16_t fadeMillis,
51
+ uint8_t r, uint8_t g, uint8_t b, uint8_t n );
50
52
 
51
53
  int blink1_setRGB(hid_device *dev, uint8_t r, uint8_t g, uint8_t b );
52
54
 
@@ -60,11 +62,11 @@ int blink1_serialnumwrite(hid_device *dev, uint8_t* serialnumstr);
60
62
  int blink1_serverdown(hid_device *dev, uint8_t on, uint16_t millis);
61
63
 
62
64
  int blink1_play(hid_device *dev, uint8_t play, uint8_t pos);
63
- int blink1_writePatternLine(hid_device *dev, uint16_t fadeMillis,
64
- uint8_t r, uint8_t g, uint8_t b,
65
+ int blink1_writePatternLine(hid_device *dev, uint16_t fadeMillis,
66
+ uint8_t r, uint8_t g, uint8_t b,
65
67
  uint8_t pos);
66
- int blink1_readPatternLine(hid_device *dev, uint16_t* fadeMillis,
67
- uint8_t* r, uint8_t* g, uint8_t* b,
68
+ int blink1_readPatternLine(hid_device *dev, uint16_t* fadeMillis,
69
+ uint8_t* r, uint8_t* g, uint8_t* b,
68
70
  uint8_t pos);
69
71
  //int blink1_playPattern(hid_device *dev,,);
70
72
 
@@ -80,4 +82,4 @@ void blink1_sleep(uint16_t delayMillis);
80
82
  }
81
83
  #endif
82
84
 
83
- #endif
85
+ #endif