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

101 lines
2.1 KiB
C++
Raw Normal View History

2015-12-07 13:56:21 -05:00
//
2021-01-29 06:31:58 -05:00
// FILE: BoolArray.cpp
2015-12-07 13:56:21 -05:00
// AUTHOR: Rob Tillaart
2021-10-19 08:40:20 -04:00
// VERSION: 0.2.4
2015-12-07 13:56:21 -05:00
// PURPOSE: BoolArray library for Arduino
2021-01-29 06:31:58 -05:00
// URL: https://github.com/RobTillaart/BoolArray.git
// http://forum.arduino.cc/index.php?topic=361167
2021-10-19 08:40:20 -04:00
//
// HISTORY
2021-01-29 06:31:58 -05:00
// 0.1.00 initial version
2021-10-19 08:40:20 -04:00
// 0.1.01 fixed constructor - Thanks WPD64 + error handling
// 0.1.02 added errorhandling
// 0.1.3 added toggle
// 0.1.4 2017-07-16 added masks for performance
// 0.2.0 2020-03-29 #pragma, readme.md,
// 0.2.1 2020-06-05 Fix library.json
// 0.2.2 2020-12-15 add arduino-CI + unit tests
// 0.2.3 2021-01-19 update readme
// 0.2.4 2021-10-19 update build-CI + badges
2015-12-07 13:56:21 -05:00
//
2021-01-29 06:31:58 -05:00
2015-12-07 13:56:21 -05:00
#include "BoolArray.h"
2021-01-29 06:31:58 -05:00
2015-12-07 13:57:27 -05:00
BoolArray::BoolArray()
{
2021-01-29 06:31:58 -05:00
_ar = NULL;
_size = 0;
2015-12-07 13:57:27 -05:00
}
2021-01-29 06:31:58 -05:00
2015-12-07 13:56:21 -05:00
BoolArray::~BoolArray()
{
2021-01-29 06:31:58 -05:00
if (_ar) free(_ar);
2015-12-07 13:56:21 -05:00
}
2021-01-29 06:31:58 -05:00
2015-12-07 13:56:21 -05:00
uint8_t BoolArray::begin(const uint16_t size)
{
2021-01-29 06:31:58 -05:00
if (size > BOOLARRAY_MAXSIZE) return BOOLARRAY_SIZE_ERROR;
2021-10-19 08:40:20 -04:00
_size = size;
_bytes = (_size + 7) / 8;
2021-01-29 06:31:58 -05:00
if (_ar) free(_ar);
2021-10-19 08:40:20 -04:00
_ar = (byte*) malloc(_bytes);
2021-01-29 06:31:58 -05:00
return BOOLARRAY_OK;
2015-12-07 13:56:21 -05:00
}
2021-01-29 06:31:58 -05:00
uint8_t BoolArray::get(const uint16_t index)
2015-12-07 13:56:21 -05:00
{
2021-01-29 06:31:58 -05:00
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = index / 8;
uint8_t bi = index & 7;
return (_ar[by] & masks[bi]) > 0;
2015-12-07 13:56:21 -05:00
}
2021-01-29 06:31:58 -05:00
uint8_t BoolArray::set(const uint16_t index, const uint8_t value)
2015-12-07 13:56:21 -05:00
{
2021-01-29 06:31:58 -05:00
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = index / 8;
uint8_t bi = index & 7;
if (value == 0) _ar[by] &= ~masks[bi];
else _ar[by] |= masks[bi];
return BOOLARRAY_OK;
2017-04-28 04:34:30 -04:00
}
2021-01-29 06:31:58 -05:00
uint8_t BoolArray::toggle(const uint16_t index)
2017-04-28 04:34:30 -04:00
{
2021-01-29 06:31:58 -05:00
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = index / 8;
uint8_t bi = index & 7;
_ar[by] ^= masks[bi];
return BOOLARRAY_OK;
2015-12-07 13:56:21 -05:00
}
2021-01-29 06:31:58 -05:00
uint8_t BoolArray::setAll(const uint8_t value)
{
2021-01-29 06:31:58 -05:00
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
uint8_t *p = _ar;
2021-10-19 08:40:20 -04:00
uint8_t t = _bytes;
2021-01-29 06:31:58 -05:00
if (value == 0)
{
while(t--) *p++ = 0;
}
else
{
2021-10-19 08:40:20 -04:00
while(t--) *p++ = 0xFF;
}
2021-01-29 06:31:58 -05:00
return BOOLARRAY_OK;
2015-12-07 13:56:21 -05:00
}
2021-01-29 06:31:58 -05:00
2020-11-27 05:10:47 -05:00
// -- END OF FILE --