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

101 lines
2.6 KiB
C
Raw Normal View History

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
2022-10-29 09:54:56 -04:00
// AUTHOR: Rob Tillaart
2023-02-08 08:03:23 -05:00
// VERSION: 0.2.6
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
2022-10-29 09:54:56 -04: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.
// The main requirement is to optimize storage space.
2015-11-22 13:16:39 -05:00
//
2022-10-29 09:54:56 -04:00
// The BitArray uses an array of segments and the space per segment
// may not exceed 256 bytes as this is a limit on some processors.
2015-11-22 13:16:39 -05:00
//
2022-10-29 09:54:56 -04:00
// Originally created to store lot of numbers between 1..6 dice rolls
// the storage is also usable to store e.g. raw 10 bit analogRead()'s.
2015-11-22 13:16:39 -05:00
2022-10-29 09:54:56 -04:00
#include "Arduino.h"
2021-01-29 06:31:58 -05:00
2023-02-08 08:03:23 -05:00
#define BITARRAY_LIB_VERSION (F("0.2.6"))
2021-01-29 06:31:58 -05:00
2020-11-27 05:10:47 -05:00
2021-12-14 05:17:14 -05:00
#define BA_SEGMENT_SIZE 200
2015-11-22 13:16:39 -05:00
2023-02-08 08:03:23 -05:00
2022-10-29 09:54:56 -04:00
// max memory is board type dependent
// note the bitArray does not use all of the RAM
// 1K - max 600
#if defined(__AVR_ATmega168__)
#define BA_MAX_SEGMENTS 3
2022-10-29 09:54:56 -04:00
// 2K - max 1600
#elif defined(__AVR_ATmega328P__)
#define BA_MAX_SEGMENTS 8
2022-10-29 09:54:56 -04:00
// 8K - max 7000
#elif defined(__AVR_ATmega1280__)
#define BA_MAX_SEGMENTS 35
2022-10-29 09:54:56 -04:00
// 8K - max 7000
#elif defined(__AVR_ATmega2560__)
#define BA_MAX_SEGMENTS 35
2022-10-29 09:54:56 -04:00
// 1.25K - max 800
#elif defined(__AVR_ATmega16U4__)
#define BA_MAX_SEGMENTS 4
2022-10-29 09:54:56 -04:00
// 2.5K - max 2000
#elif defined(__AVR_ATmega32U4__)
#define BA_MAX_SEGMENTS 10
2022-10-29 09:54:56 -04:00
// 96K (64 + 32) DUE...
#elif defined(__SAM3X8E__)
#define BA_MAX_SEGMENTS 100
2022-10-29 09:54:56 -04:00
// default max 1000
#else
#define BA_MAX_SEGMENTS 5
#endif
2015-11-22 13:16:39 -05:00
2023-02-08 08:03:23 -05:00
2021-12-14 05:17:14 -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-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();
2021-12-14 05:17:14 -05:00
uint32_t get(const uint16_t index);
uint32_t set(const uint16_t index, uint32_t value);
2021-10-19 10:11:27 -04:00
void setAll(uint32_t value);
2021-12-14 05:17:14 -05:00
uint32_t toggle(const uint16_t index);
2015-11-22 13:16:39 -05:00
private:
2021-12-14 05:17:14 -05:00
uint8_t _bitget(const uint16_t index);
void _bitset(const uint16_t index, const uint8_t value);
uint8_t _bittoggle(const uint16_t index);
2021-01-29 06:31:58 -05:00
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
};
2022-10-29 09:54:56 -04:00
2023-02-08 08:03:23 -05:00
// -- END OF FILE --
2022-10-29 09:54:56 -04:00