0.2.5 Multiplex

This commit is contained in:
rob tillaart 2022-10-23 14:12:48 +02:00
parent 226874f05b
commit dac3300279
8 changed files with 65 additions and 34 deletions

View File

@ -1,11 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- due
- zero
- leonardo
# - due
# - zero
# - leonardo
- m4
- esp32
- esp8266
- mega2560
# - mega2560
- rpipico

View File

@ -1,24 +1,13 @@
//
// FILE: Multiplex.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: Arduino library to multiplex streams
// DATE: 2021-01-09
// URL: https://github.com/RobTillaart/Multiplex
//
// HISTORY:
// 0.1.0 2021-01-09 initial version
// 0.2.0 2021-08-09 See issues #2 and #3
// 0.2.1 2021-09-12 made index(Stream) public; added stream(index)
// enable() / disable() return true on success
// added free() function
// minor refactor.
// 0.2.2 2021-09-12 add remove(Stream) + remove(index);
// 0.2.3 2021-12-22 update library.json, readme, license, minor edits
// 0.2.4 2022-06-12 add isEnabledAny() to see if writes makes sense.
// keep the streams in adding order when removing a stream.
// add getOutputCount() and resetOutputCount() to
// keep track of bytes multiplexed.
// HISTORY: see changelog.md
#include "Multiplex.h"
@ -26,7 +15,7 @@
Multiplex::Multiplex()
{
// malloc ?
// malloc ?
_size = MAX_MULTIPLEX;
reset();
}
@ -34,7 +23,7 @@ Multiplex::Multiplex()
Multiplex::~Multiplex()
{
// free ?
// free ?
}
@ -84,7 +73,7 @@ bool Multiplex::remove(uint8_t idx)
///////////////////////////////////////////
//
// WRITE - the core
// WRITE - the core
//
size_t Multiplex::write(uint8_t c)
{
@ -116,6 +105,18 @@ size_t Multiplex::write(const uint8_t *buffer, size_t size)
}
void Multiplex::flush() // see issue 13
{
for (uint8_t i = 0; i < _count; i++)
{
if (_enabled[i])
{
_stream[i]->flush();
}
}
}
uint8_t Multiplex::index(Print *stream)
{
for (uint8_t i = 0; i < _count; i++)

View File

@ -2,7 +2,7 @@
//
// FILE: Multiplex.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.4
// VERSION: 0.2.5
// PURPOSE: Arduino library to multiplex streams
// DATE: 2021-01-09
// URL: https://github.com/RobTillaart/Multiplex
@ -11,7 +11,7 @@
#include "Arduino.h"
#define MULTIPLEX_LIB_VERSION (F("0.2.4"))
#define MULTIPLEX_LIB_VERSION (F("0.2.5"))
// MAX 254 (in theory) as 0xFF is a special value
@ -31,7 +31,7 @@ public:
virtual size_t write(uint8_t c) override;
virtual size_t write(const uint8_t * buffer, size_t size) override;
bool add(Print * stream); // returns true on success
bool add(Print * stream); // returns true on success
void reset();
// remove
@ -39,6 +39,8 @@ public:
bool remove(Print * stream);
bool remove(uint8_t index);
// see issue 13
virtual void flush() override;
// CONTROL
uint8_t count() { return _count; };

View File

@ -49,11 +49,12 @@ If you want to prevent this effect, you should use **reset()** and repopulate th
### Constructor
- **Multiplex(uint8_t max_multiplex = 4)** constructor,
sets the maximum number of streams to 4 by default.
- **Multiplex()** constructor,
sets the maximum number of streams to MAX_MULTIPLEX == 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.
Effectively a remove all.
### Core
@ -66,13 +67,14 @@ Use the **index(Stream)** to get the actual index of the stream.
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.
- **virtual void flush() override** flushes all enabled streams.
With respect to the latter two functions:
With respect to the two **write()** functions:
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,
and therefore they will get the data on different times.
and therefore they will get the data at different times.
### Remove - experimental
@ -139,7 +141,7 @@ 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.
So if 6 bytes are sent to 3 streams the byte count is 3 x 6 = 18.
So if 6 bytes are sent to 3 streams the output byte count is 3 x 6 = 18.
For diagnostic purpose.
- **void resetOutputCount()** set internal counter to 0.
@ -151,20 +153,27 @@ See examples
## Future
#### 0.3.0
- 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
- add an error flag if one of the streams does not **write()**
- add **removeAll()** ==> reset()
#### should
- error handling
- add an error flag if one of the streams does not **write()**
correctly and returns 0 or less than it should.
- add more examples.
- add performance measurement
- KB / second
- move all code to .cpp
### wont
#### wont
- pack enabled flag in one or more bytes.
(not much faster + need to encode/decode)

View File

@ -89,6 +89,8 @@ void setup()
uint32_t stop = micros();
Serial.print("TIME: ");
Serial.println(stop - start);
mp.flush();
}

View File

@ -7,6 +7,7 @@ Multiplex KEYWORD1
# Methods and Functions (KEYWORD2)
write KEYWORD2
flush KEYWORD2
add KEYWORD2
remove KEYWORD2
reset KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/Multiplex"
},
"version": "0.2.4",
"version": "0.2.5",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=Multiplex
version=0.2.4
version=0.2.5
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino Library implementing a stream multiplexer