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 defaultUSB.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 UARTUSB.println(charac); // Writes the char 'a' to the UART adding an EOLUSB.println(); // Writes an EOL}
Print a string
{char string[] ="Hello";USB.print(string); // Writes a string to the UARTUSB.println(string); // Writes a string to the UART adding an EOL}
Print uint8_t (1 byte)
{uint8_tunsigned=3;USB.print(unsigned); // Writes the number '3' to the UARTUSB.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 UARTUSB.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 UARTUSB.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 UARTUSB.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}