0.2.4 BitArray

This commit is contained in:
rob tillaart 2023-02-08 14:03:23 +01:00
parent 14285e383c
commit 0a53e841da
16 changed files with 131 additions and 76 deletions

View File

@ -6,7 +6,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update

View File

@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

View File

@ -10,7 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:

View File

@ -1,12 +1,10 @@
//
// FILE: BitArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.5
// VERSION: 0.2.6
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
// http://forum.arduino.cc/index.php?topic=361167
//
// HISTORY: see changelog.md
#include "BitArray.h"
@ -32,12 +30,12 @@ BitArray::~BitArray()
uint8_t BitArray::begin(const uint8_t bits, const uint16_t size)
{
if (bits == 0 || bits > 32)
if ((bits == 0) || (bits > 32))
{
_error = BA_ELEMENT_SIZE_ERR;
return _error;
}
if ((1UL * bits * size)/8 > (1UL * BA_MAX_SEGMENTS * BA_SEGMENT_SIZE))
if (((1UL * bits * size)/8) > (1UL * BA_MAX_SEGMENTS * BA_SEGMENT_SIZE))
{
_error = BA_SIZE_ERR;
return _error;
@ -166,6 +164,7 @@ void BitArray::setAll(uint32_t value)
// }
// }
/////////////////////////////////////////////////
//
// PRIVATE
@ -183,7 +182,7 @@ inline uint8_t BitArray::_bitget(uint16_t pos)
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
return (p[by] >> bi) & 0x01; // bitRead(p[by], bi);
return (p[by] >> bi) & 0x01; // bitRead(p[by], bi);
}
@ -191,7 +190,7 @@ inline void BitArray::_bitset(uint16_t pos, uint8_t value)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
@ -200,8 +199,8 @@ inline void BitArray::_bitset(uint16_t pos, uint8_t value)
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
if (value == 0) p[by] &= ~(1 << bi); // bitClear(p[by], bi);
else p[by] |= (1 << bi); // bitSet(p[by], bi);
if (value == 0) p[by] &= ~(1 << bi); // bitClear(p[by], bi);
else p[by] |= (1 << bi); // bitSet(p[by], bi);
}
@ -209,7 +208,7 @@ inline uint8_t BitArray::_bittoggle(const uint16_t pos)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
@ -221,8 +220,9 @@ inline uint8_t BitArray::_bittoggle(const uint16_t pos)
uint8_t mask = 1 << bi;
p[by] ^= mask;
return (mask > 0);
// return ((p[by] & mask) > 0); 0.3.0
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,7 +2,7 @@
//
// FILE: bitArray.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.5
// VERSION: 0.2.6
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
@ -20,11 +20,12 @@
#include "Arduino.h"
#define BITARRAY_LIB_VERSION (F("0.2.5"))
#define BITARRAY_LIB_VERSION (F("0.2.6"))
#define BA_SEGMENT_SIZE 200
// max memory is board type dependent
// note the bitArray does not use all of the RAM
// 1K - max 600
@ -53,6 +54,7 @@
#define BA_MAX_SEGMENTS 5
#endif
#define BA_ERR 0xFFFFFFFF
#define BA_OK 0x00
#define BA_NO_MEMORY_ERR 0x01
@ -94,5 +96,5 @@ private:
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -6,7 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.5] - 2022-10-29
## [0.2.6] - 2022-10-29
- update readme.md
- fix changelog
- minor edit unit test
- update GitHub actions
- update license 2023
- add toggle example
- minor edits
## [0.2.5] - 2022-10-29
- add changelog.md
- add RP2040 in build-CI

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2015-2022 Rob Tillaart
Copyright (c) 2015-2023 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

@ -23,31 +23,16 @@ into multiple bytes, and byte borders. Depending where an element is located wri
can take more time. You need to check if your application needs more performance than
this library can deliver.
#### Related
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).
- https://github.com/RobTillaart/BitArray for elements of user defined size in bits (values 0 .. 2^n-1).
- https://github.com/RobTillaart/BoolArray for elements of 1 bit (values 0 .. 1).
- https://github.com/RobTillaart/nibbleArray for elements of 4 bits or smaller (values 0 .. 15).
## Operations
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.
The basic functions of the class are
- **set(uint16_t index, uint32_t value)**
- **get(uint16_t index)**
- **toggle(uint16_t index)**
- **setAll(uint32_t value)**
- **clear()**
Check out the examples.
## Notes
#### Notes
The BitArray class allocates dynamic memory, so called BA_SEGMENTS,
each of 200 bytes.
@ -57,12 +42,67 @@ depends on architecture.
The library is tested on AVR architecture only.
## Interface
```cpp
#include "BitArray.h"
```
#### Constructor
- **BitArray()** Constructor
- **~BitArray()** Destructor, frees dynamic memory
- **uint8_t begin(const uint8_t bits, const uint16_t size)** Frees memory used and allocates the memory requested.
The maximum number of elements is 65535 if memory allows,
the maximum element size is 32.
Better names could be **bits == elementSize** and **size == elementCount**.
#### Admin
- **uint16_t capacity()** idem.
- **uint16_t memory()** idem.
- **uint16_t bits()** idem.
- **uint16_t segments()** idem.
- **uint8_t getError()** idem.
#### base functions
- **void clear()** sets all elements to 0.
- **uint32_t get(const uint16_t index)** gets the value of the element at index.
- **uint32_t set(const uint16_t index, uint32_t value)** sets index to value.
Overwrites existing value.
Returns value.
- **void setAll(uint32_t value)** sets whole array to value.
- **uint32_t toggle(const uint16_t index)** toggles value at index.
Return value is maxValue (debug info).
## Future
- improve documentation.
#### Must
#### Should
- testing.
- is returning value in **set()** needed? (0.3.0)
- as value is a parameter **void** seems good enough.
- return value **toggle()** could be new value? (0.3.0)
- code prep is working (commented for now)
- naming parameters ** begin()** (0.3.0)
#### Could
- functional examples.
- investigate element size of 64 (for doubles) and beyond.
- move code to .cpp
- move code to .cpp (0.3.0)
#### Wont

