-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from EinarArnason/dev
- Renamed all instances to ArduinoQueue - Integrated Travis CI - Added tests
- Loading branch information
Showing
12 changed files
with
333 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.pio | ||
.vscode/.browse.c_cpp.db* | ||
.vscode/c_cpp_properties.json | ||
.vscode/launch.json | ||
.vscode/ipch | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
os: linux | ||
language: cpp | ||
compiler: clang | ||
|
||
dist: bionic | ||
addons: | ||
apt: | ||
sources: | ||
- sourceline: "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic main" | ||
key_url: "https://apt.llvm.org/llvm-snapshot.gpg.key" | ||
packages: | ||
- cmake | ||
- ninja-build | ||
- llvm-dev | ||
- libclang-dev | ||
- clang | ||
|
||
before_script: | ||
- mkdir build | ||
- cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -H./ -Bbuild -G Ninja | ||
|
||
script: | ||
- cmake --build build --config Debug --target all | ||
- cd build | ||
- ctest -C Debug -T test --output-on-failure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
// See http://go.microsoft.com/fwlink/?LinkId=827846 | ||
// for the documentation about the extensions.json format | ||
"recommendations": [ | ||
"platformio.platformio-ide" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
Author: Einar Arnason | ||
email: [email protected] | ||
A lightweight linked list type queue implementation, | ||
meant for microcontrollers. | ||
Usage and further info: | ||
https://github.com/EinarArnason/ArduinoQueue | ||
*/ | ||
|
||
#pragma once | ||
|
||
#if defined(ARDUINO) && ARDUINO >= 100 | ||
#include "Arduino.h" | ||
#elif defined(ARDUINO) && ARDUINO < 100 | ||
#include "WProgram.h" | ||
#endif | ||
|
||
template <typename T> class ArduinoQueue { | ||
class Node { | ||
public: | ||
T item; | ||
Node *next; | ||
|
||
Node() { next = nullptr; } | ||
|
||
~Node() { next = nullptr; } | ||
}; | ||
|
||
Node *_head; | ||
Node *_tail; | ||
unsigned int _max_items; | ||
unsigned int _max_memory; | ||
unsigned int _items_cnt; | ||
|
||
public: | ||
ArduinoQueue(unsigned int max_items = 100, unsigned int max_memory = 0) { | ||
_head = nullptr; | ||
_tail = nullptr; | ||
|
||
/* | ||
If the max memory has not | ||
been defined or is 0, then | ||
the queue size is set by | ||
the max number of items: | ||
*/ | ||
if (max_memory == 0) { | ||
_max_items = max_items; | ||
_max_memory = max_items * sizeof(T); | ||
} | ||
/* | ||
If the max memory has been | ||
set then the queue size is | ||
defined by the memory size | ||
when the max items is 0. | ||
If the user gave a max item | ||
size, then the queue size | ||
will be capped by the number | ||
of items. | ||
*/ | ||
else { | ||
_max_items = max_memory / sizeof(T); | ||
_max_memory = _max_items * sizeof(T); | ||
if (max_items != 0) { | ||
if (_max_items > max_items) | ||
_max_items = max_items; | ||
} | ||
} | ||
_items_cnt = 0; | ||
} | ||
|
||
~ArduinoQueue() { | ||
for (Node *node = _head; node != nullptr; node = _head) { | ||
_head = node->next; | ||
delete node; | ||
} | ||
} | ||
|
||
/* | ||
Push an item to the queue. | ||
Returns false if memory is | ||
full, or true if the item | ||
was added to queue. | ||
*/ | ||
bool enqueue(T item) { | ||
if (_items_cnt == _max_items) { | ||
return false; | ||
} | ||
Node *node = new Node; | ||
if (node == nullptr) { | ||
return false; | ||
} | ||
|
||
node->item = item; | ||
|
||
if (_head == nullptr) { | ||
_head = node; | ||
_tail = node; | ||
_items_cnt++; | ||
return true; | ||
} | ||
|
||
_tail->next = node; | ||
_tail = node; | ||
_items_cnt++; | ||
return true; | ||
} | ||
|
||
/* | ||
Pop the front of the queue. | ||
Because exceptions are not | ||
usually implemented for | ||
microcontrollers, if queue | ||
is empty, a dummy item is | ||
returned. | ||
*/ | ||
T dequeue() { | ||
if ((_items_cnt == 0) || (_head == nullptr)) { | ||
return T(); | ||
} | ||
|
||
Node *node = _head; | ||
_head = node->next; | ||
T item = node->item; | ||
delete node; | ||
node = nullptr; | ||
|
||
if (_head == nullptr) { | ||
_tail = nullptr; | ||
} | ||
|
||
_items_cnt--; | ||
return item; | ||
} | ||
|
||
/* | ||
Returns true if the queue | ||
is empty, false otherwise. | ||
*/ | ||
bool isEmpty() { return _head == nullptr; } | ||
|
||
/* | ||
Returns true if the queue | ||
is full, false otherwise. | ||
*/ | ||
bool isFull() { return _items_cnt == _max_items; } | ||
|
||
/* | ||
Returns the number of items | ||
currently in the queue. | ||
*/ | ||
unsigned int item_count() { return _items_cnt; } | ||
|
||
/* | ||
Returns the size of the | ||
queue item in bytes. | ||
*/ | ||
unsigned int item_size() { return sizeof(T); } | ||
|
||
/* | ||
Returns the size of the queue | ||
(maximum number of items) | ||
*/ | ||
unsigned int max_queue_size() { return _max_items; } | ||
|
||
/* | ||
Returns the size of the queue | ||
(maximum size in bytes) | ||
*/ | ||
unsigned int max_memory_size() { return _max_memory; } | ||
|
||
/* | ||
Get the item in the front | ||
of the queue. | ||
Because exceptions are not | ||
usually implemented for | ||
microcontrollers, if queue | ||
is empty, a dummy item is | ||
returned. | ||
*/ | ||
T front() { | ||
if ((_items_cnt == 0) || (_head == nullptr)) { | ||
return T(); | ||
} | ||
T item = _head->item; | ||
return item; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
cmake_minimum_required(VERSION 3.0.0) | ||
project(ArduinoQueue VERSION 1.2.0) | ||
|
||
include(CTest) | ||
enable_testing() | ||
|
||
add_subdirectory(${PROJECT_SOURCE_DIR}/test) | ||
|
||
add_library(ArduinoQueue INTERFACE) | ||
target_include_directories(${PROJECT_NAME} INTERFACE ${PROJECT_SOURCE_DIR}) | ||
|
||
set(CPACK_PROJECT_NAME ${PROJECT_NAME}) | ||
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) | ||
include(CPack) |
Oops, something went wrong.