prova
Dependencies: X_NUCLEO_53L0A1 mbed
Fork of 53L0A1_HandGestureRecognition by
Middlewares/ST/Gestures/GesturesDetect/Src/ring_buffer.c@7:d79cbeda2982, 2017-12-15 (annotated)
- Committer:
- mapellil
- Date:
- Fri Dec 15 14:16:16 2017 +0000
- Revision:
- 7:d79cbeda2982
Added hand gesture (L-Rswipe) with L+R sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mapellil | 7:d79cbeda2982 | 1 | /******************************************************************************* |
mapellil | 7:d79cbeda2982 | 2 | Copyright © 2015, STMicroelectronics International N.V. |
mapellil | 7:d79cbeda2982 | 3 | All rights reserved. |
mapellil | 7:d79cbeda2982 | 4 | |
mapellil | 7:d79cbeda2982 | 5 | Use and Redistribution are permitted only in accordance with licensing terms |
mapellil | 7:d79cbeda2982 | 6 | available at www.st.com under software reference X-CUBE-6180XA1, and provided |
mapellil | 7:d79cbeda2982 | 7 | that the following conditions are met: |
mapellil | 7:d79cbeda2982 | 8 | * Redistributions of source code must retain the above copyright |
mapellil | 7:d79cbeda2982 | 9 | notice, this list of conditions and the following disclaimer. |
mapellil | 7:d79cbeda2982 | 10 | * Redistributions in binary form must reproduce the above copyright |
mapellil | 7:d79cbeda2982 | 11 | notice, this list of conditions and the following disclaimer in the |
mapellil | 7:d79cbeda2982 | 12 | documentation and/or other materials provided with the distribution. |
mapellil | 7:d79cbeda2982 | 13 | * Neither the name of STMicroelectronics nor the |
mapellil | 7:d79cbeda2982 | 14 | names of its contributors may be used to endorse or promote products |
mapellil | 7:d79cbeda2982 | 15 | derived from this software without specific prior written permission. |
mapellil | 7:d79cbeda2982 | 16 | |
mapellil | 7:d79cbeda2982 | 17 | THIS SOFTWARE IS PROTECTED BY STMICROELECTRONICS PATENTS AND COPYRIGHTS. |
mapellil | 7:d79cbeda2982 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
mapellil | 7:d79cbeda2982 | 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
mapellil | 7:d79cbeda2982 | 20 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND |
mapellil | 7:d79cbeda2982 | 21 | NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED. |
mapellil | 7:d79cbeda2982 | 22 | IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY |
mapellil | 7:d79cbeda2982 | 23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
mapellil | 7:d79cbeda2982 | 24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
mapellil | 7:d79cbeda2982 | 25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
mapellil | 7:d79cbeda2982 | 26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
mapellil | 7:d79cbeda2982 | 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
mapellil | 7:d79cbeda2982 | 28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mapellil | 7:d79cbeda2982 | 29 | ********************************************************************************/ |
mapellil | 7:d79cbeda2982 | 30 | |
mapellil | 7:d79cbeda2982 | 31 | /* |
mapellil | 7:d79cbeda2982 | 32 | * $Date: 2015-11-10 11:21:53 +0100 (Tue, 10 Nov 2015) $ |
mapellil | 7:d79cbeda2982 | 33 | * $Revision: 2612 $ |
mapellil | 7:d79cbeda2982 | 34 | */ |
mapellil | 7:d79cbeda2982 | 35 | |
mapellil | 7:d79cbeda2982 | 36 | |
mapellil | 7:d79cbeda2982 | 37 | #include "ring_buffer.h" |
mapellil | 7:d79cbeda2982 | 38 | |
mapellil | 7:d79cbeda2982 | 39 | #define ABS(a) ((a>=0) ? (a) : -(a)) |
mapellil | 7:d79cbeda2982 | 40 | |
mapellil | 7:d79cbeda2982 | 41 | int RB_init(ring_buffer* rb, int size) |
mapellil | 7:d79cbeda2982 | 42 | { |
mapellil | 7:d79cbeda2982 | 43 | rb->buffer_end = rb->buffer + ((size<RB_MAX_SIZE) ? size : RB_MAX_SIZE); |
mapellil | 7:d79cbeda2982 | 44 | rb->size = size; |
mapellil | 7:d79cbeda2982 | 45 | rb->data_start = rb->buffer; |
mapellil | 7:d79cbeda2982 | 46 | rb->data_end = rb->buffer; |
mapellil | 7:d79cbeda2982 | 47 | rb->count = 0; |
mapellil | 7:d79cbeda2982 | 48 | return (size<=RB_MAX_SIZE) ? 0 : -1; |
mapellil | 7:d79cbeda2982 | 49 | } |
mapellil | 7:d79cbeda2982 | 50 | |
mapellil | 7:d79cbeda2982 | 51 | int RB_push(ring_buffer* rb, int data) |
mapellil | 7:d79cbeda2982 | 52 | { |
mapellil | 7:d79cbeda2982 | 53 | if (rb == NULL || rb->buffer == NULL) |
mapellil | 7:d79cbeda2982 | 54 | return -1; |
mapellil | 7:d79cbeda2982 | 55 | |
mapellil | 7:d79cbeda2982 | 56 | *rb->data_end = data; |
mapellil | 7:d79cbeda2982 | 57 | rb->data_end++; |
mapellil | 7:d79cbeda2982 | 58 | if (rb->data_end == rb->buffer_end) |
mapellil | 7:d79cbeda2982 | 59 | rb->data_end = rb->buffer; |
mapellil | 7:d79cbeda2982 | 60 | |
mapellil | 7:d79cbeda2982 | 61 | if (RB_full(rb)) { |
mapellil | 7:d79cbeda2982 | 62 | if ((rb->data_start + 1) == rb->buffer_end) |
mapellil | 7:d79cbeda2982 | 63 | rb->data_start = rb->buffer; |
mapellil | 7:d79cbeda2982 | 64 | else |
mapellil | 7:d79cbeda2982 | 65 | rb->data_start++; |
mapellil | 7:d79cbeda2982 | 66 | } else { |
mapellil | 7:d79cbeda2982 | 67 | rb->count++; |
mapellil | 7:d79cbeda2982 | 68 | } |
mapellil | 7:d79cbeda2982 | 69 | |
mapellil | 7:d79cbeda2982 | 70 | return 0; |
mapellil | 7:d79cbeda2982 | 71 | } |
mapellil | 7:d79cbeda2982 | 72 | |
mapellil | 7:d79cbeda2982 | 73 | int RB_pop(ring_buffer* rb) |
mapellil | 7:d79cbeda2982 | 74 | { |
mapellil | 7:d79cbeda2982 | 75 | if (rb == NULL || rb->buffer == NULL) |
mapellil | 7:d79cbeda2982 | 76 | return false; |
mapellil | 7:d79cbeda2982 | 77 | |
mapellil | 7:d79cbeda2982 | 78 | int8_t data = *rb->data_start; |
mapellil | 7:d79cbeda2982 | 79 | rb->data_start++; |
mapellil | 7:d79cbeda2982 | 80 | if (rb->data_start == rb->buffer_end) |
mapellil | 7:d79cbeda2982 | 81 | rb->data_start = rb->buffer; |
mapellil | 7:d79cbeda2982 | 82 | rb->count--; |
mapellil | 7:d79cbeda2982 | 83 | |
mapellil | 7:d79cbeda2982 | 84 | return data; |
mapellil | 7:d79cbeda2982 | 85 | } |
mapellil | 7:d79cbeda2982 | 86 | |
mapellil | 7:d79cbeda2982 | 87 | bool RB_full(ring_buffer* rb) |
mapellil | 7:d79cbeda2982 | 88 | { |
mapellil | 7:d79cbeda2982 | 89 | return rb->count == rb->size; |
mapellil | 7:d79cbeda2982 | 90 | } |
mapellil | 7:d79cbeda2982 | 91 | |
mapellil | 7:d79cbeda2982 | 92 | void RB_trace(ring_buffer*rb) |
mapellil | 7:d79cbeda2982 | 93 | { |
mapellil | 7:d79cbeda2982 | 94 | int i=0; |
mapellil | 7:d79cbeda2982 | 95 | int *ptr; |
mapellil | 7:d79cbeda2982 | 96 | |
mapellil | 7:d79cbeda2982 | 97 | ptr = rb->data_start; |
mapellil | 7:d79cbeda2982 | 98 | //trace_printf("TOF_GESTURES Ring Buffer : "); |
mapellil | 7:d79cbeda2982 | 99 | for(i=0;i<rb->count;i++) |
mapellil | 7:d79cbeda2982 | 100 | { |
mapellil | 7:d79cbeda2982 | 101 | //trace_printf("%d,",*ptr++); |
mapellil | 7:d79cbeda2982 | 102 | if(ptr == rb->buffer_end) |
mapellil | 7:d79cbeda2982 | 103 | ptr = rb->buffer; |
mapellil | 7:d79cbeda2982 | 104 | } |
mapellil | 7:d79cbeda2982 | 105 | //trace_printf("\n"); |
mapellil | 7:d79cbeda2982 | 106 | } |
mapellil | 7:d79cbeda2982 | 107 | |
mapellil | 7:d79cbeda2982 | 108 | int RB_sum(ring_buffer*rb) |
mapellil | 7:d79cbeda2982 | 109 | { |
mapellil | 7:d79cbeda2982 | 110 | int i=0; |
mapellil | 7:d79cbeda2982 | 111 | int sum=0; |
mapellil | 7:d79cbeda2982 | 112 | int *ptr; |
mapellil | 7:d79cbeda2982 | 113 | |
mapellil | 7:d79cbeda2982 | 114 | ptr = rb->data_start; |
mapellil | 7:d79cbeda2982 | 115 | for(i=0;i<rb->count;i++) |
mapellil | 7:d79cbeda2982 | 116 | { |
mapellil | 7:d79cbeda2982 | 117 | sum += *ptr++; |
mapellil | 7:d79cbeda2982 | 118 | if(ptr == rb->buffer_end) |
mapellil | 7:d79cbeda2982 | 119 | ptr = rb->buffer; |
mapellil | 7:d79cbeda2982 | 120 | } |
mapellil | 7:d79cbeda2982 | 121 | return sum; |
mapellil | 7:d79cbeda2982 | 122 | } |
mapellil | 7:d79cbeda2982 | 123 | |
mapellil | 7:d79cbeda2982 | 124 | int RB_mean(ring_buffer*rb) |
mapellil | 7:d79cbeda2982 | 125 | { |
mapellil | 7:d79cbeda2982 | 126 | return RB_sum(rb)/rb->count; |
mapellil | 7:d79cbeda2982 | 127 | } |
mapellil | 7:d79cbeda2982 | 128 | |
mapellil | 7:d79cbeda2982 | 129 | int RB_mad(ring_buffer*rb) |
mapellil | 7:d79cbeda2982 | 130 | { |
mapellil | 7:d79cbeda2982 | 131 | int i; |
mapellil | 7:d79cbeda2982 | 132 | int *ptr; |
mapellil | 7:d79cbeda2982 | 133 | int mad=0; |
mapellil | 7:d79cbeda2982 | 134 | int mean; |
mapellil | 7:d79cbeda2982 | 135 | int data; |
mapellil | 7:d79cbeda2982 | 136 | |
mapellil | 7:d79cbeda2982 | 137 | mean = RB_mean(rb); |
mapellil | 7:d79cbeda2982 | 138 | |
mapellil | 7:d79cbeda2982 | 139 | ptr = rb->data_start; |
mapellil | 7:d79cbeda2982 | 140 | for(i=0;i<rb->count;i++) |
mapellil | 7:d79cbeda2982 | 141 | { |
mapellil | 7:d79cbeda2982 | 142 | data = *ptr++; |
mapellil | 7:d79cbeda2982 | 143 | mad += ABS((data - mean)); |
mapellil | 7:d79cbeda2982 | 144 | if(ptr == rb->buffer_end) |
mapellil | 7:d79cbeda2982 | 145 | ptr = rb->buffer; |
mapellil | 7:d79cbeda2982 | 146 | } |
mapellil | 7:d79cbeda2982 | 147 | return mad/rb->count; |
mapellil | 7:d79cbeda2982 | 148 | } |
mapellil | 7:d79cbeda2982 | 149 | |
mapellil | 7:d79cbeda2982 | 150 | int RB_dir(ring_buffer*rb) |
mapellil | 7:d79cbeda2982 | 151 | { |
mapellil | 7:d79cbeda2982 | 152 | int direction = 0; |
mapellil | 7:d79cbeda2982 | 153 | int i=0; |
mapellil | 7:d79cbeda2982 | 154 | int *ptr; |
mapellil | 7:d79cbeda2982 | 155 | int value1, value2; |
mapellil | 7:d79cbeda2982 | 156 | |
mapellil | 7:d79cbeda2982 | 157 | ptr = rb->data_start; |
mapellil | 7:d79cbeda2982 | 158 | if (rb->count == 0 || rb->count == 1){ |
mapellil | 7:d79cbeda2982 | 159 | return 0; |
mapellil | 7:d79cbeda2982 | 160 | } else { |
mapellil | 7:d79cbeda2982 | 161 | value1 = *ptr++; if(ptr == rb->buffer_end) ptr = rb->buffer; |
mapellil | 7:d79cbeda2982 | 162 | value2 = *ptr++; if(ptr == rb->buffer_end) ptr = rb->buffer; |
mapellil | 7:d79cbeda2982 | 163 | direction = ((value2 - value1) > 0) ? 1 : (((value2 -value1) < 0) ? -1 : 0); |
mapellil | 7:d79cbeda2982 | 164 | value1 = value2; |
mapellil | 7:d79cbeda2982 | 165 | } |
mapellil | 7:d79cbeda2982 | 166 | |
mapellil | 7:d79cbeda2982 | 167 | for(i=2;i<rb->count;i++) |
mapellil | 7:d79cbeda2982 | 168 | { |
mapellil | 7:d79cbeda2982 | 169 | value2 = *ptr++; if(ptr == rb->buffer_end) ptr = rb->buffer; |
mapellil | 7:d79cbeda2982 | 170 | if ((direction==1) && ((value2-value1)<0)) { |
mapellil | 7:d79cbeda2982 | 171 | direction = 0; |
mapellil | 7:d79cbeda2982 | 172 | break; |
mapellil | 7:d79cbeda2982 | 173 | } |
mapellil | 7:d79cbeda2982 | 174 | if ((direction==-1) && ((value2-value1)>0)) { |
mapellil | 7:d79cbeda2982 | 175 | direction = 0; |
mapellil | 7:d79cbeda2982 | 176 | break; |
mapellil | 7:d79cbeda2982 | 177 | } |
mapellil | 7:d79cbeda2982 | 178 | if (direction==0) { |
mapellil | 7:d79cbeda2982 | 179 | direction = ((value2 - value1) > 0) ? 1 : (((value2 -value1) < 0) ? -1 : 0); |
mapellil | 7:d79cbeda2982 | 180 | } |
mapellil | 7:d79cbeda2982 | 181 | value1 = value2; |
mapellil | 7:d79cbeda2982 | 182 | } |
mapellil | 7:d79cbeda2982 | 183 | return direction; |
mapellil | 7:d79cbeda2982 | 184 | } |
mapellil | 7:d79cbeda2982 | 185 |