/*
 * 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.
 */
 
#include "nt_setup.h"
#include "board.h"

/* USER DEFINED */
/* Self-cap mode config */
const tsi_config_t hw_config =
{
    /* Self capacitance measurement config */
    .configSelfCap.commonConfig.mainClock = kTSI_MainClockSlection_0,         /*!< Set main clock */
    .configSelfCap.commonConfig.mode = kTSI_SensingModeSlection_Self,         /*!< Choose sensing mode */
    .configSelfCap.commonConfig.dvolt = kTSI_DvoltOption_0,                   /*!< DVOLT option value  */
    .configSelfCap.commonConfig.cutoff = kTSI_SincCutoffDiv_0,                /*!< Cutoff divider  */
    .configSelfCap.commonConfig.order = kTSI_SincFilterOrder_2,               /*!< SINC filter order */
    .configSelfCap.commonConfig.decimation = kTSI_SincDecimationValue_4,      /*!< SINC decimation value */
    .configSelfCap.commonConfig.chargeNum = kTSI_SscChargeNumValue_7,         /*!< SSC output bit0's period setting */
    .configSelfCap.commonConfig.noChargeNum = kTSI_SscNoChargeNumValue_5,     /*!< SSC output bit1's period setting */
    .configSelfCap.enableSensitivity = true,                                  /*!< Enable sensitivity boost of self-cap or not */
    .configSelfCap.xdn = kTSI_SensitivityXdnOption_3,                         /*!< Sensitivity XDN option   */
    .configSelfCap.ctrim = kTSI_SensitivityCtrimOption_0,                     /*!< Sensitivity CTRIM option */
    .configSelfCap.inputCurrent = kTSI_CurrentMultipleInputValue_0,           /*!< Input current multiple   */
    .configSelfCap.chargeCurrent = kTSI_CurrentMultipleChargeValue_0,         /*!< Charge/Discharge current multiple */
};

//struct nt_tsi_recalib_config recalib_config = { 
//    .SelfRangeMin     = 50000,     /*!< minimum counter value for self-cap mode */
//    .SelfRangeMax     = 60000,     /*!< maximum counter value for self-cap mode */
//    .SelfRangeDeltaMin= 400,       /*!< minimum delta value for self-cap mode */
//    .SelfRangeDeltaMax= 1300,      /*!< maximum delta value for self-cap mode */
//    .MutRangeMin      = 12000,     /*!< minimum counter value for mutual-cap mode */
//    .MutRangeMax      = 16000,     /*!< maximum counter value for mutual-cap mode */
//    .MutRangeDeltaMin = 300,       /*!< minimum delta value for mutual-cap mode */
//    .MutRangeDeltaMax = 600,       /*!< maximum delta value for mutual-cap mode */
//};


/* SAFA keydetector settings */
const struct nt_keydetector_safa keydec_safa =
{
    .signal_filter = 4,
    .base_avrg = {.n2_order = 12},
    .non_activity_avrg =  {.n2_order = NT_FILTER_MOVING_AVERAGE_MAX_ORDER},
    .entry_event_cnt = 1,//4
    .signal_to_noise_ratio = 4,
    .deadband_cnt = 4,//4 
    .min_noise_limit = 30,    
};

/* Electrodes */
/* With SAFA keydetector settings */
const struct nt_electrode electrode_X0 =
{
    .pin_input = BOARD_TSI_ELECTRODE_0, //X0
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTB5,
    .baseline_level = 32514,
};
const struct nt_electrode electrode_X1 =
{
    .pin_input = BOARD_TSI_ELECTRODE_1,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTB4,
    .baseline_level = 31565,
};
const struct nt_electrode electrode_X2 =
{
    .pin_input = BOARD_TSI_ELECTRODE_2,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTD7,
    .baseline_level = 32157,
};
const struct nt_electrode electrode_X3 =
{
    .pin_input = BOARD_TSI_ELECTRODE_3,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTD6,
    .baseline_level = 32223,
};
const struct nt_electrode electrode_X4 =
{
    .pin_input = BOARD_TSI_ELECTRODE_4,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTD5,
    .baseline_level = 31099,
};
const struct nt_electrode electrode_X5 =
{
    .pin_input = BOARD_TSI_ELECTRODE_5,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTC1,
    .baseline_level = 30703,
};

const struct nt_electrode electrode_X6 =
{
    .pin_input = BOARD_TSI_ELECTRODE_6,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTC0,
    .baseline_level = 31903,
};
const struct nt_electrode electrode_X7 =
{
    .pin_input = BOARD_TSI_ELECTRODE_7,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTB3,
    .baseline_level = 61910,
};
const struct nt_electrode electrode_X8 =
{
    .pin_input = BOARD_TSI_ELECTRODE_8,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTB2,
    .baseline_level = 61741,
};
const struct nt_electrode electrode_Y0 =
{
    .pin_input = BOARD_TSI_ELECTRODE_9,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTC6,
    .baseline_level = 32651,
};
const struct nt_electrode electrode_Y1 =
{
    .pin_input = BOARD_TSI_ELECTRODE_10,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTC7,
    .baseline_level = 32539,
};
const struct nt_electrode electrode_Y2 =
{
    .pin_input = BOARD_TSI_ELECTRODE_11,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTA0,
    .baseline_level = 32288,
};
const struct nt_electrode electrode_Y3 =
{
    .pin_input = BOARD_TSI_ELECTRODE_12,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTA1,
    .baseline_level = 32047,
};
const struct nt_electrode electrode_Y4 =
{
    .pin_input = BOARD_TSI_ELECTRODE_13,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTE8,
    .baseline_level = 32267,
};
const struct nt_electrode electrode_Y5 =
{
    .pin_input = BOARD_TSI_ELECTRODE_14,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTE3,
    .baseline_level = 31906,
};
const struct nt_electrode electrode_Y6 =
{
    .pin_input = BOARD_TSI_ELECTRODE_15,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTE4,
    .baseline_level = 31865,
};

