phyphox BLE
Plot your Arduino's data in the phyphox app and control the board from the phone
Loading...
Searching...
No Matches
phyphoxBle.h
Go to the documentation of this file.
1// phyphox BLE 2.0 — the public include.
2//
3// #include <phyphoxBle.h>
4// void setup() { PhyphoxBLE::start(); }
5// void loop() { PhyphoxBLE::write(analogRead(A0) * 3.3f / 4096); delay(50); PhyphoxBLE::poll(); }
6//
7// A few lines add phyphox to a project: this is the simple API and the library's main feature.
8// Everything else — describing views, graphs, user inputs and buttons, requesting phone
9// sensors — is in phyphoxBleExperiment.h and optional.
10//
11// The transport (which BLE library talks to the radio) is chosen here from the board macros.
12#ifndef PHYPHOXBLE
13#define PHYPHOXBLE
14
15#include <Arduino.h>
17#include "core/Server.h"
18
19// ---------------------------------------------------------------- phone sensor types
20// The strings are phyphox file-format sensor names (phyphox-docs spec/input.yml).
21#define SENSOR_ACCELEROMETER "accelerometer"
22#define SENSOR_ACCELEROMETER_WITHOUT_G "linear_acceleration"
23#define SENSOR_LINEAR_ACCELERATION "linear_acceleration"
24#define SENSOR_GRAVITY "gravity"
25#define SENSOR_GYROSCOPE "gyroscope"
26#define SENSOR_MAGNETIC_FIELD "magnetic_field"
27#define SENSOR_PRESSURE "pressure"
28#define SENSOR_LIGHT "light"
29#define SENSOR_PROXIMITY "proximity"
30#define SENSOR_TEMPERATURE "temperature"
31#define SENSOR_HUMIDITY "humidity"
32#define SENSOR_ATTITUDE "attitude"
35#define SENSOR_CUSTOM "custom"
38#define SENSOR_MAGNETOMETER SENSOR_MAGNETIC_FIELD
39
40// ---------------------------------------------------------------- colours (1.x)
41#define COLOR_RED "fe005d"
42#define COLOR_BLUE "39a2ff"
43#define COLOR_GREEN "2bfb4c"
44#define COLOR_ORANGE "ff7e22"
45#define COLOR_WHITE "ffffff"
46#define COLOR_YELLOW "edf668"
47#define COLOR_MAGENTA "eb46f4"
48
49// ---------------------------------------------------------------- transport selection
50#include "transport/Select.h"
51#if defined(PHYPHOX_BLE_USE_ESP32)
53 #define PHYPHOX_BLE_TRANSPORT phyphox::Esp32CoreBleTransport
54#elif defined(PHYPHOX_BLE_USE_ESP32_BLE4)
56 #define PHYPHOX_BLE_TRANSPORT phyphox::Esp32Ble4Transport
57#elif defined(PHYPHOX_BLE_USE_NINAB31)
59 #define PHYPHOX_BLE_TRANSPORT phyphox::NinaB31Transport
60#elif defined(PHYPHOX_BLE_USE_ARDUINOBLE)
62 #define PHYPHOX_BLE_TRANSPORT phyphox::ArduinoBleTransport
63#endif
64
65// ---------------------------------------------------------------- the simple API
66
71public:
72 // ---- start
75 static void start(const char* deviceName = "phyphox-Arduino");
77 static void start(const char* deviceName, PhyphoxBleExperiment& experiment);
79 static void start(const char* deviceName, uint8_t* xml, size_t len);
80 static void start(uint8_t* xml, size_t len) { start("phyphox-Arduino", xml, len); }
82 static void addExperiment(PhyphoxBleExperiment& experiment);
83
84 // ---- every loop()
87 static void poll();
88 static void poll(int timeoutMs);
89
90 // ---- data to the phone (channels 1…5; channel 0 is the phone's arrival time)
91 // (const references: 1.x took float&, which rejected write(analogRead(A0) * k); const& takes both)
92 static void write(const float& v1);
93 static void write(const float& v1, const float& v2);
94 static void write(const float& v1, const float& v2, const float& v3);
95 static void write(const float& v1, const float& v2, const float& v3, const float& v4);
96 static void write(const float& v1, const float& v2, const float& v3, const float& v4, const float& v5);
98 static void write(uint8_t* bytes, unsigned int len);
99 static void write(float* values, unsigned int count);
100
101 // ---- data from the phone (the last value of input channels 1…5)
102 static void read(float& v1);
103 static void read(float& v1, float& v2);
104 static void read(float& v1, float& v2, float& v3);
105 static void read(float& v1, float& v2, float& v3, float& v4);
106 static void read(float& v1, float& v2, float& v3, float& v4, float& v5);
107 static void read(uint8_t* bytes, unsigned int len);
108 static float readChannel(int channel);
109 static bool changed(int channel);
112 static void onChange(int channel, phyphox::ChangeCallback);
113 static void onPress(int channel, phyphox::PressCallback);
115 static void (*configHandler)();
116
117 // ---- events (start/pause/clear/sync from the app)
118 static void (*experimentEventHandler)();
119 static uint8_t eventType;
120 static int64_t experimentTime;
121 static int64_t systemTime;
123
124 // ---- radio parameters (before start())
127 static void setMTU(uint16_t payload);
131
132 // ---- introspection and debugging
133 static void printXML(Print* out);
134 static void printErrors(Print* out);
136 static uint16_t currentConnections;
137 static bool isSubscribed;
138 static const char* version() { return PHYPHOX_BLE_VERSION; }
139 static const char* transportName();
140 static void begin(Print* debugOut);
141
143};
144
145#endif
#define PHYPHOX_BLE_VERSION
Library version as reported by PhyphoxBLE::version(). Must match library.properties.
Definition Config.h:20
Static façade over one phyphox::Server.
Definition phyphoxBle.h:70
static float readChannel(int channel)
any input channel
static void addExperiment(PhyphoxBleExperiment &experiment)
Serve this description (copies it). May be called before or after start().
static const char * version()
Definition phyphoxBle.h:138
static phyphox::Server & server()
static void write(const float &v1, const float &v2, const float &v3, const float &v4, const float &v5)
static void read(float &v1, float &v2)
static void write(const float &v1, const float &v2, const float &v3)
static uint16_t timeout
Definition phyphoxBle.h:130
static bool changed(int channel)
true once per new value
static void start(const char *deviceName, uint8_t *xml, size_t len)
Serve a hand-written phyphox document instead of a generated one (1.x form).
static void write(const float &v1, const float &v2, const float &v3, const float &v4)
static void read(float &v1, float &v2, float &v3)
static void start(uint8_t *xml, size_t len)
Definition phyphoxBle.h:80
static const phyphox::ServerStats & stats()
static void read(float &v1, float &v2, float &v3, float &v4, float &v5)
static void printErrors(Print *out)
configuration errors, if any
static uint8_t eventType
phyphox::EventType of the last event
Definition phyphoxBle.h:119
static bool isSubscribed
the phone subscribed to the data characteristic
Definition phyphoxBle.h:137
static void poll()
Gives the BLE stack CPU time and drives the transfer.
static int64_t experimentTime
ms since the experiment was started (−1 on SYNC)
Definition phyphoxBle.h:120
static void read(uint8_t *bytes, unsigned int len)
user-XML mode: the raw cddf1003 value
static uint16_t minConInterval
Connection parameters in BLE units (1.25 ms, 1.25 ms, events, 10 ms).
Definition phyphoxBle.h:130
static void poll(int timeoutMs)
1.x signature; the timeout is ignored
static void write(const float &v1)
static void onPress(int channel, phyphox::PressCallback)
static void start(const char *deviceName="phyphox-Arduino")
Advertise as deviceName ("phyphox-Arduino" by default).
static void start(const char *deviceName, PhyphoxBleExperiment &experiment)
Start and serve experiment in one call.
static void(* configHandler)()
1.x hook: called after any value arrived from the phone (any channel, any sensor).
Definition phyphoxBle.h:115
static void write(const float &v1, const float &v2)
static void printXML(Print *out)
the document as the phone receives it
static const phyphox::ExperimentEvent & lastEvent()
static void setMTU(uint16_t payload)
Ask for an MTU payload up to 512 bytes (default 20).
static void write(uint8_t *bytes, unsigned int len)
Raw arrays, up to the MTU payload; with setRepeating(n) the app unpacks n samples.
static void read(float &v1)
static void write(float *values, unsigned int count)
static void read(float &v1, float &v2, float &v3, float &v4)
static int64_t systemTime
Unix time in ms as the phone sees it.
Definition phyphoxBle.h:121
static void(* experimentEventHandler)()
Definition phyphoxBle.h:118
static const char * transportName()
static uint16_t currentConnections
1.x field, kept up to date
Definition phyphoxBle.h:136
static uint16_t maxConInterval
Definition phyphoxBle.h:130
static void begin(Print *debugOut)
1.x: route debug output here (compiles now)
static void onChange(int channel, phyphox::ChangeCallback)
Attach a callback to an input channel after the experiment was added (the element's onChange() does t...
static uint16_t slaveLatency
Definition phyphoxBle.h:130
Definition phyphoxBleExperiment.h:34
Definition Server.h:25
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
Definition ChannelStore.h:45
Statistics a bench or a curious sketch can read (PhyphoxBLE::stats()).
Definition Server.h:18