Skip to content

Commit c6ebbde

Browse files
committed
0.1.02 - added errorhandling + performance demo sketch
1 parent c6af54c commit c6ebbde

File tree

3 files changed

+153
-18
lines changed

3 files changed

+153
-18
lines changed

libraries/BoolArray/BoolArray.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//
22
// FILE: BoolArray.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.01
4+
// VERSION: 0.1.02
55
// PURPOSE: BoolArray library for Arduino
66
// URL: http://forum.arduino.cc/index.php?topic=361167
77

88
//
99
// Released to the public domain
1010
//
11+
// 0.1.02 - added errorhandling
1112
// 0.1.01 - fixed constructor - Thanks WPD64 + error handling
1213
// 0.1.00 - initial version
1314
//
@@ -16,7 +17,7 @@
1617

1718
BoolArray::BoolArray()
1819
{
19-
_ar = null;
20+
_ar = NULL;
2021
_size = 0;
2122
}
2223

@@ -27,37 +28,49 @@ BoolArray::~BoolArray()
2728

2829
uint8_t BoolArray::begin(const uint16_t size)
2930
{
30-
if (size > BOOLARRAY_MAXSIZE) return 255;
31+
if (size > BOOLARRAY_MAXSIZE) return BOOLARRAY_SIZE_ERROR;
3132
_size = size;
3233
if (_ar) free(_ar);
33-
_ar = (byte*) malloc((_size+7)/8);
34-
return 0;
34+
_ar = (byte*) malloc((_size + 7) / 8);
35+
return BOOLARRAY_OK;
3536
}
3637

3738
uint8_t BoolArray::get(const uint16_t idx)
3839
{
39-
if (_ar == null) return 255;
40+
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
41+
if (idx >= _size) return BOOLARRAY_SIZE_ERROR;
4042
uint8_t by = idx / 8;
4143
uint8_t bi = idx & 7;
4244
return (_ar[by] >> bi) & 0x01;
4345
}
4446

45-
void BoolArray::set(const uint16_t idx, uint8_t value)
47+
uint8_t BoolArray::set(const uint16_t idx, const uint8_t value)
4648
{
47-
if (_ar == null) return;
49+
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
50+
if (idx >= _size) return BOOLARRAY_SIZE_ERROR;
4851
uint8_t by = idx / 8;
4952
uint8_t bi = idx & 7;
5053
if (value == 0) _ar[by] &= ~(1 << bi);
5154
else _ar[by] |= (1 << bi);
55+
return BOOLARRAY_OK;
5256
}
5357

54-
void BoolArray::clear()
58+
uint8_t BoolArray::clear()
5559
{
56-
if (_ar == null) return;
57-
for (int i = 0; i < (_size+7)/8; i++)
60+
return setAll(0);
61+
}
62+
63+
uint8_t BoolArray::setAll(const uint8_t value)
64+
{
65+
if (_ar == NULL) return BOOLARRAY_INIT_ERROR;
66+
uint8_t *p = _ar;
67+
uint8_t t = (_size + 7) / 8;
68+
uint8_t v = value?255:0;
69+
while(t--)
5870
{
59-
_ar[i] = 0;
71+
*p++ = v;
6072
}
73+
return BOOLARRAY_OK;
6174
}
6275

6376
// END OF FILE

libraries/BoolArray/BoolArray.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
//
44
// FILE: BoolArray.h
55
// AUTHOR: Rob dot Tillaart at gmail dot com
6-
// VERSION: 0.1.00
6+
// VERSION: 0.1.02
77
// PURPOSE: BoolArray library for Arduino
88
// HISTORY: See BoolArray.cpp
99
//
1010
// Released to the public domain
1111
//
12-
// BoolArray implement a comapct array of booleans of max size 2000
12+
// BoolArray implement a compact array of booleans of max size 2000
1313
//
1414

1515
#if defined(ARDUINO) && ARDUINO >= 100
@@ -18,8 +18,12 @@
1818
#include "WProgram.h"
1919
#endif
2020

