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

171 lines
2.3 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
2024-04-11 07:29:13 -04:00
// VERSION: 0.4.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
2021-01-29 06:31:58 -05:00
2017-12-09 14:39:12 -05:00
2022-11-22 09:51:21 -05:00
#include "Arduino.h"
2017-12-09 14:39:12 -05:00
#include "Print.h"
2021-01-29 06:31:58 -05:00
2024-04-11 07:29:13 -04:00
#define PRINTCHARARRAY_VERSION (F("0.4.0"))
2021-01-29 06:31:58 -05:00
2021-11-13 10:35:33 -05:00
#ifndef PRINTCHARARRAY_MAX_BUFFER_SIZE
2021-01-29 06:31:58 -05:00
#define PRINTCHARARRAY_MAX_BUFFER_SIZE 250
2021-11-13 10:35:33 -05:00
#endif
2021-01-29 06:31:58 -05:00
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);
};
2021-11-13 10:35:33 -05:00
2021-01-29 06:31:58 -05:00
~PrintCharArray()
{
if (_buffer) free(_buffer);
};
2017-12-09 14:39:12 -05:00
2021-11-13 10:35:33 -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;
}
2021-11-13 10:35:33 -05:00
size_t write(uint8_t * str, uint8_t length)
{
2022-11-22 09:51:21 -05:00
if ( (int(_index) + length) >= _bufSize) return 0; // does not fit.
2021-11-13 10:35:33 -05:00
uint8_t len = length;
uint8_t i = 0;
while (len--)
{
_buffer[_index++] = str[i++];
}
return length;
}
2021-01-29 06:31:58 -05:00
void clear()
{
_index = 0;
}
2021-11-13 10:35:33 -05:00
2021-01-29 06:31:58 -05:00
int available()
2024-04-11 07:29:13 -04:00
{
2021-01-29 06:31:58 -05:00
return (_bufSize - _index);
}
2017-12-09 14:39:12 -05:00
2021-11-13 10:35:33 -05:00
2023-11-16 04:36:39 -05:00
// int length() { return _index; }; // better as size()?
2021-01-29 06:31:58 -05:00
int size()
2024-04-11 07:29:13 -04:00
{
2021-01-29 06:31:58 -05:00
return _index;
}
2017-12-09 14:39:12 -05:00
2021-11-13 10:35:33 -05:00
2021-01-29 06:31:58 -05:00
int bufSize()
{
return _bufSize;
}
2017-12-09 14:39:12 -05:00
2021-11-13 10:35:33 -05:00
2024-04-11 07:29:13 -04:00
char * getBuffer()
2021-01-29 06:31:58 -05:00
{
_buffer[_index] = '\0';
2024-04-11 07:29:13 -04:00
return _buffer;
2021-01-29 06:31:58 -05:00
}
2017-12-09 14:39:12 -05:00
2021-11-13 10:35:33 -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
2021-11-13 10:35:33 -05:00
2024-04-11 07:29:13 -04:00
///////////////////////////////////////////////////////////
template<int BUFSIZE>
class PrintCharArrayT: public Print
{
public:
size_t write(uint8_t c)
{
if (_index < BUFSIZE - 1)
{
_buffer[_index++] = c;
return 1;
}
return 0;
}
size_t write(uint8_t * str, uint8_t length)
{
if ( (int(_index) + length) >= BUFSIZE) return 0; // does not fit.
uint8_t len = length;
uint8_t i = 0;
while (len--)
{
_buffer[_index++] = str[i++];
}
return length;
}
void clear()
{
_index = 0;
}
int available()
{
return (BUFSIZE - _index);
}
int size()
{
return _index;
}
int bufSize()
{
return BUFSIZE;
}
char * getBuffer()
{
_buffer[_index] = '\0';
return _buffer;
}
private:
char _buffer[BUFSIZE];
uint8_t _index = 0;
};
2022-11-22 09:51:21 -05:00
// -- END OF FILE --
2021-11-13 10:35:33 -05:00