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

102 lines
2.6 KiB
C
Raw Normal View History

2015-11-22 13:16:39 -05:00
#ifndef BitArray_H
#define BitArray_H
//
// FILE: BitArray.h
// AUTHOR: Rob dot Tillaart at gmail dot com
2017-07-16 07:39:43 -04:00
// VERSION: 0.1.8
2015-11-22 13:16:39 -05:00
// PURPOSE: BitArray library for Arduino
// HISTORY: See BitArray.cpp
//
// Released to the public domain
//
// 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
2017-07-16 07:39:43 -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
//
// Originally created to store lot of numbers between 1..6 dice rolls
// the storage is also usable to store e.g. raw 10 bit analogReads
// see demo sketches.
//
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
2017-07-16 07:39:43 -04:00
#define BITARRAY_LIB_VERSION "0.1.8"
#define BA_SEGMENT_SIZE 200
2015-11-22 13:16:39 -05:00
// max memory is board type dependant
// 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__)
#define BA_MAX_SEGMENTS 8
// 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-11-22 13:16:39 -05:00
class BitArray
{
public:
BitArray() {};
~BitArray();
2015-12-09 15:59:28 -05:00
uint8_t begin(const uint8_t bits, const uint16_t size);
2015-11-22 13:16:39 -05:00
uint16_t capacity() { return _bytes * 8 / _bits; };
uint16_t memory() { return _bytes; };
uint16_t bits() { return _bits; };
uint16_t segments() { return _segments; };
2015-12-09 15:59:28 -05:00
uint8_t getError() { return _error; };
void clear();
2015-11-22 13:16:39 -05:00
uint32_t get(const uint16_t idx);
uint32_t set(const uint16_t idx, uint32_t value);
2017-07-16 07:39:43 -04:00
uint32_t toggle(const uint16_t idx);
2015-11-22 13:16:39 -05:00
private:
2017-07-16 07:39:43 -04: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);
2015-11-22 13:16:39 -05:00
2017-07-16 07:39:43 -04: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
};
#endif