0.2.4 BoolArray

This commit is contained in:
rob tillaart 2021-10-19 14:40:20 +02:00
parent 7a0ece22b1
commit ab3f015c71
13 changed files with 156 additions and 78 deletions

View File

@ -2,6 +2,10 @@ compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
- leonardo
- due
- zero
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560

View File

@ -4,10 +4,14 @@ name: Arduino CI
on: [push, pull_request]
jobs:
arduino_ci:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Arduino-CI/action@master
# Arduino-CI/action@v0.1.1
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -1,22 +1,22 @@
//
// FILE: BoolArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.3
// VERSION: 0.2.4
// PURPOSE: BoolArray library for Arduino
// URL: https://github.com/RobTillaart/BoolArray.git
// http://forum.arduino.cc/index.php?topic=361167
// 0.2.3 2021-01-19 update readme
// 0.2.2 2020-12-15 add arduino-CI + unit tests
// 0.2.1 2020-06-05 Fix library.json
// 0.2.0 2020-03-29 #pragma, readme.md,
// 0.1.4 2017-07-16 added masks for performance
// 0.1.3 added toggle
// 0.1.02 added errorhandling
// 0.1.01 fixed constructor - Thanks WPD64 + error handling
//
// HISTORY
// 0.1.00 initial version
// 0.1.01 fixed constructor - Thanks WPD64 + error handling
// 0.1.02 added errorhandling
// 0.1.3 added toggle
// 0.1.4 2017-07-16 added masks for performance
// 0.2.0 2020-03-29 #pragma, readme.md,
// 0.2.1 2020-06-05 Fix library.json
// 0.2.2 2020-12-15 add arduino-CI + unit tests
// 0.2.3 2021-01-19 update readme
// 0.2.4 2021-10-19 update build-CI + badges
//
@ -39,9 +39,10 @@ BoolArray::~BoolArray()
uint8_t BoolArray::begin(const uint16_t size)
{
if (size > BOOLARRAY_MAXSIZE) return BOOLARRAY_SIZE_ERROR;
_size = size;
_size = size;
_bytes = (_size + 7) / 8;
if (_ar) free(_ar);
_ar = (byte*) malloc((_size + 7) / 8);
_ar = (byte*) malloc(_bytes);
return BOOLARRAY_OK;
}
@ -79,19 +80,18 @@ uint8_t BoolArray::toggle(const uint16_t index)
}
// 32 bit is even faster,
uint8_t BoolArray::setAll(const uint8_t value)
{
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
uint8_t *p = _ar;
uint8_t t = (_size + 7) / 8;
uint8_t t = _bytes;
if (value == 0)
{
while(t--) *p++ = 0;
}
else
{
while(t--) *p++ = 0xFF; // set 16 bits at once
while(t--) *p++ = 0xFF;
}
return BOOLARRAY_OK;
}

View File

