diff --git a/libraries/BoolArray/BoolArray.cpp b/libraries/BoolArray/BoolArray.cpp new file mode 100644 index 00000000..4492438c --- /dev/null +++ b/libraries/BoolArray/BoolArray.cpp @@ -0,0 +1,54 @@ +// +// FILE: BoolArray.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.1.00 +// PURPOSE: BoolArray library for Arduino +// URL: http://forum.arduino.cc/index.php?topic=361167 + +// +// Released to the public domain +// +// 0.1.00 - initial version +// + +#include "BoolArray.h" + +BoolArray::~BoolArray() +{ + if (_ar) free(_ar); +} + +uint8_t BoolArray::begin(const uint16_t size) +{ + // TODO test MAXSIZE + _size = size; + if (_ar) free(_ar); + _ar = (byte*) malloc((_size+7)/8); + return 0; +} + +uint8_t BoolArray::get(const uint16_t idx) +{ + uint8_t by = idx / 8; + uint8_t bi = idx & 7; + return (_ar[by] >> bi) & 0x01; +} + +void BoolArray::set(const uint16_t idx, uint8_t value) +{ + uint8_t by = idx / 8; + uint8_t bi = idx & 7; + if (value == 0) _ar[by] &= ~(1 << bi); + else _ar[by] |= (1 << bi); +} + +void BoolArray::clear() +{ + if (!_ar) return; + for (int i = 0; i < (_size+7)/8; i++) + { + _ar[i] = 0; + } +} + +// END OF FILE diff --git a/libraries/BoolArray/BoolArray.h b/libraries/BoolArray/BoolArray.h new file mode 100644 index 00000000..c02a989b --- /dev/null +++ b/libraries/BoolArray/BoolArray.h @@ -0,0 +1,39 @@ +#ifndef BoolArray_H +#define BoolArray_H +// +// FILE: BoolArray.h +// AUTHOR: Rob dot Tillaart at gmail dot com +// VERSION: 0.1.00 +// PURPOSE: BoolArray library for Arduino +// HISTORY: See BoolArray.cpp +// +// Released to the public domain +// +// BoolArray implement a comapct array of booleans of max size 2000 +// + +#if defined(ARDUINO) && ARDUINO >= 100 +#include "Arduino.h" +#else +#include "WProgram.h" +#endif + +#define BOOLARRAY_LIB_VERSION "0.1.00" + +class BoolArray +{ +public: + BoolArray() {}; + ~BoolArray(); + + uint8_t begin(const uint16_t size); + void clear(); + uint8_t get(const uint16_t idx); + void set(const uint16_t idx, uint8_t value); + +private: + byte * _ar; + uint16_t _size; +}; + +#endif diff --git a/libraries/BoolArray/examples/boolArrayDemo0/boolArrayDemo0.ino b/libraries/BoolArray/examples/boolArrayDemo0/boolArrayDemo0.ino new file mode 100644 index 00000000..d89f4942 --- /dev/null +++ b/libraries/BoolArray/examples/boolArrayDemo0/boolArrayDemo0.ino @@ -0,0 +1,75 @@ +// +// FILE: boolArrayDemo2.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.00 +// 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" + +BoolArray b; + +uint32_t start; +uint32_t stop; +volatile long x = 0; + +void setup() +{ + Serial.begin(115200); + Serial.print("Start "); + Serial.println(__FILE__); + Serial.print("LIB VERSION:\t"); + Serial.println(BOOLARRAY_LIB_VERSION); + + b.begin(10000); + + start = micros(); + for (int i = 0; i < 10000; i++) + { + x += b.get(i); + } + stop = micros(); + Serial.print("DURATION:\t"); + Serial.println(stop - start); + + start = micros(); + for (int i = 0; i < 10000; i++) + { + x += b.get(i); + x += b.get(i); + } + stop = micros(); + Serial.print("DURATION:\t"); + Serial.println(stop - start); + Serial.print(" X:\t"); + Serial.println(x); + + start = micros(); + for (int i = 0; i < 10000; i++) + { + b.set(i, 0); + } + stop = micros(); + Serial.print("DURATION:\t"); + Serial.println(stop - start); + + start = micros(); + for (int i = 0; i < 10000; i++) + { + b.set(i, 0); + b.set(i, 0); + } + stop = micros(); + Serial.print("DURATION:\t"); + Serial.println(stop - start); + + Serial.println("Done..."); +} + +void loop() +{ +}