Plaquette
 
Loading...
Searching...
No Matches
Plotter.h
1/*
2 * Plotter.h
3 *
4 * (c) 2025 Sofian Audry :: info(@)sofianaudry(.)com
5 * (c) 2025 Thomas O Fredericks :: tof(@)t-o-f(.)info
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20#ifndef PQ_PLOTTER_H_
21#define PQ_PLOTTER_H_
22
23#include "PqCore.h"
24#include "PlotterFormat.h"
25#include "pq_serial.h"
26
27namespace pq {
28
29enum PlotterFormatPreset {
30 PLOTTER_DEFAULT = 0,
31 PLOTTER_CSV,
32 PLOTTER_JSON
33};
34
35class Plotter : public Unit {
36public:
37 Plotter(unsigned long baudRate,
38 const char* labels = nullptr,
40
41 Plotter(SerialType& serial,
42 unsigned long baudRate,
43 const char* labels = nullptr,
45
46 explicit Plotter(Print& out,
47 const char* labels = nullptr,
49
51 static PlotterFormat formatFromPreset(PlotterFormatPreset preset, const char* labelsSchema = nullptr);
52
54 const char* labels() const { return _labels; }
55
57 void format(PlotterFormatPreset preset);
58
61
63 PlotterFormat format() { return _format; }
64
69 void precision(uint8_t digits) { _digits = digits; }
70
72 uint8_t precision() const { return _digits; }
73
79 float put(float value) override;
80
82 float get() override { return _lastValue; }
83
85 void beginPlot();
86
88 void endPlot();
89
90protected:
91 void begin() override;
92 void step() override;
93
94private:
95 // Output backend
96 Print* _out = nullptr;
97
98 // If output is serial, we begin it in begin()
99 SerialType* _serial = nullptr;
100 unsigned long _baudRate = 0;
101
102 // Mode & format
103 PlotterFormat _format;
104
105 // Labels schema (comma-separated)
106 const char* _labels = nullptr;
107 uint16_t _labelCount = 0;
108 bool _headerPrinted = false;
109
110 // Numeric precision
111 uint8_t _digits = PLAQUETTE_PRINT_DEFAULT_DIGITS;
112
113 // Row state.
114 bool _rowOpen = false;
115 uint16_t _valueIndex = 0;
116
117 float _lastValue = 0.0f;
118
119 // Plot state.
120 bool _plotOpen = true;
121
122 // Scheduling flags.
123 bool _scheduleBeginPlot = false;
124 bool _scheduleEndRow = false;
125
126private:
127 // Manually ends the current row (prints rowEnd if a row is open).
128 void _endRow();
129
130 // Internal helpers
131 void _rebuildFormat(PlotterFormatPreset preset);
132 void _ensureHeader();
133 void _openRowIfNeeded();
134 void _closeRowIfOpen(bool printEndRow);
135
136 // Print value to output.
137 void _printValue(float value);
138
139 // Label parsing helpers (comma-separated schema)
140 static bool _isSpace(char c);
141 static LabelView _trimSpaces(const char* start, const char* end);
142
143 uint16_t _countLabels(const char* schema) const;
144 LabelView _labelAt(uint16_t index) const;
145};
146
147} // namespace pq
148
149#endif // PQ_PLOTTER_H_
The main Plaquette static class containing all the units.
Definition PqCore.h:63
static Engine & primary()
Returns the main instance of Plaquette.
Definition PqCore.cpp:30
Definition Plotter.h:35
float get() override
Returns last value.
Definition Plotter.h:82
PlotterFormat format()
Returns current format.
Definition Plotter.h:63
const char * labels() const
Returns labels.
Definition Plotter.h:54
void beginPlot()
Begins new plot.
Definition Plotter.cpp:107
void precision(uint8_t digits)
Sets decimal precision of values.
Definition Plotter.h:69
float put(float value) override
Pushes value into the unit.
Definition Plotter.cpp:160
void endPlot()
Ends current plot.
Definition Plotter.cpp:117
uint8_t precision() const
Returns the decimal precision of values.
Definition Plotter.h:72
static PlotterFormat formatFromPreset(PlotterFormatPreset preset, const char *labelsSchema=nullptr)
Returns a PlotterFormat based on presets and labels.
Definition Plotter.cpp:57
A generic class representing a unit in the system.
Definition PqCore.h:373
Engine * engine() const
Returns the engine that owns this unit.
Definition PqCore.h:420
Lightweight non-owning view of a label (no allocation).
Definition PlotterFormat.h:34
Formatting specification used by Plotter to render values and (optionally) a header.
Definition PlotterFormat.h:62