View File

@ -1,14 +1,14 @@
//
// FILE: bitArrayDemo0.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// PURPOSE: demo - store 2000 dice rolls
// DATE: 14-11-2015
// DATE: 2015-11-14
// URL: https://github.com/RobTillaart/BitArray
#include "BitArray.h"
#define SAMPLES 2000
BitArray diceRolls;
@ -23,8 +23,10 @@ void setup()
Serial.print("LIB VERSION:\t");
Serial.println(BITARRAY_LIB_VERSION);
int x = diceRolls.begin(3, SAMPLES); // 3 bits can hold any value 1..6
if (x == BA_NO_MEMORY_ERR) Serial.println("no mem");
// 3 bits can hold any value 1..6
int x = diceRolls.begin(3, SAMPLES);
if (x == BA_NO_MEMORY_ERR) Serial.println("no memory");
diceRolls.clear();
Serial.print("CAPACITY:\t");
@ -105,5 +107,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,7 +1,6 @@
//
// FILE: bitArrayDemo1.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo
// DATE: 14-11-2015
// URL: https://github.com/RobTillaart/BitArray
@ -9,6 +8,7 @@
#include "BitArray.h"
BitArray b;
@ -20,10 +20,10 @@ void setup()
Serial.print("LIB VERSION:\t");
Serial.println(BITARRAY_LIB_VERSION);
// shows reclaiming memory
test(10, 1000); // 1024 steps
test(6, 1000); // 64 steps
test(4, 3000); // 16 steps
// shows reclaiming memory
test(10, 1000); // 1024 steps
test(6, 1000); // 64 steps
test(4, 3000); // 16 steps
}
@ -42,7 +42,7 @@ void test(byte bits, uint16_t samples)
for (int i = 0; i < samples; i++)
{
uint16_t x = analogRead(A0) >> (10 - bits); // note resized sample!
uint16_t x = analogRead(A0) >> (10 - bits); // note resized sample!
b.set(i, x);
}
Serial.println("\nSAMPLES:\t");
@ -60,10 +60,11 @@ void test(byte bits, uint16_t samples)
Serial.println("\n\nDone...");
}
void loop()
{
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,16 +1,15 @@
//
// FILE: bitArrayDemo2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// PURPOSE: demo performance reading boolean array
// PURPOSE: demo performance bit array
// DATE: 2015-12-06
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
#include "BitArray.h"
BitArray b;
uint32_t start;
@ -106,5 +105,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,16 +1,15 @@
//
// FILE: bitArrayDemo3.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: demo performance reading boolean array
// PURPOSE: demo performance bit array
// DATE: 2017-07-15
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
#include "BitArray.h"
BitArray b;
uint32_t start;
@ -133,5 +132,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,14 +1,14 @@
// FILE: Fibonacci.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2018-03-05
// PURPOSE: Generate Fibonacci numbers
// PURPOSE: Generate Fibonacci numbers with bit array
// URL: https://github.com/RobTillaart/BitArray
// URL: https://forum.arduino.cc/index.php?topic=532760.0
#include "BitArray.h"
#define NR 70
BitArray a;
@ -19,8 +19,8 @@ void setup()
{
Serial.begin(230400);
// 10 bit can hold 3 digits 000..999 (1000 with overflow)
// 10 bits give effective use of 1000/1024 = 97%
// 10 bit can hold 3 digits 000..999 (1000 with overflow)
// 10 bits give effective use of 1000/1024 = 97%
int x = a.begin(10, NR);
if (x != 0) Serial.println(x);
x = b.begin(10, NR);
@ -56,7 +56,7 @@ void loop()
{}
// add numbers in groups of 3 digits
// add numbers in groups of 3 digits
void add()
{
uint8_t carry = 0;
@ -66,16 +66,16 @@ void add()
uint16_t ta = a.get(i);
uint16_t tb = b.get(i);
// if there is nothing to add, skip column
// if there is nothing to add, skip column
if (ta == 0 && tb == 0 && carry == 0) continue;
// do the add
// do the add
uint16_t tc = ta + tb + carry;
// does column overflow? then correct
// does column overflow? then correct
if (tc > 999)
{
tc -= 1000;
carry = 1; // carry for next column
carry = 1; // carry for next column
}
else carry = 0;
b.set(i, tc);
@ -84,4 +84,5 @@ void add()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/BitArray.git"
},
"version": "0.2.5",
"version": "0.2.6",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=BitArray
version=0.2.5
version=0.2.6
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.

View File

@ -3,7 +3,7 @@
// AUTHOR: Rob Tillaart
// DATE: 2020-12-13
// PURPOSE: unit tests for the BitArray
// https://github.com/RobTillaart/
// https://github.com/RobTillaart/BitArray
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
@ -161,4 +161,5 @@ unittest(test_setAll)
unittest_main()
// --------
// -- END OF FILE --