/*
 * Copyright (c) 2013 - 2016, Freescale Semiconductor, Inc.
 * Copyright 2016-2017 NXP
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o 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.
 *
 * o Neither the name of the copyright holder 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.
 */
#ifndef _NT_MATRIX_PRV_H_
#define _NT_MATRIX_PRV_H_

/**
 * \defgroup matrix_private Matrix Control
 * \ingroup controls_private
 *
* Matrix implements the keyboard-like functionality on top of an array of
 * electrodes (touchpad); it is represented by the nt_control_matrix structure.
 *
 * An application may use the Electrode API to determine the touch or release states
 * of individual electrodes but the Matrix control processes the electrodes as two-dimensional array.
 * The images below show simple electrode array where the self-cap sensing is used. 
 * \image html matrix.png "Matrix Self-cap Sensing Electrodes"
 * \image latex matrix.png "Matrix Self-cap Sensing Electrodes "
 * To recognize the position on the shown layout means to activate two electrodes (horizontal and
 * vertical). When it happens the x and y coordinate of the touch position are known and the 
 * movement can be tracked. During the movement on the surface the actual x and y coordinates are changing 
 * in time and the position changes are stored to the buffer. At the end of the movement the release 
 * position is stored as well.
 *
 * The same processing is used for mutual-cap sensing based matrix where the position is known 
 * after touch on one electrode only because the mutual-cap sensing electrode consists of Tx and Rx 
 * electrodes which determines the position on the surface by similar way. Then the algorithm of the matrix
 * control is the same.
 * \image html matrix.png "Matrix Mutual-cap Sensing Electrodes"
 * \image latex matrix.png "Matrix Mutual-cap Sensing Electrodes"
 * 
 * The position recognition can be extended by simple trick. The touch on the two nearest electrodes 
 * can be considered as virtual electrodes between touched electrodes. Such a way the horizontal and 
 * vertical resolution can be extended to adding gaps between electrodes. The new resolution is twice 
 * the original resolution minus one (new_resolution = origin_resolution + electrode_gaps = 2 * origin_resolution - 1). 
 * 
 * The matrix control processes the position regardless of finger size. The large finger should activate 
 * more than one horizontal and one vertical electrodes for self-cap mode. The algorithm neglects the other
 * nearest touched electrodes to centralize the position recognition. The feature ensures to have the most precise
 * position on the surface for next processing. The position can be determined from one, two or three nearest 
 * electrodes, if more nearest electrodes are enable the position is not recognized. This rule is valid for horizontal 
 * and vertical electrodes. 
 * 
 * The matrix control algorithm enables to recognize the position not only one but two fingers simultaneously. 
 * The finger position or two finger positions are stored for the touch, release and movement (actual position) events.
 * Also, the movement tracking and gestures can process two fingers.
 * 
 * The gesture feature is the main task for the matrix control. The gestures can be divides to two groups: 
 * control gestures and unlock gestures. The control gestures are usually used to control the device, change mode, 
 * configure feature or make some user action. The unlock gesture stored by user typically used for unlocking screen,
 * wake up device or recognize the movement precisely. The matrix control enables to recognize following control gestures:
 *  - swipe up
 *  - swipe down
 *  - swipe left
 *  - swipe right
 *  - cover  
 *  - double tap
 *      
 * and the user can store arbitrary number of unlock gestures limited by free memory. The unlock gestures have higher 
 * priority then control gesture, if both unlock and control gesture are recognized the control gesture is neglected. 
 * The control gesture must be longer than three recognized positions horizontally or vertically and the movement in 
 * dominant direction must cover twice the amount recognized positions than in insignificant direction. The double tap 
 * gesture enables to define the maximum time interval between taps. For cover gesture recognizing each touchpad 
 * electrode must be active. 
 * 
 * The unlock gesture means continuous curve or curves drawn by one or two fingers. The gesture can be stored from 
 * the drawn pattern to ram memory by changing select_gesture variable (zero means no gesture to store, the number 
 * specify which gesture number will be stored) or directly define as parameter of matrix control. 
 * For permanent usage or the drawn gestures, the stored gestures must be manually rewrite as matrix gesture parameter 
 * to be stored in flash memory. 
 * 
 * After release event, the actual drawn gesture is buffered and compared with stored unlock gestures if any and 
 * in case of lower dissimilarity then specified one the gesture is recognized. The dissimilarity is value
 * providing information how much is the actual gesture like stored ones. Zero dissimilarity value means 
 * the gestures are the same. 
 *
 * During drawing the gesture, the finger or fingers position changes are stored to buffer. Maximum resolution for position 
 * is 16 values (hexadecimal value) for each dimension. If matrix is too small the resolution can be extended by creating
 * the virtual electrodes between real ones. If two nearest electrodes are activated currently the algorithm process it as
 * activated virtual electrode between real electrodes. The new resolution is: new_resolution = 2 * default_resolution - 1.
 * This feature is enabled by defining true value to increase_resolution matrix constant. The movement on matrix is buffered 
 * such a way that each position change is stored to 64-bit buffer from right to left as hexadecimal value. Therefore 
 * the same gestures drawn by different speed are not the same one because the buffer size is different. It must 
 * be considered for gesture recognition. The gesture comparison algorithm is based on summation square point to point 
 * distances. 
 *    
 * \{
 */

