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

mTouch_slider.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_slider.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 Slider Functions
00014  *                   - Implements the mTouch slider initialization, and 
00015  *                     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_SLIDERS_ENABLED)  
00061     
00062     #if defined(MTOUCH_SLIDER_SCALING_ENABLED)
00063         uint8_t   mTouch_slider   [MTOUCH_NUMBER_OF_SLIDERS];
00064         #else
00065         uint16_t  mTouch_slider   [MTOUCH_NUMBER_OF_SLIDERS];
00066     #endif
00067 
00068     #if MTOUCH_NUMBER_OF_SLIDERS > 1
00069 
00072         typedef struct {
00073 
00074             unsigned sensorCount    :4;     
00075                 
00076             #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00077             unsigned isWheel        :1;     
00078             #endif
00079             
00080         } mTouch_SliderState;  
00081         
00082         const mTouch_SliderState        mTouch_sliderState[MTOUCH_NUMBER_OF_SLIDERS] = MTOUCH_SLIDER_STRUCT_INIT;
00083 
00084     #endif
00085 
00086     const uint8_t mTouch_sliderIndex    [MTOUCH_NUMBER_OF_SLIDERS][MTOUCH_SLIDERS_MAX_SENSORS]  = MTOUCH_SLIDERINDEX_INIT;
00087 
00088     #if MTOUCH_NUMBER_OF_SLIDERS > 1
00089         #define MTOUCH_NUMSENSORINSLIDER(i)                     (uint8_t)mTouch_sliderState[i].sensorCount
00090         #define MTOUCH_GETSLIDERSENSORINDEX(slider, sensor)     mTouch_sliderIndex[slider][sensor]
00091         #define MTOUCH_ISSLIDERAWHEEL(slider)                   mTouch_sliderState[slider].isWheel
00092         #define MTOUCH_SLIDERVALUE(i)                           mTouch_slider[i]
00093     #else
00094         #define MTOUCH_NUMSENSORINSLIDER(i)                     MTOUCH_NUMBER_SLIDER0_SENSORS
00095         #define MTOUCH_GETSLIDERSENSORINDEX(slider, sensor)     mTouch_sliderIndex[0][sensor]
00096         #define MTOUCH_ISSLIDERAWHEEL(slider)                   MTOUCH_SLIDERS_ISWHEEL0
00097         #define MTOUCH_SLIDERVALUE(i)                           mTouch_slider[0]
00098     #endif
00099 
00100     
00101 //*************************************************************************
00102 // mTouch_SliderDecode() - Slider and Wheel Decoding Module
00103 //*************************************************************************
00104 void mTouch_SliderDecode(void)
00105 {
00106     #if MTOUCH_NUMBER_OF_SLIDERS > 1
00107     for (int8_t sliderIndex = MTOUCH_NUMBER_OF_SLIDERS-1; sliderIndex >= 0; sliderIndex--)
00108     {
00109     #endif
00110     
00111         uint8_t mTouch_decode = 0;
00112         
00113         // Step 1: Determine if any of the sensors assigned to the slider are currently pressed
00114         #if (MTOUCH_NUMBER_OF_SLIDERS == 1) && (MTOUCH_NUMBER_SLIDER0_SENSORS == 2)
00115             if ((mTouch_GetButtonState(MTOUCH_SLIDER0_SENSOR0) == MTOUCH_PRESSED) ||
00116                 (mTouch_GetButtonState(MTOUCH_SLIDER0_SENSOR1) == MTOUCH_PRESSED))  mTouch_decode = 1; 
00117         #elif (MTOUCH_NUMBER_OF_SLIDERS == 1) && (MTOUCH_NUMBER_SLIDER0_SENSORS == 3)
00118             if ((mTouch_GetButtonState(MTOUCH_SLIDER0_SENSOR0) == MTOUCH_PRESSED) ||
00119                 (mTouch_GetButtonState(MTOUCH_SLIDER0_SENSOR1) == MTOUCH_PRESSED) ||
00120                 (mTouch_GetButtonState(MTOUCH_SLIDER0_SENSOR2) == MTOUCH_PRESSED))  mTouch_decode = 1; 
00121         #else
00122             for (int8_t i = MTOUCH_NUMSENSORINSLIDER(sliderIndex)-1; i >= 0; i--)
00123             {
00124                 if (mTouch_GetButtonState(MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, i)) == MTOUCH_PRESSED)
00125                 {
00126                     mTouch_decode = 1;
00127                 }
00128             }
00129         #endif        
00130         
00131         // Step 2: If there are, decode the location of the slider
00132         if (mTouch_decode)
00133         {
00134             int32_t   newSlider       = 0;
00135             uint8_t   maxIndex        = 0;
00136             
00137             #if MTOUCH_NUMBER_OF_SLIDERS > 1 || ((MTOUCH_NUMBER_OF_SLIDERS == 1) && (MTOUCH_NUMBER_SLIDER0_SENSORS > 2))
00138             uint8_t   indexMinusOne;
00139             uint8_t   indexPlusOne;
00140             #endif
00141          
00142             #if defined(MTOUCH_SLIDER_SCALING_ENABLED)
00143             #if defined(MTOUCH_SLIDERS_DIVIDE3_USED) || defined(MTOUCH_SLIDERS_DIVIDE5_USED) || defined(MTOUCH_SLIDERS_DIVIDE6_USED) || defined(MTOUCH_SLIDERS_DIVIDE7_USED)
00144             uint16_t  divisionResult;
00145             #endif
00146             #endif
00147          
00148             // Step 2a: Find the sensor on the slider which has the largest delta value...
00149             #if (MTOUCH_NUMBER_OF_SLIDERS == 1) && (MTOUCH_NUMBER_SLIDER0_SENSORS == 2)
00150                 if (mTouch_deltas[MTOUCH_SLIDER0_SENSOR0] > mTouch_deltas[MTOUCH_SLIDER0_SENSOR1])
00151                 {
00152                     maxIndex = 0;
00153                 } else {
00154                     maxIndex = 1;
00155                 }
00156             #elif (MTOUCH_NUMBER_OF_SLIDERS == 1) && (MTOUCH_NUMBER_SLIDER0_SENSORS == 3)
00157                 if (mTouch_deltas[MTOUCH_SLIDER0_SENSOR0] > mTouch_deltas[MTOUCH_SLIDER0_SENSOR1])
00158                 {
00159                     if (mTouch_deltas[MTOUCH_SLIDER0_SENSOR0] > mTouch_deltas[MTOUCH_SLIDER0_SENSOR2])
00160                     {
00161                         maxIndex = 0;
00162                     } else {
00163                         maxIndex = 2;
00164                     }
00165                 } else {
00166                     if (mTouch_deltas[MTOUCH_SLIDER0_SENSOR1] > mTouch_deltas[MTOUCH_SLIDER0_SENSOR2])
00167                     {
00168                         maxIndex = 1;
00169                     } else {
00170                         maxIndex = 2;
00171                     }
00172                 }
00173             #else
00174                 for (int8_t i = MTOUCH_NUMSENSORINSLIDER(sliderIndex)-1; i >= 0; i--)
00175                 {
00176                     if (mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, i)] > newSlider)     // Use the mTouch_sliderIndex array to find all 
00177                     {                                                                               // the sensors assigned to the slider.
00178                         maxIndex    = i;
00179                         newSlider   = mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, i)];
00180                     }
00181                 }
00182             #endif
00183             
00184             
00185             // Step 2b: Decode the position when the largest delta is one of the sensors on the end
00186             if (maxIndex == 0)                                                              // If the largest delta value is on index 0...
00187             {   
00188             
00189                 // mTouch_sliderIndex   [sliderIndex][0]    :: Stores the mTouch sensor index of this slider's 'sensor0'
00190                 // mTouch_deltas        [i]                 :: Stores the current delta values for each mTouch sensor
00191                 
00192                 #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00193                 if (MTOUCH_ISSLIDERAWHEEL(sliderIndex) == 0)
00194                 {
00195                 #endif
00196                 
00197                     //
00198                     //      |           delta[0]                |
00199                     //      | (  -------------------  ) - 0x100 | = Slider Value
00200                     //      |    delta[0] + delta[1]            |  
00201                     //
00202 
00203                     #define MTOUCH_SLIDER_2SENSOR_DECODE0(deltaFirst, deltaSec)     \
00204                         newSlider   = (int32_t)((int32_t)(deltaFirst) << 8);        \
00205                         newSlider  /= (int16_t)(deltaFirst + deltaSec);             \
00206                         newSlider  -= 0x100;                                        \
00207                         if (newSlider < 0)  { newSlider = -newSlider; }
00208                         
00209                     // Far-left Slider Decode  
00210                     MTOUCH_SLIDER_2SENSOR_DECODE0(mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, 0)], mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, 1)]);
00211                     
00212                     mTouch_decode = 0;      // Finished
00213                     
00214                 #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00215                 }
00216                 else
00217                 {
00218                     indexMinusOne   = MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, MTOUCH_NUMSENSORINSLIDER(sliderIndex) - 1);
00219                     indexPlusOne    = MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, 1);
00220                     
00221                     mTouch_decode++;    // Perform wheel decoding
00222                 }
00223                 #endif
00224 
00225             }
00226             else if (maxIndex == MTOUCH_NUMSENSORINSLIDER(sliderIndex) - 1)
00227             {   
00228                 #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00229                 if (MTOUCH_ISSLIDERAWHEEL(sliderIndex) == 0)
00230                 {
00231                 #endif
00232             
00233                     //
00234                     //                  delta[last]                                    
00235                     //       (  ---------------------------  ) + 0x100 * sensorPosition  = Slider Value
00236                     //          delta[last] + delta[last-1]                              
00237                     //
00238                     #define MTOUCH_SLIDER_2SENSOR_DECODEN(deltaLast, deltaSecLast, index)   \
00239                         newSlider   = (int32_t)((int32_t)(deltaSecLast) << 8);              \
00240                         newSlider  /= (int16_t)(deltaLast + deltaSecLast);                  \
00241                         newSlider   = (int32_t)(index << 8) - newSlider;
00242                     
00243                     // Far-right Slider Decode
00244                     MTOUCH_SLIDER_2SENSOR_DECODEN(mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, MTOUCH_NUMSENSORINSLIDER(sliderIndex) - 1)], mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, MTOUCH_NUMSENSORINSLIDER(sliderIndex) - 2)], (MTOUCH_NUMSENSORINSLIDER(sliderIndex) - 1));
00245                     
00246                     mTouch_decode   = 0;        // Finished
00247                     
00248                 #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00249                 }
00250                 else
00251                 {
00252                     indexMinusOne   = MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, MTOUCH_NUMSENSORINSLIDER(sliderIndex) - 2);
00253                     indexPlusOne    = MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, 0);
00254                     
00255                     mTouch_decode++;        // Perform wheel decoding
00256                 }
00257                 #endif
00258             
00259             }
00260             //#endif
00261             
00262             // Slider and Wheel Decode - Middle Sensors
00263             #if MTOUCH_NUMBER_OF_SLIDERS > 1 || ((MTOUCH_NUMBER_OF_SLIDERS == 1) && (MTOUCH_NUMBER_SLIDER0_SENSORS > 2))
00264             if (mTouch_decode != 0)
00265             {   
00266                 if (mTouch_decode == 1)
00267                 {
00268                     indexMinusOne   = MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, maxIndex - 1);
00269                     indexPlusOne    = MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, maxIndex + 1);
00270                 }
00271                 
00272                 if (mTouch_deltas[indexPlusOne] > mTouch_deltas[indexMinusOne])
00273                 {
00274                 
00275                     //
00276                     //                delta[i+1]                            
00277                     //       (  ---------------------  )  + (0x100 * i) = Slider Value
00278                     //          delta[i] + delta[i+1]                      
00279                     //
00280                     #define MTOUCH_SLIDER_2SENSOR_DECODERIGHT(deltaMax, deltaPlusOne, index)    \
00281                         newSlider   = (int32_t)((int32_t)(deltaPlusOne) << 8);                  \
00282                         newSlider  /= (int16_t)(deltaMax + deltaPlusOne);                       \
00283                         newSlider  += (int32_t)(index << 8);
00284                     
00285                     // Right neighbor bigger
00286                     MTOUCH_SLIDER_2SENSOR_DECODERIGHT(mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, maxIndex)], mTouch_deltas[indexPlusOne], maxIndex);
00287                 }
00288                 else
00289                 {
00290                     //
00291                     //                             delta[i-1]                       
00292                     //      (0x100 * i) - (  ---------------------  )  = Slider Value
00293                     //                       delta[i] + delta[i-1]                  
00294                     //
00295                     
00296                     #define MTOUCH_SLIDER_2SENSOR_DECODELEFT(deltaMax, deltaMinusOne, index)    \
00297                         newSlider   = (int32_t)((int32_t)(deltaMinusOne) << 8);                 \
00298                         newSlider  /= (int16_t)(deltaMax + deltaMinusOne);                      \
00299                         newSlider   = (int32_t)(index << 8) - newSlider;                                                  
00300                     
00301                     // Left neighbor bigger
00302                     MTOUCH_SLIDER_2SENSOR_DECODELEFT(mTouch_deltas[MTOUCH_GETSLIDERSENSORINDEX(sliderIndex, maxIndex)], mTouch_deltas[indexMinusOne], maxIndex);
00303                 }
00304             }
00305             #endif
00306             
00307             // Scale the newSlider value based on the number of sensors in this 
00308             //   slider to ensure a final result between 0 and 255
00309             #if defined(MTOUCH_SLIDER_SCALING_ENABLED) || defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00310             
00311                 #if     (MTOUCH_NUMBER_OF_SLIDERS == 1) || \
00312                         (MTOUCH_NUMBER_OF_SLIDERS == 2) && (MTOUCH_NUMBER_SLIDER0_SENSORS == MTOUCH_NUMBER_SLIDER1_SENSORS) || \
00313                         (MTOUCH_NUMBER_OF_SLIDERS == 3) && (MTOUCH_NUMBER_SLIDER0_SENSORS == MTOUCH_NUMBER_SLIDER1_SENSORS) && (MTOUCH_NUMBER_SLIDER1_SENSORS == MTOUCH_NUMBER_SLIDER2_SENSORS)
00314                             // If there is only one slider -OR- if the enabled sliders all have the
00315                             //  same number of sensors, customize the scaling math to minimize the
00316                             //  number of logic checks and program instructions.
00317                             #if (MTOUCH_NUMBER_SLIDER0_SENSORS == 2)
00318                                 MTOUCH_SLIDER_DIVIDE(1);
00319                             #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 3)
00320                                 MTOUCH_SLIDER_DIVIDE(2);
00321                             #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 4)
00322                                 MTOUCH_SLIDER_DIVIDE(3);
00323                             #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 5)
00324                                 MTOUCH_SLIDER_DIVIDE(4);
00325                             #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 6)
00326                                 MTOUCH_SLIDER_DIVIDE(5);
00327                             #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 7)
00328                                 MTOUCH_SLIDER_DIVIDE(6);
00329                             #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 8)
00330                                 MTOUCH_SLIDER_DIVIDE(7);
00331                             #endif
00332                 #elif   (MTOUCH_NUMBER_OF_SLIDERS == 2) && (MTOUCH_NUMBER_SLIDER0_SENSORS != MTOUCH_NUMBER_SLIDER1_SENSORS) || \
00333                         (MTOUCH_NUMBER_OF_SLIDERS == 3) && (MTOUCH_NUMBER_SLIDER0_SENSORS != MTOUCH_NUMBER_SLIDER1_SENSORS) && (MTOUCH_NUMBER_SLIDER1_SENSORS == MTOUCH_NUMBER_SLIDER2_SENSORS)
00334                                                     // If there are only two sliders, minimize the logic overhead by performing
00335                                                     //  a single if check on the slider index value. 
00336                     if      (sliderIndex == 0   
00337                     {   
00338                         #if (MTOUCH_NUMBER_SLIDER0_SENSORS == 2)
00339                             MTOUCH_SLIDER_DIVIDE(1);
00340                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 3)
00341                             MTOUCH_SLIDER_DIVIDE(2);
00342                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 4)
00343                             MTOUCH_SLIDER_DIVIDE(3);
00344                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 5)
00345                             MTOUCH_SLIDER_DIVIDE(4);
00346                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 6)
00347                             MTOUCH_SLIDER_DIVIDE(5);
00348                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 7)
00349                             MTOUCH_SLIDER_DIVIDE(6);
00350                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 8)
00351                             MTOUCH_SLIDER_DIVIDE(7);
00352                         #endif
00353                     }
00354                     else                        
00355                     {   
00356                         #if (MTOUCH_NUMBER_SLIDER1_SENSORS == 2)
00357                             MTOUCH_SLIDER_DIVIDE(1);
00358                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 3)
00359                             MTOUCH_SLIDER_DIVIDE(2);
00360                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 4)
00361                             MTOUCH_SLIDER_DIVIDE(3);
00362                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 5)
00363                             MTOUCH_SLIDER_DIVIDE(4);
00364                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 6)
00365                             MTOUCH_SLIDER_DIVIDE(5);
00366                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 7)
00367                             MTOUCH_SLIDER_DIVIDE(6);
00368                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 8)
00369                             MTOUCH_SLIDER_DIVIDE(7);
00370                         #endif
00371                     }
00372                 #elif   (MTOUCH_NUMBER_OF_SLIDERS == 3)
00373                                                     // If there are three sliders, minimize the logic overhead by performing
00374                                                     //  two if checks on the slider index value.
00375                     if      (sliderIndex == 0)  
00376                     {   
00377                         #if (MTOUCH_NUMBER_SLIDER0_SENSORS == 2)
00378                             MTOUCH_SLIDER_DIVIDE(1);
00379                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 3)
00380                             MTOUCH_SLIDER_DIVIDE(2);
00381                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 4)
00382                             MTOUCH_SLIDER_DIVIDE(3);
00383                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 5)
00384                             MTOUCH_SLIDER_DIVIDE(4);
00385                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 6)
00386                             MTOUCH_SLIDER_DIVIDE(5);
00387                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 7)
00388                             MTOUCH_SLIDER_DIVIDE(6);
00389                         #elif (MTOUCH_NUMBER_SLIDER0_SENSORS == 8)
00390                             MTOUCH_SLIDER_DIVIDE(7);
00391                         #endif
00392                     }
00393                     else if (sliderIndex == 1)  
00394                     {   
00395                         #if (MTOUCH_NUMBER_SLIDER1_SENSORS == 2)
00396                             MTOUCH_SLIDER_DIVIDE(1);
00397                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 3)
00398                             MTOUCH_SLIDER_DIVIDE(2);
00399                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 4)
00400                             MTOUCH_SLIDER_DIVIDE(3);
00401                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 5)
00402                             MTOUCH_SLIDER_DIVIDE(4);
00403                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 6)
00404                             MTOUCH_SLIDER_DIVIDE(5);
00405                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 7)
00406                             MTOUCH_SLIDER_DIVIDE(6);
00407                         #elif (MTOUCH_NUMBER_SLIDER1_SENSORS == 8)
00408                             MTOUCH_SLIDER_DIVIDE(7);
00409                         #endif
00410                     }
00411                     else                        
00412                     {   
00413                         #if (MTOUCH_NUMBER_SLIDER2_SENSORS == 2)
00414                             MTOUCH_SLIDER_DIVIDE(1);
00415                         #elif (MTOUCH_NUMBER_SLIDER2_SENSORS == 3)
00416                             MTOUCH_SLIDER_DIVIDE(2);
00417                         #elif (MTOUCH_NUMBER_SLIDER2_SENSORS == 4)
00418                             MTOUCH_SLIDER_DIVIDE(3);
00419                         #elif (MTOUCH_NUMBER_SLIDER2_SENSORS == 5)
00420                             MTOUCH_SLIDER_DIVIDE(4);
00421                         #elif (MTOUCH_NUMBER_SLIDER2_SENSORS == 6)
00422                             MTOUCH_SLIDER_DIVIDE(5);
00423                         #elif (MTOUCH_NUMBER_SLIDER2_SENSORS == 7)
00424                             MTOUCH_SLIDER_DIVIDE(6);
00425                         #elif (MTOUCH_NUMBER_SLIDER2_SENSORS == 8)
00426                             MTOUCH_SLIDER_DIVIDE(7);
00427                         #endif
00428                     }
00429                 #else
00430                     // If there are four or more sliders, just implement a switch 
00431                     //  statement that will divide by the necessary amount.
00432                     switch(MTOUCH_NUMSENSORINSLIDER(sliderIndex))
00433                     {
00434                         #if defined(MTOUCH_SLIDERS_DIVIDE2_USED)            // Only implements cases that will be used.
00435                             case 3: MTOUCH_SLIDER_DIVIDE(2);  break;
00436                         #endif
00437                         #if defined(MTOUCH_SLIDERS_DIVIDE3_USED)
00438                             case 4: MTOUCH_SLIDER_DIVIDE(3);  break;
00439                         #endif
00440                         #if defined(MTOUCH_SLIDERS_DIVIDE4_USED)
00441                             case 5: MTOUCH_SLIDER_DIVIDE(4);  break;  
00442                         #endif
00443                         #if defined(MTOUCH_SLIDERS_DIVIDE5_USED)
00444                             case 6: MTOUCH_SLIDER_DIVIDE(5);  break;  
00445                         #endif
00446                         #if defined(MTOUCH_SLIDERS_DIVIDE6_USED)
00447                             case 7: MTOUCH_SLIDER_DIVIDE(6);  break;  
00448                         #endif
00449                         #if defined(MTOUCH_SLIDERS_DIVIDE7_USED)
00450                             case 8: MTOUCH_SLIDER_DIVIDE(7);  break;  
00451                         #endif
00452                             default:                            break;
00453                     }
00454                 #endif
00455             #endif
00456             
00457             // Implement a maximum update speed for the slider. 
00458             // Only allow it to change by a fixed, maximum amount each decode.
00459             #if defined(MTOUCH_SLIDER_STEP_SIZE)
00460                 #if MTOUCH_SLIDER_STEP_SIZE != 0
00461                 
00462                     #if defined(MTOUCH_SLIDER_SCALING_ENABLED)
00463                         #define MTOUCH_SLIDER_EQUALS_NEWSLIDER()    MTOUCH_SLIDERVALUE(sliderIndex) = (uint8_t)(newSlider & 0xFF)   // 8-bit result
00464                     #else
00465                         #define MTOUCH_SLIDER_EQUALS_NEWSLIDER()    MTOUCH_SLIDERVALUE(sliderIndex) = (uint16_t)newSlider;          // 16-bit result
00466                     #endif
00467                 
00468                     #if defined(MTOUCH_SLIDER_SCALING_ENABLED)
00469                     if (newSlider == 256) newSlider--;          // If 256, make equal to 255 to fit within 8 bits.
00470                     #endif
00471                     
00472                     #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00473                         if ((uint16_t)(newSlider) > (uint16_t)(MTOUCH_SLIDERVALUE(sliderIndex) + MTOUCH_SLIDER_STEP_SIZE))
00474                         {
00475                                                                                                                     // If the new wheel value has increased...
00476                             if ((uint16_t)(newSlider) > (uint16_t)(MTOUCH_SLIDERVALUE(sliderIndex) + 0x7F))         // Check if the scrolling is just scrolling across the 0 and 255 transition
00477                             {                                                                                       // Special math for step size for transition point
00478                                 if(MTOUCH_SLIDERVALUE(sliderIndex) >= MTOUCH_SLIDER_STEP_SIZE)                       
00479                                     MTOUCH_SLIDERVALUE(sliderIndex) - =  MTOUCH_SLIDER_STEP_SIZE;                    
00480                                 else
00481                                     MTOUCH_SLIDERVALUE(sliderIndex) = 0xFF - (MTOUCH_SLIDER_STEP_SIZE - MTOUCH_SLIDERVALUE(sliderIndex));
00482                             }
00483                             else
00484                             {                                                                                        // The scrolling is not near transition point
00485                                 MTOUCH_SLIDERVALUE(sliderIndex) + = MTOUCH_SLIDER_STEP_SIZE;                         // and move step is smaller than step size
00486                             }
00487 
00488                         }
00489                         else if ((uint16_t)(newSlider+ MTOUCH_SLIDER_STEP_SIZE) < (uint16_t)(MTOUCH_SLIDERVALUE(sliderIndex)) )   
00490                         {                                                                                            //If the new wheel value has increased...
00491                                                                                                                      // Check if the scrolling is just scrolling across the 0 and 255 transition
00492                             if ((uint16_t)(newSlider + 0x7F )< (uint16_t)(MTOUCH_SLIDERVALUE(sliderIndex)))          // Special math for step size for transition point
00493                             {                                                                                       
00494                                 if((0xFF - MTOUCH_SLIDERVALUE(sliderIndex) > MTOUCH_SLIDER_STEP_SIZE))                       
00495                                     MTOUCH_SLIDERVALUE(sliderIndex) +=  MTOUCH_SLIDER_STEP_SIZE;                         
00496                                 else
00497                                     MTOUCH_SLIDERVALUE(sliderIndex) = MTOUCH_SLIDER_STEP_SIZE - (0xFF -MTOUCH_SLIDERVALUE(sliderIndex) );
00498                             }
00499                             else
00500                             {                                                                                        // The scrolling is not near transition point
00501                                 MTOUCH_SLIDERVALUE(sliderIndex) - = MTOUCH_SLIDER_STEP_SIZE;                         // and move step is smaller than step size
00502                             }
00503                         }
00504                         else
00505                         {
00506                             MTOUCH_SLIDER_EQUALS_NEWSLIDER();                                                       // mTouch_slider is near an underflow condition
00507                                                                                                                     //  and newSlider is not increasing its value.
00508                                                                                                                     // Simply storing the result as-is.
00509                         }
00510                     #else
00511                         if ((uint16_t)(newSlider) > (uint16_t)(MTOUCH_SLIDERVALUE(sliderIndex) + MTOUCH_SLIDER_STEP_SIZE))           
00512                         {                                                                                           
00513                             #if defined(MTOUCH_SLIDER_SCALING_ENABLED)                                              // If the new slider value has increased...
00514                             if (MTOUCH_SLIDERVALUE(sliderIndex) < (0xFF - MTOUCH_SLIDER_STEP_SIZE))                 // Check if the slider's value is close to 
00515                             {                                                                                       //  overflowing. If scaling is enabled, it
00516                             #endif                                                                                  //  is stored as an unsigned char.
00517                                 MTOUCH_SLIDERVALUE(sliderIndex) += MTOUCH_SLIDER_STEP_SIZE;                         // Not going to overflow - increment by the
00518                             #if defined(MTOUCH_SLIDER_SCALING_ENABLED)                                              //  maximum step size.
00519                             } else {                                                                                // We might overflow - so just load the
00520                                 MTOUCH_SLIDER_EQUALS_NEWSLIDER();                                                   //  8-bit value from newSlider.
00521                             }
00522                             #endif
00523                         }                                                                         
00524                         else if (MTOUCH_SLIDERVALUE(sliderIndex) > MTOUCH_SLIDER_STEP_SIZE)                         // If mTouch_slider is less than or close
00525                         {                                                                                           //  in value to newSlider...
00526                             if ((uint16_t)(newSlider) < MTOUCH_SLIDERVALUE(sliderIndex) - MTOUCH_SLIDER_STEP_SIZE)  // If newSlider is much less...
00527                             {                                                                                       //
00528                                 MTOUCH_SLIDERVALUE(sliderIndex) -= MTOUCH_SLIDER_STEP_SIZE;                         //  Decrement by the maximum step size 
00529                             } else {                                                                                // If newSlider is about equal to mTouchSlider...
00530                                 MTOUCH_SLIDER_EQUALS_NEWSLIDER();                                                   //  Store newSlider as our new result 
00531                             }                                                                               
00532                         }                                                                                   
00533                         else                                                                                
00534                         {   
00535                             MTOUCH_SLIDER_EQUALS_NEWSLIDER();                                                       // mTouch_slider is near an underflow condition
00536                                                                                                                     //  and newSlider is not increasing its value.
00537                                                                                                                     // Simply storing the result as-is.
00538                         } 
00539                     #endif
00540                 #else                                                                           // No maximum step size defined.
00541                     MTOUCH_SLIDER_EQUALS_NEWSLIDER();                                           // Simply store the result as-is.
00542                 #endif
00543             #else                                                                               // No maximum step size defined.
00544                 MTOUCH_SLIDER_EQUALS_NEWSLIDER();                                               // Simply store the result as-is.
00545             #endif
00546         }
00547         
00548     #if MTOUCH_NUMBER_OF_SLIDERS > 1
00549     }
00550     #endif
00551     
00552     #if defined(MTOUCH_SLIDERS_WHEELS_IMPLEMENTED)
00553     for (int8_t i = SPEEDLOGWINDOW - 1; i >= 1; i--)
00554     {
00555         scrollingStep[i] = scrollingStep[i-1];                                      //shift the log data
00556     }
00557     scrollingStep[0] = mTouch_GetSlider(0) - lastSlidervalue;                       //log the last moving steps
00558     
00559     // Solve the 0,255 point problem
00560          if ((mTouch_GetSlider(0) - lastSlidervalue) < -100)    { scrollingStep[0]  = (int8_t)(mTouch_GetSlider(0) - lastSlidervalue + 0xFF); }
00561     else if ((mTouch_GetSlider(0) - lastSlidervalue) >  100)    { scrollingStep[0]  = (int8_t)(mTouch_GetSlider(0) - lastSlidervalue - 0xFF); }
00562     else                                                        { scrollingStep[0]  = (int8_t)(mTouch_GetSlider(0) - lastSlidervalue);        }
00563 
00564     int16_t  currentSpeed = 0;
00565     for(int8_t i = SPEEDLOGWINDOW - 1; i >= 0  ; i--)
00566     {
00567         currentSpeed += scrollingStep[i];                       // Sum up all the speed in the log window, then average
00568     }
00569     
00570     currentSpeed   /= SPEEDLOGWINDOW;
00571     currentSpeed    = (currentSpeed *3  + lastSpeed) >> 2;      // Average the speed
00572     lastSpeed       = currentSpeed;
00573     lastSlidervalue = mTouch_GetSlider(0);                      // Log the last position for next step calculation
00574     
00575          if (currentSpeed >  2) {   wheelStatus.direction   = CLOCKWISE;        }
00576     else if (currentSpeed < -2) {   wheelStatus.direction   = COUNTERCLOCKWISE; }
00577     else                        {   wheelStatus.direction   = NOSCROLLING;      }
00578 
00579     if (currentSpeed < 0)       {   wheelStatus.speed       = -currentSpeed;    }
00580     else                        {   wheelStatus.speed       =  currentSpeed;    }
00581     #endif
00582 }
00583 
00584 #endif // End - #if defined(MTOUCH_SLIDERS_ENABLED)  

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