phyphox BLE
Plot your Arduino's data in the phyphox app and control the board from the phone
Loading...
Searching...
No Matches
phyphoxBleExperiment.h
Go to the documentation of this file.
1// phyphox BLE 2.0 — the experiment description API.
2//
3// A sketch describes what the phyphox app should show (views, graphs, values, user inputs,
4// buttons, export sets) and which phone sensors it wants, then hands the description to
5// PhyphoxBLE::addExperiment(). The library generates the phyphox XML from it on demand and
6// transfers it when a phone connects.
7//
8// Lifetime rules (the same as 1.x): build everything in setup(), call addExperiment() while all
9// objects are still alive — it copies the description into one exactly-sized block — and pass
10// string literals or static buffers, never temporaries (docs/concepts.md, "Strings").
11//
12// There are no capacity limits: views, elements, curves, options, export sets and sensors are
13// linked into intrusive lists (each object carries one `next` pointer), so an object can be in
14// one list at a time and the block is sized from what was actually added.
15//
16// This header is Arduino-independent so the core can be unit-tested on a host.
17#ifndef PHYPHOX_BLE_EXPERIMENT
18#define PHYPHOX_BLE_EXPERIMENT
19
20#include <stdint.h>
21#include "core/Config.h"
22#include "core/ElementData.h"
23
25#define STYLE_LINES "lines"
26#define STYLE_DOTS "dots"
27#define STYLE_VBARS "vbars"
28#define STYLE_HBARS "hbars"
29#define STYLE_MAP "map"
30#define LAYOUT_AUTO "auto"
31#define LAYOUT_EXTEND "extend"
32#define LAYOUT_FIXED "fixed"
33
35public:
38 PhyphoxBleExperiment(const char* title, const char* category = nullptr, const char* description = nullptr);
41
42 // ------------------------------------------------------------------ view elements
43
44 class View;
45
47 class Element {
48 public:
49 Element(const Element&) = delete;
50 Element& operator=(const Element&) = delete;
51 virtual ~Element() {}
52 virtual void setLabel(const char*);
56 void setXMLAttribute(const char*);
59 void setVisibility(int inputChannel);
61 const phyphox::ElementData& data() const { return data_; }
62 virtual bool isExportData() const { return false; }
63 Element* next = nullptr;
64 View* owner = nullptr;
68 const Element* aliasOf = nullptr;
69 protected:
72 };
73
74 class Graph : public Element {
75 public:
78 class Subgraph {
79 public:
80 Subgraph() = default;
81 Subgraph(const Subgraph&) = delete;
82 void setChannel(int x, int y);
83 void setColor(const char* hex6);
84 void setStyle(const char* style);
85 void setLinewidth(float);
86 void setLineWidth(float w) { setLinewidth(w); }
89 Subgraph* next = nullptr;
90 };
91
92 Graph() : Element(phyphox::EL_GRAPH) {}
93 explicit Graph(const char* label) : Graph() { setLabel(label); }
94
95 // 1.x setters
96 void setUnitX(const char*);
97 void setUnitY(const char*);
98 void setLabelX(const char*);
99 void setLabelY(const char*);
100 void setXPrecision(int);
101 void setYPrecision(int);
102 void setTimeOnX(bool);
103 void setTimeOnY(bool);
104 void setSystemTime(bool);
105 void setChannel(int x, int y);
107 void addChannel(int x, int y, const char* color);
108 void setStyle(const char*);
109 void setColor(const char*);
110 void setLinewidth(float);
111 void setLineWidth(float w) { setLinewidth(w); }
112 void setMinX(float, const char* layout);
113 void setMaxX(float, const char* layout);
114 void setMinY(float, const char* layout);
115 void setMaxY(float, const char* layout);
116 // new in 2.0 (D16, D17)
117 void setLinearTime(bool);
118 void setLogX(bool);
119 void setLogY(bool);
120 void setFollowX(bool);
125 void setAspectRatio(float);
126 void setUnitYperX(const char*);
127
129 Subgraph* subgraphs = nullptr;
131 };
132
133 class Value : public Element {
134 public:
135 Value() : Element(phyphox::EL_VALUE) {}
136 explicit Value(const char* label, int channel = 1) : Value() { setLabel(label); setChannel(channel); }
137 void setPrecision(int);
138 void setUnit(const char*);
139 void setColor(const char*);
140 void setChannel(int);
141 // new in 2.0
142 void setSize(float);
143 void setFactor(float);
144 void setScientific(bool);
147 void addMap(float min, float max, const char* text);
148 void addMapBelow(float max, const char* text);
149 void addMapAbove(float min, const char* text);
150 void setMaps(int n, const float* mins, const float* maxs, const char* const texts[]);
151
153 const phyphox::MapEntry* mapsArray = nullptr;
154 const float* mapsMins = nullptr;
155 const float* mapsMaxs = nullptr;
156 const char* const* mapsTexts = nullptr;
157 uint8_t mapCount = 0;
158 };
159
161 class InputElement : public Element {
162 public:
163 void setChannel(int);
164 void setDefault(float);
167 protected:
169 };
170
171 class Button;
172
173 class Edit : public InputElement {
174 public:
175 Edit() : InputElement(phyphox::EL_EDIT) {}
176 explicit Edit(const char* label, int channel = 0) : Edit() { setLabel(label); if (channel) setChannel(channel); }
177 void setUnit(const char*);
178 void setSigned(bool);
179 void setDecimal(bool);
180 // new in 2.0
181 void setMin(float);
182 void setMax(float);
183 void setFactor(float);
186 };
187
188 class Toggle : public InputElement {
189 public:
190 Toggle() : InputElement(phyphox::EL_TOGGLE) {}
191 explicit Toggle(const char* label, int channel = 0) : Toggle() { setLabel(label); if (channel) setChannel(channel); }
192 };
193
194 class Slider : public InputElement {
195 public:
196 Slider() : InputElement(phyphox::EL_SLIDER) {}
197 Slider(const char* label, float minValue, float maxValue, float stepSize, int channel = 0);
198 void setMinValue(float);
199 void setMaxValue(float);
200 void setStepSize(float);
201 void setPrecision(int);
202 void setShowValue(bool);
203 void setColor(const char*);
204 };
205
206 class Dropdown : public InputElement {
207 public:
208 Dropdown() : InputElement(phyphox::EL_DROPDOWN) {}
209 explicit Dropdown(const char* label, int channel = 0) : Dropdown() { setLabel(label); if (channel) setChannel(channel); }
211 void setOptions(int n, const char* const labels[], const float* values);
213 void addOption(const char* label, float value);
214 void setColor(const char*);
215
217 const char* const* optionLabels = nullptr;
218 const float* optionValues = nullptr;
219 uint8_t optionCount = 0;
220 };
221
224 class Button : public InputElement {
225 public:
226 Button() : InputElement(phyphox::EL_BUTTON) {}
227 explicit Button(const char* label, int channel = 0) : Button() { setLabel(label); if (channel) setChannel(channel); }
228 void setValue(float);
230 };
231
232 class InfoField : public Element {
233 public:
234 InfoField() : Element(phyphox::EL_INFO) {}
235 explicit InfoField(const char* text) : InfoField() { setInfo(text); }
236 void setInfo(const char*);
237 void setColor(const char*);
238 // new in 2.0
239 void setSize(float);
240 void setAlign(const char* leftRightCenter);
241 void setBold(bool);
242 void setItalic(bool);
243 };
244
245 class Separator : public Element {
246 public:
247 Separator() : Element(phyphox::EL_SEPARATOR) {}
248 void setHeight(float);
249 void setColor(const char*);
250 };
251
253 class View {
254 public:
255 View() = default;
256 explicit View(const char* label) { setLabel(label); }
257 View(const View&) = delete;
259 void setLabel(const char*);
260 void setXMLAttribute(const char*);
263 Element* elements = nullptr;
265 View* next = nullptr;
267 };
268
269 // ------------------------------------------------------------------ export
270
271 class ExportData : public Element {
272 public:
273 ExportData() : Element(phyphox::EL_NONE) {}
274 ExportData(const char* label, int channel) : ExportData() { setLabel(label); setDatachannel(channel); }
275 void setDatachannel(int);
276 void setDataChannel(int c) { setDatachannel(c); }
277 void setLabel(const char*) override;
278 bool isExportData() const override { return true; }
281 };
282
283 class ExportSet {
284 public:
285 ExportSet() = default;
286 explicit ExportSet(const char* label) { setLabel(label); }
287 ExportSet(const ExportSet&) = delete;
288 void setLabel(const char*);
289 void setXMLAttribute(const char*);
293 ExportData* entries = nullptr;
295 ExportSet* next = nullptr;
297 };
298
299 // ------------------------------------------------------------------ phone sensors
300
302 class Sensor {
303 public:
304 Sensor() = default;
305 explicit Sensor(const char* type) { setType(type); }
306 Sensor(const Sensor&) = delete;
307 void setType(const char*);
308 void setAverage(bool);
309 void setRate(int hz);
312 void setRateStrategy(const char*);
313 void setStride(int n);
316 void setNameFilter(const char*);
317 void setTypeFilter(int androidSensorType);
319 void mapChannel(const char* component, int ch);
320 void setComponent(const char*);
321 void setXMLAttribute(const char*);
323 Sensor* next = nullptr;
324 };
325
326 // ------------------------------------------------------------------ the experiment
327
328 void setTitle(const char*);
329 void setCategory(const char*);
330 void setDescription(const char*);
331 void setColor(const char*);
333 void setRepeating(int);
341
343 View* views = nullptr;
344 View* lastView = nullptr;
345 Sensor* sensors = nullptr;
346 Sensor* lastSensor = nullptr;
349};
350
351#endif
#define PHYPHOX_BLE_INLINE_OPTIONS
Entries the incremental addMap()/addOption() setters keep inside the builder object before addExperim...
Definition Config.h:41
New in 2.0.
Definition phyphoxBleExperiment.h:224
void onPress(phyphox::PressCallback)
Button(const char *label, int channel=0)
Definition phyphoxBleExperiment.h:227
Button()
Definition phyphoxBleExperiment.h:226
Definition phyphoxBleExperiment.h:206
Dropdown()
Definition phyphoxBleExperiment.h:208
uint8_t optionCount
Definition phyphoxBleExperiment.h:219
const char *const * optionLabels
setOptions(): the caller's arrays
Definition phyphoxBleExperiment.h:217
void setOptions(int n, const char *const labels[], const float *values)
1.x form: n options from two arrays (copied at addExperiment; the arrays may be locals).
phyphox::MapEntry inlineOptions[PHYPHOX_BLE_INLINE_OPTIONS]
Definition phyphoxBleExperiment.h:216
const float * optionValues
Definition phyphoxBleExperiment.h:218
void setColor(const char *)
void addOption(const char *label, float value)
New in 2.0; up to PHYPHOX_BLE_INLINE_OPTIONS this way, any number via setOptions().
Dropdown(const char *label, int channel=0)
Definition phyphoxBleExperiment.h:209
Definition phyphoxBleExperiment.h:173
void submitWith(Button &)
Deliver this field only when button is pressed (format 1.20, triggerId).
Edit()
Definition phyphoxBleExperiment.h:175
Edit(const char *label, int channel=0)
Definition phyphoxBleExperiment.h:176
void setUnit(const char *)
Base of every view element. Holds the plain data the serializer reads.
Definition phyphoxBleExperiment.h:47
const Element * aliasOf
An element added to a second view is represented there by a small heap-allocated alias that points ba...
Definition phyphoxBleExperiment.h:68
phyphox::ElementData data_
Definition phyphoxBleExperiment.h:71
virtual void setLabel(const char *)
void setXMLAttribute(const char *)
Raw attribute text appended verbatim to the element's start tag, e.g.
const phyphox::ElementData & data() const
Definition phyphoxBleExperiment.h:61
virtual ~Element()
Definition phyphoxBleExperiment.h:51
Element(const Element &)=delete
View * owner
the view whose list holds this object (set by addElement)
Definition phyphoxBleExperiment.h:64
void setVisibility(int inputChannel)
Show the element only while the last value in that input channel is > 0 (a toggle's channel,...
Element * next
intrusive list link, set by View::addElement()
Definition phyphoxBleExperiment.h:63
Element(phyphox::ElementType t)
Definition phyphoxBleExperiment.h:70
virtual bool isExportData() const
Definition phyphoxBleExperiment.h:62
Element & operator=(const Element &)=delete
phyphox::ElementData & data()
Definition phyphoxBleExperiment.h:60
Definition phyphoxBleExperiment.h:271
bool isExportData() const override
Definition phyphoxBleExperiment.h:278
ExportData(const char *label, int channel)
Definition phyphoxBleExperiment.h:274
ExportData * nextEntry
Definition phyphoxBleExperiment.h:280
phyphox::ExportDataData exportData
Definition phyphoxBleExperiment.h:279
ExportData()
Definition phyphoxBleExperiment.h:273
void setDatachannel(int)
0 = time, 1…5 = data channels
void setLabel(const char *) override
void setDataChannel(int c)
Definition phyphoxBleExperiment.h:276
Definition phyphoxBleExperiment.h:283
ExportSet * next
Definition phyphoxBleExperiment.h:295
void setXMLAttribute(const char *)
ExportData * lastEntry
Definition phyphoxBleExperiment.h:294
phyphox::ErrorRecord error
Definition phyphoxBleExperiment.h:296
ExportData * entries
list head
Definition phyphoxBleExperiment.h:293
ExportSet & addElement(ExportData &)
void addElement(Element &)
1.x signature; must be an ExportData
ExportSet(const char *label)
Definition phyphoxBleExperiment.h:286
phyphox::ExportSetData data
Definition phyphoxBleExperiment.h:292
ExportSet(const ExportSet &)=delete
One additional curve in a graph.
Definition phyphoxBleExperiment.h:78
Subgraph(const Subgraph &)=delete
void setStyle(const char *style)
void setLineWidth(float w)
Definition phyphoxBleExperiment.h:86
phyphox::ErrorRecord error
Definition phyphoxBleExperiment.h:88
Subgraph * next
Definition phyphoxBleExperiment.h:89
void setChannel(int x, int y)
0 = time, 1…5 = data channels
phyphox::SubgraphData data
Definition phyphoxBleExperiment.h:87
Definition phyphoxBleExperiment.h:74
void setSuppressScientificNotation(bool)
Graph()
Definition phyphoxBleExperiment.h:92
void setChannel(int x, int y)
the first curve
void setLabelX(const char *)
void setMinY(float, const char *layout)
void addChannel(int x, int y, const char *color)
1.x shortcut: add a curve in one call
void setUnitYperX(const char *)
void setMinX(float, const char *layout)
void setMaxY(float, const char *layout)
void setUnitY(const char *)
Subgraph * lastSubgraph
Definition phyphoxBleExperiment.h:130
void addSubgraph(Subgraph &)
Subgraph first
the curve setChannel/setStyle/setColor configure
Definition phyphoxBleExperiment.h:128
void setLinewidth(float)
first curve
void setStyle(const char *)
first curve
void setUnitX(const char *)
Subgraph * subgraphs
added curves, in order
Definition phyphoxBleExperiment.h:129
void setLabelY(const char *)
void setLineWidth(float w)
Definition phyphoxBleExperiment.h:111
void setMaxX(float, const char *layout)
Graph(const char *label)
Definition phyphoxBleExperiment.h:93
void setColor(const char *)
first curve
Definition phyphoxBleExperiment.h:232
InfoField()
Definition phyphoxBleExperiment.h:234
InfoField(const char *text)
Definition phyphoxBleExperiment.h:235
void setAlign(const char *leftRightCenter)
void setInfo(const char *)
the text (the info element's label)
Common API of the elements that send a value to the board.
Definition phyphoxBleExperiment.h:161
void setChannel(int)
input channel 1…PHYPHOX_BLE_INPUT_CHANNELS
InputElement(phyphox::ElementType t)
Definition phyphoxBleExperiment.h:168
void onChange(phyphox::ChangeCallback)
Called on the board whenever the phone delivers a new value for this channel.
A sensor of the phone whose readings the phone sends to the board.
Definition phyphoxBleExperiment.h:302
void setType(const char *)
a SENSOR_* constant (phyphox file-format name)
void setRateStrategy(const char *)
How the phone reconciles the requested rate with what its sensor offers: "auto", "request",...
void setStride(int n)
use only every n-th reading (1.14)
void setNameFilter(const char *)
Select a sensor by name (the app's device info lists them) — needed for SENSOR_CUSTOM,...
void setTypeFilter(int androidSensorType)
select by Android sensor type constant (1.19)
Sensor(const char *type)
Definition phyphoxBleExperiment.h:305
void mapChannel(const char *component, int ch)
Deliver component ("x", "y", "z", "abs", "accuracy", "t") into input channel ch.
void setXMLAttribute(const char *)
Sensor * next
Definition phyphoxBleExperiment.h:323
void setComponent(const char *)
1.x: like mapChannel(component, next free)
Sensor(const Sensor &)=delete
phyphox::SensorData data
Definition phyphoxBleExperiment.h:322
Definition phyphoxBleExperiment.h:245
Separator()
Definition phyphoxBleExperiment.h:247
Definition phyphoxBleExperiment.h:194
void setColor(const char *)
new in 2.0
Slider(const char *label, float minValue, float maxValue, float stepSize, int channel=0)
Slider()
Definition phyphoxBleExperiment.h:196
Definition phyphoxBleExperiment.h:188
Toggle(const char *label, int channel=0)
Definition phyphoxBleExperiment.h:191
Toggle()
Definition phyphoxBleExperiment.h:190
Definition phyphoxBleExperiment.h:133
const phyphox::MapEntry * mapsArray
reserved
Definition phyphoxBleExperiment.h:153
void addMap(float min, float max, const char *text)
Show text instead of the number while min <= value <= max (D17); the first match wins.
void setChannel(int)
data channel 1…5 shown
void setUnit(const char *)
Value()
Definition phyphoxBleExperiment.h:135
phyphox::MapEntry inlineMaps[PHYPHOX_BLE_INLINE_OPTIONS]
Definition phyphoxBleExperiment.h:152
void addMapBelow(float max, const char *text)
const char *const * mapsTexts
Definition phyphoxBleExperiment.h:156
const float * mapsMins
setMaps(): the caller's arrays, read at addExperiment
Definition phyphoxBleExperiment.h:154
Value(const char *label, int channel=1)
Definition phyphoxBleExperiment.h:136
const float * mapsMaxs
Definition phyphoxBleExperiment.h:155
void setColor(const char *)
void addMapAbove(float min, const char *text)
void setMaps(int n, const float *mins, const float *maxs, const char *const texts[])
uint8_t mapCount
Definition phyphoxBleExperiment.h:157
A tab in the app. Links its elements into a list until addExperiment() copies them.
Definition phyphoxBleExperiment.h:253
View & addElement(Element &)
View * next
Definition phyphoxBleExperiment.h:265
void setXMLAttribute(const char *)
View(const char *label)
Definition phyphoxBleExperiment.h:256
~View()
frees the aliases this view created
phyphox::ErrorRecord error
Definition phyphoxBleExperiment.h:266
Element * elements
list head
Definition phyphoxBleExperiment.h:263
View(const View &)=delete
void setLabel(const char *)
phyphox::ViewData data
Definition phyphoxBleExperiment.h:262
Element * lastElement
Definition phyphoxBleExperiment.h:264
Definition phyphoxBleExperiment.h:34
View * lastView
Definition phyphoxBleExperiment.h:344
void setCategory(const char *)
Sensor * sensors
Definition phyphoxBleExperiment.h:345
PhyphoxBleExperiment & addExportSet(ExportSet &)
PhyphoxBleExperiment & addView(View &)
PhyphoxBleExperiment & addSensor(Sensor &)
PhyphoxBleExperiment(const PhyphoxBleExperiment &)=delete
void setResendUnchanged(bool)
1.x behaviour: the phone writes every input's value every analysis cycle, not only on change (decisio...
void setTitle(const char *)
phyphox::ExperimentData data
experiment-level fields; arrays filled at addExperiment
Definition phyphoxBleExperiment.h:342
void setSubscribeOnStart(bool)
View * views
list heads
Definition phyphoxBleExperiment.h:343
ExportSet * lastExportSet
Definition phyphoxBleExperiment.h:348
ExportSet * exportSets
Definition phyphoxBleExperiment.h:347
PhyphoxBleExperiment & operator=(const PhyphoxBleExperiment &)=delete
Sensor * lastSensor
Definition phyphoxBleExperiment.h:346
void setColor(const char *)
PhyphoxBleExperiment()=default
PhyphoxBleExperiment(const char *title, const char *category=nullptr, const char *description=nullptr)
Shortcut for setTitle/setCategory/setDescription.
void setDescription(const char *)
void setRepeating(int)
Samples per data notification when using the array write; emits the repeating attribute.
Definition ChannelStore.h:11
ElementType
Definition ElementData.h:22
void(* ChangeCallback)(float value)
Callback types. Plain function pointers (captureless lambdas convert), never std::function.
Definition ElementData.h:19
void(* PressCallback)()
Definition ElementData.h:20
One view element. label and xmlAttribute are common to all; the union holds the rest.
Definition ElementData.h:146
void init(ElementType t)
Activate the union member for t with its defaults (the builders call this).
The first error recorded on an element, with the setter it came from.
Definition Errors.h:26
The whole experiment.
Definition ElementData.h:211
Definition ElementData.h:178
Definition ElementData.h:184
A value map entry (text shown while min <= value <= max) or a dropdown option (text + value).
Definition ElementData.h:68
Definition ElementData.h:193
One curve of a graph. Channel 0 is the phone's arrival time, 1…5 the data channels.
Definition ElementData.h:35
Definition ElementData.h:171