• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

mTouch_proximity.c

Go to the documentation of this file.
00001 /*************************************************************************
00002  *  © 2012 Microchip Technology Inc.                                       
00003  *  
00004  *  Project Name:    mTouch Framework v2.1
00005  *  FileName:        mTouch_proximity.c
00006  *  Dependencies:    mTouch.h
00007  *  Processor:       See documentation for supported PIC® microcontrollers 
00008  *  Compiler:        HI-TECH Ver. 9.81 or later
00009  *  IDE:             MPLAB® IDE v8.50 (or later) or MPLAB® X                        
00010  *  Hardware:         
00011  *  Company:         
00012  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00013  *  Description:     mTouch Proximity Functions
00014  *                   - Implements the mTouch proximity sensor 
00015  *                     initialization, filtering, and decoding.
00016  *                   - See the documentation for more information about
00017  *                     implementing the framework with your application.
00018  *************************************************************************/
00019 /**************************************************************************
00020  * MICROCHIP SOFTWARE NOTICE AND DISCLAIMER: You may use this software, and 
00021  * any derivatives created by any person or entity by or on your behalf, 
00022  * exclusively with Microchip's products in accordance with applicable
00023  * software license terms and conditions, a copy of which is provided for
00024  * your referencein accompanying documentation. Microchip and its licensors 
00025  * retain all ownership and intellectual property rights in the 
00026  * accompanying software and in all derivatives hereto. 
00027  * 
00028  * This software and any accompanying information is for suggestion only. 
00029  * It does not modify Microchip's standard warranty for its products. You 
00030  * agree that you are solely responsible for testing the software and 
00031  * determining its suitability. Microchip has no obligation to modify, 
00032  * test, certify, or support the software. 
00033  * 
00034  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 
00035  * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED 
00036  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 
00037  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE, ITS INTERACTION WITH 
00038  * MICROCHIP'S PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY 
00039  * APPLICATION. 
00040  * 
00041  * IN NO EVENT, WILL MICROCHIP BE LIABLE, WHETHER IN CONTRACT, WARRANTY, 
00042  * TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), STRICT 
00043  * LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, 
00044  * SPECIAL, PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, 
00045  * FOR COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, 
00046  * HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY 
00047  * OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW, 
00048  * MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS 
00049  * SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID 
00050  * DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. 
00051  * 
00052  * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF 
00053  * THESE TERMS. 
00054  *************************************************************************/
00058 #include "mTouch.h"
00059 
00060 #if defined(MTOUCH_PROXIMITY_ENABLED)
00061     typedef struct
00062     {
00063         uint8_t         currentTime;
00064         uint16_t        deviation;              
00065         
00066         uint16_t        sortBuffer  [MTOUCH_PROX_BUFFER_SIZE];
00067         uint8_t         timeIndex   [MTOUCH_PROX_BUFFER_SIZE];
00068     } mTouch_Prox_Variables;
00069     
00070     
00071     
00072     // Local Variables
00073     mTouch_Prox_Variables   mTouch_proxVariables[MTOUCH_NUMBER_PROXIMITY];
00074     
00075     
00076     void            mTouch_ProxInit             (void);
00077     void            mTouch_ProxUpdateBaseline   (void);
00078     uint8_t         mTouch_ProxStateMachine     (void);
00079 
00080     
00081     #if MTOUCH_NUMBER_PROXIMITY > 1
00082     const uint8_t   mTouch_proxIndex    [MTOUCH_NUMBER_SENSORS] = MTOUCH_PROXINDEX_ARRAY_INIT;
00083     #endif
00084 
00085 void mTouch_ProxInit(void)
00086 {
00087     #if MTOUCH_NUMBER_PROXIMITY > 1
00088     for (int8_t j = MTOUCH_NUMBER_PROXIMITY-1; j >= 0; j--)
00089     {
00090         #define MTOUCH_PROX_tempIndex   j
00091     #else
00092         #define MTOUCH_PROX_tempIndex   0
00093     #endif
00094     
00095         mTouch_proxVariables[MTOUCH_PROX_tempIndex].currentTime = 0;
00096         mTouch_proxVariables[MTOUCH_PROX_tempIndex].deviation   = 0;
00097         
00098         for (int8_t i = MTOUCH_PROX_BUFFER_SIZE-1; i >= 0; i--)
00099         {
00100             mTouch_proxVariables[MTOUCH_PROX_tempIndex].sortBuffer[i] = 0;
00101             mTouch_proxVariables[MTOUCH_PROX_tempIndex].timeIndex[i]  = i;
00102         }  
00103         
00104     #if MTOUCH_NUMBER_PROXIMITY > 1
00105     }    
00106     #endif
00107 }    
00108 
00109 
00110 //================================================================================================
00111 //          _____                _         ____                _           _ _         ____                     _      
00112 //  _ __ __|_   _|__  _   _  ___| |__     |  _ \ _ __ _____  _(_)_ __ ___ (_) |_ _   _|  _ \  ___  ___ ___   __| | ___ 
00113 // | '_ ` _ \| |/ _ \| | | |/ __| '_ \    | |_) | '__/ _ \ \/ / | '_ ` _ \| | __| | | | | | |/ _ \/ __/ _ \ / _` |/ _ \
00114 // | | | | | | | (_) | |_| | (__| | | |   |  __/| | | (_) >  <| | | | | | | | |_| |_| | |_| |  __/ (_| (_) | (_| |  __/
00115 // |_| |_| |_|_|\___/ \__,_|\___|_| |_|___|_|   |_|  \___/_/\_\_|_| |_| |_|_|\__|\__, |____/ \___|\___\___/ \__,_|\___|
00116 //                                   |_____|                                     |___/  
00117 //================================================================================================
00118 
00119 
00120 uint8_t mTouch_ProxStateMachine(void)
00121 {    
00122 
00123     #if MTOUCH_NUMBER_PROXIMITY == 1
00124     
00125         #define     MTOUCH_PROX_time            mTouch_proxVariables[0].timeIndex
00126         #define     MTOUCH_PROX_value           mTouch_proxVariables[0].sortBuffer
00127         #define     MTOUCH_PROX_currentTime     mTouch_proxVariables[0].currentTime
00128         #define     MTOUCH_PROX_deviation       mTouch_proxVariables[0].deviation
00129         #define     MTOUCH_PROX_state           mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].state
00130         #define     MTOUCH_PROX_timer           mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].timer
00131         #define     MTOUCH_PROX_debounce        mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].debounce
00132         #define     MTOUCH_PROX_timeout         mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].timeout
00133         
00134     #else
00135 
00136         #define     MTOUCH_PROX_time            (*prox).timeIndex
00137         #define     MTOUCH_PROX_value           (*prox).sortBuffer
00138         #define     MTOUCH_PROX_currentTime     (*prox).currentTime
00139         #define     MTOUCH_PROX_deviation       (*prox).deviation
00140         #define     MTOUCH_PROX_state           (*sensor).state
00141         #define     MTOUCH_PROX_timer           (*sensor).timer
00142         #define     MTOUCH_PROX_debounce        (*sensor).debounce
00143         #define     MTOUCH_PROX_timeout         (*sensor).timeout
00144 
00145     mTouch_Prox_Variables*  prox            = &mTouch_proxVariables[mTouch_proxIndex[MTOUCH_CURRENTSCAN_VALUE]];
00146     mTouch_SensorVariables* sensor          = &mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE];
00147     
00148     #endif
00149     
00150     #if defined(MTOUCH_PROX_USE_32BIT_ACCUM)    
00151     uint32_t    mean        = 0;                // Stores the mean reading of the prox sensor
00152     #else
00153     uint16_t    mean        = 0;                // Stores the mean reading of the prox sensor
00154     #endif
00155     
00156     // Perform median sort and filter logic
00157     do
00158     {
00159         uint16_t    newValue    = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
00160         uint8_t     newTime     = MTOUCH_PROX_currentTime;
00161         uint8_t     i           = 0;  
00162         
00163         while (MTOUCH_PROX_time[i] != MTOUCH_PROX_currentTime)  // As long as we have not hit the time index that
00164         {                                                       //  will be replaced in the array, continue.
00165             if (MTOUCH_PROX_value[i] > newValue)                // If the value in the sorted array is greater
00166             {                                                   //  than the new value, drop the new value here
00167                 uint16_t tempValue  = MTOUCH_PROX_value[i];     //  and pick up the value in the array.
00168                 uint8_t tempTime    = MTOUCH_PROX_time[i];
00169                 
00170                 MTOUCH_PROX_value[i] = newValue;                //  Drop the new value
00171                 MTOUCH_PROX_time [i] = newTime;
00172                 
00173                 newValue            = tempValue;                //  Now we start shifting these values up through
00174                 newTime             = tempTime;                 //  the sorted list until we hit the time index
00175             }                                                   //  that is to be replaced.
00176             i++;
00177         }
00178         
00179         MTOUCH_PROX_value[i]    = newValue;         // Remove the value at the current time index
00180         MTOUCH_PROX_time[i]     = newTime;          //  and place the held value there.
00181         
00182         for (i++; i < MTOUCH_PROX_BUFFER_SIZE; i++)             // Now, loop through the values larger than
00183         {                                                       //  the one we just removed. 
00184             if (newValue > MTOUCH_PROX_value[i])                // If the value in the sorted array is less
00185             {                                                   //  than the new value, push the value in the
00186                 MTOUCH_PROX_value[i-1]  = MTOUCH_PROX_value[i]; //  array down one location, and move the new
00187                 MTOUCH_PROX_time [i-1]  = MTOUCH_PROX_time[i];  //  value up.
00188                 
00189                 MTOUCH_PROX_value[i]    = newValue;
00190                 MTOUCH_PROX_time[i]     = newTime;
00191             }
00192             else break;                                         // If the value in the sorted array is now 
00193         }                                                       //  greater than the new value, stop.
00194         
00195         if (++MTOUCH_PROX_currentTime >= MTOUCH_PROX_BUFFER_SIZE)   
00196         {                                                       // Increment the current-time index value and    
00197             MTOUCH_PROX_currentTime = 0;                        //  rollover, if necessary.
00198         }
00199         
00200     } while(0);
00201     
00202     // Sum the median values
00203     do                          
00204     {
00205     
00206     //
00207     //                      MTOUCH_PROX_REMOVE_EXTREME value vs. which array locations
00208     //                      are used to calculate the mean of the proximity sensor.
00209     //                      ==========================================================
00210     //  Buffer Index Value |0   1   2   3   4   5   6   7   8   9   10  11  12  13  14|
00211     //                     |==========================================================|
00212     //  Size             5 |1   2   -   2   1|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|
00213     //     of            9 |1   2   3   4   -   4   3   2   1|XXXXXXXXXXXXXXXXXXXXXXXX|
00214     //      Buffer      15 |1   2   3   4   5   6   7   -   7   6   5   4   3   2   1 |
00215     //                      ==========================================================
00216     //
00217         #if (MTOUCH_PROX_BUFFER_SIZE == 15) 
00218             #if     (MTOUCH_PROX_REMOVE_EXTREME == 1)
00219                 #define MTOUCH_MEAN_SCALE   13
00220             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 2)
00221                 #define MTOUCH_MEAN_SCALE   11
00222             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 3)
00223                 #define MTOUCH_MEAN_SCALE   9
00224             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 4)
00225                 #define MTOUCH_MEAN_SCALE   7
00226             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 5)
00227                 #define MTOUCH_MEAN_SCALE   5
00228             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 6)
00229                 #define MTOUCH_MEAN_SCALE   3
00230             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 7)
00231                 #define MTOUCH_MEAN_SCALE   1
00232             #endif
00233         #elif (MTOUCH_PROX_BUFFER_SIZE == 9) 
00234             #if     (MTOUCH_PROX_REMOVE_EXTREME == 1)
00235                 #define MTOUCH_MEAN_SCALE   7
00236             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 2)
00237                 #define MTOUCH_MEAN_SCALE   5
00238             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 3)
00239                 #define MTOUCH_MEAN_SCALE   3
00240             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 4)
00241                 #define MTOUCH_MEAN_SCALE   1
00242             #endif
00243         #elif (MTOUCH_PROX_BUFFER_SIZE == 5)
00244             #if     (MTOUCH_PROX_REMOVE_EXTREME == 1)
00245                 #define MTOUCH_MEAN_SCALE   3
00246             #elif   (MTOUCH_PROX_REMOVE_EXTREME == 2)
00247                 #define MTOUCH_MEAN_SCALE   1
00248             #endif
00249         #endif
00250         
00251         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 2)) || \
00252             ((MTOUCH_PROX_BUFFER_SIZE ==  9) && (MTOUCH_PROX_REMOVE_EXTREME < 2)) || \
00253             ((MTOUCH_PROX_BUFFER_SIZE ==  5) && (MTOUCH_PROX_REMOVE_EXTREME < 2))
00254         mean += MTOUCH_PROX_value[1];
00255         #endif
00256         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 3)) || \
00257             ((MTOUCH_PROX_BUFFER_SIZE ==  9) && (MTOUCH_PROX_REMOVE_EXTREME < 3)) || \
00258              (MTOUCH_PROX_BUFFER_SIZE ==  5)
00259         mean += MTOUCH_PROX_value[2];
00260         #endif
00261         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 4)) || \
00262             ((MTOUCH_PROX_BUFFER_SIZE ==  9) && (MTOUCH_PROX_REMOVE_EXTREME < 4)) || \
00263             ((MTOUCH_PROX_BUFFER_SIZE ==  5) && (MTOUCH_PROX_REMOVE_EXTREME < 2))
00264         mean += MTOUCH_PROX_value[3];
00265         #endif
00266         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 5)) || \
00267              (MTOUCH_PROX_BUFFER_SIZE ==  9)
00268         mean += MTOUCH_PROX_value[4];
00269         #endif
00270         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 6)) || \
00271             ((MTOUCH_PROX_BUFFER_SIZE ==  9) && (MTOUCH_PROX_REMOVE_EXTREME < 4))
00272         mean += MTOUCH_PROX_value[5];
00273         #endif
00274         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 7)) || \
00275             ((MTOUCH_PROX_BUFFER_SIZE ==  9) && (MTOUCH_PROX_REMOVE_EXTREME < 3))
00276         mean += MTOUCH_PROX_value[6];
00277         #endif
00278         #if  (MTOUCH_PROX_BUFFER_SIZE == 15)                                      || \
00279             ((MTOUCH_PROX_BUFFER_SIZE ==  9) && (MTOUCH_PROX_REMOVE_EXTREME < 2))
00280         mean += MTOUCH_PROX_value[7];
00281         #endif
00282         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 7))
00283         mean += MTOUCH_PROX_value[8];
00284         #endif
00285         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 6))
00286         mean += MTOUCH_PROX_value[9];
00287         #endif
00288         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 5))
00289         mean += MTOUCH_PROX_value[10];
00290         #endif
00291         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 4))
00292         mean += MTOUCH_PROX_value[11];
00293         #endif
00294         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 3))
00295         mean += MTOUCH_PROX_value[12];
00296         #endif
00297         #if ((MTOUCH_PROX_BUFFER_SIZE == 15) && (MTOUCH_PROX_REMOVE_EXTREME < 2))
00298         mean += MTOUCH_PROX_value[13];
00299         #endif
00300         
00301         // Now scale the mean value by the number of summed values.
00302         // These code snippets implement division by a fixed integer using
00303         // bit-shifts only. This keeps the execution time and program
00304         // memory requirements low. Since we'll be performing this to
00305         // every value the same way, the fact that the divisor is not
00306         // exactly the desired number is not a problem.
00307         do 
00308         {
00309             #if defined(MTOUCH_PROX_USE_32BIT_ACCUM)
00310             uint32_t tempCopy = 0;                        // Stores the mean reading of the prox sensor
00311             #else            
00312             uint16_t tempCopy = 0;                // Stores the mean reading of the prox sensor
00313             #endif
00314         #if     (MTOUCH_MEAN_SCALE == 13)       // Actual Divisor for this snippet: 12.96
00315             mean     >>= 4;     // 4
00316             tempCopy += mean;
00317             mean     >>= 3;     // 7
00318             tempCopy += mean;
00319             mean     >>= 1;     // 8
00320             tempCopy += mean;
00321             mean     >>= 1;     // 9
00322             tempCopy += mean;
00323             mean     >>= 1;     // 10
00324             mean     += tempCopy;
00325         #elif   (MTOUCH_MEAN_SCALE == 11)       // Actual Divisor for this snippet: 11.01
00326             mean     >>= 4;     // 4
00327             tempCopy += mean;
00328             mean     >>= 2;     // 6
00329             tempCopy += mean;
00330             mean     >>= 1;     // 7
00331             tempCopy += mean;
00332             mean     >>= 1;     // 8
00333             tempCopy += mean;
00334             mean     >>= 2;     // 10
00335             mean     += tempCopy;
00336         #elif   (MTOUCH_MEAN_SCALE == 9)        // Actual Divisor for this snippet: 8.98
00337             mean     >>= 4;     // 4
00338             tempCopy += mean;
00339             mean     >>= 1;     // 5
00340             tempCopy += mean;
00341             mean     >>= 1;     // 6
00342             tempCopy += mean;
00343             mean     >>= 3;     // 9
00344             mean     += tempCopy;
00345         #elif   (MTOUCH_MEAN_SCALE == 7)        // Actual Divisor for this snippet: 7.01
00346             mean     >>= 3;     // 3
00347             tempCopy += mean;
00348             mean     >>= 3;     // 6
00349             tempCopy += mean;
00350             mean     >>= 3;     // 9
00351             mean     += tempCopy;
00352         #elif   (MTOUCH_MEAN_SCALE == 5)        // Actual Divisor for this snippet: 4.995
00353             mean     >>= 3;     // 3
00354             tempCopy += mean;
00355             mean     >>= 1;     // 4
00356             tempCopy += mean;
00357             mean     >>= 3;     // 7
00358             tempCopy += mean;
00359             mean     >>= 1;     // 8
00360             tempCopy += mean;
00361             mean     >>= 2;     // 10
00362             mean     += tempCopy;
00363         #elif   (MTOUCH_MEAN_SCALE == 3)        // Actual Divisor for this snippet: 3.003
00364             mean     >>= 2;     // 2
00365             tempCopy += mean;
00366             mean     >>= 2;     // 4
00367             tempCopy += mean;
00368             mean     >>= 2;     // 6
00369             tempCopy += mean;
00370             mean     >>= 2;     // 8
00371             tempCopy += mean;
00372             mean     >>= 2;     // 10
00373             mean     += tempCopy;
00374         #endif
00375     } while (0);
00376     } while (0);
00377  
00378  
00379     // Now, with the new mean value, update the deviation amount.     
00380     //
00381     // NOTE: This code attempts to integrate the delta values over time. It can add distance
00382     //       to the proximity sensor, but has not been made easily configurable yet. Variable
00383     //       overflow is possible unless an experienced programmer can ensure it won't happen
00384     //       by adjusting the size of variables.
00385     //
00386     //    #define MTOUCH_PROX_DEV_GAIN    2
00387     //    MTOUCH_PROX_deviation  -= (uint16_t)(MTOUCH_PROX_deviation >> MTOUCH_PROX_DEV_GAIN); 
00388     //    MTOUCH_PROX_deviation  += mean - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00389     //    
00390     //    if ((mean - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE)) > 10)
00391     //    {
00392     //        MTOUCH_PROX_deviation  += mean - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00393     //    }
00394 
00395     // For now, treat the deviation variable as a simple 'delta' value for the state machine.
00396     MTOUCH_PROX_deviation = mean - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00397     if ((int16_t)MTOUCH_PROX_deviation < 0)
00398     {
00399         MTOUCH_PROX_deviation = 0;          // Delta value is not allowed to go negative.
00400     }
00401     
00402     // Update the raw data array for this sensor by storing the median-filtered value.
00403     mTouch_sensorData[MTOUCH_CURRENTSCAN_VALUE] = mean;
00404     
00405     // Proximity State Machine
00406     //
00407     // NOTE: You may notice some strong similarities between this state machine and the
00408     //       regular button state machine. Long term goal is to combine these states and have
00409     //       unique code only for the pre-state-machine logic.
00410     switch(MTOUCH_PROX_state)
00411     {
00412         case MTOUCH_PROX_INITIALIZING:
00413             if (--(MTOUCH_PROX_timer) <= 0)     // If initialization delay has elapsed
00414             {                                   // NOTE: This delay determined by
00415                                                 //       MTOUCH_POWER_UP_SAMPLES
00416                 //-----------------------------
00417                 // State Transition -> Released
00418                 //-----------------------------
00419                 MTOUCH_PROX_state               = MTOUCH_PROX_NOT_ACTIVATED;    // Sensor is stable. Buffer has been filled.
00420                 #if defined(MTOUCH_STATE_DEBOUNCE)                              // Initialize sensor's debounce counter
00421                 MTOUCH_PROX_debounce            = MTOUCH_pDEBOUNCE_VALUE;   
00422                 #endif
00423                 
00424                 MTOUCH_PROX_deviation           = 0;                    // Reset the accumulated deviation
00425                 
00426                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] = mean;
00427             }
00428             else                                                        // If initialization delay is not complete
00429             {
00430                 mTouch_state.areInitialized     = 0;                    // Disable data transfer until initialized
00431                 
00432                 #if MTOUCH_NUM_MODES > 1
00433                 mTouch_state.allReleased        = 0;                    // Clear the all-sensors-are-released flag.
00434                 #endif
00435             }
00436             return 0;
00437             
00438         case MTOUCH_PROX_NOT_ACTIVATED:
00439         
00440             // If reading has crossed the press threshold
00441             if(MTOUCH_PROX_deviation > (int16_t)mTouch_GetPressThreshold(MTOUCH_CURRENTSCAN_VALUE))  
00442             {
00443                 #if defined(MTOUCH_STATE_DEBOUNCE)
00444                 if (MTOUCH_PROX_debounce == 0)                          // Check the debounce counter
00445                 {                                                       
00446                     MTOUCH_PROX_debounce  = MTOUCH_rDEBOUNCE_VALUE;     // Initialize the pressed state's debounce variable
00447                 #endif
00448                 
00449                     //-----------------------------
00450                     // State Transition -> Pressed
00451                     //-----------------------------
00452                     MTOUCH_PROX_state     = MTOUCH_PROX_ACTIVATED;      // Sensor is now pressed
00453                     
00454                     #if defined(MTOUCH_BUTTON_TIMEOUT)                  // If the press timer is enabled
00455                     #if MTOUCH_BUTTON_TIMEOUT > 0
00456                     MTOUCH_PROX_timer    = MTOUCH_pTIMEOUT_VALUE;       // Initialize the press timer counter
00457                     #endif                                              // NOTE: This delay determined by
00458                     #endif                                              //       MTOUCH_BUTTON_TIMEOUT
00459                     
00460                     #if MTOUCH_NUM_MODES > 1
00461                     mTouch_state.allReleased = 0;                       // Clear the all-sensors-are-released flag.
00462                     #endif
00463                     
00464                     mTouch_state.buttonStateChange = 1;                 // Set flag notifying that a change has occured
00465                                                                         // to a sensor's 'button' state in this decode
00466                     
00467                 #if defined(MTOUCH_STATE_DEBOUNCE)
00468                 }
00469                 else
00470                 {
00471                     MTOUCH_PROX_debounce--;
00472                 }
00473                 #endif
00474             }
00475             else                                                        // If the reading has not crossed the press threshold
00476             {
00477                 #if defined(MTOUCH_STATE_DEBOUNCE)
00478                 MTOUCH_PROX_debounce      = MTOUCH_pDEBOUNCE_VALUE;     // Reset the debounce variable   
00479                 #endif
00480                 
00481                 // NOTE: This is the only state (MTOUCH_RELEASED) and condition 
00482                 //       (delta < PressThreshold) where the average is able to
00483                 //       be updated. Also note how we only use the reading to
00484                 //       update the baseline after we have verified it is not
00485                 //       crossing any thresholds.
00486                 return 1; 
00487             }
00488             return 0; 
00489             
00490         case MTOUCH_PROX_ACTIVATED:
00491             #if defined(MTOUCH_BUTTON_TIMEOUT)                      // (Only if the press timer has been enabled)
00492             #if MTOUCH_BUTTON_TIMEOUT > 0   
00493 
00494             if (--MTOUCH_PROX_timer <= 0)                           // Decrement the press timer counter
00495             {     
00496                 //-----------------------------
00497                 // State Transition -> Released
00498                 //-----------------------------
00499                 MTOUCH_PROX_state     = MTOUCH_PROX_NOT_ACTIVATED;  // Sensor is now released
00500                 MTOUCH_PROX_timeout   = 1;                          // Signal that a timeout has occurred
00501                 
00502                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE]   = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
00503                 
00504                 #if defined(MTOUCH_STATE_DEBOUNCE)
00505                 MTOUCH_PROX_debounce = MTOUCH_pDEBOUNCE_VALUE;      // Initialize the debounce counter for the MTOUCH_PRESSED state
00506                 #endif                                              // and check if it has reached 0.
00507                 
00508                 mTouch_state.buttonStateChange = 1;                 // Set flag notifying that a change has occured
00509                                                                     // to a sensor's 'button' state in this decode
00510                 break;
00511             }
00512             
00513             #endif
00514             #endif
00515             
00516             // Look only at the threshold to determine release.
00517             if(MTOUCH_PROX_deviation < (int16_t)mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE))   
00518             {                                                       // If the reading has crossed the release threshold
00519             
00520                 #if defined(MTOUCH_STATE_DEBOUNCE)
00521                 if (MTOUCH_PROX_debounce == 0)                      // Check if the debounce counter has reached 0.
00522                 {                                               
00523                     MTOUCH_PROX_debounce = MTOUCH_pDEBOUNCE_VALUE;
00524                 #endif
00525                 
00526                     //-----------------------------
00527                     // State Transition -> Released
00528                     //-----------------------------
00529                     MTOUCH_PROX_state = MTOUCH_PROX_NOT_ACTIVATED;  // Sensor is now released
00530                     
00531                     mTouch_state.buttonStateChange = 1;             // Set flag notifying that a change has occured
00532                                                                     // to a sensor's 'button' state in this decode
00533                     
00534                 #if defined(MTOUCH_STATE_DEBOUNCE)
00535                 } else {
00536                 
00537                     MTOUCH_PROX_debounce--;
00538                 
00539                     #if MTOUCH_NUM_MODES > 1
00540                     mTouch_state.allReleased = 0;                   // Clear the all-sensors-are-released flag.
00541                     #endif
00542                     
00543                 }
00544                 #endif    
00545                 
00546             }
00547             else                                                    // If the reading has not crossed the release threshold
00548             {
00549             
00550                 #if defined(MTOUCH_STATE_DEBOUNCE)
00551                 MTOUCH_PROX_debounce  = MTOUCH_rDEBOUNCE_VALUE;     // Reset the debounce counter
00552                 #endif                    
00553                 #if MTOUCH_NUM_MODES > 1
00554                 mTouch_state.allReleased = 0;                       // Clear the all-sensors-are-released flag.
00555                 #endif
00556                 
00557             }
00558             return 0;
00559     }
00560     return 0;
00561 }
00562 
00563 
00564 #endif

mTouch Framework v2.1 documentation by  Click here to visit our website at www.microchip.com