Giter Site home page Giter Site logo

arduino-queue.h's Introduction

Build Status

Queue.h

Generic C++ circular queue for Arduino embedded projects.

Why?

Because I need one, and I need it to be generic. I dabble with arduino robotics a bit and this is the cornerstone for getting the most out of the microcontroller loop - ie dont delay, just stick stuff in a queue and process it when you can.

Constructor

Queue<T> queue = Queue<T>(int maxlength = 256);

Creates a queue of a generic type T with a maximum queue size. If maxlength is not defined it will default to 256.

NOTE: If the queue grows to maxlength items (and you dont take them out) any additional items added will drop out of the queue. Please bear this in mind when defining maxlength so it is a reasonable balance between RAM usage and functional usefulness.

Examples

Queue<byte> queue = Queue<byte>(1000); // Queue of max 1000 bytes
Queue<int> queue = Queue<int>(); // Queue of max 256 int
Queue<char> queue = Queue<char>(260); // Queue of max 260 chars
Queue<Point> queue = Queue<Point>(10); // Queue of max 10 'Point', where 'Point' is a struct 
Queue<String> queue = Queue<String>(); // Queue of max 256 'String', where 'String' is a class

Methods

queue.push(T item);

Adds a generic item (of T type) at the back of the queue.

Example

Queue<byte> queue = Queue<byte>();
byte a = 255;
byte b = 0;
queue.push(a);
queue.push(b);

T item = queue.pop();

Gets a generic item (of T type) from the front of the queue.

T item = queue.peek();

Same as .pop() but keeps the item in the queue.

Example

Queue<byte> queue = Queue<byte>(); 
byte a = 255;
byte b = 0;
queue.push(a);
queue.push(b);
assert(a == queue.pop()); // true
assert(b == queue.peek()); // true
assert(b == queue.pop()); // true

int front = queue.front();

Gets the current position in the front of the queue. Used for testing queue logic.

Example

Queue<byte> queue = Queue<byte>(); 
byte a = 255;
byte b = 0;
queue.push(a);
assert(1 == queue.front()); // true
queue.push(b);
assert(2 == queue.front()); // true

int back = queue.back();

Gets the current position at the back of the queue. Used for testing queue logic.

Example

Queue<byte> queue = Queue<byte>(); 
byte a = 255;
byte b = 0;
queue.push(a);
assert(0 == queue.back()); // true
assert(1 == queue.front()); // true
queue.pop();
assert(1 == queue.back()); // true
assert(1 == queue.front()); // true

queue.clear();

Removes all items from the queue.

Example

Queue<byte> queue = Queue<byte>(); 
queue.push(1);
queue.push(2);
queue.clear();
queue.push(3);
assert(3 == queue.pop()); // true

All Together now

The following example is testable using 'Serial Monitor' over USB connection from Arduino IDE.

Copy the file Queue.h in the same folder as your *.ino code file.

Make sure you set the baud rate to 115200 and No new line.

#include "Queue.h"

Queue<char> queue = Queue<char>(5); // Max 5 chars!

void setup() {
  Serial.begin(115200);
}

void loop() {
  while(Serial.available()) {
    queue.push(Serial.read());
  }

  Serial.print(millis() / 1000);
  Serial.print(": ");
  int count = queue.count();
  if (count > 0) {
    Serial.print("Found ");
    Serial.print(count);
    Serial.print(" items.. '");
    Serial.print(queue.pop());
    Serial.print("' is the oldest. We are ");
    Serial.print(queue.front());
    Serial.print(" in front and ");
    Serial.print(queue.back());
    Serial.print(" in back. Next is.. '");
    Serial.print(queue.peek());
    Serial.println("'.");
  } else {
    Serial.println("Nothing to process..."); 
  }
  delay(2000);
}

arduino-queue.h's People

Contributors

chaeplin avatar ianfixes avatar orlin369 avatar sdesalas avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.