Does Serial pc(USBTX, USBRX) become stdout?

19 Apr 2012

This is a two part question. First, when I declare Serial pc(USBTX, USBRX), does the USB-Serial port also become stdout? I am starting to work with BlueUSB to learn (hopefully) how to receive data from a USB keyboard. In the process I find many uses of printf in the BlueUSB code that indeed result in output to the USBRX port.

The second part: In the BlueUSB code I find something that I am guessing is "overloading" of printf:

00036 void printf(const BD_ADDR* addr)

00037 {

00038 const u8* a = addr->addr;

00039 printf("%02X:%02X:%02X:%02X:%02X:%02X",a[5],a[4],a[3],a[2],a[1],a[0]);

00040 }

I am completely new to any uses of C++, so this caught me completely off guard. My guess now is that the compiler finds the printf function seen above in the BlueUSB source and magically figures out that it is an overloaded use of printf and that the call to printf inside the function is then taken by the compiler to be from the cstdio library (or where ever it comes from)?

I will appreciate any guidance related to these topics.

Thanks,

Chuck

19 Apr 2012

Chuck Broadwell wrote:

First, when I declare Serial pc(USBTX, USBRX), does the USB-Serial port also become stdout?

I believe that stdout and stderr always use the serial port connected to the USBTX and USBRX pins. Even if you hadn't defined a "Serial pc" object, the stdio would still use those pins but the baud rate would be fixed at 9600 baud. Since you have this pc Serial object, you can use it to change the baud rate. You can also use the methods on the Serial object (such as pc.printf()) to send serial data to/from USBTX/USBRX pins instead of using the standard C APIs such as printf(). The methods on the Serial object have the advantage that they aren't line buffered. For example, this line of code:

printf("Hello...");

wouldn't be sent to your PC immediately since the standard C library is waiting for a newline '\n' to be sent before it actually sends the queued up data. However this line of code:

pc.printf("Hello...");

would be sent to the PC immediately and no queueing would occur.

Chuck Broadwell wrote:

The second part: In the BlueUSB code I find something that I am guessing is "overloading" of printf:

Sounds right to me. It might also be namespaced to be a method of a class if this code you are showing above was found within a C++ class definition. If it is a global function then the compiler can tell which one is being called by the type of the first parameter to the function. In one case it is a BD_ADDR pointer and the other is a pointer to null terminated string.

19 Apr 2012

Adam,

Thanks, all useful information. I have much to learn.

Chuck