GY-63_MS5611/libraries/Multiplex/Multiplex.cpp

190 lines
2.6 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: Multiplex.cpp
// AUTHOR: Rob Tillaart
2023-11-13 11:43:04 -05:00
// VERSION: 0.2.7
2021-01-29 06:31:58 -05:00
// PURPOSE: Arduino library to multiplex streams
// DATE: 2021-01-09
// URL: https://github.com/RobTillaart/Multiplex
#include "Multiplex.h"
Multiplex::Multiplex()
{
2022-10-23 08:12:48 -04:00
// malloc ?
2021-09-14 11:02:45 -04:00
_size = MAX_MULTIPLEX;
2021-01-29 06:31:58 -05:00
reset();
}
2021-09-14 11:02:45 -04:00
Multiplex::~Multiplex()
{
2022-10-23 08:12:48 -04:00
// free ?
2021-09-14 11:02:45 -04:00
}
2021-01-29 06:31:58 -05:00
void Multiplex::reset()
{
2021-09-14 11:02:45 -04:00
for (uint8_t i = 0; i < _size; i++)
{
_enabled[i] = false;
}
2021-01-29 06:31:58 -05:00
_count = 0;
2022-06-13 06:51:39 -04:00
_outputCount = 0;
2021-01-29 06:31:58 -05:00
}
bool Multiplex::add(Print * stream)
{
if (_count >= _size) return false;
2021-09-14 11:02:45 -04:00
if (index(stream) != 0xFF) return false;
2021-01-29 06:31:58 -05:00
_enabled[_count] = true;
_stream[_count++] = stream;
return true;
};
2021-09-19 07:30:34 -04:00
bool Multiplex::remove(Print * stream)
{
uint8_t idx = index(stream);
2022-06-13 06:51:39 -04:00
if (idx >= _count) return false;
2021-09-19 07:30:34 -04:00
return remove(idx);
};
bool Multiplex::remove(uint8_t idx)
{
if (idx >= _count) return false;
_count--;
2022-06-13 06:51:39 -04:00
while (idx < _count)
2021-09-19 07:30:34 -04:00
{
2022-06-13 06:51:39 -04:00
_stream[idx] = _stream[idx + 1];
_enabled[idx] = _enabled[idx + 1];
idx++;
2021-09-19 07:30:34 -04:00
}
return true;
};
2021-01-29 06:31:58 -05:00
///////////////////////////////////////////
//
2022-10-23 08:12:48 -04:00
// WRITE - the core
2021-01-29 06:31:58 -05:00
//
size_t Multiplex::write(uint8_t c)
{
2021-09-14 11:02:45 -04:00
size_t n = 0;
2021-01-29 06:31:58 -05:00
for (uint8_t i = 0; i < _count; i++)
{
if (_enabled[i])
{
n += _stream[i]->write(c);
}
}
2022-06-13 06:51:39 -04:00
_outputCount += n;
2021-01-29 06:31:58 -05:00
return n;
}
2021-09-14 11:02:45 -04:00
size_t Multiplex::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
2021-08-10 16:25:19 -04:00
for (uint8_t i = 0; i < _count; i++)
{
if (_enabled[i])
{
n += _stream[i]->write(buffer, size);
}
}
2022-06-13 06:51:39 -04:00
_outputCount += n;
2021-08-10 16:25:19 -04:00
return n;
}
2021-09-14 11:02:45 -04:00
2023-01-19 13:18:30 -05:00
void Multiplex::flush() // see issue #13
2022-10-23 08:12:48 -04:00
{
for (uint8_t i = 0; i < _count; i++)
{
if (_enabled[i])
{
2023-01-19 13:18:30 -05:00
_stream[i]->flush();
}
2022-10-23 08:12:48 -04:00
}
}
2021-12-22 06:37:31 -05:00
uint8_t Multiplex::index(Print *stream)
2021-08-10 16:25:19 -04:00
{
2021-09-14 11:02:45 -04:00
for (uint8_t i = 0; i < _count; i++)
2021-08-10 16:25:19 -04:00
{
2021-12-22 06:37:31 -05:00
if (stream == _stream[i])
2021-08-10 16:25:19 -04:00
{
return i;
}
}
return 0xFF;
}
2021-01-29 06:31:58 -05:00
2021-09-14 11:02:45 -04:00
2021-12-22 06:37:31 -05:00
Print * Multiplex::stream(uint8_t n)
2021-01-29 06:31:58 -05:00
{
2021-09-14 11:02:45 -04:00
if (n >= _count) return NULL;
return _stream[n];
}
bool Multiplex::enable(uint8_t n)
{
if (n >= _count) return false;
_enabled[n] = true;
return true;
2021-01-29 06:31:58 -05:00
}
2021-09-14 11:02:45 -04:00
bool Multiplex::enableStream(Print *stream)
2021-08-10 16:25:19 -04:00
{
return enable(index(stream));
}
2021-01-29 06:31:58 -05:00
2021-09-14 11:02:45 -04:00
bool Multiplex::disable(uint8_t n)
2021-01-29 06:31:58 -05:00
{
2021-09-14 11:02:45 -04:00
if (n >= _count) return false;
_enabled[n] = false;
return true;
2021-01-29 06:31:58 -05:00
}
2021-09-14 11:02:45 -04:00
bool Multiplex::disableStream(Print *stream)
2021-08-10 16:25:19 -04:00
{
return disable(index(stream));
}
2021-01-29 06:31:58 -05:00
2021-09-14 11:02:45 -04:00
2021-01-29 06:31:58 -05:00
bool Multiplex::isEnabled(uint8_t n)
{
2021-09-14 11:02:45 -04:00
if (n >= _count) return false;
2021-01-29 06:31:58 -05:00
return _enabled[n];
}
2021-09-14 11:02:45 -04:00
2022-06-13 06:51:39 -04:00
bool Multiplex::isEnabledAny()
{
for (uint8_t i = 0; i < _count; i++)
{
if (_enabled[i]) return true;
}
return false;
}
2021-08-10 16:25:19 -04:00
bool Multiplex::isEnabledStream(Print *stream)
{
return isEnabled(index(stream));
}
2021-09-14 11:02:45 -04:00
2023-01-19 13:18:30 -05:00
// -- END OF FILE --
2021-12-22 06:37:31 -05:00