2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
[![Arduino CI](https://github.com/RobTillaart/Multiplex/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
2021-09-14 11:02:45 -04:00
|
|
|
[![JSON check](https://github.com/RobTillaart/Multiplex/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/Multiplex/actions/workflows/jsoncheck.yml)
|
|
|
|
[![Arduino-lint](https://github.com/RobTillaart/Multiplex/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/Multiplex/actions/workflows/arduino-lint.yml)
|
2021-01-29 06:31:58 -05:00
|
|
|
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Multiplex/blob/master/LICENSE)
|
|
|
|
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Multiplex.svg?maxAge=3600)](https://github.com/RobTillaart/Multiplex/releases)
|
|
|
|
|
2021-12-22 06:37:31 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
# Multiplex
|
|
|
|
|
2021-09-14 11:02:45 -04:00
|
|
|
Arduino Library implementing a (Print) stream multiplexer.
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
## Description
|
|
|
|
|
2021-09-14 11:02:45 -04:00
|
|
|
Multiplex is a library in which one can add multiple Print streams.
|
2022-06-13 06:51:39 -04:00
|
|
|
If one prints to the multiplexer the data is sent to all the streams that were added and are enabled.
|
2021-08-10 16:25:19 -04:00
|
|
|
|
2021-09-14 11:02:45 -04:00
|
|
|
Streams can be enabled or disabled by calling `enable()/disable()` passing either an index (based on the order
|
2022-06-13 06:51:39 -04:00
|
|
|
in which `add()` was called; 0 is first) or a pointer to the `Print`
|
2021-09-14 11:02:45 -04:00
|
|
|
object that was passed to `add(Print *)` by calling `enableStream()/disableStream()`
|
2022-06-13 06:51:39 -04:00
|
|
|
Default all streams are enabled.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Number of streams
|
|
|
|
|
|
|
|
The maximum number of streams to add is default 4.
|
|
|
|
This value is defined in the **multiplex.h file** and can be overruled on the command line.
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
#define MAX_MULTIPLEX 4
|
|
|
|
```
|
|
|
|
|
|
|
|
This value can be set to 254 MAX as the number 255 / 0xFF is used as a NOT_FOUND flag.
|
2021-08-10 16:25:19 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
### Removal of streams
|
|
|
|
|
|
|
|
Since 0.2.2 the library has **experimental** remove support.
|
|
|
|
|
|
|
|
The effect of the current implementation of removal is that the order in which the streams are handled
|
|
|
|
can change as the indices are affected.
|
|
|
|
If you want to prevent this effect, you should use **reset()** and repopulate the multiplexer.
|
2021-09-19 07:30:34 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
## Interface
|
|
|
|
|
2021-09-14 11:02:45 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
### Constructor
|
|
|
|
|
2022-10-23 08:12:48 -04:00
|
|
|
- **Multiplex()** constructor,
|
|
|
|
sets the maximum number of streams to MAX_MULTIPLEX == 4 by default.
|
2021-09-14 11:02:45 -04:00
|
|
|
MAX number is 254 as 255 (0xFF) is used as a flag for **NOT FOUND**.
|
|
|
|
- **~Multiplex()** destructor
|
|
|
|
- **void reset()** resets the count in the multiplexer to zero streams.
|
2022-10-23 08:12:48 -04:00
|
|
|
Effectively a remove all.
|
2021-09-14 11:02:45 -04:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
### Core
|
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
- **bool add(Print \* stream)** add another Print stream.
|
|
|
|
Returns true on success.
|
|
|
|
Returns false if no space left, or when stream already in multiplex.
|
|
|
|
Use the **index(Stream)** to get the actual index of the stream.
|
2021-09-19 07:30:34 -04:00
|
|
|
- **size_t write(uint8_t c)** workhorse of the Print interface.
|
2021-09-14 11:02:45 -04:00
|
|
|
Writes a character to all enabled streams.
|
2021-12-22 06:37:31 -05:00
|
|
|
- **size_t write(const uint8_t \* buffer, size_t size)**
|
2021-09-14 11:02:45 -04:00
|
|
|
Writes a buffer of size characters to all enabled streams.
|
2022-10-23 08:12:48 -04:00
|
|
|
- **virtual void flush() override** flushes all enabled streams.
|
2021-09-19 07:30:34 -04:00
|
|
|
|
2022-10-23 08:12:48 -04:00
|
|
|
With respect to the two **write()** functions:
|
2022-06-13 06:51:39 -04:00
|
|
|
|
|
|
|
The performance will be affected by the number of streams and their performance.
|
|
|
|
One slow stream can hold them all.
|
|
|
|
Note: the different streams will get the data in order of addition,
|
2022-10-23 08:12:48 -04:00
|
|
|
and therefore they will get the data at different times.
|
2022-06-13 06:51:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
### Remove - experimental
|
2021-09-19 07:30:34 -04:00
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
(since 0.2.2)
|
2021-09-19 07:30:34 -04:00
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
Removing a stream is still experimental. Feedback is welcome.
|
|
|
|
Removing a stream changes the internal indices used.
|
|
|
|
So if you want to use **remove()** in your sketch,
|
|
|
|
only use the functions with a stream as parameter as these will always work correctly.
|
|
|
|
Alternative is to request the **uint8_t index(Stream)** after you called
|
|
|
|
**remove()** and update your own used indices.
|
2021-09-19 07:30:34 -04:00
|
|
|
|
|
|
|
- **bool remove(Print \* stream)** remove a Print stream.
|
2022-06-13 06:51:39 -04:00
|
|
|
Returns false if the stream is not in the multiplexer.
|
2021-09-19 07:30:34 -04:00
|
|
|
- **bool remove(uint8_t index)** remove a stream by its internal index.
|
|
|
|
Returns false if index is out of range.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
### Control
|
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
#### Info
|
|
|
|
|
2021-09-14 11:02:45 -04:00
|
|
|
- **uint8_t count()** returns number of streams added, MAX 4 in initial release
|
2021-01-29 06:31:58 -05:00
|
|
|
- **uint8_t size()** returns size which is 4 in the current release.
|
2022-06-13 06:51:39 -04:00
|
|
|
- **uint8_t free()** returns number of free slots, 0 .. 4.
|
|
|
|
|
2021-09-19 07:30:34 -04:00
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
#### enable / disable
|
2021-09-19 07:30:34 -04:00
|
|
|
|
|
|
|
Note: the index based functions are (slightly) faster than the stream based functions.
|
|
|
|
How much faster depends on **MAX_MULTIPLEX** as the stream based functions do a lookup in every call.
|
2021-09-14 11:02:45 -04:00
|
|
|
- **bool enable(uint8_t index)** enable the stream at index.
|
|
|
|
Returns true on success, false otherwise.
|
|
|
|
- **bool enableStream(Stream \* stream)** enable the stream.
|
|
|
|
Returns true on success, false otherwise.
|
|
|
|
- **bool disable(uint8_t index)** disable the stream at index.
|
|
|
|
Returns true on success, false otherwise.
|
|
|
|
- **bool disableStream(Stream \* stream)** disable the stream.
|
|
|
|
Returns true on success, false otherwise.
|
|
|
|
- **bool isEnabled(uint8_t index)** returns true if the stream at index is enabled,
|
|
|
|
false otherwise.
|
|
|
|
- **bool isEnabledStream(Stream \* stream)** returns true if the stream is enabled,
|
|
|
|
false otherwise.
|
2022-06-13 06:51:39 -04:00
|
|
|
- **bool isEnabledAny()** returns true if at least one stream is enabled.
|
|
|
|
If false is returned it makes no sense to print anything.
|
|
|
|
|
|
|
|
|
|
|
|
#### Lookup functions
|
|
|
|
|
2021-09-14 11:02:45 -04:00
|
|
|
- **uint8_t index(Print \*stream)** returns the index of the stream if it was added,
|
|
|
|
otherwise it returns 0xFF == 255.
|
2021-09-20 13:23:26 -04:00
|
|
|
Can be used to check if a stream is added to the multiplexer.
|
2021-09-14 11:02:45 -04:00
|
|
|
- **Print \* stream(uint8_t index)** returns the stream at index or NULL otherwise.
|
|
|
|
Convenience function.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
### OutputCount - experimental
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
(since 0.2.4)
|
|
|
|
|
|
|
|
The output count function was added for diagnostic tests.
|
|
|
|
It is kept in the library for now, however it might be removed in the future.
|
|
|
|
Footprint is minimal.
|
|
|
|
|
|
|
|
- **uint32_t getOutputCount()** returns number of bytes multiplexed.
|
2022-10-23 08:12:48 -04:00
|
|
|
So if 6 bytes are sent to 3 streams the output byte count is 3 x 6 = 18.
|
2022-06-13 06:51:39 -04:00
|
|
|
For diagnostic purpose.
|
|
|
|
- **void resetOutputCount()** set internal counter to 0.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
|
|
|
## Operation
|
|
|
|
|
|
|
|
See examples
|
2022-06-13 06:51:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
## Future
|
|
|
|
|
2022-10-23 08:12:48 -04:00
|
|
|
#### 0.3.0
|
|
|
|
|
2022-06-13 06:51:39 -04:00
|
|
|
- set size in constructor
|
|
|
|
- dynamic memory for all internal arrays
|
|
|
|
- breaking change ==> 0.3.0
|
|
|
|
- should **int add()** return the index? Or -1 if fails.
|
|
|
|
- usable idea
|
|
|
|
- breaking change ==> 0.3.0
|
2022-10-23 08:12:48 -04:00
|
|
|
- add **removeAll()** ==> reset()
|
|
|
|
|
|
|
|
#### should
|
|
|
|
|
|
|
|
- error handling
|
|
|
|
- add an error flag if one of the streams does not **write()**
|
2022-06-13 06:51:39 -04:00
|
|
|
correctly and returns 0 or less than it should.
|
|
|
|
- add more examples.
|
|
|
|
- add performance measurement
|
|
|
|
- KB / second
|
2022-10-23 08:12:48 -04:00
|
|
|
- move all code to .cpp
|
2022-06-13 06:51:39 -04:00
|
|
|
|
2022-10-23 08:12:48 -04:00
|
|
|
#### wont
|
2022-06-13 06:51:39 -04:00
|
|
|
|
|
|
|
- pack enabled flag in one or more bytes.
|
|
|
|
(not much faster + need to encode/decode)
|
|
|
|
- add names?
|
|
|
|
(too much memory)
|
|
|
|
- test to prevent circular addition of streams?
|
|
|
|
- no, is risk of the user.
|
|
|
|
|