GY-63_MS5611/libraries/PrintCharArray/PrintCharArray.h

83 lines
1.4 KiB
C
Raw Normal View History

2020-11-27 05:28:57 -05:00
#pragma once
2017-12-09 14:39:12 -05:00
//
// FILE: PrintCharArray.h
// AUTHOR: Rob Tillaart
2021-01-29 06:31:58 -05:00
// VERSION: 0.3.0
2017-12-09 14:39:12 -05:00
// PURPOSE: Class that captures prints into a char array
// DATE: 2017-12-07
2020-11-27 05:28:57 -05:00
// URL: https://github.com/RobTillaart/PrintCharArray
2017-12-09 14:39:12 -05:00
//
2021-01-29 06:31:58 -05:00
// 0.1.0 2017-12-07 initial version
// 0.1.1 2020-04-28 minor optimization
// 0.2.0 2020-04-30 dynamic memory
// 0.2.1 2020-06-19 fix library.json
// 0.3.0 2021-01-06 Arduino-CI + unit test, free -> available()
2017-12-09 14:39:12 -05:00
#include "Print.h"
2021-01-29 06:31:58 -05:00
#define PRINTCHARARRAY_VERSION (F("0.3.0"))
#define PRINTCHARARRAY_MAX_BUFFER_SIZE 250
2017-12-09 14:39:12 -05:00
class PrintCharArray: public Print
{
2021-01-29 06:31:58 -05:00
public:
PrintCharArray(uint8_t size = 100)
{
_bufSize = constrain(size, 20, PRINTCHARARRAY_MAX_BUFFER_SIZE);
_buffer = (char *) malloc(_bufSize);
};
~PrintCharArray()
{
if (_buffer) free(_buffer);
};
2017-12-09 14:39:12 -05:00
2021-01-29 06:31:58 -05:00
size_t write(uint8_t c)
{
if (_index < _bufSize - 1)
2017-12-09 14:39:12 -05:00
{
2021-01-29 06:31:58 -05:00
_buffer[_index++] = c;
return 1;
2017-12-09 14:39:12 -05:00
}
2021-01-29 06:31:58 -05:00
return 0;
}
void clear()
{
_index = 0;
}
int available()
{
return (_bufSize - _index);
}
2017-12-09 14:39:12 -05:00
2021-01-29 06:31:58 -05:00
// int length() { return _index; }; // better as size()?
int size()
{
return _index;
}
2017-12-09 14:39:12 -05:00
2021-01-29 06:31:58 -05:00
int bufSize()
{
return _bufSize;
}
2017-12-09 14:39:12 -05:00
2021-01-29 06:31:58 -05:00
char * getBuffer()
{
_buffer[_index] = '\0';
return _buffer;
}
2017-12-09 14:39:12 -05:00
2021-01-29 06:31:58 -05:00
private:
char* _buffer;
uint8_t _bufSize = 0;
uint8_t _index = 0;
2017-12-09 14:39:12 -05:00
};
2020-11-27 05:28:57 -05:00
2017-12-09 14:39:12 -05:00
// -- END OF FILE --