Skip to content

Move proximity gesture code in a dedicated library #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
COPYRIGHT(c) 2017 STMicroelectronics

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of STMicroelectronics nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# Proximity_Gesture
Gesture library for proximity sensors
# Proximity Gesture

Arduino library to support gesture-detection using proximity sensors (VL53L0X or VL6180X).
The APIs provide single swipe gesture detection, directional (left/right) swipe gesture detection and
single tap gesture detection.

## Note

You can find examples based on this library in the following packages:
* VL53L0X: https://github.com/stm32duino/VL53L0X
* X-NUCLEO-53L0A1: https://github.com/stm32duino/X-NUCLEO-53L0A1
* X-NUCLEO-6180XA1: https://github.com/stm32duino/X-NUCLEO-6180XA1

## Documentation

You can find the source files at
https://github.com/stm32duino/Proximity_Gesture
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=STM32duino Proximity Gesture
version=1.0.0
author=AST
maintainer=stm32duino
sentence=Allows performing simple gestures detection using proximity sensors
paragraph=This library provides single swipe gesture detection, directional (left/right) swipe gesture detection and single tap gesture detection.
category=Device Control
url=https://github.com/stm32duino/Proximity_Gesture
architectures=stm32
184 changes: 184 additions & 0 deletions src/ring_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*******************************************************************************
Copyright � 2015, STMicroelectronics International N.V.
All rights reserved.

Use and Redistribution are permitted only in accordance with licensing terms
available at www.st.com under software reference X-CUBE-6180XA1, and provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROTECTED BY STMICROELECTRONICS PATENTS AND COPYRIGHTS.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/

/*
* $Date: 2015-11-10 11:21:53 +0100 (Tue, 10 Nov 2015) $
* $Revision: 2612 $
*/


#include "ring_buffer.h"

#define ABS(a) ((a>=0) ? (a) : -(a))

int RB_init(ring_buffer_t* rb, int size)
{
rb->buffer_end = rb->buffer + ((size<RB_MAX_SIZE) ? size : RB_MAX_SIZE);
rb->size = size;
rb->data_start = rb->buffer;
rb->data_end = rb->buffer;
rb->count = 0;
return (size<=RB_MAX_SIZE) ? 0 : -1;
}

int RB_push(ring_buffer_t* rb, int data)
{
if (rb == NULL || rb->buffer == NULL)
return -1;

*rb->data_end = data;
rb->data_end++;
if (rb->data_end == rb->buffer_end)
rb->data_end = rb->buffer;

if (RB_full(rb)) {
if ((rb->data_start + 1) == rb->buffer_end)
rb->data_start = rb->buffer;
else
rb->data_start++;
} else {
rb->count++;
}

return 0;
}

int RB_pop(ring_buffer_t* rb)
{
if (rb == NULL || rb->buffer == NULL)
return false;

int8_t data = *rb->data_start;
rb->data_start++;
if (rb->data_start == rb->buffer_end)
rb->data_start = rb->buffer;
rb->count--;

return data;
}

bool RB_full(ring_buffer_t* rb)
{
return rb->count == rb->size;
}

void RB_trace(ring_buffer_t*rb)
{
int i=0;
int *ptr;

ptr = rb->data_start;
//trace_printf("TOF_GESTURES Ring Buffer : ");
for(i=0;i<rb->count;i++)
{
//trace_printf("%d,",*ptr++);
if(ptr == rb->buffer_end)
ptr = rb->buffer;
}
//trace_printf("\n");
}

int RB_sum(ring_buffer_t*rb)
{
int i=0;
int sum=0;
int *ptr;

ptr = rb->data_start;
for(i=0;i<rb->count;i++)
{
sum += *ptr++;
if(ptr == rb->buffer_end)
ptr = rb->buffer;
}
return sum;
}

int RB_mean(ring_buffer_t*rb)
{
return RB_sum(rb)/rb->count;
}

int RB_mad(ring_buffer_t*rb)
{
int i;
int *ptr;
int mad=0;
int mean;
int data;

mean = RB_mean(rb);

ptr = rb->data_start;
for(i=0;i<rb->count;i++)
{
data = *ptr++;
mad += ABS((data - mean));
if(ptr == rb->buffer_end)
ptr = rb->buffer;
}
return mad/rb->count;
}

