Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /*******************************************************************************
  2. *   touch.c
  3. *   Atmel Corporation:  http://www.atmel.com \n
  4. *   Support email:  touch@atmel.com
  5. ******************************************************************************/
  6.  
  7. /*  License
  8. *   Copyright (c) 2010, Atmel Corporation All rights reserved.
  9. *
  10. *   Redistribution and use in source and binary forms, with or without
  11. *   modification, are permitted provided that the following conditions are met:
  12. *
  13. *   1. Redistributions of source code must retain the above copyright notice,
  14. *   this list of conditions and the following disclaimer.
  15. *
  16. *   2. Redistributions in binary form must reproduce the above copyright notice,
  17. *   this list of conditions and the following disclaimer in the documentation
  18. *   and/or other materials provided with the distribution.
  19. *
  20. *   3. The name of ATMEL may not be used to endorse or promote products derived
  21. *   from this software without specific prior written permission.
  22. *
  23. *   THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  24. *   WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. *   MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  26. *   SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  27. *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. /*  This is an automatically generated file. Do not modify the contents manually.
  35.     Instead, use the Project Builder Wizard to make changes to the design.
  36. */
  37. /*----------------------------------------------------------------------------
  38.                             compiler information
  39. ----------------------------------------------------------------------------*/
  40.  
  41. /*----------------------------------------------------------------------------
  42.                                 include files
  43. ----------------------------------------------------------------------------*/
  44.  
  45.  
  46.  
  47.  
  48. #include <avr/io.h>
  49. #include <avr/interrupt.h>
  50.  
  51. #define __enable_interrupt()  sei()
  52. #define __delay_cycles(n)     __builtin_avr_delay_cycles(n)
  53.  
  54. /*  now include touch api.h with the localization defined above */
  55. #include "src/QTouch/touch_api.h"
  56. #include "touch.h"
  57. #ifdef _DEBUG_INTERFACE_
  58. /* Include files for QTouch Studio integration */
  59. #include "QDebug.h"
  60. #include "QDebugTransport.h"
  61. #endif
  62.  
  63. /*----------------------------------------------------------------------------
  64.                             manifest constants
  65. ----------------------------------------------------------------------------*/
  66.  
  67.  
  68. /*----------------------------------------------------------------------------
  69.                             type definitions
  70. ----------------------------------------------------------------------------*/
  71.  
  72. /*----------------------------------------------------------------------------
  73.                                 prototypes
  74. ----------------------------------------------------------------------------*/
  75.  
  76. /*  Assign the parameters values to global configuration parameter structure    */
  77. static void qt_set_parameters( void );
  78. /*  Configure the sensors */
  79. static void config_sensors(void);
  80.  
  81. #ifdef _DEBUG_INTERFACE_
  82. #ifdef _QDEBUG_TIME_STAMPS_
  83. void set_timestamp1(void); // used for timestamping
  84. #endif
  85. #endif
  86.  
  87. /*----------------------------------------------------------------------------
  88.                             Structure Declarations
  89. ----------------------------------------------------------------------------*/
  90.  
  91. /*----------------------------------------------------------------------------
  92.                                     macros
  93. ----------------------------------------------------------------------------*/
  94. #ifdef _DEBUG_INTERFACE_
  95.  #ifdef _QDEBUG_TIME_STAMPS_
  96. /* This below code is used for timestamping related information */
  97.  
  98.  
  99.  
  100.   #define TIMESTAMP0  asm("cli");   timestamp0_lword = (uint16_t)TIMER_COUNTER_L;timestamp0_lword |= (uint16_t)(TIMER_COUNTER_H << 8); timestamp0_hword = current_time_ms_touch; asm("sei");
  101.   #define TIMESTAMP1  asm("cli"); timestamp1_lword = (uint16_t)TIMER_COUNTER_L;timestamp1_lword |= (uint16_t)(TIMER_COUNTER_H << 8); timestamp1_hword = current_time_ms_touch; asm("sei");
  102.   #define TIMESTAMP2  asm("cli"); timestamp2_lword = (uint16_t)TIMER_COUNTER_L;timestamp2_lword |= (uint16_t)(TIMER_COUNTER_H << 8); timestamp2_hword = current_time_ms_touch; asm("sei");
  103.   #define TIMESTAMP3  asm("cli"); timestamp3_lword = (uint16_t)TIMER_COUNTER_L;timestamp3_lword |= (uint16_t)(TIMER_COUNTER_H << 8); timestamp3_hword = current_time_ms_touch; asm("sei");
  104.   #define TIMESTAMP4  asm("cli"); timestamp4_lword = (uint16_t)TIMER_COUNTER_L;timestamp4_lword |= (uint16_t)(TIMER_COUNTER_H << 8); timestamp4_hword = current_time_ms_touch; asm("sei");
  105.  
  106.  #endif
  107. #else
  108.  
  109. #define TIMESTAMP0   {}
  110. #define TIMESTAMP1   {}
  111. #define TIMESTAMP2   {}
  112. #define TIMESTAMP3   {}
  113. #define TIMESTAMP4   {}
  114. #endif
  115.  
  116.  
  117. /*----------------------------------------------------------------------------
  118.                                 global variables
  119. ----------------------------------------------------------------------------*/
  120.  
  121. #ifdef _DEBUG_INTERFACE_
  122.  #ifdef _QDEBUG_TIME_STAMPS_
  123.   uint16_t timestamp0_hword=0;
  124.   uint16_t timestamp0_lword=0;
  125.   uint16_t timestamp1_hword=0;
  126.   uint16_t timestamp1_lword=0;
  127.   uint16_t timestamp2_hword=0;
  128.   uint16_t timestamp2_lword=0;
  129.   uint16_t timestamp3_hword=0;
  130.   uint16_t timestamp3_lword=0;
  131.   uint16_t timestamp4_hword=0;
  132.   uint16_t timestamp4_lword=0;
  133.  #endif
  134. #endif
  135.  
  136. /*----------------------------------------------------------------------------
  137.                                 extern variables
  138. ----------------------------------------------------------------------------*/
  139. /* This configuration data structure parameters if needs to be changed will be
  140.    changed in the qt_set_parameters function */
  141. extern qt_touch_lib_config_data_t qt_config_data;
  142. /* touch output - measurement data */
  143. extern qt_touch_lib_measure_data_t qt_measure_data;
  144. /* Get sensor delta values */
  145. extern int16_t qt_get_sensor_delta( uint8_t sensor);
  146. #ifdef QTOUCH_STUDIO_MASKS
  147. extern TOUCH_DATA_T SNS_array[2][2];
  148. extern TOUCH_DATA_T SNSK_array[2][2];
  149. #endif
  150.  
  151. extern uint8_t time_to_measure_touch;
  152. extern uint16_t current_time_ms_touch;
  153.  
  154. /* Output can be observed in the watch window using this pointer */
  155. qt_touch_lib_measure_data_t *pqt_measure_data = &qt_measure_data;
  156.  
  157.  
  158. /*----------------------------------------------------------------------------
  159.                                 static variables
  160. ----------------------------------------------------------------------------*/
  161.  
  162. /*============================================================================
  163. Name    :   touch_init
  164. ------------------------------------------------------------------------------
  165. Purpose :   This will initialize touch related code.
  166. Input   :   n/a
  167. Output  :   n/a
  168. Notes   :
  169. ============================================================================*/
  170.  
  171. void touch_init( void )
  172. {
  173.  
  174.  
  175.  
  176.     #ifdef QTOUCH_STUDIO_MASKS
  177.     SNS_array[0][0]= 0x27;
  178.     SNS_array[0][1]= 0x10;
  179.     SNS_array[1][0]= 0x0;
  180.     SNS_array[1][1]= 0x0;
  181.  
  182.     SNSK_array[0][0]= 0x27;
  183.     SNSK_array[0][1]= 0x10;
  184.     SNSK_array[1][0]= 0x0;
  185.     SNSK_array[1][1]= 0x0;
  186.     #endif
  187.  
  188.  
  189.  
  190.  
  191.     /* Configure the Sensors as keys or Keys With Rotor/Sliders in this function */
  192.     config_sensors();
  193.  
  194.     /* initialise touch sensing */
  195.     qt_init_sensing();
  196.  
  197.     /*  Set the parameters like recalibration threshold, Max_On_Duration etc in this function by the user */
  198.     qt_set_parameters( );
  199.  
  200.     /*  Address to pass address of user functions   */
  201.     /*  This function is called after the library has made capacitive measurements,
  202.     *   but before it has processed them. The user can use this hook to apply filter
  203.     *   functions to the measured signal values.(Possibly to fix sensor layout faults)    */
  204.        #ifdef _DEBUG_INTERFACE_
  205.        #ifdef _QDEBUG_TIME_STAMPS_
  206.               qt_filter_callback = &set_timestamp1;
  207.        #else
  208.               qt_filter_callback = 0;
  209.        #endif
  210.        #else
  211.               qt_filter_callback = 0;
  212.        #endif
  213.  
  214.  
  215.  
  216.  
  217. #ifdef _DEBUG_INTERFACE_
  218.     /* Initialize debug protocol */
  219.     QDebug_Init();
  220. #endif
  221.  
  222.     /* enable interrupts */
  223.     __enable_interrupt();
  224.  
  225.  
  226. #ifdef _DEBUG_INTERFACE_
  227.     /* Process commands from PC */
  228.     QDebug_ProcessCommands();
  229. #endif
  230.  
  231. }
  232.  
  233.  
  234. #ifdef _DEBUG_INTERFACE_
  235. #ifdef _QDEBUG_TIME_STAMPS_
  236. void set_timestamp1(void)
  237. {
  238.     TIMESTAMP1;
  239. }
  240. #endif
  241. #endif
  242.  
  243. /*============================================================================
  244. Name    :   touch_measure
  245. ------------------------------------------------------------------------------
  246. Purpose :   This will call all the functions for touch related measurement.
  247. Input   :   n/a
  248. Output  :   n/a
  249. Notes   :
  250. ============================================================================*/
  251.  
  252. void touch_measure(void)
  253. {
  254.  
  255.     /*status flags to indicate the re-burst for library*/
  256.     static uint16_t status_flag = 0u;
  257.     static uint16_t burst_flag = 0u;
  258.  
  259.       if( time_to_measure_touch )
  260.         {
  261.  
  262.             /*  clear flag: it's time to measure touch  */
  263.             time_to_measure_touch = 0u;
  264.  
  265.             do {
  266.                 #ifdef _DEBUG_INTERFACE_
  267.                 #ifdef _QDEBUG_TIME_STAMPS_
  268.                         TIMESTAMP0;
  269.                 #endif
  270.                 #endif
  271.  
  272.                 /*  one time measure touch sensors    */
  273.                 status_flag = qt_measure_sensors( current_time_ms_touch );
  274.  
  275.                 #ifdef _DEBUG_INTERFACE_
  276.                 #ifdef _QDEBUG_TIME_STAMPS_
  277.                       TIMESTAMP2;
  278.                 #endif
  279.                 #endif
  280.  
  281.                 burst_flag = status_flag & QTLIB_BURST_AGAIN;
  282. #ifdef _DEBUG_INTERFACE_
  283.                 /* send debug data */
  284.                 QDebug_SendData(status_flag);
  285.                 /* Process commands from PC */
  286.                 QDebug_ProcessCommands();
  287. #endif
  288.             #ifdef _DEBUG_INTERFACE_
  289.             #ifdef _QDEBUG_TIME_STAMPS_
  290.                 TIMESTAMP3;
  291.             #endif
  292.             #endif
  293.  
  294.                 /* Time-critical host application code goes here */
  295.  
  296.             }while (burst_flag) ;
  297.  
  298.         }
  299.  
  300.  
  301. }
  302.  
  303.  
  304. /*============================================================================
  305. Name    :   qt_set_parameters
  306. ------------------------------------------------------------------------------
  307. Purpose :   This will fill the default threshold values in the configuration
  308.             data structure.But User can change the values of these parameters .
  309. Input   :   n/a
  310. Output  :   n/a
  311. Notes   :   Generated Code from QTouch Studio. Do not change
  312. ============================================================================*/
  313.  
  314. static void qt_set_parameters( void )
  315. {
  316.  
  317. /*  This will be modified by the user to different values   */
  318. qt_config_data.qt_di              = 2;
  319. qt_config_data.qt_neg_drift_rate  = 20;
  320. qt_config_data.qt_pos_drift_rate  = 5;
  321. qt_config_data.qt_max_on_duration = 0;
  322. qt_config_data.qt_drift_hold_time = 20;
  323. qt_config_data.qt_recal_threshold = 1;
  324. qt_config_data.qt_pos_recal_delay = 0;
  325.  
  326. }
  327.  
  328. /*============================================================================
  329. Name    :   config_sensors -
  330. ------------------------------------------------------------------------------
  331. Purpose :   Configure the sensors
  332. Input   :   n/a
  333. Output  :   n/a
  334. Notes   :   Generated code from QTouch Studio. Do not change
  335. ============================================================================*/
  336. static void config_sensors(void)
  337. {
  338.     qt_enable_key( CHANNEL_3, NO_AKS_GROUP, 10u, HYST_6_25 );
  339.     qt_enable_key( CHANNEL_4, NO_AKS_GROUP, 10u, HYST_6_25 );
  340.     qt_enable_slider( CHANNEL_0, CHANNEL_2, NO_AKS_GROUP, 10u, HYST_6_25, RES_8_BIT, 0u  );
  341.  
  342. }
  343.  
  344.  
  345.