0.2.4 BitArray

This commit is contained in:
rob tillaart 2021-12-14 11:17:14 +01:00
parent be3624d7c4
commit 9e35d9b6bc
8 changed files with 52 additions and 39 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: BitArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.3
// VERSION: 0.2.4
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
// http://forum.arduino.cc/index.php?topic=361167
@ -21,6 +21,7 @@
// 0.2.1 2020-06-05 fix library.json
// 0.2.2 2020-12-14 add Arduino-CI + unit test
// 0.2.3 2021-10-19 update Arduino-CI + setAll(value)
// 0.2.4 2021-12-14 update library.json, license, minor edits
#include "BitArray.h"
@ -82,12 +83,12 @@ uint8_t BitArray::begin(const uint8_t bits, const uint16_t size)
}
uint32_t BitArray::get(const uint16_t idx)
uint32_t BitArray::get(const uint16_t index)
{
// if (_error != BA_OK) return BA_ERR;
// if (idx >= _size) return BA_IDX_RANGE;
// if (index >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = idx * _bits;
uint16_t pos = index * _bits;
for (uint8_t i = _bits; i-- > 0;)
{
@ -98,11 +99,11 @@ uint32_t BitArray::get(const uint16_t idx)
}
uint32_t BitArray::set(const uint16_t idx, uint32_t value)
uint32_t BitArray::set(const uint16_t index, uint32_t value)
{
// if (_error != BA_OK) return BA_ERR;
// if (idx >= _size) return BA_IDX_RANGE;
uint16_t pos = idx * _bits;
// if (index >= _size) return BA_IDX_RANGE;
uint16_t pos = index * _bits;
uint32_t mask = 1UL;
for (uint8_t i = 0; i < _bits; i++)
{
@ -114,12 +115,12 @@ uint32_t BitArray::set(const uint16_t idx, uint32_t value)
}
uint32_t BitArray::toggle(const uint16_t idx)
uint32_t BitArray::toggle(const uint16_t index)
{
// if (_error != BA_OK) return BA_ERR;
// if (idx >= _size) return BA_IDX_RANGE;
// if (index >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = idx * _bits;
uint16_t pos = index * _bits;
for (uint8_t i = _bits; i-- > 0;)
{
v <<= 1;

View File

@ -2,7 +2,7 @@
//
// FILE: bitArray.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.2.3
// VERSION: 0.2.4
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
@ -21,11 +21,11 @@
#include "Arduino.h"
#define BITARRAY_LIB_VERSION (F("0.2.3"))
#define BITARRAY_LIB_VERSION (F("0.2.4"))
#define BA_SEGMENT_SIZE 200
#define BA_SEGMENT_SIZE 200
// max memory is board type dependent
// note the bitArray does not use all of the RAM
@ -55,12 +55,12 @@
#define BA_MAX_SEGMENTS 5
#endif
#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
#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
class BitArray
@ -78,15 +78,15 @@ public:
uint8_t getError() { return _error; };
void clear();
uint32_t get(const uint16_t idx);
uint32_t set(const uint16_t idx, uint32_t value);
uint32_t get(const uint16_t index);
uint32_t set(const uint16_t index, uint32_t value);
void setAll(uint32_t value);
uint32_t toggle(const uint16_t idx);
uint32_t toggle(const uint16_t index);
private:
uint8_t _bitget(const uint16_t idx);
void _bitset(const uint16_t idx, const uint8_t value);
uint8_t _bittoggle(const uint16_t idx);
uint8_t _bitget(const uint16_t index);
void _bitset(const uint16_t index, const uint8_t value);
uint8_t _bittoggle(const uint16_t index);
uint16_t _bytes = 0;
uint8_t _bits = 0;

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2015-2021 Rob Tillaart
Copyright (c) 2015-2022 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -8,7 +8,7 @@
# BitArray
Arduino library for compact array of objects with a size expressed in bits. typically 1..10
Arduino library for compact array of objects with a size expressed in bits, typically 1..10.
## Description
@ -16,7 +16,7 @@ Arduino library for compact array of objects with a size expressed in bits. typi
The BitArray class allows the user to instantiate an array of elements, each of the same size in bits.
For example one could create an array of 100 throws with a dice. Normally this would take 100 bytes,
but BitArray can store one throw in 3 bits, so 100 throws in approx 40 bytes.
Another example is to store multiple 10 bit analogRead() values efficiently
Another example is to store multiple 10 bit analogRead() values efficiently.
The class is optimized for storage and takes care of efficiently packing the elements
into multiple bytes, and byte borders. Depending where an element is located writing and reading
@ -24,14 +24,15 @@ can take more time. You need to check if your application needs more performance
this library can deliver.
The BitArray library is one from a set of three:
- **BitArray** for elements of user defined size in bits (values 0 .. 2^n-1)
- **BoolArray** for elements of 1 bit (values 0 .. 1)
- **nybbleArray** for elements of 4 bits (values 0 .. 15)
- **BitArray** for elements of user defined size in bits (values 0 .. 2^n-1).
- **BoolArray** for elements of 1 bit (values 0 .. 1).
- **nybbleArray** for elements of 4 bits (values 0 .. 15).
## Operations
In the function **begin(#elementsize, #elements)** the element size and number of elements
In the function **begin(elementSize, elements)** the element size and number of elements
needs to be defined. The maximum number of elements is 65535 if memory allows,
the maximum element size is 32.
@ -58,8 +59,8 @@ The library is tested on AVR architecture only.
## Future
- testing
- functional examples
- documentation
- improve documentation.
- testing.
- functional examples.
- investigate element size of 64 (for doubles) and beyond.

View File

@ -32,6 +32,7 @@ void setup()
test(4, 2500);
}
void test(uint8_t bits, uint16_t cnt)
{
b.begin(bits, cnt);
@ -99,6 +100,11 @@ void test(uint8_t bits, uint16_t cnt)
Serial.println("Done...");
}
void loop()
{
}
}
// -- END OF FILE --

View File

@ -8,18 +8,22 @@ BitArray KEYWORD1
begin KEYWORD2
capacity KEYWORD2
memory KEYWORD2
bits KEYWORD2
segments KEYWORD2
getError KEYWORD2
clear KEYWORD2
get KEYWORD2
set KEYWORD2
toggle KEYWORD2
setAll KEYWORD2
# Constants (LITERAL1)
BITARRAY_LIB_VERSION LITERAL1
BA_ERR LITERAL1
BA_OK LITERAL1
BA_NO_MEMORY_ERR LITERAL1

View File

@ -15,8 +15,9 @@
"type": "git",
"url": "https://github.com/RobTillaart/BitArray.git"
},
"version": "0.2.3",
"version": "0.2.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"
"platforms": "*",
"headers": "BitArray.h"
}

View File

@ -1,5 +1,5 @@
name=BitArray
version=0.2.3
version=0.2.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for compact array of objects with a size expressed in bits.