Move master branch from from mios git repo
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
Found the code via http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267762668/9
|
||||
|
||||
Noticed it's not interrupt safe so did a quick-and-dirty sprinkling of volatile keywors and it happens to work
|
||||
|
||||
Also limited the FIFO size the 255 (char vs int, for strategic variables) to conserve memory.
|
||||
|
||||
See also http://code.google.com/p/alexanderbrevig/downloads/list
|
||||
@@ -0,0 +1,89 @@
|
||||
#ifndef SimpleFIFO_h
|
||||
#define SimpleFIFO_h
|
||||
/*
|
||||
||
|
||||
|| @file SimpleFIFO.h
|
||||
|| @version 1.2
|
||||
|| @author Alexander Brevig
|
||||
|| @contact alexanderbrevig@gmail.com
|
||||
||
|
||||
|| @description
|
||||
|| | A simple FIFO class, mostly for primitive types but can be used with classes if assignment to int is allowed
|
||||
|| | This FIFO is not dynamic, so be sure to choose an appropriate size for it
|
||||
|| #
|
||||
||
|
||||
|| @license
|
||||
|| | Copyright (c) 2010 Alexander Brevig
|
||||
|| | This library is free software; you can redistribute it and/or
|
||||
|| | modify it under the terms of the GNU Lesser General Public
|
||||
|| | License as published by the Free Software Foundation; version
|
||||
|| | 2.1 of the License.
|
||||
|| |
|
||||
|| | This library is distributed in the hope that it will be useful,
|
||||
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
|| | Lesser General Public License for more details.
|
||||
|| |
|
||||
|| | You should have received a copy of the GNU Lesser General Public
|
||||
|| | License along with this library; if not, write to the Free Software
|
||||
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|| #
|
||||
||
|
||||
*/
|
||||
template<typename T, int rawSize>
|
||||
class SimpleFIFO {
|
||||
public:
|
||||
const char size; //speculative feature, in case it's needed
|
||||
|
||||
SimpleFIFO();
|
||||
|
||||
T dequeue(); //get next element
|
||||
bool enqueue( T element ); //add an element
|
||||
T peek() const; //get the next element without releasing it from the FIFO
|
||||
void flush(); //[1.1] reset to default state
|
||||
|
||||
//how many elements are currently in the FIFO?
|
||||
char count() { return numberOfElements; }
|
||||
|
||||
private:
|
||||
#ifndef SimpleFIFO_NONVOLATILE
|
||||
volatile char numberOfElements;
|
||||
volatile char nextIn;
|
||||
volatile char nextOut;
|
||||
volatile T raw[rawSize];
|
||||
#else
|
||||
char numberOfElements;
|
||||
char nextIn;
|
||||
char nextOut;
|
||||
T raw[rawSize];
|
||||
#endif
|
||||
};
|
||||
|
||||
template<typename T, int rawSize>
|
||||
SimpleFIFO<T,rawSize>::SimpleFIFO() : size(rawSize) {
|
||||
flush();
|
||||
}
|
||||
template<typename T, int rawSize>
|
||||
bool SimpleFIFO<T,rawSize>::enqueue( T element ) {
|
||||
if ( count() >= rawSize ) { return false; }
|
||||
numberOfElements++;
|
||||
nextIn %= size;
|
||||
raw[nextIn] = element;
|
||||
nextIn++; //advance to next index
|
||||
return true;
|
||||
}
|
||||
template<typename T, int rawSize>
|
||||
T SimpleFIFO<T,rawSize>::dequeue() {
|
||||
numberOfElements--;
|
||||
nextOut %= size;
|
||||
return raw[ nextOut++];
|
||||
}
|
||||
template<typename T, int rawSize>
|
||||
T SimpleFIFO<T,rawSize>::peek() const {
|
||||
return raw[ nextOut % size];
|
||||
}
|
||||
template<typename T, int rawSize>
|
||||
void SimpleFIFO<T,rawSize>::flush() {
|
||||
nextIn = nextOut = numberOfElements = 0;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#include <SimpleFIFO.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
SimpleFIFO<int,10> sFIFO; //store 10 ints
|
||||
|
||||
sFIFO.enqueue(1);
|
||||
sFIFO.enqueue(2);
|
||||
sFIFO.enqueue(3);
|
||||
sFIFO.enqueue(4);
|
||||
sFIFO.enqueue(5);
|
||||
|
||||
Serial.print(F("Peek: "));
|
||||
Serial.println(sFIFO.peek());
|
||||
|
||||
for (int i=0; i<sFIFO.count(); i++) {
|
||||
Serial.print(F("Dequeue "));
|
||||
Serial.println(sFIFO.dequeue());
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
//nothing
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#######################################
|
||||
# Syntax Coloring Map For SimpleFIFO
|
||||
#######################################
|
||||
|
||||
#######################################
|
||||
# Datatypes (KEYWORD1)
|
||||
#######################################
|
||||
|
||||
SimpleFIFO KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Methods and Functions (KEYWORD2)
|
||||
#######################################
|
||||
|
||||
peek KEYWORD2
|
||||
enqueue KEYWORD2
|
||||
dequeue KEYWORD2
|
||||
flush KEYWORD2
|
||||
count KEYWORD2
|
||||
|
||||
#######################################
|
||||
# Constants (LITERAL1)
|
||||
#######################################
|
||||
|
||||
size LITERAL1
|
||||
Reference in New Issue
Block a user