Update hub_demo.ino

updated hub_demo.ino with @Lauszus suggestions
This commit is contained in:
stumpja 2016-02-26 10:59:02 -05:00 committed by Kristian Sloth Lauszus
parent fadf774235
commit 3413f5b21e

View file

@ -60,14 +60,14 @@ void setup()
next_time = millis() + 10000; next_time = millis() + 10000;
} }
byte getdevdescr( byte addr, byte &num_conf ); uint8_t getdevdescr( uint8_t addr, uint8_t &num_conf );
void PrintDescriptors(uint8_t addr) void PrintDescriptors(uint8_t addr)
{ {
uint8_t rcode = 0; uint8_t rcode = 0;
byte num_conf = 0; uint8_t num_conf = 0;
rcode = getdevdescr( (byte)addr, num_conf ); rcode = getdevdescr( (uint8_t)addr, num_conf );
if ( rcode ) if ( rcode )
{ {
printProgStr(Gen_Error_str); printProgStr(Gen_Error_str);
@ -112,11 +112,11 @@ void loop()
} }
} }
byte getdevdescr( byte addr, byte &num_conf ) uint8_t getdevdescr( uint8_t addr, uint8_t &num_conf )
{ {
USB_DEVICE_DESCRIPTOR buf; USB_DEVICE_DESCRIPTOR buf;
byte rcode; uint8_t rcode;
rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf ); rcode = Usb.getDevDescr( addr, 0, DEV_DESCR_LEN, ( uint8_t *)&buf );
if ( rcode ) { if ( rcode ) {
return ( rcode ); return ( rcode );
} }
@ -199,13 +199,13 @@ void printhubdescr(uint8_t *descrptr, uint8_t addr)
// PrintHubPortStatus(&Usb, addr, i, 1); // PrintHubPortStatus(&Usb, addr, i, 1);
} }
byte getconfdescr( byte addr, byte conf ) uint8_t getconfdescr( uint8_t addr, uint8_t conf )
{ {
uint8_t buf[ BUFSIZE ]; uint8_t buf[ BUFSIZE ];
uint8_t* buf_ptr = buf; uint8_t* buf_ptr = buf;
byte rcode; uint8_t rcode;
byte descr_length; uint8_t descr_length;
byte descr_type; uint8_t descr_type;
unsigned int total_length; unsigned int total_length;
rcode = Usb.getConfDescr( addr, 0, 4, conf, buf ); //get total length rcode = Usb.getConfDescr( addr, 0, 4, conf, buf ); //get total length
LOBYTE( total_length ) = buf[ 2 ]; LOBYTE( total_length ) = buf[ 2 ];
@ -241,37 +241,37 @@ byte getconfdescr( byte addr, byte conf )
} }
// function to get all string descriptors // function to get all string descriptors
byte getallstrdescr(uint8_t addr) uint8_t getallstrdescr(uint8_t addr)
{ {
byte rcode; uint8_t rcode;
Usb.Task(); Usb.Task();
if( Usb.getUsbTaskState() >= 0x80 ) { // state configuring or higher if ( Usb.getUsbTaskState() >= USB_STATE_CONFIGURING ) { // state configuring or higher
USB_DEVICE_DESCRIPTOR buf; USB_DEVICE_DESCRIPTOR buf;
rcode = Usb.getDevDescr( addr, 0, 0x12, ( uint8_t *)&buf ); rcode = Usb.getDevDescr( addr, 0, DEV_DESCR_LEN, ( uint8_t *)&buf );
if ( rcode ) { if ( rcode ) {
return ( rcode ); return ( rcode );
} }
Serial.println("String Descriptors:"); Serial.println("String Descriptors:");
if( buf.iManufacturer > 0 ) { if ( buf.iManufacturer > 0 ) {
Serial.print("Manufacturer:\t\t"); Serial.print("Manufacturer:\t\t");
rcode = getstrdescr( addr, buf.iManufacturer ); // get manufacturer string rcode = getstrdescr( addr, buf.iManufacturer ); // get manufacturer string
if( rcode ) { if ( rcode ) {
Serial.println( rcode, HEX ); Serial.println( rcode, HEX );
} }
Serial.print("\r\n"); Serial.print("\r\n");
} }
if( buf.iProduct > 0 ) { if ( buf.iProduct > 0 ) {
Serial.print("Product:\t\t"); Serial.print("Product:\t\t");
rcode = getstrdescr( addr, buf.iProduct ); // get product string rcode = getstrdescr( addr, buf.iProduct ); // get product string
if( rcode ) { if ( rcode ) {
Serial.println( rcode, HEX ); Serial.println( rcode, HEX );
} }
Serial.print("\r\n"); Serial.print("\r\n");
} }
if( buf.iSerialNumber > 0 ) { if ( buf.iSerialNumber > 0 ) {
Serial.print("Serial:\t\t\t"); Serial.print("Serial:\t\t\t");
rcode = getstrdescr( addr, buf.iSerialNumber ); // get serial string rcode = getstrdescr( addr, buf.iSerialNumber ); // get serial string
if( rcode ) { if ( rcode ) {
Serial.println( rcode, HEX ); Serial.println( rcode, HEX );
} }
Serial.print("\r\n"); Serial.print("\r\n");
@ -280,40 +280,40 @@ byte getallstrdescr(uint8_t addr)
} }
// function to get single string description // function to get single string description
unsigned int getstrdescr( unsigned int addr, byte idx ) unsigned int getstrdescr( unsigned int addr, uint8_t idx )
{ {
byte buf[ 66 ]; uint8_t buf[ 66 ];
unsigned int rcode; unsigned int rcode;
byte length; uint8_t length;
byte i; uint8_t i;
unsigned short langid; unsigned short langid;
rcode = Usb.getStrDescr( addr, 0, 1, 0, 0, buf ); //get language table length rcode = Usb.getStrDescr( addr, 0, 1, 0, 0, buf ); //get language table length
if( rcode ) { if ( rcode ) {
Serial.println("Error retrieving LangID table length"); Serial.println("Error retrieving LangID table length");
return( rcode ); return ( rcode );
} }
length = buf[ 0 ]; //length is the first byte length = buf[ 0 ]; //length is the first byte
rcode = Usb.getStrDescr( addr, 0, length, 0, 0, buf ); //get language table rcode = Usb.getStrDescr( addr, 0, length, 0, 0, buf ); //get language table
if( rcode ) { if ( rcode ) {
Serial.print("Error retrieving LangID table "); Serial.print("Error retrieving LangID table ");
return( rcode ); return ( rcode );
} }
langid = word(buf[3], buf[2]); langid = word(buf[3], buf[2]);
rcode = Usb.getStrDescr( addr, 0, 1, idx, langid, buf ); rcode = Usb.getStrDescr( addr, 0, 1, idx, langid, buf );
if( rcode ) { if ( rcode ) {
Serial.print("Error retrieving string length "); Serial.print("Error retrieving string length ");
return( rcode ); return ( rcode );
} }
length = buf[ 0 ]; length = buf[ 0 ];
rcode = Usb.getStrDescr( addr, 0, length, idx, langid, buf ); rcode = Usb.getStrDescr( addr, 0, length, idx, langid, buf );
if( rcode ) { if ( rcode ) {
Serial.print("Error retrieving string "); Serial.print("Error retrieving string ");
return( rcode ); return ( rcode );
} }
for( i = 2; i < length; i+=2 ) { for ( i = 2; i < length; i += 2 ) {
Serial.print((char) buf[i]); Serial.print((char) buf[i]);
} }
return( rcode ); return ( rcode );
} }
/* prints hex numbers with leading zeroes */ /* prints hex numbers with leading zeroes */
@ -397,8 +397,8 @@ void printepdescr( uint8_t* descr_ptr )
/*function to print unknown descriptor */ /*function to print unknown descriptor */
void printunkdescr( uint8_t* descr_ptr ) void printunkdescr( uint8_t* descr_ptr )
{ {
byte length = *descr_ptr; uint8_t length = *descr_ptr;
byte i; uint8_t i;
printProgStr(Unk_Header_str); printProgStr(Unk_Header_str);
printProgStr(Unk_Length_str); printProgStr(Unk_Length_str);
print_hex( *descr_ptr, 8 ); print_hex( *descr_ptr, 8 );