#include "nt_controls.h"
#include "nt_types.h"
#include "nt_control_matrix.h"
#include "nt_setup.h"    

#ifndef NT_MATRIX_UNLOCK_GESTURES 
    #define NT_MATRIX_UNLOCK_GESTURES 1
    #warning "The NT_MATRIX_UNLOCK_GESTURES macro was not defined! Please define in nt_setup.h" 
#endif

     
/* forward declaration */
struct nt_control_matrix;
struct nt_coordinate_xy;

/** 
 * Matrix Matrix control flags. 
 */
enum nt_control_matrix_flags {
    NT_MATRIX_RELEASE_FLAG             = 1 << NT_FLAGS_SPECIFIC_SHIFT(0), /*!< Release event */
    NT_MATRIX_TOUCH_FLAG               = 1 << NT_FLAGS_SPECIFIC_SHIFT(1), /*!< Key-touch event */
    NT_MATRIX_MOVEMENT_FLAG            = 1 << NT_FLAGS_SPECIFIC_SHIFT(2), /*!< Movement event */
    NT_MATRIX_SET_UNLOCK_GESTURE_FLAG  = 1 << NT_FLAGS_SPECIFIC_SHIFT(3), /*!< New gesture is stored */ 
    NT_MATRIX_GET_UNLOCK_GESTURE_FLAG  = 1 << NT_FLAGS_SPECIFIC_SHIFT(4), /*!< Stored gesture is recognized */   
    NT_MATRIX_GET_CONTROL_GESTURE_FLAG = 1 << NT_FLAGS_SPECIFIC_SHIFT(5), /*!< Control gesture is recognized */     
};

/**
 *  The Matrix RAM structure used to store volatile parameters of the control.
 *
 *  You must allocate this structure and put a pointer into the nt_control_matrix
 *  structure when it is being registered in the system.
 */
struct nt_control_matrix_data {
    nt_points64                buffer;                             /*!< Buffer to store movement on x and y axis. */
    nt_points64                gesture[NT_MATRIX_UNLOCK_GESTURES];  /*!< Recorded gesture */
    uint32_t                   last_electode_states;               /*!< Last electrodes states */
    uint32_t                   double_tap_next_time;               /*!< Time instant for second tap to make valid double tap */
    uint16_t                   touchpad_mask_horizontal;           /*!< Touchpad mask for horizontal coordinate */ 
    uint32_t                   touchpad_mask;                      /*!< Internal mask defining touchpad electrodes used
                                                                        (horizontal and vertical electrode must be next to each other) */ 
    nt_points16                touch_position;                     /*!< Touchpad touch position on x and y axis */
    nt_points16                actual_position;                    /*!< Touchpad touch position on x and y axis */
    nt_points16                release_position;                   /*!< Touchpad release position on y and y axis */ 
    uint16_t                   gesture_diff[5][NT_MATRIX_UNLOCK_GESTURES];/// for debugging only 
    uint16_t                   gesture_size[NT_MATRIX_UNLOCK_GESTURES]; 
    uint16_t                   two_points_gesture_flag;            /*!< Indicates two points gesture */ 
    uint8_t                    buffer_position;                    /*!< Store position in buffer. */
    uint8_t                    select_gesture;                     /*!< Number code of gesture to be stored */
    uint8_t                    recognized_gesture;                 /*!< Number code of recognized gesture */ 
    uint8_t                    gesture_memory;                     /*!< Variable select the memory for stored gesture. RAM will be used used if selected_gesture > 0 was made */  
    uint8_t                    get_position_add_val[6];            /*!< Constants for get_position function defined due to increase_resolution constant */
    nt_control_matrix_callback callback;                           /*!< Matrix callback handler. */
};

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

/** \} end of matrix_private group */

#endif /* _NT_MATRIX_PRV_H_ */
