2021-01-29 12:31:58 +01:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: Multiplex.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-09-19 13:30:34 +02:00
|
|
|
// VERSION: 0.2.2
|
2021-01-29 12:31:58 +01:00
|
|
|
// PURPOSE: Arduino library to multiplex streams
|
|
|
|
// DATE: 2021-01-09
|
|
|
|
// URL: https://github.com/RobTillaart/Multiplex
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
2021-09-19 13:30:34 +02:00
|
|
|
#define MULTIPLEX_LIB_VERSION (F("0.2.2"))
|
2021-01-29 12:31:58 +01:00
|
|
|
|
|
|
|
|
2021-09-14 17:02:45 +02:00
|
|
|
#ifndef MAX_MULTIPLEX
|
|
|
|
#define MAX_MULTIPLEX 4 // MAX 254 as 0xFF is a special value.
|
|
|
|
#endif
|
2021-01-29 12:31:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Multiplex: public Print
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Multiplex();
|
2021-09-14 17:02:45 +02:00
|
|
|
~Multiplex();
|
|
|
|
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
// CORE
|
2021-09-14 17:02:45 +02:00
|
|
|
virtual size_t write(uint8_t c) override;
|
2021-08-10 22:25:19 +02:00
|
|
|
virtual size_t write(const uint8_t *buffer, size_t size) override;
|
2021-09-19 13:30:34 +02:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
bool add(Print * stream); // returns true on success
|
|
|
|
void reset();
|
|
|
|
|
2021-09-19 13:30:34 +02:00
|
|
|
// remove
|
|
|
|
// use with care as it changes the indices.
|
|
|
|
bool remove(Print * stream);
|
|
|
|
bool remove(uint8_t index);
|
|
|
|
|
2021-09-14 17:02:45 +02:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
// CONTROL
|
2021-09-14 17:02:45 +02:00
|
|
|
uint8_t count() { return _count; };
|
|
|
|
uint8_t size() { return _size; };
|
|
|
|
uint8_t free() { return _size - _count; };
|
|
|
|
|
|
|
|
// returns true on success, false otherwise.
|
|
|
|
bool enable(uint8_t index);
|
|
|
|
bool enableStream(Print * stream);
|
|
|
|
bool disable(uint8_t index);
|
|
|
|
bool disableStream(Print * stream);
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
bool isEnabled(uint8_t index);
|
2021-08-10 22:25:19 +02:00
|
|
|
bool isEnabledStream(Print * stream);
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2021-09-14 17:02:45 +02:00
|
|
|
uint8_t index(Print *stream);
|
|
|
|
Print * stream(uint8_t index);
|
|
|
|
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
private:
|
|
|
|
Print * _stream[MAX_MULTIPLEX];
|
2021-09-14 17:02:45 +02:00
|
|
|
bool _enabled[MAX_MULTIPLEX];
|
2021-09-19 13:30:34 +02:00
|
|
|
// bool _free[MAX_MULTIPLEX];
|
2021-09-14 17:02:45 +02:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
uint8_t _count;
|
|
|
|
uint8_t _size;
|
|
|
|
};
|
|
|
|
|
|
|
|
// -- END OF FILE --
|