int RB_dir(ring_buffer_t*rb)
{
int direction = 0;
int i=0;
int *ptr;
int value1, value2;

ptr = rb->data_start;
if (rb->count == 0 || rb->count == 1){
return 0;
} else {
value1 = *ptr++; if(ptr == rb->buffer_end) ptr = rb->buffer;
value2 = *ptr++; if(ptr == rb->buffer_end) ptr = rb->buffer;
direction = ((value2 - value1) > 0) ? 1 : (((value2 -value1) < 0) ? -1 : 0);
value1 = value2;
}

for(i=2;i<rb->count;i++)
{
value2 = *ptr++; if(ptr == rb->buffer_end) ptr = rb->buffer;
if ((direction==1) && ((value2-value1)<0)) {
direction = 0;
break;
}
if ((direction==-1) && ((value2-value1)>0)) {
direction = 0;
break;
}
if (direction==0) {
direction = ((value2 - value1) > 0) ? 1 : (((value2 -value1) < 0) ? -1 : 0);
}
value1 = value2;
}
return direction;
}
144 changes: 144 additions & 0 deletions src/ring_buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*******************************************************************************
Copyright � 2015, STMicroelectronics International N.V.
All rights reserved.

Use and Redistribution are permitted only in accordance with licensing terms
available at www.st.com under software reference X-CUBE-6180XA1, and provided
that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of STMicroelectronics nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROTECTED BY STMICROELECTRONICS PATENTS AND COPYRIGHTS.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
********************************************************************************/
/*
* @file ring_buffer.h
* $Date: 2015-11-10 11:21:53 +0100 (Tue, 10 Nov 2015) $
* $Revision: 2612 $
*/

#ifndef RING_BUFFER_H_
#define RING_BUFFER_H_

#include "tof_gestures_platform.h"

#ifdef __cplusplus
extern "C" {
#endif

/** @defgroup misc_ring_buffer Ring Buffer
* @brief Simple ring buffer implementation
@par Description
Ring buffer is implemented as a static array of integers of size #RB_MAX_SIZE. The functional size of the ring buffer is
programmable. When the ring_buffer is full, new elements added are replacing the older elements. This is typically used
to keep an history of the last ranging values measured from the ToF device.
* @ingroup misc
* @{
*/

/** Ring Buffer maximum size */
#define RB_MAX_SIZE 16

/**
* @struct ring_buffer
* @brief Simple ring buffer of int with a programmable size (max size is #RB_MAX_SIZE)
*/
typedef struct
{
int buffer[RB_MAX_SIZE];
int* buffer_end;
int* data_start;
int* data_end;
int count;
int size;
} ring_buffer_t;

/**
* @brief Initialize Ring Buffer
* @param rb Ring Buffer pointer
* @param size Number of int elements (max size is #RB_MAX_SIZE)
* @return 0 on success or -1 if size is greater than #RB_MAX_SIZE
*/
int RB_init(ring_buffer_t* rb, int size);

/**
* @brief Push one element in Ring Buffer (after the last element)
* @par Function Description
* If ring buffer is full, added element is replacing the oldest element in the ring buffer
* @param rb Ring Buffer pointer
* @param data Element to add
* @return 0 on success
*/
int RB_push(ring_buffer_t* rb, int data);

/**
* @brief pop one element in Ring Buffer (the last element)
* @param rb Ring Buffer pointer
* @return element
*/
int RB_pop(ring_buffer_t* rb);

/**
* @brief Check if ring buffer is full
* @param rb Ring Buffer pointer
* @return true if full else false
*/
bool RB_full(ring_buffer_t* rb);

/**
* @brief print/trace all elements in the ring buffer
* @param rb Ring Buffer pointer
* @note The TRACE key must be defined in the project
*/
void RB_trace(ring_buffer_t*rb);

/**
* @brief Return the sum of elements in the ring buffer
* @param rb Ring Buffer pointer
* @return The sum
*/
int RB_sum(ring_buffer_t*rb);

/**
* @brief Return the mean of all elements in the ring buffer
* @param rb Ring Buffer pointer
* @return The mean (rounded to integer)
*/
int RB_mean(ring_buffer_t*rb);

/**
* @brief Return the mean of the absolute differences of each element with the mean
* @param rb Ring Buffer pointer
* @return The mad (rounded to integer)
*/
int RB_mad(ring_buffer_t*rb);

/**
* @brief Return the direction of the curve of points stored in the buffer
* @param rb Ring Buffer pointer
* @return 1 if constantly increase, -1 if constantly decrease, 0 otherwise
*/
int RB_dir(ring_buffer_t*rb);

/** @} */

#ifdef __cplusplus
}
#endif
#endif /* RING_BUFFER_H_ */
Loading