Plotter
An output unit that streams values to a text-based output using a configurable format. The Plotter is designed for quick signal inspection in tools such as the Arduino Serial Plotter, while also supporting structured formats (e.g., CSV, JSON) for logging and external processing.
To create a new Plotter on the default serial port with a specific baudrate:
Plotter plotter(baudrate);
Values are sent in clear text and arranged in rows. Each call to put() (usually
via the >> operator) appends one value to the current row. The row is then finalized
automatically at the end of the Plaquette engine step (or when the Plotter decides to
close the row).
void step() {
value1 >> plotter;
value2 >> plotter;
...
valueN >> plotter;
}
The Plotter can optionally take a comma-separated list of labels (const char*)
such as "wave,signal". When labels are provided, some formats will include a header
(e.g., CSV) or will use key/value rendering (e.g., JSON objects, "label":value).
Plotter plotter(baudrate, "label1,label2,...,labelN");
Tip
On Arduino, you can visualize the data using the Serial Plotter by selecting Tools > Serial Plotter.
Caution
Avoid using a Monitor unit or calling Serial functions while using a Plotter, as
it risks breaking the output syntax of the plotter.
Example
Streaming multiple values per row.
#include <Plaquette.h>
Plotter plotter(115200); // baudrate = 115200
Wave wave(SINE);
AnalogIn signal(A0);
void step() {
wave >> plotter;
signal >> plotter;
}
To add labels to the output:
Plotter plotter(115200, "wave,signal");
You can also change the format by using presets (PLOTTER_CSV, PLOTTER_JSON):
void begin() {
plotter.format(PLOTTER_CSV);
}
Depending on the chosen format and whether labels are provided, labels may be rendered as a
header (CSV), as keys (JSON objects), or as label:value pairs (default space-delimited mode with
labels).
Reference
-
class Plotter : public pq::Unit
Public Functions
-
inline const char *labels() const
Returns labels.
-
void format(PlotterFormatPreset preset)
Sets format based on preset.
-
void format(PlotterFormat format)
Sets format based on custom format.
-
inline PlotterFormat format()
Returns current format.
-
inline void precision(uint8_t digits)
Sets decimal precision of values.
- Parameters:
digits – the number of digits after the point
-
inline uint8_t precision() const
Returns the decimal precision of values.
-
virtual float put(float value) override
Pushes value into the unit.
- Parameters:
value – the value sent to the unit
- Returns:
the new value of the unit
-
inline virtual float get() override
Returns last value.
-
void beginPlot()
Begins new plot.
-
void endPlot()
Ends current plot.
-
inline float seconds() const
Returns engine time in seconds.
-
inline uint32_t milliSeconds() const
Returns engine time in milliseconds.
-
inline uint64_t microSeconds() const
Returns engine time in microseconds.
-
inline unsigned long nSteps() const
Returns number of engine steps.
-
inline float sampleRate() const
Returns engine sample rate.
-
inline float samplePeriod() const
Returns enginesample period.
-
inline operator float()
Object can be used directly to access its value.
-
inline virtual float mapTo(float toLow, float toHigh)
Maps value to new range.
This function guarantees that the value is within [toLow, toHigh]. If the unit’s values are unbounded, returns get() constrained to [toLow, toHigh].
-
inline explicit operator bool()
Operator that allows usage in conditional expressions.
Public Static Functions
-
static PlotterFormat formatFromPreset(PlotterFormatPreset preset, const char *labelsSchema = nullptr)
Returns a PlotterFormat based on presets and labels.
-
static inline bool analogToDigital(float f)
Converts analog (float) value to digital (bool) value.
-
static inline float digitalToAnalog(bool b)
Converts digital (bool) value to analog (float) value.
-
inline const char *labels() const