USB Library

Waspmote Libraries

Waspmote USB Files

WaspUSB.h ; WaspUSB.cpp

Constructor

To start using Waspmote USB library, an object from class 'WaspUSB' must be created. This object, called USB, is created inside the Waspmote USB library and it is public to all libraries. It is used through the guide to show how the Waspmote USB library works.

When creating this constructor, one variable is initialized. This variable specifies the number of the UART that USB is going to use (UART0 in this case).

Pre-Defined Constants

There are some constants defined in 'WaspUSB .h' related with the different kind of numbers that can be printed on the screen.

Initialization

Two functions have been created to open and close the UART used to communicate via USB.

Example of use:

{
    USB.ON(); // Opens the UART at 115200 bps by default
    USB.OFF(); // Closes the UART
}

Reading data

Two functions have been developed for reading data or checking if data is available in the UART. One more function has been developed to free the UART buffer.

Example of use:

{
    int data_read=0;
    if(USB.available())
    {
        // If data is available '1' is returned
        data_read=USB.read(); // Reads data from UART0
    }
    USB.flush(); // Frees the UART buffer. All the data unread are lost.
}

Printing data

Some functions have been created to write data to the UART.

Example of use:

Print a character

{
    char charac='a';
    USB.print(charac); // Writes the char 'a' to the UART
    USB.println(charac); // Writes the char 'a' to the UART adding an EOL
    USB.println(); // Writes an EOL
}

Print a string

{
    char string[] = "Hello";
    USB.print(string); // Writes a string to the UART
    USB.println(string); // Writes a string to the UART adding an EOL
}

Print uint8_t (1 byte)

{
    uint8_t unsigned=3;
    USB.print(unsigned); // Writes the number '3' to the UART
    USB.println(unsigned); // Writes the number '3' to the UART adding an EOL
}

Print int (2 bytes)

{
    int integer=54345;
    USB.print(integer); // Writes the number '54345' to the UART
    USB.println(integer); // Writes the number '54345' to the UART adding an EOL
}

Print long (4 bytes)

{
    long long_int=1234567;
    USB.print(long_int); //Writes the number '1234567' to the UART
    USB.println(long_int); //Writes the number '1234567' to the UART adding an EOL
}

Print float (4 bytes)

{
    float float_num=1.23456;
    USB.print(float_num); //Writes the number '1.23456' to the UART
    USB.println(float_num); //Writes the number '1.23456' to the UART adding an EOL
}

Print hexadecimal number (1 byte)

{
    uint8_t num = 0x14;
    USB.printHex(num); // Writes the number 0x14 in base 16
}

Print arrays:

{
    uint8_t array[5] = { 0x31, 0x32, 0x33, 0x34, 0x35 };
    USB.print( array, 5 );
    USB.println( array, 5 ); // adding EOL
}

Print arrays in HEX format:

{
    uint8_t array[5] = { 0x31, 0x32, 0x33, 0x34, 0x35 };
    USB.printHex( array, 5 );
    USB.printHexln( array, 5 ); // adding EOL
}

Print numbers in different formats

{
    USB.println(78); // Writes 78 in integer format: \'78\'
    USB.println(78, BIN); // Writes 78 in binary format: \'1001110\'
    USB.println(78, OCT); // Writes 78 in octal format: \'116\'
    USB.println(78, DEC); // Writes 78 in integer format: \'78\'
    USB.println(78, HEX); // Writes 78 in hexadecimal format: \'4E\'
}

Print formatted data

{
    int var1=0xABCD;
    int var2=3.1416;
    int var3=32767; //max signed int
    int var4=32768; //overflows signed int range
    long var5=2147483647; //max signed long int
    long var6=2147483648; //overflows signed long range
    unsigned long var7=4294967295; //max unsigned long
    USB.printf(\"%s\n\", \"Hello world\"); // Writes "Hello World" with EOL
    USB.printf(\"millis: %lu\n\",millis()); // Writes "millis: 356" with EOL
    USB.printf(\"hexadecimal: %x\n\", var1); // Writes "hexadecimal: abcd" with EOL
    USB.printf(\"decimal: %d\n\", var1); // Writes "decimal: -21555" with EOL
    USB.printf(\"unsigned int: %u\n\", var1);// Writes "unsigned int: 43981" with EOL
    USB.printf(\"float: %f\n\", var2); // It is not possible to print floats this way
    USB.printf(\"int: %d\n\", var3); // Writes "int: 32767" with EOL
    USB.printf(\"\'32768\' overflows: %d\n\", var4); // Writes "\'32768\' overflows: -32768"
    USB.printf(\"signed long: %ld\n\", var5);// Writes "signed long: 2147483647" with EOL
    USB.printf(\"\'2147483648\' overflows: %ld\n\", var6);Writes "\'2147483648\' overflows: -2147483648"
    USB.printf(\"unsigned long: %lu\n\", var7); // Writes "unsigned long: 4294967295"
}

USB function example: http://www.development.libelium.com/usb-01-usb-functions

Formatted print function example: http://www.development.libelium.com/usb-02-usb-printf-function

Last updated