2015-12-07 19:56:21 +01:00
|
|
|
//
|
2021-01-29 12:31:58 +01:00
|
|
|
// FILE: BoolArray.cpp
|
2015-12-07 19:56:21 +01:00
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-29 17:05:43 +02:00
|
|
|
// VERSION: 0.2.6
|
2015-12-07 19:56:21 +01:00
|
|
|
// PURPOSE: BoolArray library for Arduino
|
2022-10-29 17:05:43 +02:00
|
|
|
// URL: https://github.com/RobTillaart/BoolArray.git
|
|
|
|
// http://forum.arduino.cc/index.php?topic=361167
|
2021-10-19 14:40:20 +02:00
|
|
|
//
|
2022-10-29 17:05:43 +02:00
|
|
|
// HISTORY: see changelog.md
|
2015-12-07 19:56:21 +01:00
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2015-12-07 19:56:21 +01:00
|
|
|
#include "BoolArray.h"
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2015-12-07 19:57:27 +01:00
|
|
|
BoolArray::BoolArray()
|
|
|
|
{
|
2021-12-14 14:48:42 +01:00
|
|
|
_array = NULL;
|
2021-01-29 12:31:58 +01:00
|
|
|
_size = 0;
|
2015-12-07 19:57:27 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2015-12-07 19:56:21 +01:00
|
|
|
BoolArray::~BoolArray()
|
|
|
|
{
|
2021-12-14 14:48:42 +01:00
|
|
|
if (_array) free(_array);
|
2015-12-07 19:56:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2015-12-07 19:56:21 +01:00
|
|
|
uint8_t BoolArray::begin(const uint16_t size)
|
|
|
|
{
|
2021-01-29 12:31:58 +01:00
|
|
|
if (size > BOOLARRAY_MAXSIZE) return BOOLARRAY_SIZE_ERROR;
|
2021-10-19 14:40:20 +02:00
|
|
|
_size = size;
|
|
|
|
_bytes = (_size + 7) / 8;
|
2021-12-14 14:48:42 +01:00
|
|
|
if (_array) free(_array);
|
|
|
|
_array = (byte*) malloc(_bytes);
|
2021-01-29 12:31:58 +01:00
|
|
|
return BOOLARRAY_OK;
|
2015-12-07 19:56:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
|
|
|
uint8_t BoolArray::get(const uint16_t index)
|
2015-12-07 19:56:21 +01:00
|
|
|
{
|
2021-12-14 14:48:42 +01:00
|
|
|
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
|
2021-01-29 12:31:58 +01:00
|
|
|
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
|
|
|
|
uint8_t by = index / 8;
|
|
|
|
uint8_t bi = index & 7;
|
2021-12-14 14:48:42 +01:00
|
|
|
return (_array[by] & _masks[bi]) > 0;
|
2015-12-07 19:56:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
|
|
|
uint8_t BoolArray::set(const uint16_t index, const uint8_t value)
|
2015-12-07 19:56:21 +01:00
|
|
|
{
|
2021-12-14 14:48:42 +01:00
|
|
|
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
|
2021-01-29 12:31:58 +01:00
|
|
|
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
|
|
|
|
uint8_t by = index / 8;
|
|
|
|
uint8_t bi = index & 7;
|
2021-12-14 14:48:42 +01:00
|
|
|
if (value == 0) _array[by] &= ~_masks[bi];
|
|
|
|
else _array[by] |= _masks[bi];
|
2021-01-29 12:31:58 +01:00
|
|
|
return BOOLARRAY_OK;
|
2017-04-28 10:34:30 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
|
|
|
uint8_t BoolArray::toggle(const uint16_t index)
|
2017-04-28 10:34:30 +02:00
|
|
|
{
|
2021-12-14 14:48:42 +01:00
|
|
|
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
|
2021-01-29 12:31:58 +01:00
|
|
|
if (index >= _size) return BOOLARRAY_SIZE_ERROR;
|
|
|
|
uint8_t by = index / 8;
|
|
|
|
uint8_t bi = index & 7;
|
2021-12-14 14:48:42 +01:00
|
|
|
_array[by] ^= _masks[bi];
|
2021-01-29 12:31:58 +01:00
|
|
|
return BOOLARRAY_OK;
|
2015-12-07 19:56:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2015-12-22 10:16:44 +01:00
|
|
|
uint8_t BoolArray::setAll(const uint8_t value)
|
|
|
|
{
|
2021-12-14 14:48:42 +01:00
|
|
|
if (_array == NULL) return BOOLARRAY_INIT_ERROR;
|
|
|
|
uint8_t *p = _array;
|
2021-10-19 14:40:20 +02:00
|
|
|
uint8_t t = _bytes;
|
2021-01-29 12:31:58 +01:00
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
while(t--) *p++ = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-19 14:40:20 +02:00
|
|
|
while(t--) *p++ = 0xFF;
|
|
|
|
}
|
2021-01-29 12:31:58 +01:00
|
|
|
return BOOLARRAY_OK;
|
2015-12-07 19:56:21 +01:00
|
|
|
}
|
|
|
|
|
2021-01-29 12:31:58 +01:00
|
|
|
|
2020-11-27 11:10:47 +01:00
|
|
|
// -- END OF FILE --
|
2022-10-29 17:05:43 +02:00
|
|
|
|