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

mTouch.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.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 Framework filtering and decoding modules
00014  *                   - No application code should be implemented in this
00015  *                     or any other mTouch framework file. This will allow
00016  *                     for better customer support and easier upgrades to
00017  *                     later firmware versions. Use the main.c and user-
00018  *                     generated files to implement your application.
00019  *                   - See the documentation located in the docs/ folder
00020  *                     for a more information about how the framework is
00021  *                     implemented.
00022  *************************************************************************/
00023 /**************************************************************************
00024  * MICROCHIP SOFTWARE NOTICE AND DISCLAIMER: You may use this software, and 
00025  * any derivatives created by any person or entity by or on your behalf, 
00026  * exclusively with Microchip's products in accordance with applicable
00027  * software license terms and conditions, a copy of which is provided for
00028  * your referencein accompanying documentation. Microchip and its licensors 
00029  * retain all ownership and intellectual property rights in the 
00030  * accompanying software and in all derivatives hereto. 
00031  * 
00032  * This software and any accompanying information is for suggestion only. 
00033  * It does not modify Microchip's standard warranty for its products. You 
00034  * agree that you are solely responsible for testing the software and 
00035  * determining its suitability. Microchip has no obligation to modify, 
00036  * test, certify, or support the software. 
00037  * 
00038  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 
00039  * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED 
00040  * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 
00041  * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE, ITS INTERACTION WITH 
00042  * MICROCHIP'S PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY 
00043  * APPLICATION. 
00044  * 
00045  * IN NO EVENT, WILL MICROCHIP BE LIABLE, WHETHER IN CONTRACT, WARRANTY, 
00046  * TORT (INCLUDING NEGLIGENCE OR BREACH OF STATUTORY DUTY), STRICT 
00047  * LIABILITY, INDEMNITY, CONTRIBUTION, OR OTHERWISE, FOR ANY INDIRECT, 
00048  * SPECIAL, PUNITIVE, EXEMPLARY, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, 
00049  * FOR COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, 
00050  * HOWSOEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY 
00051  * OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWABLE BY LAW, 
00052  * MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS 
00053  * SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID 
00054  * DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. 
00055  * 
00056  * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF 
00057  * THESE TERMS. 
00058  *************************************************************************/
00064 //========================================================================
00065 // INCLUDES 
00066 //========================================================================
00067 #include "mTouch.h"
00068   
00069     
00070 //========================================================================
00071 // GLOBAL VARIABLES
00072 //========================================================================
00083 
00084     mTouch_State                mTouch_state;           // Collection of single-bit flags    
00085     mTouch_StateVariables       mTouch_stateVars;       // Sensor variables and other counters
00086     uint16_t                    mTouch_average          [MTOUCH_NUMBER_SENSORS];    
00087 
00088     #if defined(MCOMM_ENABLED)
00089     #if (MTOUCH_NUMBER_SENSORS <= 8)
00090         uint8_t                 mTouch_stateMask;          
00091     #elif (MTOUCH_NUMBER_SENSORS <= 16)
00092         uint16_t                mTouch_stateMask;
00093     #elif (MTOUCH_NUMBER_SENSORS <= 24)
00094         uint24_t                mTouch_stateMask;
00095     #elif (MTOUCH_NUMBER_SENSORS <= 32)
00096         uint32_t                mTouch_stateMask;  
00097     #endif
00098     #endif
00099 
00100     #if defined(MTOUCH_SLIDERS_ENABLED) || defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00101         uint16_t                mTouch_deltas           [MTOUCH_NUMBER_SENSORS];
00102     #endif
00103     
00104     #if defined(MTOUCH_TOGGLE_ENABLED)
00105     #if (MTOUCH_NUMBER_SENSORS <= 8)
00106         uint8_t                 mTouch_toggle;          
00107     #elif (MTOUCH_NUMBER_SENSORS <= 16)
00108         uint16_t                mTouch_toggle;
00109     #elif (MTOUCH_NUMBER_SENSORS <= 24)
00110         uint24_t                mTouch_toggle;
00111     #elif (MTOUCH_NUMBER_SENSORS <= 32)
00112         uint32_t                mTouch_toggle;  
00113     #endif
00114     #endif
00115 
00116     #if defined(MTOUCH_MATRIX_ENABLED) && defined(MTOUCH_MOST_PRESSED_ONLY)
00117     
00118         #define MTOUCH_MOST_PRESSED_INDEX       0
00119         #define MTOUCH_MATRIX_ROW_INDEX         1
00120         #define MTOUCH_MATRIX_COL_INDEX         2
00121         
00122         #define MTOUCH_MOSTPRESSED_ARRAYSIZE    3
00123         
00124         uint8_t                         mTouch_matrix;                  // Output value
00125         uint8_t                         mTouch_matrix_latch;            // Latch variable used to compare the new matrix value with the old
00126         mTouch_MostPressedData*         mTouch_mostPressed;             // Pointer
00127         mTouch_MostPressedData          mTouch_mostPressedData [3];     // Stores max-pressed sensor data
00128         const mTouch_MostPressedIndex   mTouch_mostPressedIndex[3] =    {   
00129                                                                             { MTOUCH_SENSOR_START,          MTOUCH_SENSOR_END        },
00130                                                                             { MTOUCH_MATRIX_ROW_START,      MTOUCH_MATRIX_ROW_END    },
00131                                                                             { MTOUCH_MATRIX_COLUMN_START,   MTOUCH_MATRIX_COLUMN_END }
00132                                                                         };
00133     #elif defined(MTOUCH_MATRIX_ENABLED)
00134     
00135         #define MTOUCH_MATRIX_ROW_INDEX         0
00136         #define MTOUCH_MATRIX_COL_INDEX         1
00137         #define MTOUCH_DUMMY_MOST_PRESSED_INDEX 2
00138         
00139         #define MTOUCH_MOSTPRESSED_ARRAYSIZE    2
00140         uint8_t                         mTouch_matrix;                  // Output value
00141         uint8_t                         mTouch_matrix_latch;            // Latch variable used to compare the new matrix value with the old
00142         mTouch_MostPressedData*         mTouch_mostPressed;             // Pointer
00143         mTouch_MostPressedData          mTouch_mostPressedData [3];     // Stores max-pressed sensor data
00144                                                                         // NOTE: The 3rd (index 2) array location is a dummy location used to
00145                                                                         // allow all non-matrix sensors to be implemented normally, without
00146                                                                         // a most-pressed algorithm limiting the number that can be
00147                                                                         // active at one time.
00148         
00149         const mTouch_MostPressedIndex   mTouch_mostPressedIndex[2] =    {   
00150                                                                             { MTOUCH_MATRIX_ROW_START,      MTOUCH_MATRIX_ROW_END    },
00151                                                                             { MTOUCH_MATRIX_COLUMN_START,   MTOUCH_MATRIX_COLUMN_END }
00152                                                                         };
00153     
00154     #elif defined(MTOUCH_MOST_PRESSED_ONLY)
00155     
00156         #define MTOUCH_MOST_PRESSED_INDEX       0
00157         #define MTOUCH_MOSTPRESSED_ARRAYSIZE    1
00158         
00159         mTouch_MostPressedData*         mTouch_mostPressed;             // Pointer
00160         mTouch_MostPressedData          mTouch_mostPressedData [1];     // Stores max-pressed sensor data
00161         const mTouch_MostPressedIndex   mTouch_mostPressedIndex[1] =    {   { 0,                            MTOUCH_NUMBER_SENSORS-1 } };
00162         
00163     #endif
00164     
00165 
00167 
00168 //========================================================================
00169 // CONSTANT ARRAYS
00170 //========================================================================
00180 
00181     #if !defined(MTOUCH_EEPROM_ENABLED)
00182     // If EEPROM is enabled, the thresholds will be stored in EEPROM. 
00183     //  Otherwise we need to define a constant array for them.
00184     const uint16_t  mTouch_pressThreshold       [MTOUCH_NUMBER_SENSORS] = PRESS_THRESHOLD_INIT;     
00185     const uint16_t  mTouch_releaseThreshold     [MTOUCH_NUMBER_SENSORS] = RELEASE_THRESHOLD_INIT;   
00186     #endif
00187 
00189 
00190 //========================================================================
00191 // FUNCTION PROTOTYPES                
00192 //========================================================================
00193 void mTouch_Init                    (void);
00194 void mTouch_Decode                  (void);
00195 void mTouch_UpdateBaseline          (void);
00196 void mTouch_ChangeState             (void);
00197 
00198 uint8_t mTouch_ButtonStateMachine   (void);
00199 
00200 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00201 void mTouch_FindMostPressed         (uint8_t);
00202 #endif
00203 #if defined(MTOUCH_MATRIX_ENABLED)
00204 void mTouch_UpdateMatrixOutput      (void);
00205 #endif
00206 #if defined(MTOUCH_ERROR_DETECTION_ENABLED)
00207 void mTouch_ErrorDetect             (void);
00208 #endif
00209 
00210 
00211 #if (MTOUCH_NUMBER_SENSORS > 1) && ((defined(MTOUCH_PROXIMITY_ENABLED) && (MTOUCH_NUMBER_SENSORS != MTOUCH_NUMBER_PROXIMITY)))
00212 uint8_t (*const mTouch_StateMachine [MTOUCH_NUMBER_SENSORS])(void) = {                              // Constant array of function pointers
00213                                                                         MTOUCH_STATEMACHINE_0 
00214                                                                         MTOUCH_STATEMACHINE_1 
00215                                                                         MTOUCH_STATEMACHINE_2 
00216                                                                         MTOUCH_STATEMACHINE_3 
00217                                                                         MTOUCH_STATEMACHINE_4 
00218                                                                         MTOUCH_STATEMACHINE_5 
00219                                                                         MTOUCH_STATEMACHINE_6 
00220                                                                         MTOUCH_STATEMACHINE_7 
00221                                                                         MTOUCH_STATEMACHINE_8 
00222                                                                         MTOUCH_STATEMACHINE_9 
00223                                                                         MTOUCH_STATEMACHINE_10 
00224                                                                         MTOUCH_STATEMACHINE_11 
00225                                                                         MTOUCH_STATEMACHINE_12 
00226                                                                         MTOUCH_STATEMACHINE_13 
00227                                                                         MTOUCH_STATEMACHINE_14 
00228                                                                         MTOUCH_STATEMACHINE_15 
00229                                                                         MTOUCH_STATEMACHINE_16 
00230                                                                         MTOUCH_STATEMACHINE_17 
00231                                                                         MTOUCH_STATEMACHINE_18 
00232                                                                         MTOUCH_STATEMACHINE_19 
00233                                                                         MTOUCH_STATEMACHINE_20 
00234                                                                         MTOUCH_STATEMACHINE_21 
00235                                                                         MTOUCH_STATEMACHINE_22 
00236                                                                         MTOUCH_STATEMACHINE_23 
00237                                                                         MTOUCH_STATEMACHINE_24 
00238                                                                         MTOUCH_STATEMACHINE_25 
00239                                                                         MTOUCH_STATEMACHINE_26 
00240                                                                         MTOUCH_STATEMACHINE_27 
00241                                                                         MTOUCH_STATEMACHINE_28 
00242                                                                         MTOUCH_STATEMACHINE_29 
00243                                                                      };
00244 #endif
00245 
00246 //================================================================================================
00247 //          _____                _         ___       _ _   
00248 //  _ __ __|_   _|__  _   _  ___| |__     |_ _|_ __ (_) |_ 
00249 // | '_ ` _ \| |/ _ \| | | |/ __| '_ \     | || '_ \| | __|
00250 // | | | | | | | (_) | |_| | (__| | | |    | || | | | | |_ 
00251 // |_| |_| |_|_|\___/ \__,_|\___|_| |_|___|___|_| |_|_|\__|
00252 //                                   |_____|   
00253 //================================================================================================
00258 void mTouch_Init(void)
00259 {
00260     #if defined(MTOUCH_EEPROM_ENABLED)
00261     mTouch_EEPROM_Init();
00262     #endif
00263     
00267     #if (MTOUCH_NUMBER_SENSORS == 1)
00268         #define sensorIndex 0       // If only one sensor is implemented, don't implement
00269                                     // the 'for' loop, just replace all 'sensorIndex'
00270                                     // text with the value '0'.
00271     #else
00272     // TIP: By declaring the index variable within the 'for' instead of before it, the
00273     //      compiler is able to recycle that register in memory immediately after the
00274     //      loop's execution and use it for something else. Limiting the life-span of 
00275     //      temporary variables such as this helps limit the total amount of temporary 
00276     //      variable space that is required.
00277     for (int8_t sensorIndex = MTOUCH_NUMBER_SENSORS-1; sensorIndex >= 0; sensorIndex--)
00278     {
00279     #endif
00280     
00281         mTouch_AcquisitionData* acqData     = &mTouch_acqData[sensorIndex];
00282         (*acqData).result.v &= 0x000F;                                            
00283         (*acqData).result.v |= (uint16_t)(0x47F << 4);  
00284         
00285         mTouch_stateVars.sensor[sensorIndex].state           = MTOUCH_INITIALIZING;
00286         #if defined(MTOUCH_BUTTON_TIMEOUT) && (MTOUCH_BUTTON_TIMEOUT > 0)   // (Only if the press timer has been enabled)
00287         mTouch_stateVars.sensor[sensorIndex].timeout         = 0;
00288         #endif
00289         mTouch_stateVars.sensor[sensorIndex].timer           = MTOUCH_POWER_UP_SAMPLES;
00290         mTouch_stateVars.sensor[sensorIndex].baselineCount   = MTOUCH_BASELINE_RATE_VALUE;
00291         #if defined(MTOUCH_STATE_DEBOUNCE)
00292         mTouch_stateVars.sensor[sensorIndex].debounce        = 0;
00293         #endif
00294         
00295         mTouch_average      [sensorIndex] = 0;
00296         #if defined(MTOUCH_SLIDERS_ENABLED) || defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00297         mTouch_deltas       [sensorIndex] = 0;
00298         #endif
00299         
00300     #if (MTOUCH_NUMBER_SENSORS == 1)
00301         #undef sensorIndex          // Turn off the text replacement on 'sensorIndex'
00302     #else
00303     }
00304     #endif
00305         
00306     #if defined(MTOUCH_TOGGLE_ENABLED)
00307     mTouch_toggle = 0;
00308     #endif
00309     
00311     #if defined(MTOUCH_SLIDERS_ENABLED) 
00312         #if (MTOUCH_NUMBER_OF_SLIDERS == 1)
00313         mTouch_slider[0] = 0;
00314         #elif (MTOUCH_NUMBER_OF_SLIDERS == 2)
00315         mTouch_slider[0] = 0;
00316         mTouch_slider[1] = 0;
00317         #else
00318         for (int8_t i = MTOUCH_NUMBER_OF_SLIDERS-1; i >= 0; i--)
00319         {
00320             mTouch_slider       [i] = 0;
00321         }
00322         #endif
00323     #endif
00324     
00326     #if MTOUCH_NUM_MODES > 1
00327     mTouch_state.skippedDecode      = 0;
00328     mTouch_state.allReleased        = 0;
00329     mTouch_state.justChanged        = 0;
00330     #endif
00331     
00332     mTouch_state.dataReady          = 0;
00333     mTouch_state.areInitialized     = 0; 
00334     
00336     #if defined(MCOMM_ENABLED)
00337     mTouch_stateMask                = 0;
00338     #endif
00339     
00340     #if defined(MTOUCH_ACTIVE_RUNS_AS_MAINLOOP)
00341     mTouch_state.isrServiced        = 0;
00342     mTouch_state.isRepeatScan       = 0;
00343     #endif
00344     
00346     #if defined(CVD_GUARD_DACOUT_ENABLED)
00347     PIC_DACOUT_LAT_C                = 0;
00348     PIC_DACOUT_TRIS_C               = 0;    
00349     #endif
00350     
00351     #if defined(MTOUCH_PROXIMITY_ENABLED)
00352     mTouch_ProxInit();
00353     #endif
00354     
00355     #if MTOUCH_NUM_MODES > 1
00356     mTouch_ChangeMode(0);
00357     #else
00358     mTouch_ChangeMode();
00359     #endif
00360     
00362     MTOUCH_ADC_INIT();
00363     
00365     #if MTOUCH_ISR_TIMER != 0
00366     PEIE = 1;
00367     #endif
00368     mTouch_EnableScanning();
00370 
00371 }
00372 
00373 
00374 //================================================================================================
00375 //          _____                _         ____                     _      
00376 //  _ __ __|_   _|__  _   _  ___| |__     |  _ \  ___  ___ ___   __| | ___ 
00377 // | '_ ` _ \| |/ _ \| | | |/ __| '_ \    | | | |/ _ \/ __/ _ \ / _` |/ _ \
00378 // | | | | | | | (_) | |_| | (__| | | |   | |_| |  __/ (_| (_) | (_| |  __/
00379 // |_| |_| |_|_|\___/ \__,_|\___|_| |_|___|____/ \___|\___\___/ \__,_|\___|
00380 //                                   |_____|  
00381 //================================================================================================
00391 void mTouch_Decode(void)
00392 {
00393     
00394     // Disable current mTouch scanning while this is in process. This helps guarantee stack
00395     // overflows will not occur on 12F cores while also ensuring the raw data array will not
00396     // change mid-decode.
00397     mTouch_DisableScanning();
00398     
00399     // Initialize mTouch decode state flags used to check the state of all sensors at once.
00400     mTouch_state.areInitialized     = 1;
00401     mTouch_state.buttonStateChange  = 0;
00402     #if MTOUCH_NUM_MODES > 1
00403     mTouch_state.allReleased        = 1;
00404     #endif
00405     
00406     #if MTOUCH_NUM_MODES > 1
00407     if (mTouch_state.justChanged == 1)     // Trash the first sample on wake-up.
00408     {
00409         mTouch_state.justChanged = 0;
00410         mTouch_state.skippedDecode = 1;
00411         mTouch_EnableScanning();
00412         return;                         
00413     }
00414     mTouch_state.skippedDecode = 0;
00415     #endif
00416     
00417     // Initialize the index value
00418     #if MTOUCH_NUM_MODES > 1
00419     mTouch_currentScan      = mTouch_mode[mTouch_modeIndex];
00420     #else
00421     #if MTOUCH_NUMBER_SENSORS > 1
00422     mTouch_currentScan      = 0;
00423     #endif
00424     #endif
00425     
00426     // Populate delta array
00427     #if defined(MTOUCH_REQUIRES_DELTA_ARRAY)
00428     do
00429     {
00436         int16_t delta = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE) - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00437         
00438         if (delta < 0)  // Shifts should increase. Setting to 0 helps to prevent triggering
00439         {               // on negative spikes.
00440             delta = 0;  
00441         }
00442         mTouch_deltas[MTOUCH_CURRENTSCAN_VALUE] = (uint16_t) delta;
00443         
00444         mTouch_currentScan++;
00445     } while (MTOUCH_CURRENTSCAN_VALUE != MTOUCH_NUMBER_SENSORS);
00446     #endif
00447     
00448     // MATRIX Variable Service
00449     #if defined(MTOUCH_MATRIX_ENABLED)
00450 
00451     #if MTOUCH_NUM_MODES > 1    
00452         // TODO: If modes are enabled, check flag to see if this mode is decoding the matrix
00453         // RIGHT NOW: Matrix is decoded during all states.
00454     #endif
00455     
00456     mTouch_FindMostPressed(MTOUCH_MATRIX_ROW_INDEX);
00457     mTouch_FindMostPressed(MTOUCH_MATRIX_COL_INDEX);
00458     #endif
00459     
00460     // MOST-PRESSED Variable Service
00461     #if defined(MTOUCH_MOST_PRESSED_ONLY) && !defined(MTOUCH_MATRIX_SENSORS_ONLY)
00462     
00463     #if MTOUCH_NUM_MODES > 1    
00464         // TODO: If modes are enabled, check flag to see if this mode is decoding most-pressed
00465         // RIGHT NOW: Matrix is decoded during all states.
00466     #endif
00467     
00468     mTouch_FindMostPressed(MTOUCH_MOST_PRESSED_INDEX);
00469     #endif
00470     
00471     //
00472     // Decode
00473     //
00474     #if MTOUCH_NUM_MODES > 1
00475     mTouch_currentScan      = mTouch_mode[mTouch_modeIndex];
00476     #else
00477     #if MTOUCH_NUMBER_SENSORS > 1
00478     mTouch_currentScan      = 0;
00479     #endif
00480     #endif
00481     
00482     #if MTOUCH_NUMBER_SENSORS > 1
00483     do
00484     {    
00485         #if defined(MTOUCH_PROXIMITY_ENABLED) && (MTOUCH_NUMBER_SENSORS != MTOUCH_NUMBER_PROXIMITY)
00486         if (mTouch_StateMachine[MTOUCH_CURRENTSCAN_VALUE]())
00487         #elif defined(MTOUCH_PROXIMITY_ENABLED)
00488         if (mTouch_ProxStateMachine())
00489         #else
00490         if (mTouch_ButtonStateMachine())
00491         #endif
00492     #else
00493         if ((*MTOUCH_STATEMACHINE_0)())
00494     #endif
00495         {
00496             mTouch_UpdateBaseline();
00497         }
00498         
00499     #if MTOUCH_NUMBER_SENSORS > 1
00500         mTouch_currentScan++;
00501     } while (MTOUCH_CURRENTSCAN_VALUE != MTOUCH_NUMBER_SENSORS);
00502     #endif
00503     
00504     //
00505     // Update structured outputs:
00506     //  Sliders, Wheels, Matrix, State mask
00507     //
00512     #if defined(MTOUCH_SLIDERS_ENABLED)  
00513     mTouch_SliderDecode();
00514     #endif
00515     
00516     #if defined(MTOUCH_MATRIX_ENABLED)  
00517     mTouch_UpdateMatrixOutput();
00518     #endif
00519     
00520     #if defined(MCOMM_ENABLED)
00521     if (mTouch_state.buttonStateChange)             // Update the mTouch state mask variable with its new value
00522     {
00523         mTouch_stateMask = 0;
00524         
00525         #if MTOUCH_NUMBER_SENSORS > 1
00526         for (int8_t i = MTOUCH_NUMBER_SENSORS-1; i >= 0; i--)
00527         {
00528             if (mTouch_GetButtonState(i) == MTOUCH_PRESSED)
00529             {
00530                 mTouch_stateMask |= 1 << i;
00531             }
00532         }
00533         #else
00534         if (mTouch_GetButtonState(0) == MTOUCH_PRESSED)
00535             mTouch_stateMask = 1;
00536         #endif
00537     }
00538     #endif
00539 
00544     #if defined(MCOMM_ENABLED) && defined(MCOMM_UART_IMPLEMENTED)
00545     if (mTouch_state.areInitialized)                // Only trasmit data if all sensors are initialized
00546     {
00547     #if MTOUCH_NUM_MODES > 1   
00548         if (mTouch_state.change == 0)               // If the system is not going to be changing modes right now,
00549         {
00550     #endif
00551     
00552         #if defined(MCOMM_TWO_WAY_ENABLED) && defined(MCOMM_ENABLE_STREAM)
00553             mComm_streamConfig.go = 1;
00554         #endif
00555     
00556             mComm_Service();                        // Transmit data packet using mTouch Communication module
00557             
00558     #if MTOUCH_NUM_MODES > 1   
00559         }
00560     #endif
00561     }
00562     #endif
00563     
00564     #if MTOUCH_NUM_MODES > 1
00565     mTouch_ChangeMode(mTouch_modeIndex);
00566     #else
00567     mTouch_ChangeMode();
00568     #endif
00569     
00573     mTouch_EnableScanning();                // Enable the timer's interrupt
00574     
00575 }
00576 
00577 
00578 
00579 uint8_t mTouch_ButtonStateMachine(void)
00580 {
00581     #if MTOUCH_NUMBER_SENSORS == 1
00582     
00583         #define     MTOUCH_SENSOR_state           mTouch_stateVars.sensor[0].state
00584         #define     MTOUCH_SENSOR_timer           mTouch_stateVars.sensor[0].timer
00585         #define     MTOUCH_SENSOR_debounce        mTouch_stateVars.sensor[0].debounce
00586         #define     MTOUCH_SENSOR_timeout         mTouch_stateVars.sensor[0].timeout
00587         
00588     #else
00589 
00590         #define     MTOUCH_SENSOR_state           (*sensor).state
00591         #define     MTOUCH_SENSOR_timer           (*sensor).timer
00592         #define     MTOUCH_SENSOR_debounce        (*sensor).debounce
00593         #define     MTOUCH_SENSOR_timeout         (*sensor).timeout
00594 
00595     mTouch_SensorVariables* sensor          = &mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE];
00596     
00597     #endif
00598 
00599         
00600         #if defined(MTOUCH_REQUIRES_DELTA_ARRAY)
00601         uint16_t delta = mTouch_deltas[MTOUCH_CURRENTSCAN_VALUE];
00602         #else
00603         uint16_t delta = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE) - mTouch_GetAverage(MTOUCH_CURRENTSCAN_VALUE);
00604         
00605         if ((int16_t) delta < 0)    // Shifts should increase. Setting to 0 helps to prevent triggering
00606         {                           // on negative spikes.
00607             delta = 0;  
00608         }
00609         #endif
00610 
00611         
00612         #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)  
00613         #if defined(MTOUCH_MATRIX_ENABLED)                  // If matrix is enabled,
00614         #if !defined(MTOUCH_MATRIX_SENSORS_ONLY)            // and there are more than just row and column sensors,
00615         #if defined(MTOUCH_MATRIX_DEFS_AT_START)            // and the matrix row/columns come BEFORE normal sensors...
00616         if (MTOUCH_CURRENTSCAN_VALUE < MTOUCH_SENSOR_START) //      Is this sensor part of the matrix?
00617         #else                                               // and the matrix row/columns come AFTER normal sensors...
00618         if (MTOUCH_CURRENTSCAN_VALUE > MTOUCH_SENSOR_END)   //      Is this sensor part of the matrix?
00619         #endif
00620         {
00621         #endif                                                          // If this sensor is part of the matrix,
00622             #if defined(MTOUCH_MATRIX_ROWS_FIRST)                       // and rows are defined first...
00623             if (MTOUCH_CURRENTSCAN_VALUE < MTOUCH_MATRIX_COLUMN_START)  //      Is this sensor a row?
00624             #else                                                       // and columns are defined first...
00625             if (MTOUCH_CURRENTSCAN_VALUE > MTOUCH_MATRIX_COLUMN_END)    //      Is this sensor a row?
00626             #endif
00627             {
00628                 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MATRIX_ROW_INDEX];  // Sensor is a matrix row.
00629             }
00630             else                                        
00631             {
00632                 mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MATRIX_COL_INDEX];  // Sensor is a matrix column.
00633             }
00634         #if !defined(MTOUCH_MATRIX_SENSORS_ONLY)        // If there are other sensors besides those implementing
00635         }                                               // the matrix...
00636         else
00637         {   
00638             #if defined(MTOUCH_MOST_PRESSED_ONLY)
00639             mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MOST_PRESSED_INDEX];
00640             #else
00641             
00642             // If matrix is enabled, but the non-matrix sensors are NOT supposed to be decoded
00643             // in the 'most pressed' algorithm (ie. if all non-matrix sensors can be pressed at
00644             // the same time), then use the dummy location of the mostPressed structure array 
00645             // to store this sensor's data as if it were the most-pressed of its group. 
00646             mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_DUMMY_MOST_PRESSED_INDEX];
00647             (*mTouch_mostPressed).maxIndex = MTOUCH_CURRENTSCAN_VALUE;
00648             (*mTouch_mostPressed).maxValue = delta;
00649             #endif
00650         }
00651         #endif
00652         #else   // If there isn't a matrix being implemented, most-pressed must be.
00653             mTouch_mostPressed = &mTouch_mostPressedData[MTOUCH_MOST_PRESSED_INDEX];
00654         #endif
00655         #endif
00656         
00657     switch(MTOUCH_SENSOR_state)                                 // Begin state machine
00658     {
00671         case MTOUCH_INITIALIZING:    
00672         {
00673             if (--(MTOUCH_SENSOR_timer) <= 0)                       // If initialization delay has elapsed
00674             {                                                   // NOTE: This delay determined by
00675                                                                 //       MTOUCH_POWER_UP_SAMPLES
00676                 //-----------------------------
00677                 // State Transition -> Released
00678                 //-----------------------------
00679                 MTOUCH_SENSOR_state             = MTOUCH_RELEASED;  // Sensor is now released and stable  
00680                 
00681                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);  
00682                 
00683                 #if defined(MTOUCH_STATE_DEBOUNCE)              // Initialize sensor's debounce counter
00684                 MTOUCH_SENSOR_debounce          = MTOUCH_pDEBOUNCE_VALUE;   
00685                 #endif
00686                 
00687             }
00688             else                                                // If initialization delay is not complete
00689             {
00690             
00691                 mTouch_state.areInitialized  = 0;               // Disable data transfer until initialized
00692                 
00693                 #if MTOUCH_NUM_MODES > 1   
00694                 mTouch_state.allReleased     = 0;               // Clear the all-sensors-are-released flag.
00695                 #endif
00696                 
00697             }
00698             
00699             return 0;         
00700         }                                                   
00701         
00722         case MTOUCH_RELEASED:                                           // Sensor is released and stable
00723         {                  
00724 
00725             if(delta > (int16_t)mTouch_GetPressThreshold(MTOUCH_CURRENTSCAN_VALUE))  // If reading has crossed the press threshold
00726             {
00727                 
00728                 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)                
00729                                                                         // If this sensor is the most pressed, and is
00730                                                                         // at least MTOUCH_MOST_PRESSED_THRESHOLD
00731                                                                         // more pressed than the second-most-pressed.
00732                 if ((MTOUCH_CURRENTSCAN_VALUE == (*mTouch_mostPressed).maxIndex) && 
00733                    ((*mTouch_mostPressed).maxValue > MTOUCH_MOST_PRESSED_THRESHOLD))
00734                 {
00735                 #endif
00736                 
00737                     #if defined(MTOUCH_STATE_DEBOUNCE)
00738                     if (MTOUCH_SENSOR_debounce == 0)                            // Check the debounce counter
00739                     {                                                       
00740                         MTOUCH_SENSOR_debounce  = MTOUCH_rDEBOUNCE_VALUE;       // Initialize the pressed state's debounce variable
00741                     #endif
00742                     
00743                         //-----------------------------
00744                         // State Transition -> Pressed
00745                         //-----------------------------
00746                         MTOUCH_SENSOR_state     = MTOUCH_PRESSED;               // Sensor is now pressed
00747                         
00748                         #if defined(MTOUCH_BUTTON_TIMEOUT)                  // If the press timer is enabled
00749                         #if MTOUCH_BUTTON_TIMEOUT > 0
00750                         MTOUCH_SENSOR_timer    = MTOUCH_pTIMEOUT_VALUE;         // Initialize the press timer counter
00751                         #endif                                              // NOTE: This delay determined by
00752                         #endif                                              //       MTOUCH_BUTTON_TIMEOUT
00753                         
00754                         #if MTOUCH_NUM_MODES > 1   
00755                         mTouch_state.allReleased = 0;                       // Clear the all-sensors-are-released flag.
00756                         #endif
00757                         
00758                         #if defined(MTOUCH_TOGGLE_ENABLED)
00759                         mTouch_ToggleToggle(MTOUCH_CURRENTSCAN_VALUE);      // Alternate toggle between 'on' and 'off'.
00760                         #endif
00761                         
00762                         mTouch_state.buttonStateChange = 1;                 // Set flag notifying that a change has occured
00763                                                                             // to a sensor's 'button' state in this decode
00764                         
00765                     #if defined(MTOUCH_STATE_DEBOUNCE)
00766                     }
00767                     else
00768                     {
00769                         MTOUCH_SENSOR_debounce--;
00770                     }
00771                     #endif
00772                     
00773                 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)    
00774                 }
00775                 else                                                    // If this sensor is not the most pressed or if it
00776                 {                                                       // is not significantly more pressed than the others,
00777                     #if defined(MTOUCH_STATE_DEBOUNCE)
00778                     MTOUCH_SENSOR_debounce  = MTOUCH_pDEBOUNCE_VALUE;       // reset the debounce variable 
00779                     #endif
00780                 }
00781                 #endif
00782                 
00783             }
00784             else                                                        // If the reading has not crossed the press threshold
00785             {
00786                 #if defined(MTOUCH_STATE_DEBOUNCE)
00787                 MTOUCH_SENSOR_debounce      = MTOUCH_pDEBOUNCE_VALUE;       // Reset the debounce variable   
00788                 #endif
00789                 
00790                 // NOTE: This is the only state (MTOUCH_RELEASED) and condition 
00791                 //       (delta < PressThreshold) where the average is able to
00792                 //       be updated. Also note how we only use the reading to
00793                 //       update the baseline after we have verified it is not
00794                 //       crossing any thresholds.
00795                 return 1; 
00796             }
00797             return 0;            
00798         }
00799 
00831         case MTOUCH_PRESSED:
00832         {
00833 
00834             #if defined(MTOUCH_BUTTON_TIMEOUT)                  // (Only if the press timer has been enabled)
00835             #if MTOUCH_BUTTON_TIMEOUT > 0   
00836 
00837             if (--(MTOUCH_SENSOR_timer) <= 0)                       // Decrement the press timer counter
00838             {     
00839                 //-----------------------------
00840                 // State Transition -> Released
00841                 //-----------------------------
00842                 MTOUCH_SENSOR_state     = MTOUCH_RELEASED;          // Sensor is now released
00843                 MTOUCH_SENSOR_timeout   = 1;                        // Signal that a timeout has occurred
00844                 
00845                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE]   = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
00846                 
00847                 #if defined(MTOUCH_STATE_DEBOUNCE)
00848                 MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;    // Initialize the debounce counter for the MTOUCH_PRESSED state
00849                 #endif                                          // and check if it has reached 0.
00850                 
00851                 mTouch_state.buttonStateChange = 1;             // Set flag notifying that a change has occured
00852                                                                 // to a sensor's 'button' state in this decode
00853                 
00854                 break;
00855             }
00856             
00857             #endif
00858             #endif
00859             
00860             #if !defined(MTOUCH_MOST_PRESSED_ONLY) && !defined(MTOUCH_MATRIX_ENABLED)
00861                                                                                                     // If most pressed is not enabled,
00862             if  (delta < (int16_t)mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE))             // Look only at the threshold to determine release.
00863             #else                                                                                   // Otherwise,
00864             if ((delta < (int16_t)mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE))      ||     // Check the threshold,
00865                 (MTOUCH_CURRENTSCAN_VALUE != (*mTouch_mostPressed).maxIndex)                 ||     // Make sure this sensor is still the most-pressed,
00866                 ((*mTouch_mostPressed).maxValue < MTOUCH_MOST_PRESSED_THRESHOLD) )                  // and is most-pressed by a significant amount.
00867             #endif
00868             {                                                   // If the reading has crossed the release threshold
00869             
00870                 #if defined(MTOUCH_STATE_DEBOUNCE)
00871                 if (MTOUCH_SENSOR_debounce == 0)                    // Check if the debounce counter has reached 0.
00872                 {                                               
00873                     MTOUCH_SENSOR_debounce = MTOUCH_pDEBOUNCE_VALUE;
00874                 #endif
00875                 
00876                     //-----------------------------
00877                     // State Transition -> Released
00878                     //-----------------------------
00879                     MTOUCH_SENSOR_state = MTOUCH_RELEASED;          // Sensor is now released
00880                     
00881                     mTouch_state.buttonStateChange = 1;         // Set flag notifying that a change has occured
00882                                                                 // to a sensor's 'button' state in this decode
00883                     
00884                 #if defined(MTOUCH_STATE_DEBOUNCE)
00885                 } else {
00886                 
00887                     MTOUCH_SENSOR_debounce--;
00888                 
00889                     #if MTOUCH_NUM_MODES > 1   
00890                     mTouch_state.allReleased = 0;               // Clear the all-sensors-are-released flag.
00891                     #endif
00892                     
00893                 }
00894                 #endif    
00895                 
00896             }
00897             else                                                // If the reading has not crossed the release threshold
00898             {
00899             
00900                 #if defined(MTOUCH_STATE_DEBOUNCE)
00901                 MTOUCH_SENSOR_debounce  = MTOUCH_rDEBOUNCE_VALUE;   // Reset the debounce counter
00902                 #endif                    
00903                 #if MTOUCH_NUM_MODES > 1   
00904                 mTouch_state.allReleased = 0;                   // Clear the all-sensors-are-released flag.
00905                 #endif
00906                 
00907             }
00908             return 0;
00909         }
00910         
00911         default: break; 
00912         
00913     }   // End state machine switch command
00914     return 0;
00915 }
00916 
00917 #if defined(MTOUCH_MOST_PRESSED_ONLY) || defined(MTOUCH_MATRIX_ENABLED)
00918 //================================================================================================
00919 //          _____                _         _____ _           _ __  __           _   ____                             _ 
00920 //  _ __ __|_   _|__  _   _  ___| |__     |  ___(_)_ __   __| |  \/  | ___  ___| |_|  _ \ _ __ ___  ___ ___  ___  __| |
00921 // | '_ ` _ \| |/ _ \| | | |/ __| '_ \    | |_  | | '_ \ / _` | |\/| |/ _ \/ __| __| |_) | '__/ _ \/ __/ __|/ _ \/ _` |
00922 // | | | | | | | (_) | |_| | (__| | | |   |  _| | | | | | (_| | |  | | (_) \__ \ |_|  __/| | |  __/\__ \__ \  __/ (_| |
00923 // |_| |_| |_|_|\___/ \__,_|\___|_| |_|___|_|   |_|_| |_|\__,_|_|  |_|\___/|___/\__|_|   |_|  \___||___/___/\___|\__,_|
00924 //                                   |_____|                                    
00925 //================================================================================================
00926 void mTouch_FindMostPressed(uint8_t index)
00927 {
00928     //
00929     // Initialize variables
00930     //
00931     mTouch_mostPressed                  = &mTouch_mostPressedData[index];
00932     (*mTouch_mostPressed).maxIndex      = 0;
00933     (*mTouch_mostPressed).maxValue      = 0;
00934     uint16_t              secValue      = 0; 
00935 
00936     // Find the most pressed sensor and store it's index value
00937     for(int8_t i = mTouch_mostPressedIndex[index].end; i >= mTouch_mostPressedIndex[index].start; i--)
00938     {
00939         if (mTouch_deltas[i] > (*mTouch_mostPressed).maxValue)
00940         {
00941             (*mTouch_mostPressed).maxValue = mTouch_deltas[i];      
00942             (*mTouch_mostPressed).maxIndex = i;                     
00943         }
00944     }
00945     
00946     // Find the second-most-pressed row for comparison with the most-pressed.
00947     for(int8_t i = mTouch_mostPressedIndex[index].end; i >= mTouch_mostPressedIndex[index].start; i--)
00948     {
00949         if (i != (*mTouch_mostPressed).maxIndex)                                      
00950         {
00951             if (mTouch_deltas[i] > secValue)
00952             {
00953                 secValue = mTouch_deltas[i]; 
00954             }
00955         }
00956     }
00957     
00958     // Store the difference between the most-pressed sensor's 
00959     //  delta and the second-most-pressed sensor's delta.
00960     (*mTouch_mostPressed).maxValue = (*mTouch_mostPressed).maxValue - secValue;
00961 }
00962 #endif
00963 
00964 #if defined(MTOUCH_MATRIX_ENABLED)
00965 
00966 //================================================================================================
00967 //         _____                _        _   _           _       _       __  __       _        _       ___        _               _   
00968 // _ __ __|_   _|__  _   _  ___| |__    | | | |_ __   __| | __ _| |_ ___|  \/  | __ _| |_ _ __(_)_  __/ _ \ _   _| |_ _ __  _   _| |_ 
00969 //| '_ ` _ \| |/ _ \| | | |/ __| '_ \   | | | | '_ \ / _` |/ _` | __/ _ \ |\/| |/ _` | __| '__| \ \/ / | | | | | | __| '_ \| | | | __|
00970 //| | | | | | | (_) | |_| | (__| | | |  | |_| | |_) | (_| | (_| | ||  __/ |  | | (_| | |_| |  | |>  <| |_| | |_| | |_| |_) | |_| | |_ 
00971 //|_| |_| |_|_|\___/ \__,_|\___|_| |_|___\___/| .__/ \__,_|\__,_|\__\___|_|  |_|\__,_|\__|_|  |_/_/\_\\___/ \__,_|\__| .__/ \__,_|\__|
00972 //                                  |_____|   |_|                                                                    |_|    
00973 //================================================================================================
00974 void mTouch_UpdateMatrixOutput(void)
00975 {
00976     mTouch_matrix = 0xFF;                                                       // Reset the matrix output value
00977     
00978     for(int8_t rowIndex = MTOUCH_MATRIX_ROW_END;                                
00979         rowIndex >= MTOUCH_MATRIX_ROW_START; 
00980         rowIndex--)                                                             // For each ROW in the matrix, check to see if
00981     {                                                                           // any are considered pressed. Only one is able
00982         if (mTouch_GetButtonState(rowIndex) == MTOUCH_PRESSED)                  // to be in the pressed state at a time.
00983         {
00984             for(int8_t colIndex = MTOUCH_MATRIX_COLUMN_END; 
00985                 colIndex >= MTOUCH_MATRIX_COLUMN_START; 
00986                 colIndex--)                                                     // For each COLUMN in the matrix, check to see if
00987             {                                                                   // any are considered pressed. Only one is able
00988                 if (mTouch_GetButtonState(colIndex) == MTOUCH_PRESSED)          // to be in the pressed state at a time.
00989                 {
00990                     mTouch_matrix   = colIndex - MTOUCH_MATRIX_COLUMN_START;    // Store the matrix output value
00991                     mTouch_matrix <<= MTOUCH_MATRIX_ROW_OFFSET;                 
00992                     mTouch_matrix  += rowIndex - MTOUCH_MATRIX_ROW_START;       // Use the APIs to access the row and column info
00993                     break;  // Break out of the 'for' loop. Column found.
00994                 }
00995             }    
00996             break;  // Break out of the 'for' loop. Row found.
00997         }
00998     }  
00999 }
01000 #endif
01001 
01002 
01003 
01004 //================================================================================================
01005 //          _____                _        _   _           _       _          _                                 
01006 //  _ __ __|_   _|__  _   _  ___| |__    | | | |_ __   __| | __ _| |_ ___   / \__   _____ _ __ __ _  __ _  ___ 
01007 // | '_ ` _ \| |/ _ \| | | |/ __| '_ \   | | | | '_ \ / _` |/ _` | __/ _ \ / _ \ \ / / _ \ '__/ _` |/ _` |/ _ \
01008 // | | | | | | | (_) | |_| | (__| | | |  | |_| | |_) | (_| | (_| | ||  __// ___ \ V /  __/ | | (_| | (_| |  __/
01009 // |_| |_| |_|_|\___/ \__,_|\___|_| |_|___\___/| .__/ \__,_|\__,_|\__\___/_/   \_\_/ \___|_|  \__,_|\__, |\___|
01010 //                                   |_____|   |_|                                                  |___/      
01011 //================================================================================================
01012 
01021 void mTouch_UpdateBaseline(void)
01022 {
01023     uint16_t reading = mTouch_GetSensor(MTOUCH_CURRENTSCAN_VALUE);
01038     #if (MTOUCH_BUTTON_TIMEOUT > 0)
01039     if (mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].timeout)
01040     {
01041         if ((int16_t)(mTouch_average[MTOUCH_CURRENTSCAN_VALUE] - reading) > (int16_t)(mTouch_GetPressThreshold(MTOUCH_CURRENTSCAN_VALUE) >> 1))    // Is the delta at least half the press threshold in the negative direction?
01042         {
01043             mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCorrection = 1;
01044             mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].timeout = 0;
01045         }
01046     }
01047     #endif
01048     
01049     if (mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCorrection)
01050     {
01051         mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount = 1; 
01052         
01053         if ((int16_t)(mTouch_average[MTOUCH_CURRENTSCAN_VALUE] - reading) < (int16_t)(mTouch_GetReleaseThreshold(MTOUCH_CURRENTSCAN_VALUE)))       // Is the delta at least half the press threshold in the negative direction?
01054         { 
01055             mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCorrection    = 0;
01056         }
01057     }
01058     
01059     #if defined(MTOUCH_BLCOUNT_16BITS_REQUIRED)                // If the baseline counter has been exceeded
01060     if ((uint16_t)(--(mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount)) == 0)    
01061     #else
01062     if ((uint8_t)(--(mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount)) == 0) 
01063     #endif
01064     {                                                           // Reset the baseline counter
01065         mTouch_stateVars.sensor[MTOUCH_CURRENTSCAN_VALUE].baselineCount = MTOUCH_BASELINE_RATE_VALUE;  
01066 
01067         #if defined(MTOUCH_EEPROM_ENABLED)
01068         if (MTOUCH_BASELINE_WEIGHT_VALUE == 1)  // If 'V2' communications are enabled, this configuration
01069         {                                       // option is changable. So we need to check it each time.
01070         #endif
01071         
01072             #if defined(MTOUCH_EEPROM_ENABLED) || (MTOUCH_BASELINE_WEIGHT == 1)
01073                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE]  += reading;              // Update the baseline with an equal
01074                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE] >>= 1;                    // weight given to both.
01075             #endif                                              
01076         
01077         #if defined(MTOUCH_EEPROM_ENABLED)
01078         }
01079         else
01080         {
01081         #endif
01082 
01083             #if defined(MTOUCH_EEPROM_ENABLED) || (MTOUCH_BASELINE_WEIGHT != 1) 
01084                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE]  -= (uint16_t)(mTouch_average[MTOUCH_CURRENTSCAN_VALUE] >> MTOUCH_BASELINE_WEIGHT_VALUE); 
01085                 mTouch_average[MTOUCH_CURRENTSCAN_VALUE]  += (uint16_t)(reading >> MTOUCH_BASELINE_WEIGHT_VALUE);
01086             #endif
01087 
01088         #if defined(MTOUCH_EEPROM_ENABLED)
01089         }
01090         #endif
01091         
01092     }
01094 }
01095 
01096 
01097 //================================================================================================
01098 //          _____                _         _____                     ____       _            _   
01099 //  _ __ __|_   _|__  _   _  ___| |__     | ____|_ __ _ __ ___  _ __|  _ \  ___| |_ ___  ___| |_ 
01100 // | '_ ` _ \| |/ _ \| | | |/ __| '_ \    |  _| | '__| '__/ _ \| '__| | | |/ _ \ __/ _ \/ __| __|
01101 // | | | | | | | (_) | |_| | (__| | | |   | |___| |  | | | (_) | |  | |_| |  __/ ||  __/ (__| |_ 
01102 // |_| |_| |_|_|\___/ \__,_|\___|_| |_|___|_____|_|  |_|  \___/|_|  |____/ \___|\__\___|\___|\__|
01103 //                                   |_____|    
01104 //================================================================================================ 
01105 #if defined(MTOUCH_ERROR_DETECTION_ENABLED)     
01106 void mTouch_ErrorDetect(void)
01107 {
01108     mTouch_DisableScanning();
01109     MTOUCH_GENERATE_SHORTCHECKS();
01110     mTouch_EnableScanning();
01111 }
01112 #endif

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