Skip to content

Commit 7fc99fa

Browse files
committed
Move collections to namespace for better management of names.
1 parent d6035d0 commit 7fc99fa

File tree

8 files changed

+237
-183
lines changed

8 files changed

+237
-183
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Use the platformIO library manager to get the library. It's called 'SimpleCollec
2525

2626
We must understand that this list is associative and sorted by a key, it is based on a binary search algorithm, so it is relatively slow to insert into the underlying array as it will need to be inserted into the array at the right point. However, in return for this, lookup by key is very fast - in big-O notation it is approximately Log(N) or in simple terms to look up in a 256 item list by key would take maximum 8 iterations. However, insertion carries a possible copy penalty if the items need reordering.
2727

28+
All collections in this library are in the namespace tccollection, by default SimpleCollection.h adds a statement to use this namespace automatically.
29+
2830
### Restrictions on what you put in the list
2931

3032
This list works by copying items into the list, so the things you store must follow a couple of simple rules.
@@ -125,9 +127,9 @@ There is an example that shows the usage of the circular buffer, but the API is
125127
We first create an instance and indicate the size needed, the size is fixed and if the writer exceeds the reader, it will wrap and data is lost.
126128

127129
#include <SimpleCollections.h>
128-
#include <CircularBuffer.h>
130+
#include <SCCircularBuffer.h>
129131

130-
CircularBuffer buffer(20);
132+
SCCircularBuffer buffer(20);
131133

132134
### Writing to the buffer
133135

examples/circularBuffer/circularBuffer.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
#include <Arduino.h>
1212
#include <SimpleCollections.h>
13-
#include <CircularBuffer.h>
13+
#include <SCCircularBuffer.h>
1414

1515
#define INTERRUPT_PIN 2
1616

17-
CircularBuffer buffer(32);
17+
SCCircularBuffer buffer(32);
1818

1919
volatile uint8_t counter = 0;
2020

examples/mbedExample/mbedExample.cpp

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <mbed.h>
2222
#include <SimpleCollections.h>
23+
#include <SCCircularBuffer.h>
2324

2425
BufferedSerial console(USBTX, USBRX, 115200);
2526

@@ -46,7 +47,20 @@ class MbedStorage {
4647
};
4748

4849
BtreeList<uint32_t, MbedStorage> myList;
50+
SCCircularBuffer buffer(20);
51+
Thread bufferWriter;
52+
volatile bool running = true;
4953

54+
//
55+
// Yet another thread that constantly raises events to test thread safety of task manager.
56+
//
57+
uint8_t loopCounter = 0;
58+
void bufferWriterThreadProc() {
59+
while(running) {
60+
ThisThread::sleep_for(500ms);
61+
buffer.put(loopCounter++);
62+
}
63+
}
5064

