mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
107 lines
4.3 KiB
Markdown
107 lines
4.3 KiB
Markdown
|
|
[![Arduino CI](https://github.com/RobTillaart/Multiplex/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
|
[![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)
|
|
[![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)
|
|
|
|
|
|
# Multiplex
|
|
|
|
Arduino Library implementing a (Print) stream multiplexer.
|
|
|
|
|
|
## Description
|
|
|
|
Multiplex is a library in which one can add multiple Print streams.
|
|
If one prints to the multiplexer the data is sent to all the streams that were added.
|
|
|
|
The maximum number of streams to add is 4.
|
|
This value is defined in the **multiplex.h file** with `#define MAX_MULTIPLEX`.
|
|
This value can be set to 254 MAX as the number 255 / 0xFF is used as a NOT_FOUND flag.
|
|
|
|
Streams can be enabled or disabled by calling `enable()/disable()` passing either an index (based on the order
|
|
in which `add` was called; 0 is first) or a pointer to the `Print`
|
|
object that was passed to `add(Print *)` by calling `enableStream()/disableStream()`
|
|
|
|
It is not possible to remove a stream from the multiplexer (yet), as this would affect the indices used.
|
|
Solution is to reset and repopulate for now.
|
|
|
|
**Note**: 0.2.2 has **experimental** remove support.
|
|
|
|
|
|
## Interface
|
|
|
|
|
|
### Constructor
|
|
|
|
- **Multiplex(uint8_t max_multiplex = 4)** constructor,
|
|
sets the maximum number of streams to 4 by default.
|
|
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.
|
|
|
|
|
|
### Core
|
|
|
|
- **size_t write(uint8_t c)** workhorse of the Print interface.
|
|
Writes a character to all enabled streams.
|
|
- **size_t write(const uint8_t \* buffer, size_t size)**
|
|
Writes a buffer of size characters to all enabled streams.
|
|
- **bool add(Print \* stream)** add another Print stream.
|
|
Returns false if no space left.
|
|
Use the **index(Stream)** to get the actual index of the stream.
|
|
|
|
|
|
#### Experimental 0.2.2
|
|
|
|
Removing a stream is experimental.
|
|
It changes the internal indices used, so if you want to use **remove()** in your sketch, you
|
|
should only use the functions with a stream as parameter as these will always work correctly.
|
|
Alternative is to request the **index(Stream)** after you called **remove()** and update your indices.
|
|
|
|
- **bool remove(Print \* stream)** remove a Print stream.
|
|
Returns false if not in the multiplexer.
|
|
- **bool remove(uint8_t index)** remove a stream by its internal index.
|
|
Returns false if index is out of range.
|
|
|
|
|
|
### Control
|
|
|
|
- **uint8_t count()** returns number of streams added, MAX 4 in initial release
|
|
- **uint8_t size()** returns size which is 4 in the current release.
|
|
- **uint8_t free()** returns number of free slots.
|
|
|
|
|
|
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.
|
|
- **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.
|
|
- **uint8_t index(Print \*stream)** returns the index of the stream if it was added,
|
|
otherwise it returns 0xFF == 255.
|
|
Can be used to check if a stream is added to the multiplexer.
|
|
- **Print \* stream(uint8_t index)** returns the stream at index or NULL otherwise.
|
|
Convenience function.
|
|
|
|
|
|
## Future
|
|
|
|
- set size in constructor - dynamic memory
|
|
- pack enabled flag in one or more bytes
|
|
- add names?
|
|
|
|
|
|
## Operation
|
|
|
|
See examples
|