2020-11-27 05:10:47 -05:00
|
|
|
#pragma once
|
2015-11-22 13:16:39 -05:00
|
|
|
//
|
2020-11-27 05:10:47 -05:00
|
|
|
// FILE: bitArray.h
|
2015-11-22 13:16:39 -05:00
|
|
|
// AUTHOR: Rob dot Tillaart at gmail dot com
|
2021-10-19 10:11:27 -04:00
|
|
|
// VERSION: 0.2.3
|
2015-11-22 13:16:39 -05:00
|
|
|
// PURPOSE: BitArray library for Arduino
|
2020-11-27 05:10:47 -05:00
|
|
|
// URL: https://github.com/RobTillaart/BitArray
|
|
|
|
|
|
|
|
|
2015-11-22 13:16:39 -05:00
|
|
|
// BitArray allows you to make a compact array of objects with a size
|
|
|
|
// expressed in bits. typically 1..10.
|
|
|
|
// The interface uses uint32_t as that will be enough for most purposes.
|
2021-10-19 10:11:27 -04:00
|
|
|
// The main requirement is to optimize storage space.
|
2017-07-16 07:39:43 -04:00
|
|
|
//
|
2021-10-19 10:11:27 -04:00
|
|
|
// The BitArray uses an array of segments and the space per segment
|
2017-07-16 07:39:43 -04:00
|
|
|
// may not exceed 256 bytes as this is a limit on some processors.
|
2015-11-22 13:16:39 -05:00
|
|
|
//
|
|
|
|
// Originally created to store lot of numbers between 1..6 dice rolls
|
2021-10-19 10:11:27 -04:00
|
|
|
// the storage is also usable to store e.g. raw 10 bit analogRead()'s.
|
2015-11-22 13:16:39 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2021-10-19 10:11:27 -04:00
|
|
|
#define BITARRAY_LIB_VERSION (F("0.2.3"))
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
|
2015-12-07 15:05:45 -05:00
|
|
|
#define BA_SEGMENT_SIZE 200
|
2015-11-22 13:16:39 -05:00
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
// max memory is board type dependent
|
2015-12-07 15:05:45 -05:00
|
|
|
// note the bitArray does not use all of the RAM
|
|
|
|
// 1K - max 600
|
|
|
|
#if defined(__AVR_ATmega168__)
|
|
|
|
#define BA_MAX_SEGMENTS 3
|
|
|
|
// 2K - max 1600
|
|
|
|
#elif defined(__AVR_ATmega328P__)
|
2015-12-06 14:50:00 -05:00
|
|
|
#define BA_MAX_SEGMENTS 8
|
2015-12-07 15:05:45 -05:00
|
|
|
// 8K - max 7000
|
|
|
|
#elif defined(__AVR_ATmega1280__)
|
|
|
|
#define BA_MAX_SEGMENTS 35
|
|
|
|
// 8K - max 7000
|
|
|
|
#elif defined(__AVR_ATmega2560__)
|
|
|
|
#define BA_MAX_SEGMENTS 35
|
|
|
|
// 1.25K - max 800
|
|
|
|
#elif defined(__AVR_ATmega16U4__)
|
|
|
|
#define BA_MAX_SEGMENTS 4
|
|
|
|
// 2.5K - max 2000
|
|
|
|
#elif defined(__AVR_ATmega32U4__)
|
|
|
|
#define BA_MAX_SEGMENTS 10
|
|
|
|
// 96K (64 + 32) DUE...
|
|
|
|
#elif defined(__SAM3X8E__)
|
|
|
|
#define BA_MAX_SEGMENTS 100
|
|
|
|
// default max 1000
|
|
|
|
#else
|
|
|
|
#define BA_MAX_SEGMENTS 5
|
|
|
|
#endif
|
2015-11-22 13:16:39 -05:00
|
|
|
|
2015-12-09 15:59:28 -05:00
|
|
|
#define BA_ERR 0xFFFFFFFF
|
|
|
|
#define BA_OK 0x00
|
|
|
|
#define BA_NO_MEMORY_ERR 0x01
|
|
|
|
#define BA_IDX_RANGE_ERR 0x02
|
|
|
|
#define BA_ELEMENT_SIZE_ERR 0x03
|
|
|
|
#define BA_SIZE_ERR 0x04
|
2015-11-22 13:16:39 -05:00
|
|
|
|
2015-12-06 14:50:00 -05:00
|
|
|
|
2015-11-22 13:16:39 -05:00
|
|
|
class BitArray
|
|
|
|
{
|
|
|
|
public:
|
2021-01-29 06:31:58 -05:00
|
|
|
BitArray();
|
|
|
|
~BitArray();
|
2015-11-22 13:16:39 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t begin(const uint8_t bits, const uint16_t size);
|
2015-11-22 13:16:39 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint16_t capacity() { return _bytes * 8 / _bits; };
|
|
|
|
uint16_t memory() { return _bytes; };
|
|
|
|
uint16_t bits() { return _bits; };
|
|
|
|
uint16_t segments() { return _segments; };
|
|
|
|
uint8_t getError() { return _error; };
|
2015-11-22 13:16:39 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
void clear();
|
|
|
|
uint32_t get(const uint16_t idx);
|
|
|
|
uint32_t set(const uint16_t idx, uint32_t value);
|
2021-10-19 10:11:27 -04:00
|
|
|
void setAll(uint32_t value);
|
2021-01-29 06:31:58 -05:00
|
|
|
uint32_t toggle(const uint16_t idx);
|
2015-11-22 13:16:39 -05:00
|
|
|
|
|
|
|
private:
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t _bitget(const uint16_t idx);
|
|
|
|
void _bitset(const uint16_t idx, const uint8_t value);
|
|
|
|
uint8_t _bittoggle(const uint16_t idx);
|
|
|
|
|
|
|
|
uint16_t _bytes = 0;
|
|
|
|
uint8_t _bits = 0;
|
|
|
|
uint8_t _segments = 0;
|
|
|
|
uint8_t * _ar[BA_MAX_SEGMENTS];
|
|
|
|
uint8_t _error = BA_NO_MEMORY_ERR;
|
2015-11-22 13:16:39 -05:00
|
|
|
};
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
// -- END OF FILE --
|