+ BoolArray 0.1.3 added toggle

This commit is contained in:
RobTillaart 2017-04-28 10:34:30 +02:00
parent e695543ca6
commit 6290c7a348
4 changed files with 54 additions and 17 deletions

View File

@ -1,13 +1,14 @@
//
// FILE: BoolArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.02
// VERSION: 0.1.3
// PURPOSE: BoolArray library for Arduino
// URL: http://forum.arduino.cc/index.php?topic=361167
//
// Released to the public domain
//
// 0.1.3 - added toggle
// 0.1.02 - added errorhandling
// 0.1.01 - fixed constructor - Thanks WPD64 + error handling
// 0.1.00 - initial version
@ -41,7 +42,8 @@ uint8_t BoolArray::get(const uint16_t idx)
if (idx >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = idx / 8;
uint8_t bi = idx & 7;
return (_ar[by] >> bi) & 0x01;
uint8_t mask = 1 << bi;
return (_ar[by] & mask) > 0;
}
uint8_t BoolArray::set(const uint16_t idx, const uint8_t value)
@ -50,8 +52,20 @@ uint8_t BoolArray::set(const uint16_t idx, const uint8_t value)
if (idx >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = idx / 8;
uint8_t bi = idx & 7;
if (value == 0) _ar[by] &= ~(1 << bi);
else _ar[by] |= (1 << bi);
uint8_t mask = 1 << bi;
if (value == 0) _ar[by] &= ~mask;
else _ar[by] |= mask;
return BOOLARRAY_OK;
}
uint8_t BoolArray::toggle(const uint16_t idx)
{
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
if (idx >= _size) return BOOLARRAY_SIZE_ERROR;
uint8_t by = idx / 8;
uint8_t bi = idx & 7;
uint8_t mask = 1 << bi;
_ar[by] ^= mask;
return BOOLARRAY_OK;
}

View File

@ -3,13 +3,14 @@
//
// FILE: BoolArray.h
// AUTHOR: Rob dot Tillaart at gmail dot com
// VERSION: 0.1.02
// VERSION: 0.1.3
// PURPOSE: BoolArray library for Arduino
// HISTORY: See BoolArray.cpp
//
// Released to the public domain
//
// BoolArray implement a compact array of booleans of max size 2000
// BoolArray implement a compact array of booleans of max size 2000.
// For larger arrays one need to modify the code, or use BitArray.
//
#if defined(ARDUINO) && ARDUINO >= 100
@ -18,7 +19,7 @@
#include "WProgram.h"
#endif
#define BOOLARRAY_LIB_VERSION "0.1.02"
#define BOOLARRAY_LIB_VERSION "0.1.3"
#define BOOLARRAY_MAXSIZE (250*8)
#define BOOLARRAY_OK 0x00
#define BOOLARRAY_ERROR 0xFF
@ -36,6 +37,7 @@ public:
uint8_t setAll(const uint8_t value);
uint8_t get(const uint16_t idx);
uint8_t set(const uint16_t idx, const uint8_t value);
uint8_t toggle(const uint16_t idx);
private:
uint8_t * _ar;

View File

@ -1,14 +1,15 @@
//
// FILE: boolArrayDemo2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo performance reading boolean array
// VERSION: 0.1.1
// PURPOSE: demo performance boolean array
// 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
//
#include "BoolArray.h"
BoolArray b;
@ -22,12 +23,12 @@ void setup()
Serial.begin(115200);
Serial.print("Start ");
Serial.println(__FILE__);
Serial.print("LIB VERSION:\t");
Serial.print("BOOLARRAY_LIB_VERSION:\t");
Serial.println(BOOLARRAY_LIB_VERSION);
b.begin(1000);
Serial.println("\nget");
Serial.println("\nget");
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -49,7 +50,7 @@ void setup()
Serial.print(" X:\t");
Serial.println(x);
Serial.println("\nset");
Serial.println("\nset");
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -69,7 +70,7 @@ void setup()
Serial.print("DURATION:\t");
Serial.println(stop - start);
Serial.println("\nclear");
Serial.println("\nclear");
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -89,7 +90,7 @@ void setup()
Serial.print("DURATION:\t");
Serial.println(stop - start);
Serial.println("\nsetAll");
Serial.println("\nsetAll");
start = micros();
for (int i = 0; i < 1000; i++)
{
@ -109,9 +110,29 @@ void setup()
Serial.print("DURATION:\t");
Serial.println(stop - start);
Serial.println("\ntoggle");
start = micros();
for (int i = 0; i < 1000; i++)
{
b.toggle(i);
}
stop = micros();
Serial.print("DURATION:\t");
Serial.println(stop - start);
start = micros();
for (int i = 0; i < 1000; i++)
{
b.toggle(i);
b.toggle(i);
}
stop = micros();
Serial.print("DURATION:\t");
Serial.println(stop - start);
Serial.println("Done...");
}
void loop()
{
}
}

View File

@ -1,5 +1,5 @@
name=BoolArray
version=0.1.02
version=0.1.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library to implement a compact array of booleans of max size 2000.