21-
#define BOOLARRAY_LIB_VERSION "0.1.01"
21+
#define BOOLARRAY_LIB_VERSION "0.1.02"
2222
#define BOOLARRAY_MAXSIZE (250*8)
23+
#define BOOLARRAY_OK 0x00
24+
#define BOOLARRAY_ERROR 0xFF
25+
#define BOOLARRAY_SIZE_ERROR 0xFE
26+
#define BOOLARRAY_INIT_ERROR 0xFD
2327

2428
class BoolArray
2529
{
@@ -28,12 +32,13 @@ class BoolArray
2832
~BoolArray();
2933

3034
uint8_t begin(const uint16_t size);
31-
void clear();
35+
uint8_t clear();
36+
uint8_t setAll(const uint8_t value);
3237
uint8_t get(const uint16_t idx);
33-
void set(const uint16_t idx, uint8_t value);
38+
uint8_t set(const uint16_t idx, const uint8_t value);
3439

3540
private:
36-
byte * _ar;
41+
uint8_t * _ar;
3742
uint16_t _size;
3843
};
3944

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//
2+
// FILE: boolArrayDemo2.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE: demo performance reading boolean array
6+
// DATE: 2015-12-12
7+
// URL: https://forum.arduino.cc/index.php?topic=361167.0
8+
//
9+
// Released to the public domain
10+
//
11+
12+
#include "BoolArray.h"
13+
14+
BoolArray b;
15+
16+
uint32_t start;
17+
uint32_t stop;
18+
volatile long x = 0;
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.print("Start ");
24+
Serial.println(__FILE__);
25+
Serial.print("LIB VERSION:\t");
26+
Serial.println(BOOLARRAY_LIB_VERSION);
27+
28+
b.begin(1000);
29+
30+
Serial.println("\nget");
31+
start = micros();
32+
for (int i = 0; i < 1000; i++)
33+
{
34+
x += b.get(i);
35+
}
36+
stop = micros();
37+
Serial.print("DURATION:\t");
38+
Serial.println(stop - start);
39+
40+
start = micros();
41+
for (int i = 0; i < 1000; i++)
42+
{
43+
x += b.get(i);
44+
x += b.get(i);
45+
}
46+
stop = micros();
47+
Serial.print("DURATION:\t");
48+
Serial.println(stop - start);
49+
Serial.print(" X:\t");
50+
Serial.println(x);
51+
52+
Serial.println("\nset");
53+
start = micros();
54+
for (int i = 0; i < 1000; i++)
55+
{
56+
b.set(i, 0);
57+
}
58+
stop = micros();
59+
Serial.print("DURATION:\t");
60+
Serial.println(stop - start);
61+
62+
start = micros();
63+
for (int i = 0; i < 1000; i++)
64+
{
65+
b.set(i, 0);
66+
b.set(i, 0);
67+
}
68+
stop = micros();
69+
Serial.print("DURATION:\t");
70+
Serial.println(stop - start);
71+
72+
Serial.println("\nclear");
73+
start = micros();
74+
for (int i = 0; i < 1000; i++)
75+
{
76+
b.clear();
77+
}
78+
stop = micros();
79+
Serial.print("DURATION:\t");
80+
Serial.println(stop - start);
81+
82+
start = micros();
83+
for (int i = 0; i < 1000; i++)
84+
{
85+
b.clear();
86+
b.clear();
87+
}
88+
stop = micros();
89+
Serial.print("DURATION:\t");
90+
Serial.println(stop - start);
91+
92+
Serial.println("\nsetAll");
93+
start = micros();
94+
for (int i = 0; i < 1000; i++)
95+
{
96+
b.setAll(1);
97+
}
98+
stop = micros();
99+
Serial.print("DURATION:\t");
100+
Serial.println(stop - start);
101+
102+
start = micros();
103+
for (int i = 0; i < 1000; i++)
104+
{
105+
b.setAll(1);
106+
b.setAll(1);
107+
}
108+
stop = micros();
109+
Serial.print("DURATION:\t");
110+
Serial.println(stop - start);
111+
112+
Serial.println("Done...");
113+
}
114+
115+
void loop()
116+
{
117+
}

0 commit comments

Comments
 (0)