|
| 1 | +/******************************************************************************\ |
| 2 | + * Copyright (c) 2020-2024 |
| 3 | + * Author(s): Volker Fischer |
| 4 | + ****************************************************************************** |
| 5 | + * This program is free software; you can redistribute it and/or modify it under |
| 6 | + * the terms of the GNU General Public License as published by the Free Software |
| 7 | + * Foundation; either version 2 of the License, or (at your option) any later |
| 8 | + * version. |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
| 12 | + * details. |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program; if not, write to the Free Software Foundation, Inc., |
| 15 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 16 | +\******************************************************************************/ |
| 17 | + |
| 18 | +#pragma once |
| 19 | + |
| 20 | +//#define USE_SERIAL_DEBUG_PLOTTING |
| 21 | + |
| 22 | +#define VERSION_MAJOR 0 |
| 23 | +#define VERSION_MINOR 9 |
| 24 | + |
| 25 | +#define MAX_NUM_PADS 12 // a maximum of 12 pads are supported |
| 26 | +#define MAX_NUM_PAD_INPUTS 5 // a maximum of 5 sensors per pad is supported (where one is rim and one is the sum of three) |
| 27 | + |
| 28 | +inline void update_fifo(const float input, |
| 29 | + const int fifo_length, |
| 30 | + float* fifo_memory) |
| 31 | +{ |
| 32 | + // move all values in the history one step back and put new value on the top |
| 33 | + for (int i = 0; i < fifo_length - 1; i++) |
| 34 | + { |
| 35 | + fifo_memory[i] = fifo_memory[i + 1]; |
| 36 | + } |
| 37 | + fifo_memory[fifo_length - 1] = input; |
| 38 | +} |
| 39 | + |
| 40 | +inline void allocate_initialize(float** array_memory, |
| 41 | + const int array_length) |
| 42 | +{ |
| 43 | + // (delete and) allocate memory |
| 44 | + if (*array_memory != nullptr) |
| 45 | + { |
| 46 | + delete[] * array_memory; |
| 47 | + } |
| 48 | + |
| 49 | + *array_memory = new float[array_length]; |
| 50 | + |
| 51 | + // initialization values |
| 52 | + for (int i = 0; i < array_length; i++) |
| 53 | + { |
| 54 | + (*array_memory)[i] = 0.0f; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class FastWriteFIFO |
| 59 | +{ |
| 60 | + public: |
| 61 | + void initialize(const int len) |
| 62 | + { |
| 63 | + pointer = 0; |
| 64 | + fifo_length = len; |
| 65 | + allocate_initialize(&fifo_memory, len); |
| 66 | + } |
| 67 | + |
| 68 | + void add(const float input) |
| 69 | + { |
| 70 | + // write new value and increment data pointer with wrap around |
| 71 | + fifo_memory[pointer] = input; |
| 72 | + pointer = (pointer + 1) % fifo_length; |
| 73 | + } |
| 74 | + |
| 75 | + const float operator[](const int index) |
| 76 | + { |
| 77 | + return fifo_memory[(pointer + index) % fifo_length]; |
| 78 | + } |
| 79 | + |
| 80 | + protected: |
| 81 | + float* fifo_memory = nullptr; |
| 82 | + int pointer; |
| 83 | + int fifo_length; |
| 84 | +}; |
0 commit comments