TinyRobotics
Loading...
Searching...
No Matches
MultiOutput.h
1#pragma once
2#include <vector>
3
4#include "Print.h"
5
6namespace tinyrobotics {
7
8/**
9 * @class MultiOutput
10 * @ingroup communication
11 * @brief Utility class for writing output to multiple Print streams
12 * simultaneously.
13 *
14 * MultiOutput allows you to broadcast print output to several Print targets
15 * (e.g., Serial, file, network) at once. It implements the Print interface, so
16 * it can be used anywhere a Print object is expected.
17 *
18 * Example usage:
19 * @code
20 * MultiOutput multi;
21 * multi.addOutput(Serial);
22 * multi.addOutput(myFile);
23 * multi.println("Hello, world!"); // Prints to both Serial and file
24 * @endcode
25 *
26 * This is useful for logging, debugging, or mirroring output to multiple
27 * destinations in embedded systems.
28 */
29class MultiOutput : public Print {
30 public:
31 /// Defines a MultiOutput with no final output: Define your outputs with add()
32 MultiOutput() = default;
33
34 /**
35 * @brief Construct MultiOutput with a variable number of Print objects.
36 *
37 * Example usage:
38 * @code
39 * MultiOutput multi(Serial, myFile);
40 * @endcode
41 *
42 * @param outputs Variable number of Print references.
43 */
44 template <typename... Prints>
45 MultiOutput(Prints&... outputs) {
46 addOutputs(outputs...);
47 }
48
49 virtual ~MultiOutput() { clear(); }
50
51 void add(Print& print) { vector.push_back(&print); }
52 void flush() {
53 for (size_t j = 0; j < vector.size(); j++) {
54 vector[j]->flush();
55 }
56 }
57
58 size_t write(const uint8_t* data, size_t len) override {
59 for (auto& out : vector) {
60 int open = len;
61 int start = 0;
62 // create copy of data to avoid that one output changes the data for the
63 // other outputs
64 uint8_t copy[len];
65 memcpy(copy, data, len);
66 while (open > 0) {
67 int written = out->write(copy + start, open);
68 open -= written;
69 start += written;
70 }
71 }
72 return len;
73 }
74
75 size_t write(uint8_t ch) override {
76 for (size_t j = 0; j < vector.size(); j++) {
77 int open = 1;
78 while (open > 0) {
79 open -= vector[j]->write(ch);
80 }
81 }
82 return 1;
83 }
84
85 /// Removes all output components
86 void clear() { vector.clear(); }
87
88 protected:
89 std::vector<Print*> vector;
90
91 /// support for Pipleline
92 void setOutput(Print& out) { add(out); }
93
94 // Helper to add multiple outputs
95 void addOutputs() {}
96 template <typename First, typename... Rest>
97 void addOutputs(First& first, Rest&... rest) {
98 add(first);
99 addOutputs(rest...);
100 }
101};
102
103} // namespace tinyrobotics
Utility class for writing output to multiple Print streams simultaneously.
Definition: MultiOutput.h:29
void clear()
Removes all output components.
Definition: MultiOutput.h:86
MultiOutput(Prints &... outputs)
Construct MultiOutput with a variable number of Print objects.
Definition: MultiOutput.h:45
void setOutput(Print &out)
support for Pipleline
Definition: MultiOutput.h:92
MultiOutput()=default
Defines a MultiOutput with no final output: Define your outputs with add()