@ -2,7 +2,7 @@
//
// FILE: BoolArray.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.3
// VERSION: 0.2.4
// PURPOSE: BoolArray library for Arduino
// URL: https://github.com/RobTillaart/BoolArray.git
@ -14,9 +14,9 @@
#include "Arduino.h"
#define BOOLARRAY_LIB_VERSION (F("0.2.3"))
#define BOOLARRAY_LIB_VERSION (F("0.2.4"))
#define BOOLARRAY_MAXSIZE (250 * 8)
#define BOOLARRAY_MAXSIZE (250 * 8) // 2000
#define BOOLARRAY_OK 0x00
#define BOOLARRAY_ERROR 0xFF
@ -33,7 +33,7 @@ public:
uint8_t begin(const uint16_t size);
uint16_t size() { return _size; };
uint16_t memory() { return (_size + 7) / 8; };
uint8_t memory() { return _bytes; };
uint8_t setAll(const uint8_t value);
uint8_t clear() { return setAll(0); };
@ -42,9 +42,10 @@ public:
uint8_t toggle(const uint16_t index);
private:
uint8_t masks[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uint8_t masks[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uint8_t * _ar;
uint16_t _size;
uint16_t _size = 0;
uint8_t _bytes = 0;
};
// -- END OF FILE --

View File

@ -1,5 +1,7 @@
[![Arduino CI](https://github.com/RobTillaart/BoolArray/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/BoolArray/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/BoolArray/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/BoolArray/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/BoolArray/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/BoolArray/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/BoolArray.svg?maxAge=3600)](https://github.com/RobTillaart/BoolArray/releases)
@ -19,9 +21,9 @@ The class is optimized for storage by packing 8 elements of the array in one byt
You need to check if your application needs more performance than this library can deliver.
The BoolArray 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)
BoolArray is faster than BitArray as it only supports single bits and does not need to merge parts
of different bytes to read/write a value. However BoolArray currently only supports 2000 bits while
@ -30,16 +32,16 @@ BitArray can support more.
## Interface
- ** BoolArray()** Constructor
- **BoolArray()** Constructor
- **~BoolArray()** Destructor
- **uint8_t begin(size)** dynamically allocates size elements. Returns **BOOLARRAY_OK** on success.
- **uint8_t begin(uint16_t size)** dynamically allocates size elements (8 bools in one byte). Returns **BOOLARRAY_OK** on success.
- **uint16_t size()** returns number of bool elements.
- **uint16_t memory()** returns # bytes used
- **uint8_t setAll(value)** Sets all elements to false (0) or true (all other values).
- **uint8_t set(index, value)** Set the element to false (0) or true (all other values).
- **uint8_t get(index)** Return 0 or 1 OR an error value which can be interpreted as true.
- **uint16_t memory()** returns number of bytes used.
- **uint8_t setAll(uint8_t value)** Sets all elements to false (0) or true (all other values).
- **uint8_t set(uint16_t index, uint8_t value)** Set the element to false (0) or true (all other values).
- **uint8_t get(uint16_t index)** Return 0 or 1 OR an error value which can be interpreted as true.
So one need to check these carefully.
- **uint8_t toggle(index)** Toggles element at index. Returns **BOOLARRAY_OK** on success.
- **uint8_t toggle(uint16_t index)** Toggles element at index. Returns **BOOLARRAY_OK** on success.
- **uint8_t clear()** Sets all elements to false.
@ -50,8 +52,16 @@ Check out the examples.
## Notes
The BoolArray class dynamicly allocates memory.
The BoolArray class allocates dynamic memory.
The **BOOLARRAY_MAXSIZE** is set to 2000, this was chosen as **malloc()** can only allocate 255 bytes
in one call on an UNO. This is not checked with the recent versions of the IDE anymore.
in one call on an UNO. This is not checked with the recent versions of the IDE any more.
The library is tested on AVR architecture only.
## Future
- performance test on ESP32
- performance for **clear()** dedicated loop vs **setAll(0)** call
- update examples.

View File

@ -1,13 +1,11 @@
//
// FILE: boolArrayDemo2.ino
// FILE: boolArrayDemo0.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.2.0
// VERSION: 0.2.4
// PURPOSE: demo performance reading boolean array
// DATE: 2015-12-06
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
// Released to the public domain
//
#include "BoolArray.h"
@ -57,7 +55,7 @@ void loop()
void test0()
{
Serial.println();
Serial.println("SET TEST0");
Serial.println("TEST SET(1)");
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -77,17 +75,17 @@ void test0()
duration2 = micros() - start;
Serial.print("DURATION:\t");
Serial.println(duration2);
Serial.print("\t\t\t");
Serial.println(duration2 - duration1);
Serial.print(" X:\t");
Serial.println(x);
Serial.print("\t\t");
Serial.print(duration2 - duration1);
Serial.print("\t");
Serial.println((duration2 - duration1) / (1.0 * b.size()));
}
void test1()
{
Serial.println();
Serial.println("SET TEST0");
Serial.println("TEST SET(0)");
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -107,17 +105,17 @@ void test1()
duration2 = micros() - start;
Serial.print("DURATION:\t");
Serial.println(duration2);
Serial.print("\t\t\t");
Serial.println(duration2 - duration1);
Serial.print(" X:\t");
Serial.println(x);
Serial.print("\t\t");
Serial.print(duration2 - duration1);
Serial.print("\t");
Serial.println((duration2 - duration1) / (1.0 * b.size()));
}
void test2()
{
Serial.println();
Serial.println("GET TEST");
Serial.println("TEST GET(i)");
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
@ -137,40 +135,54 @@ void test2()
duration2 = micros() - start;
Serial.print("DURATION:\t");
Serial.println(duration2);
Serial.print("\t\t\t");
Serial.println(duration2 - duration1);
Serial.print(" X:\t");
Serial.println(x);
Serial.print("\t\t");
Serial.print(duration2 - duration1);
Serial.print("\t");
Serial.println((duration2 - duration1) / (1.0 * b.size()));
}
void test3()
{
Serial.println();
Serial.println("SET TEST");
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
{
b.set(i, 0);
}
duration1 = micros();
Serial.print("DURATION:\t");
duration1 = micros() - start;
Serial.print("TEST SET(0):\t");
Serial.println(duration1);
delay(10);
start = micros();
b.setAll(0);
duration2 = micros() - start;
Serial.print("TEST SETALL(0):\t");
Serial.println(duration2);
Serial.print("FACTOR:\t\t");
Serial.println(1.0 * duration1 / duration2);
Serial.println();
delay(10);
start = micros();
for (int i = 0; i < BOOLARRAY_MAXSIZE; i++)
{
b.set(i, 0);
b.set(i, 0);
b.set(i, 1);
}
duration2 = micros();
Serial.print("DURATION:\t");
duration1 = micros() - start;
Serial.print("TEST SET(1):\t");
Serial.println(duration1);
delay(10);
start = micros();
b.setAll(1);
duration2 = micros() - start;
Serial.print("TEST SETALL(1):\t");
Serial.println(duration2);
Serial.print("\t\t\t");
Serial.println(duration2 - duration1);
Serial.print(" X:\t");
Serial.println(x);
Serial.print("FACTOR:\t\t");
Serial.println(1.0 * duration1 / duration2);
}
// -- END OF FILE --

View File

@ -6,8 +6,6 @@
// DATE: 2015-12-12
// URL: https://forum.arduino.cc/index.php?topic=361167.0
//
// Released to the public domain
//
// 0.1.1 - added performance for toggle
//

View File

@ -0,0 +1,24 @@
BOOLARRAY_LIB_VERSION: 0.2.3
Bool array size: 1000
get
DURATION: 5648
DURATION: 9964
X: 0
set
DURATION: 4176
DURATION: 7568
clear
DURATION: 42396
DURATION: 84352
setAll
DURATION: 50332
DURATION: 100100
toggle
DURATION: 3996
DURATION: 7320
Done...

View File

@ -0,0 +1,24 @@
BOOLARRAY_LIB_VERSION: 0.2.4
Bool array size: 1008
get
DURATION: 5652
DURATION: 9964
X: 0
set
DURATION: 4176
DURATION: 7572
clear
DURATION: 41512
DURATION: 82576
setAll
DURATION: 49504
DURATION: 98468
toggle
DURATION: 3996
DURATION: 7320
Done..

View File

@ -1,6 +1,6 @@
# Syntax Coloring Map For BoolArray
# Syntax Colouring Map For BoolArray
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
BoolArray KEYWORD1

View File

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

View File

@ -1,5 +1,5 @@
name=BoolArray
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 booleans of max size 2000 (UNO).

View File

@ -19,6 +19,7 @@
// assertFalse(actual)
// assertNull(actual)
#include <ArduinoUnitTests.h>
#include "Arduino.h"