Where does the "Print" type come from?

I’m trying to get myself as familiar with the source code as I can. On line 189 of FOCMotor.h, I see Print apparently used as a type:

    void useMonitoring(Print &serial);

I can’t make sense of that however. Am I misreading it? I tried grepping the entire source code for “Print”, but wasn’t able to find it declared anywhere, and I haven’t been able to find anything by googling it in conjunction with the Arduino library.

For what it’s worth, I also am not sure I understand what Print* is doing on line 213:

    Print* monitor_port; //!< Serial terminal variable if provided

Can someone tell me what those lines are doing? Thank you!

Hi,

“Print” in this case is a superclass of “Serial”. It depends a bit on the configuration and board you are using, but the class hierarchy is typically something like this:

Serial is an instance of “HardwareSerial” (board-dependent implementation class)
HardwareSerial is a subclass of “Stream” (Stream.h, part of Arduino framework)
Stream is a subclass of “Print” (Print.h, part of Arduino framework)

The reason Print is used (rather Serial directly), is because this is more versatile. For example, WiFi connections on ESP32 boards are also instances of “Print”, so you could send your monitoring output to WiFi.

Print* monitor_port;

This line is a field declaration. It is a pointer to a Print object. This is where the monitoring class keeps which “Print” object it was initialised with, and hence where it should send its messages.

Thanks for the comprehensive answer @runger! t was very helpful.

I have another question now: What does the function _isset() do? I see these two define statements:

#define _isset(a) ( (a) != (NOT_SET) )
#define NOT_SET -12345.0

but, I don’t understand two things: 1. how a function can be in a define statement, 2. two what the significance of the value -12345.0 is.

Any help would be appreciated!

Hi, and most welcome.

For _isset(), as you noted yourself it is a preprocessor macro. #define and the pre-processor in general can do a lot more things than people realise :slight_smile: Whether it is a good idea to use this power is another question.

So _isset() is a function defined in the preprocessor, which takes an argument and compares it to the other #define constant -12345.0
This is a purely arbitrary value, chosen to flag the “not set” case.

This is a common pattern (though IMHO not a good practice!) - use a special value to signal special cases, like error or in this case “no value”. This is because the C type “float” always has a value - there is no way to set “No Value”. So SimpleFOC introduces the constant -12345.0 as a special value for “Not set”.
It saves having to make an extra variable like “bool isset”, or working with a pointer (which could then be NULL).