
/*
 * Copyright (c) 2013 - 2016, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * 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 Freescale Semiconductor, Inc. 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"
#include "nt_drv_tsi_driver.h"

/* USER DEFINED */

/*
 * Key Detectors 
 * The same Keydetectors AFID / SAFA can be used for all electrodes 
 * Or different keydetectors may be used for different electrodes for testing
 */

///* AFID keydetector settings */
//const struct nt_keydetector_afid keydec_afid = 
//{
//  .signal_filter = 1,
//  .fast_signal_filter = {
//      .cutoff = 6
//  },
//  .slow_signal_filter = {
//      .cutoff = 2
//  },
//  .base_avrg = {.n2_order = 12},
//  .reset_rate = 10,
//  .asc = { 
//        .touch_threshold_fall_rate = 1000, 
//        .noise_resets_minimum = 256,
//        .resets_for_touch = 5, 
//        },
//};
/* 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,//10
    .signal_to_noise_ratio = 4,
    .deadband_cnt = 4,//10
    .min_noise_limit = 50,    
};

/* 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_PTB2,
    .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_PTB3,
    .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_PTB16,
    .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_PTB17,
    .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_PTB18,
    .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_PTB19,
    .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_PTA0,
    .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_PTA3,
    .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_PTB1,
    .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_PTB0,
    .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_PTA2,
    .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_PTA4,
    .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_PTC1,
    .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_PTC2,
    .baseline_level = 31865,
};


/* 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,  NULL};

static const struct nt_electrode  * const module_0_electrodes_debug[] = {
       &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,  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_x_debug[] = {
       &electrode_X0, &electrode_X1, &electrode_X2, &electrode_X3, &electrode_X4, &electrode_X5, &electrode_X6, 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,  NULL};

/* Normal RUN mode HW cfg. */
const tsi_config_t hw_config =
{
  .prescaler = kTSI_ElecOscPrescaler_32div,     /*!< Prescaler */
  .extchrg = kTSI_ExtOscChargeCurrent_32uA,     /*!< Electrode charge current */
  .refchrg = kTSI_RefOscChargeCurrent_32uA,     /*!< Reference charge current */
  .nscn = kTSI_ConsecutiveScansNumber_24time,//kTSI_ConsecutiveScansNumber_24time,   /*!< Number of scans. */
  .mode = kTSI_AnalogModeSel_Capacitive,
  .dvolt = kTSI_OscVolRailsOption_0,
  .thresh = 0,                                  /*!< High threshold. */
  .thresl = 0,                                  /*!< Low threshold. */
  .filter = kTSI_FilterBits_0,                  /* Must be set due to common init for noise mode and non-noise mode */
};

const struct nt_module_tsi_params tsi_params = 
{
  .noise = 
  {
    .noise_filter = 
    {
      .coef1 = 2,
    },
    .update_rate = 50,  /*!< switch every 50ms to noise mode and try to get result */
    .noise_mode_timeout = 100,                  /*!< stay 100ms in noise mode */
  },
  
  .config = NULL,                          /*!< HW config used for noise mode */
  
};

struct nt_module tsi_module =
{
    .interface = &nt_module_tsi_interface,
    .electrodes = &module_0_electrodes_debug[0],
    .config = (void*)&hw_config,
    .instance = 0,
    .module_params = NULL               /*!< for noise mode use "tsi_params" */
};

#if USE_MATRIX
static struct nt_control_matrix matrix_params =
{
   .double_tap_interval     = 500,/* 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= 0,//7, /* electode number in x touchpad coordinate */
   .touchpad_size_vertical  = 0, /* 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}}}, // =
};
#endif

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. */
};

#if USE_MATRIX

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

#endif

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,
};
/* System */
#if USE_MATRIX
const struct nt_control * const controls[] = {&aslider_x,&aslider_y, &matrix_0, NULL};
#else
const struct nt_control * const controls[] = {&aslider_x,&aslider_y, NULL};// &matrix_0,
#endif

const struct nt_module * const modules[] = {&tsi_module, NULL};

const struct nt_system system_0 = {
    .controls = &controls[0],
    .modules = &modules[0],
    .time_period = 5, /*!< NT internal time tisc = 5ms */
    .init_time = 50,  /*!< Initial stabilisation time after startup = 50ms */
};

void set_sebug(uint8_t debug_enable)
{
  if(debug_enable == DEBUG_ENABLE)
   { 
#if USE_MATRIX
    matrix_params.touchpad_size_horizontal = 7;
    matrix_params.touchpad_size_vertical   = 7;
    matrix_0.electrodes = module_0_electrodes_debug;
#endif    
    aslider_x.electrodes= module_0_electrodes_x_debug;
    tsi_module.electrodes = &module_0_electrodes_debug[0];
   }else
    {
#if USE_MATRIX
      matrix_0.electrodes = module_0_electrodes;
      matrix_params.touchpad_size_horizontal = 9;
      matrix_params.touchpad_size_vertical   = 7;
#endif
      aslider_x.electrodes= module_0_electrodes_x;
      tsi_module.electrodes = &module_0_electrodes[0];
    }
};