const struct nt_electrode electrode_Y7 =
{
    .pin_input = BOARD_TSI_ELECTRODE_16,
    .keydetector_interface = &nt_keydetector_safa_interface,
    .keydetector_params.safa = &keydec_safa,
    .gpio_input = GPIO_PTE5,
    .baseline_level = 31865,
};
/* For correct position recognition the electrodes must be arranged in the array. */
/* The mutual-cap electrodes must be aligned from left to right of the top line, then from left to right of the line lower and lower. */
/* the self-cap electodes must be aligned from left to right and the from the top to the lowest row. */ 
/* Modules */
static const struct nt_electrode  * const module_0_electrodes[] = {
       &electrode_X0, &electrode_X1, &electrode_X2, &electrode_X3, &electrode_X4, &electrode_X5, &electrode_X6, &electrode_X7,&electrode_X8,
       &electrode_Y0, &electrode_Y1, &electrode_Y2, &electrode_Y3, &electrode_Y4, &electrode_Y5, &electrode_Y6, &electrode_Y7 ,  NULL};

static const struct nt_electrode  * const module_0_electrodes_x[] = {
       &electrode_X0, &electrode_X1, &electrode_X2, &electrode_X3, &electrode_X4, &electrode_X5, &electrode_X6, &electrode_X7,&electrode_X8, NULL};

static const struct nt_electrode  * const module_0_electrodes_y[] = {
       &electrode_Y0, &electrode_Y1, &electrode_Y2, &electrode_Y3, &electrode_Y4, &electrode_Y5, &electrode_Y6, &electrode_Y7,  NULL};

//static const struct nt_control_matrix matrix_params =
//{
//   .double_tap_interval     = 350,/* Interval between two touch to recognize double tap */
//   .dissimilarity_max       = 3, /* 0 means the same, higher number means higher gesture differences relative to its size */
//                                  /*   if increase_resolution is set to true, double dissimilarity_max parameter */
//   .touchpad_size_horizontal= 9, /* electode number in x touchpad coordinate */
//   .touchpad_size_vertical  = 8, /* electode number in y touchpad coordinate */
//   .sensing_mode            = false, /* touchpad uses self-capacitance mode */
//   .increase_resolution     = false, /* false...default, true...enhances resolution. (placing virtual electrodes between real ones) */
//   .atan = {{90,90,90,90,90},{ 0,45,63,72,76},{ 0,27,45,56,63},{ 0,18,34,45,53},{ 0,14,27,37,45}},
//   .gesture ={{{0x0007765544333223,0x0006666777776666},{0x0000000000000000,0x0000000000000000}}, // Z
//              {{0x0005555554454455,0x0007766554322112},{0x0000000000000000,0x0000000000000000}}, // I
//              {{0x0000000087888877,0x0000000022223444},{0x0000000000000000,0x0000000000000000}}, // U  
//              {{0x0087777665443323,0x0006666666666666},{0x0007777665443323,0x0006666666666666}}}, // =            
//};

/***********************************************/
/* Definition of control                      */
/***********************************************/

struct nt_control_aslider aslider_params = {
    .range = 255,  /*!< Maximum range for the ram->position value. */
    .insensitivity = 0, /*!< Set insensitivity to ignore changes less than 0. */
};


//struct nt_control matrix_0 =
//{
//  .interface = &nt_control_matrix_interface,
//  .electrodes = module_0_electrodes,
//  .control_params.matrix = &matrix_params,  
//};

struct nt_control aslider_x =
{
  .interface = &nt_control_aslider_interface,
  .electrodes = module_0_electrodes_x,
  .control_params.aslider = &aslider_params,
};
struct nt_control aslider_y =
{
  .interface = &nt_control_aslider_interface,
  .electrodes = module_0_electrodes_y,
  .control_params.aslider = &aslider_params,
};
/* GPIO interface for safety tests */
extern const struct nt_module_gpio_user_interface gpio_interface;

struct nt_module tsi_module =
{
    .interface = &nt_module_tsi_interface,
    .electrodes = &module_0_electrodes[0],
    .config = (void*)&hw_config,
    //.safety_interface = &nt_safety_interface,
    //.safety_params.gpio = &my_safety_params,     
    .instance = 0,
};

const struct nt_control * const controls[] = {&aslider_x,&aslider_y, NULL}; //&matrix_0, NULL};
const struct nt_module * const modules[] = {&tsi_module, NULL};

const struct nt_system system_0 = {
    .controls = &controls[0],
    .modules = &modules[0],
    .time_period = 35,
    .init_time = 400,
    //.safety_period_multiple = 100,
};
