prova

Dependencies:   X_NUCLEO_53L0A1 mbed

Fork of 53L0A1_HandGestureRecognition by ST

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?

UserRevisionLine numberNew 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 #include "tof_gestures.h"
mapellil 7:d79cbeda2982 37 #include "tof_gestures_DIRSWIPE_1.h"
mapellil 7:d79cbeda2982 38
mapellil 7:d79cbeda2982 39 int tof_gestures_initDIRSWIPE_1(int32_t rangeThreshold_mm, int minSwipeDuration, int maxSwipeDuration, bool handMustCoverBoth, Gesture_DIRSWIPE_1_Data_t *data){
mapellil 7:d79cbeda2982 40 int status=0;
mapellil 7:d79cbeda2982 41 // Init left and right motion
mapellil 7:d79cbeda2982 42 status |= tof_initMotion(rangeThreshold_mm, &(data->motionDetectorLeft));
mapellil 7:d79cbeda2982 43 status |= tof_initMotion(rangeThreshold_mm, &(data->motionDetectorRight));
mapellil 7:d79cbeda2982 44 data->minSwipeDuration = minSwipeDuration;
mapellil 7:d79cbeda2982 45 data->maxSwipeDuration = maxSwipeDuration;
mapellil 7:d79cbeda2982 46 data->handMustCoverBoth = handMustCoverBoth;
mapellil 7:d79cbeda2982 47 data->state = GESTURES_DIRSWIPE_1_START;
mapellil 7:d79cbeda2982 48 data->timestamp = 0;
mapellil 7:d79cbeda2982 49 return status;
mapellil 7:d79cbeda2982 50 }
mapellil 7:d79cbeda2982 51
mapellil 7:d79cbeda2982 52 int tof_gestures_detectDIRSWIPE_1(int32_t left_range_mm, int32_t right_range_mm, Gesture_DIRSWIPE_1_Data_t *data){
mapellil 7:d79cbeda2982 53 int return_code = GESTURES_NULL;
mapellil 7:d79cbeda2982 54 int r_motion_code, l_motion_code;
mapellil 7:d79cbeda2982 55 int timestamp = GET_TIME_STAMP();
mapellil 7:d79cbeda2982 56 int duration=0;
mapellil 7:d79cbeda2982 57
mapellil 7:d79cbeda2982 58 // Get left and right motion
mapellil 7:d79cbeda2982 59 l_motion_code = tof_getMotion(left_range_mm, &(data->motionDetectorLeft));
mapellil 7:d79cbeda2982 60 r_motion_code = tof_getMotion(right_range_mm, &(data->motionDetectorRight));
mapellil 7:d79cbeda2982 61
mapellil 7:d79cbeda2982 62 TOF_GESTURES_DEBUG(DIRSWIPE_1,"lmotion=%d, rmotion=%d", l_motion_code, r_motion_code);
mapellil 7:d79cbeda2982 63
mapellil 7:d79cbeda2982 64 switch(data->state){
mapellil 7:d79cbeda2982 65 case GESTURES_DIRSWIPE_1_START:
mapellil 7:d79cbeda2982 66 if((!data->handMustCoverBoth && r_motion_code == GESTURES_MOTION_RAISE_UP) || (data->handMustCoverBoth && r_motion_code == GESTURES_MOTION_RAISE_UP && l_motion_code == GESTURES_MOTION_DOWN_STATE)){
mapellil 7:d79cbeda2982 67 if(data->motionDetectorRight.duration > data->minSwipeDuration){
mapellil 7:d79cbeda2982 68 data->gesture_start_from_right = true;
mapellil 7:d79cbeda2982 69 data->state = GESTURES_DIRSWIPE_1_END;
mapellil 7:d79cbeda2982 70 data->timestamp = timestamp;
mapellil 7:d79cbeda2982 71 TOF_GESTURES_DEBUG(DIRSWIPE_1,"Start right to left (%d ms)",data->motionDetectorRight.duration);
mapellil 7:d79cbeda2982 72 }else{
mapellil 7:d79cbeda2982 73 return_code = GESTURES_DISCARDED_TOO_FAST;
mapellil 7:d79cbeda2982 74 }
mapellil 7:d79cbeda2982 75 }else if((!data->handMustCoverBoth && l_motion_code == GESTURES_MOTION_RAISE_UP) || (data->handMustCoverBoth && r_motion_code == GESTURES_MOTION_DOWN_STATE && l_motion_code == GESTURES_MOTION_RAISE_UP)){
mapellil 7:d79cbeda2982 76 if(data->motionDetectorRight.duration > data->minSwipeDuration){
mapellil 7:d79cbeda2982 77 data->gesture_start_from_right = false;
mapellil 7:d79cbeda2982 78 data->state = GESTURES_DIRSWIPE_1_END;
mapellil 7:d79cbeda2982 79 data->timestamp = timestamp;
mapellil 7:d79cbeda2982 80 TOF_GESTURES_DEBUG(DIRSWIPE_1,"Start left to right(%d ms)",data->motionDetectorRight.duration);
mapellil 7:d79cbeda2982 81 }else{
mapellil 7:d79cbeda2982 82 return_code = GESTURES_DISCARDED_TOO_FAST;
mapellil 7:d79cbeda2982 83 }
mapellil 7:d79cbeda2982 84 }
mapellil 7:d79cbeda2982 85 break;
mapellil 7:d79cbeda2982 86 case GESTURES_DIRSWIPE_1_END:
mapellil 7:d79cbeda2982 87 duration = timestamp - data->timestamp;
mapellil 7:d79cbeda2982 88 if(duration > data->maxSwipeDuration){
mapellil 7:d79cbeda2982 89 // Gesture is too long : discard it
mapellil 7:d79cbeda2982 90 data->state = GESTURES_DIRSWIPE_1_START;
mapellil 7:d79cbeda2982 91 TOF_GESTURES_DEBUG(DIRSWIPE_1,"Too slow");
mapellil 7:d79cbeda2982 92 return_code = GESTURES_DISCARDED_TOO_SLOW;
mapellil 7:d79cbeda2982 93 }else if ((data->gesture_start_from_right && l_motion_code == GESTURES_MOTION_RAISE_UP) || (!data->gesture_start_from_right && r_motion_code == GESTURES_MOTION_RAISE_UP)){
mapellil 7:d79cbeda2982 94 // Gesture detected
mapellil 7:d79cbeda2982 95 data->state = GESTURES_DIRSWIPE_1_START;
mapellil 7:d79cbeda2982 96 return_code = (data->gesture_start_from_right) ? GESTURES_SWIPE_RIGHT_LEFT : GESTURES_SWIPE_LEFT_RIGHT;
mapellil 7:d79cbeda2982 97 TOF_GESTURES_DEBUG(DIRSWIPE_1,"DETECTED");
mapellil 7:d79cbeda2982 98 }else{
mapellil 7:d79cbeda2982 99 // Waiting...
mapellil 7:d79cbeda2982 100 TOF_GESTURES_DEBUG(DIRSWIPE_1,"Waiting...");
mapellil 7:d79cbeda2982 101 return_code = GESTURES_STARTED;
mapellil 7:d79cbeda2982 102 }
mapellil 7:d79cbeda2982 103 break;
mapellil 7:d79cbeda2982 104 }
mapellil 7:d79cbeda2982 105 return return_code;
mapellil 7:d79cbeda2982 106 }
mapellil 7:d79cbeda2982 107