├── FIFO_Test ├── FIFO.cpp ├── FIFO.h └── FIFO_Test.ino ├── LICENSE └── README.md /FIFO_Test/FIFO.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * FIFO Buffer 3 | * Implementation uses arrays to conserve memory 4 | * 5 | * The MIT License (MIT) 6 | * 7 | * Copyright (c) 2015 Daniel Eisterhold 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | 28 | #include "FIFO.h" 29 | 30 | FIFO::FIFO() { 31 | head = 0; 32 | tail = 0; 33 | numElements = 0; 34 | } 35 | 36 | FIFO::~FIFO() { 37 | } 38 | 39 | void FIFO::push(uint8_t data) { 40 | if(numElements == FIFO_SIZE) { 41 | // Serial.println(F("Buffer full")); 42 | return; 43 | } 44 | else { 45 | //Increment size 46 | numElements++; 47 | 48 | //Only move the tail if there is more than one element 49 | if(numElements > 1) { 50 | //Increment tail location 51 | tail++; 52 | 53 | //Make sure tail is within the bounds of the array 54 | tail %= FIFO_SIZE; 55 | } 56 | 57 | //Store data into array 58 | buffer[tail] = data; 59 | } 60 | } 61 | 62 | uint8_t FIFO::pop() { 63 | if(numElements == 0) { 64 | // Serial.println(F("Buffer empty")); 65 | return NULL; 66 | } 67 | else { 68 | //Decrement size 69 | numElements--; 70 | 71 | uint8_t data = buffer[head]; 72 | 73 | if(numElements >= 1) { 74 | //Move head up one position 75 | head++; 76 | 77 | //Make sure head is within the bounds of the array 78 | head %= FIFO_SIZE; 79 | } 80 | 81 | return data; 82 | } 83 | } 84 | 85 | int FIFO::size() { 86 | return numElements; 87 | } 88 | 89 | -------------------------------------------------------------------------------- /FIFO_Test/FIFO.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FIFO Buffer 3 | * Implementation uses arrays to conserve memory 4 | * 5 | * The MIT License (MIT) 6 | * 7 | * Copyright (c) 2015 Daniel Eisterhold 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | 28 | #ifndef __FIFO__ 29 | #define __FIFO__ 30 | 31 | #include "Arduino.h" 32 | 33 | #define FIFO_SIZE 64 34 | 35 | class FIFO { 36 | private: 37 | int head; 38 | int tail; 39 | int numElements; 40 | uint8_t buffer[FIFO_SIZE]; 41 | public: 42 | FIFO(); 43 | ~FIFO(); 44 | void push(uint8_t data); 45 | uint8_t pop(); 46 | int size(); 47 | }; 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /FIFO_Test/FIFO_Test.ino: -------------------------------------------------------------------------------- 1 | /* 2 | * FIFO Buffer Test Program 3 | * Pushes then pops items onto FIFO buffer. 4 | * 5 | * The MIT License (MIT) 6 | * 7 | * Copyright (c) 2015 Daniel Eisterhold 8 | * 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy 10 | * of this software and associated documentation files (the "Software"), to deal 11 | * in the Software without restriction, including without limitation the rights 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 | * copies of the Software, and to permit persons to whom the Software is 14 | * furnished to do so, subject to the following conditions: 15 | * 16 | * The above copyright notice and this permission notice shall be included in all 17 | * copies or substantial portions of the Software. 18 | * 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 | * SOFTWARE. 26 | */ 27 | 28 | #include "FIFO.h" 29 | 30 | FIFO myBuffer; 31 | 32 | char test[64] = "Hello, World!!!\nHello, World!!!\nHello, World!!!\nHello, World!!!"; 33 | 34 | void setup() { 35 | //Open serial port at 9600 baud 36 | Serial.begin(9600); 37 | 38 | //Wait until the serial port has opened 39 | while (!Serial) delay(1); 40 | 41 | //Wait a little bit to make sure we don't get any garbage on the serial monitor 42 | delay(100); 43 | } 44 | 45 | void loop() { 46 | //Push the test array above onto the FIFO buffer 47 | for (byte i = 0; i < sizeof(test); i++) { 48 | Serial.write(test[i]); 49 | myBuffer.push(test[i]); 50 | delay(100); 51 | } 52 | 53 | Serial.write("\n\n"); 54 | 55 | //Pop items off the FIFO buffer until it's empty 56 | while (myBuffer.size() > 0) { 57 | Serial.write((char)myBuffer.pop()); 58 | delay(100); 59 | } 60 | 61 | while (1); 62 | } 63 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Daniel Eisterhold 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino-FIFO 2 | Simple FIFO buffer for Arduino or other embedded processors 3 | 4 | The default size of the buffer is 64 bytes. 5 | With the default buffer size this library uses only 70 bytes of memory. 6 | (64 byte buffer + 3 * 2 byte integers) 7 | 8 | If you wish to change the buffer size then edit the ```#define FIFO_SIZE 64``` inside of "FIFO.h" 9 | --------------------------------------------------------------------------------