31 #include "littleWire.h"
35 int LastFamilyDiscrepancy;
41 static unsigned char dscrc_table[] = {
42 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
43 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
44 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
45 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
46 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
47 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
48 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
49 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
50 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
51 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
52 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
53 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
54 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
55 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
56 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
57 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
63 struct usb_device *dev;
71 for (bus = usb_busses; bus; bus = bus->next)
73 for (dev = bus->devices; dev; dev = dev->next)
76 char description[256];
80 if((dev->descriptor.idVendor == VENDOR_ID) && (dev->descriptor.idProduct == PRODUCT_ID))
85 if (dev->descriptor.iSerialNumber)
87 ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber,
string,
sizeof(
string));
90 lwResults[lw_totalDevices].serialNumber = atoi(
string);
91 lwResults[lw_totalDevices].lw_device = dev;
101 return lw_totalDevices;
106 littleWire *tempHandle = NULL;
108 if(desiredID > (lw_totalDevices-1))
113 tempHandle = usb_open(lwResults[desiredID].lw_device);
120 littleWire *tempHandle = NULL;
121 int temp_id = 0xDEAF;
124 for(i=0;i<lw_totalDevices;i++)
126 if(lwResults[i].serialNumber == mySerial)
138 littleWire *tempHandle = NULL;
141 usbOpenDevice(&tempHandle, VENDOR_ID,
"*", PRODUCT_ID,
"*",
"*", NULL, NULL );
148 lwStatus=usb_control_msg(lwHandle, 0xC0, 34, 0, 0, rxBuffer, 8, USB_TIMEOUT);
157 if(serialNumber > 999)
161 else if(serialNumber < 100)
166 sprintf(serBuf,
"%d",serialNumber);
168 lwStatus=usb_control_msg(lwHandle, 0xC0, 55, (serBuf[1]<<8)|serBuf[0],serBuf[2], rxBuffer, 8, USB_TIMEOUT);
171 void digitalWrite(littleWire* lwHandle,
unsigned char pin,
unsigned char state)
174 lwStatus=usb_control_msg(lwHandle, 0xC0, 18, pin, 0, rxBuffer, 8, USB_TIMEOUT);
176 lwStatus=usb_control_msg(lwHandle, 0xC0, 19, pin, 0, rxBuffer, 8, USB_TIMEOUT);
180 void pinMode(littleWire* lwHandle,
unsigned char pin,
unsigned char mode)
183 lwStatus=usb_control_msg(lwHandle, 0xC0, 13, pin, 0, rxBuffer, 8, USB_TIMEOUT);
185 lwStatus=usb_control_msg(lwHandle, 0xC0, 14, pin, 0, rxBuffer, 8, USB_TIMEOUT);
189 unsigned char digitalRead(littleWire* lwHandle,
unsigned char pin)
191 lwStatus=usb_control_msg(lwHandle, 0xC0, 20, pin, 0, rxBuffer, 8, USB_TIMEOUT);
196 void internalPullup(littleWire* lwHandle,
unsigned char pin,
unsigned char state)
199 lwStatus=usb_control_msg(lwHandle, 0xC0, 18, pin, 0, rxBuffer, 8, USB_TIMEOUT);
201 lwStatus=usb_control_msg(lwHandle, 0xC0, 19, pin, 0, rxBuffer, 8, USB_TIMEOUT);
207 lwStatus=usb_control_msg(lwHandle, 0xC0, 35, (voltageRef<<8) | 0x07, 0, rxBuffer, 8, USB_TIMEOUT);
210 unsigned int analogRead(littleWire* lwHandle,
unsigned char channel)
212 lwStatus=usb_control_msg(lwHandle, 0xC0, 15, channel, 0, rxBuffer, 8, USB_TIMEOUT);
214 return ((rxBuffer[1] *256) + (rxBuffer[0]));
219 lwStatus=usb_control_msg(lwHandle, 0xC0, 16, 0, 0, rxBuffer, 8, USB_TIMEOUT);
224 lwStatus=usb_control_msg(lwHandle, 0xC0, 32, 0, 0, rxBuffer, 8, USB_TIMEOUT);
229 lwStatus=usb_control_msg(lwHandle, 0xC0, 17, channelA, channelB, rxBuffer, 8, USB_TIMEOUT);
237 lwStatus=usb_control_msg(lwHandle, 0xC0, 22, 4, 0, rxBuffer, 8, USB_TIMEOUT);
240 lwStatus=usb_control_msg(lwHandle, 0xC0, 22, 3, 0, rxBuffer, 8, USB_TIMEOUT);
243 lwStatus=usb_control_msg(lwHandle, 0xC0, 22, 2, 0, rxBuffer, 8, USB_TIMEOUT);
246 lwStatus=usb_control_msg(lwHandle, 0xC0, 22, 1, 0, rxBuffer, 8, USB_TIMEOUT);
249 lwStatus=usb_control_msg(lwHandle, 0xC0, 22, 0, 0, rxBuffer, 8, USB_TIMEOUT);
256 lwStatus=usb_control_msg(lwHandle, 0xC0, 23, 0, 0, rxBuffer, 8, USB_TIMEOUT);
259 void spi_sendMessage(littleWire* lwHandle,
unsigned char * sendBuffer,
unsigned char * inputBuffer,
unsigned char length ,
unsigned char mode)
264 lwStatus=usb_control_msg(lwHandle, 0xC0, (0xF0 + length + (mode<<3) ), (sendBuffer[1]<<8) + sendBuffer[0] , (sendBuffer[3]<<8) + sendBuffer[2], rxBuffer, 8, USB_TIMEOUT);
265 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
266 for(i=0;i<length;i++)
267 inputBuffer[i]=rxBuffer[i];
270 unsigned char debugSpi(littleWire* lwHandle,
unsigned char message)
272 lwStatus=usb_control_msg(lwHandle, 0xC0, 33, 0, 0, rxBuffer, 8, USB_TIMEOUT);
273 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
279 lwStatus=usb_control_msg(lwHandle, 0xC0, 31, duration, 0, rxBuffer, 8, USB_TIMEOUT);
284 lwStatus=usb_control_msg(lwHandle, 0xC0, 44, 0, 0, rxBuffer, 8, USB_TIMEOUT);
287 unsigned char i2c_start(littleWire* lwHandle,
unsigned char address7bit,
unsigned char direction)
291 temp = (address7bit << 1) | direction;
293 lwStatus=usb_control_msg(lwHandle, 0xC0, 45, temp, 0, rxBuffer, 8, USB_TIMEOUT);
294 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
298 void i2c_write(littleWire* lwHandle,
unsigned char* sendBuffer,
unsigned char length,
unsigned char endWithStop)
300 lwStatus=usb_control_msg(lwHandle, 0xC0, (0xE0 + length + (endWithStop<<3) ), (sendBuffer[1]<<8) + sendBuffer[0] , (sendBuffer[3]<<8) + sendBuffer[2], rxBuffer, 8, USB_TIMEOUT);
303 void i2c_read(littleWire* lwHandle,
unsigned char* readBuffer,
unsigned char length,
unsigned char endWithStop)
308 lwStatus=usb_control_msg(lwHandle, 0xC0, 46, (length<<8) + 1, 1, rxBuffer, 8, USB_TIMEOUT);
310 lwStatus=usb_control_msg(lwHandle, 0xC0, 46, (length<<8) + 0, 0, rxBuffer, 8, USB_TIMEOUT);
314 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
316 for(i=0;i<length;i++)
317 readBuffer[i]=rxBuffer[i];
322 lwStatus=usb_control_msg(lwHandle, 0xC0, 49, duration, 0, rxBuffer, 8, USB_TIMEOUT);
327 lwStatus=usb_control_msg(lwHandle, 0xC0, 51, bitValue, 0, rxBuffer, 8, USB_TIMEOUT);
332 lwStatus=usb_control_msg(lwHandle, 0xC0, 42, messageToSend, 0, rxBuffer, 8, USB_TIMEOUT);
338 lwStatus=usb_control_msg(lwHandle, 0xC0, 43, 0, 0, rxBuffer, 8, USB_TIMEOUT);
340 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
346 lwStatus=usb_control_msg(lwHandle, 0xC0, 50, 0, 0, rxBuffer, 8, USB_TIMEOUT);
347 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
353 lwStatus=usb_control_msg(lwHandle, 0xC0, 41, 0, 0, rxBuffer, 8, USB_TIMEOUT);
355 lwStatus=usb_control_msg(lwHandle, 0xC0, 40, 0, 0, rxBuffer, 8, USB_TIMEOUT);
361 lwStatus=usb_control_msg(lwHandle, 0xC0, 47, state, 0, rxBuffer, 8, USB_TIMEOUT);
364 void softPWM_write(littleWire* lwHandle,
unsigned char ch1,
unsigned char ch2,
unsigned char ch3)
366 lwStatus=usb_control_msg(lwHandle, 0xC0, 48, (ch2<<8) | ch1, ch3, rxBuffer, 8, USB_TIMEOUT);
369 void ws2812_write(littleWire* lwHandle,
unsigned char pin,
unsigned char r,
unsigned char g,
unsigned char b)
371 lwStatus=usb_control_msg(lwHandle, 0xC0, 54, (g<<8) | pin | 0x30, (b<<8) | r, rxBuffer, 8, USB_TIMEOUT);
376 lwStatus=usb_control_msg(lwHandle, 0xC0, 54, pin | 0x10, 0, rxBuffer, 8, USB_TIMEOUT);
379 void ws2812_preload(littleWire* lwHandle,
unsigned char r,
unsigned char g,
unsigned char b)
381 lwStatus=usb_control_msg(lwHandle, 0xC0, 54, (g<<8) | 0x20, (b<<8) | r, rxBuffer, 8, USB_TIMEOUT);
384 int customMessage(littleWire* lwHandle,
unsigned char* receiveBuffer,
unsigned char command,
unsigned char d1,
unsigned char d2,
unsigned char d3,
unsigned char d4)
388 rc = lwStatus=usb_control_msg(lwHandle, 0xC0, command, (d2<<8)|d1, (d4<<8)|d3, rxBuffer, 8, USB_TIMEOUT);
390 receiveBuffer[i]=rxBuffer[i];
398 unsigned char docrc8(
unsigned char value)
402 crc8 = dscrc_table[crc8 ^ value];
410 int last_zero, rom_byte_number, search_result;
411 int id_bit, cmp_id_bit;
412 unsigned char rom_byte_mask, search_direction;
431 LastFamilyDiscrepancy = 0;
446 if ((id_bit == 1) && (cmp_id_bit == 1))
451 if (id_bit != cmp_id_bit)
452 search_direction = id_bit;
457 if (id_bit_number < LastDiscrepancy)
458 search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
461 search_direction = (id_bit_number == LastDiscrepancy);
464 if (search_direction == 0)
466 last_zero = id_bit_number;
470 LastFamilyDiscrepancy = last_zero;
476 if (search_direction == 1)
477 ROM_NO[rom_byte_number] |= rom_byte_mask;
479 ROM_NO[rom_byte_number] &= ~rom_byte_mask;
490 if (rom_byte_mask == 0)
492 docrc8(ROM_NO[rom_byte_number]);
498 while(rom_byte_number < 8);
501 if (!((id_bit_number < 65) || (crc8 != 0)))
504 LastDiscrepancy = last_zero;
507 if (LastDiscrepancy == 0)
515 if (!search_result || !ROM_NO[0])
519 LastFamilyDiscrepancy = 0;
523 return search_result;
528 littleWire* temp = lwHandle;
533 LastFamilyDiscrepancy = 0;
539 if (lwStatus<0)
return lwStatus;
544 if (lwStatus<0)
switch (lwStatus) {
545 case -1:
return "I/O Error";
break;
546 case -2:
return "Invalid paramenter";
break;
547 case -3:
return "Access error";
break;
548 case -4:
return "No device";
break;
549 case -5:
return "Not found";
break;
550 case -6:
return "Busy";
break;
551 case -7:
return "Timeout";
break;
552 case -8:
return "Overflow";
break;
553 case -9:
return "Pipe";
break;
554 case -10:
return "Interrupted";
break;
555 case -11:
return "No memory";
break;
556 case -12:
return "Not supported";
break;
557 case -99:
return "Other";
break;
558 default:
return "unknown";