5165
void log(const char* toLog) {
5266
console.write(toLog, strlen(toLog));
@@ -60,26 +74,35 @@ int main() {
6074
myList.add(MbedStorage("Aloha"));
6175
myList.add(MbedStorage(frenchWelcome));
6276

63-
while(1) {
64-
log("Iterate list using C++ range");
65-
for (auto d: myList) {
66-
log(d.getData());
67-
}
77+
log("Iterate list using C++ range");
78+
for (auto d: myList) {
79+
log(d.getData());
80+
}
6881

69-
log("Iterate list using forEach");
70-
myList.forEachItem([](MbedStorage *storage) {
71-
log(storage->getData());
72-
});
82+
log("Iterate list using forEach");
83+
myList.forEachItem([](MbedStorage *storage) {
84+
log(storage->getData());
85+
});
86+
87+
log("Find an item by key");
88+
auto toCheck = myList.getByKey(frenchWelcome.getKey());
89+
if (toCheck && toCheck->getKey() == frenchWelcome.getKey()) {
90+
log("Found item using key");
91+
} else {
92+
log("Find by key failed");
93+
}
94+
95+
bufferWriter.start(bufferWriterThreadProc);
7396

74-
log("Find an item by key");
75-
auto toCheck = myList.getByKey(frenchWelcome.getKey());
76-
if (toCheck && toCheck->getKey() == frenchWelcome.getKey()) {
77-
log("Found item using key");
78-
} else {
79-
log("Find by key failed");
80-
}
8197

82-
ThisThread::sleep_for(2000ms);
98+
while(1) {
99+
if(buffer.available()) {
100+
log("received buffer");
101+
char sz[50];
102+
sprintf(sz, "received buffer %i", buffer.get());
103+
log(sz);
104+
}
105+
ThisThread::sleep_for(100ms);
83106
}
84107
return 0;
85108
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
* This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
44
*/
55

6-
#include "CircularBuffer.h"
6+
#include "SCCircularBuffer.h"
77

8-
CircularBuffer::CircularBuffer(uint16_t size) : readerPosition(0), writerPosition(0), bufferSize(size), buffer(new uint8_t[size]) {}
8+
using namespace tccollection;
99

10-
CircularBuffer::~CircularBuffer() {
10+
SCCircularBuffer::SCCircularBuffer(uint16_t size) : readerPosition(0), writerPosition(0), bufferSize(size), buffer(new uint8_t[size]) {}
11+
12+
SCCircularBuffer::~SCCircularBuffer() {
1113
delete[] buffer;
1214
}
1315

14-
uint16_t CircularBuffer::nextPosition(position_ptr_t positionPtr) {
16+
uint16_t SCCircularBuffer::nextPosition(position_ptr_t positionPtr) {
1517
bool successfullyUpdated = false;
1618
position_t existing;
1719
while(!successfullyUpdated) {
Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
typedef volatile uint32_t* position_ptr_t;
1515
typedef volatile uint32_t position_t;
1616
inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal) {
17-
return core_util_atomic_cas_u32(ptr, &expected, newVal);
17+
uint32_t exp = expected;
18+
return core_util_atomic_cas_u32(ptr, &exp, newVal);
1819
}
1920
inline uint16_t readAtomic(position_ptr_t ptr) { return *(ptr); }
2021
#elif defined(ESP8266)
@@ -56,31 +57,41 @@ inline bool casAtomic(position_ptr_t ptr, position_t expected, position_t newVal
5657
inline uint16_t readAtomic(position_ptr_t ptr) { return *ptr; }
5758
#endif
5859

60+
namespace tccollection {
61+
5962
/**
6063
* Create a circular buffer that has a fixed size and can be filled and read at the same time one character at a time.
61-
* It is very thread safe on ESP32 and mbed based boards, it is moderately thread safe on other boards.
64+
* It is very thread safe on ESP32 and mbed based boards, it is thread safe on other boards, unless you are using that
65+
* board with an unexpected RTOS, in which case you would need to determine suitability yourself.
6266
*
6367
* Imagine the buffer like a circle with a given number of segments, both the reader and writer pointers start at 0.
6468
* As the data structure fills up the writer will move around, and the reader can try and "keep up". Once the reader
6569
* or writer gets to the end it will go back to 0 (start). This is by design and therefore, IT'S POSSIBLE TO LOSE DATA.
6670
*/
67-
class CircularBuffer {
68-
private:
69-
position_t readerPosition;
70-
position_t writerPosition;
71-
const uint16_t bufferSize;
72-
uint8_t *const buffer;
73-
74-
public:
75-
explicit CircularBuffer(uint16_t size);
76-
~CircularBuffer();
77-
78-
bool available() const { return readerPosition != writerPosition; }
79-
void put(uint8_t by) { buffer[nextPosition(&writerPosition)] = by;}
80-
uint8_t get() { return buffer[nextPosition(&readerPosition)]; }
81-
private:
82-
uint16_t nextPosition(position_ptr_t by);
83-
};
71+
class SCCircularBuffer {
72+
private:
73+
position_t readerPosition;
74+
position_t writerPosition;
75+
const uint16_t bufferSize;
76+
uint8_t *const buffer;
77+
78+
public:
79+
explicit SCCircularBuffer(uint16_t size);
80+
81+
~SCCircularBuffer();
82+
83+
bool available() const { return readerPosition != writerPosition; }
84+
85+
void put(uint8_t by) { buffer[nextPosition(&writerPosition)] = by; }
86+
87+
uint8_t get() { return buffer[nextPosition(&readerPosition)]; }
88+
89+
private:
90+
uint16_t nextPosition(position_ptr_t by);
91+
};
92+
93+
}
94+
8495

8596

8697
#endif //SIMPLECOLLECTIONS_CIRCULARBUFFER_H

src/SimpleCollections.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include <malloc.h>
1010
#endif
1111
#include "SimpleCollections.h"
12-
#include <IoLogging.h>
1312

1413
using namespace ioaTreeInternal;
14+
using namespace tccollection;
1515

1616
BtreeStorage::BtreeStorage(bsize_t size, GrowByMode howToGrow, bsize_t itemSize, KeyAccessor keyAccess, CopyOperator copyOperator) {
1717
currentCapacity = size;
@@ -66,8 +66,6 @@ void BtreeStorage::removeIndex(bsize_t index) {
6666
// given the insert position, work out the number of items to move
6767
int amtToMove = (currentSize - index) - 1;
6868

69-
serdebugF4("remove ", amtToMove, index, currentCapacity);
70-
7169
// move the instances in reverse order using their assignment operator.
7270
if(amtToMove > 0) {
7371
for (bsize_t i = index; i < index + amtToMove; ++i) {

0 commit comments

Comments
 (0)