CCS/CapTouch_Tuning/Library/CTS_Layer.c
/***************************************************************************/
/**
 * @brief   Determine the position on a wheel
 * @param   groupOfElements Pointer to wheel
 * @return  result position on wheel or illegal value if no touch
 ******************************************************************************/
@brief   Determine the position on a wheel
@param   groupOfElements Pointer to wheel
@return  result position on wheel or illegal value if no touch
@brief Determine the position on a wheel @param groupOfElements Pointer to wheel @return result position on wheel or illegal value if no touch
uint16_t TI_CAPT_Wheel(const struct Sensor* groupOfElements)
{
    uint8_t index;
    int16_t position;
    #ifndef RAM_FOR_FLASH
    uint16_t *measCnt;
    measCnt = (uint16_t *)malloc(groupOfElements->numElements * sizeof(uint16_t));
    if(measCnt ==0)
    {
        while(1);
    }
    #endif
    position = ILLEGAL_SLIDER_WHEEL_POSITION;
    TI_CAPT_Custom(groupOfElements, measCnt);
    if(ctsStatusReg & EVNT)
    {
        index = Dominant_Element(groupOfElements, &measCnt[0]);
        if(index == 0)
        {
          Special case of 1st element in slider, add 1st, last, and 2nd
          position = measCnt[0] + measCnt[groupOfElements->numElements -1] + measCnt[1];
        }
        else if(index == (groupOfElements->numElements -1))
        {
          Special case of Last element in slider, add last, 1st, and 2nd to last
          position = measCnt[index] + measCnt[0] + measCnt[index-1];
        }
        else
        {
          position = measCnt[index] + measCnt[index+1] + measCnt[index-1];
        } 
        if(position > groupOfElements->sensorThreshold)
        {
            position = index*(groupOfElements->points/groupOfElements->numElements);
            position += (groupOfElements->points/groupOfElements->numElements)/2;
            if(index == 0)
            {
              Special case of 1st element in slider, which only has one neighbor, measCnt[1]
              measCnt is limited to maxResponse within dominantElement function
              position += (measCnt[1]*(groupOfElements->points/groupOfElements->numElements))/100;
              position -= (measCnt[groupOfElements->numElements -1]*(groupOfElements->points/groupOfElements->numElements))/100;
              if(position < 0)
              {
                position = position + (int16_t)groupOfElements->points;
                position++;  -1 goes to points
              }
            }
            else if(index == (groupOfElements->numElements -1))
            {
              Special case of Last element in slider, which only has one neighbor, measCnt[x-1] or measCnt[numElements-1]
              measCnt is limited to maxResponse within dominantElement function
              position += (measCnt[0]*(groupOfElements->points/groupOfElements->numElements))/100;
              position -= (measCnt[index-1]*(groupOfElements->points/groupOfElements->numElements))/100;
             if(position > groupOfElements->points)
              if(position >= groupOfElements->points)
              {
                position = position - (int16_t)groupOfElements->points;
             position--; points+1 goes to 0
              }
            }
            else
            {
              position += (measCnt[index+1]*(groupOfElements->points/groupOfElements->numElements))/100;
              position -= (measCnt[index-1]*(groupOfElements->points/groupOfElements->numElements))/100;
            } 
            if((position > groupOfElements->points) || position < 0)
            {
                  position = ILLEGAL_SLIDER_WHEEL_POSITION;
            }     
        }
        else 
        {
            position = ILLEGAL_SLIDER_WHEEL_POSITION;
        }
    }
    #ifndef RAM_FOR_FLASH
    free(measCnt);
    #endif
    return position;
}
uint16_t TI_CAPT_Wheel ( const struct Sensor * groupOfElements ) { uint8_t index ; int16_t position ; #ifndef RAM_FOR_FLASH uint16_t * measCnt ; measCnt = ( uint16_t * ) malloc ( groupOfElements -> numElements * sizeof ( uint16_t ) ) ; if ( measCnt == 0 ) { while ( 1 ) ; } #endif position = ILLEGAL_SLIDER_WHEEL_POSITION ; TI_CAPT_Custom ( groupOfElements , measCnt ) ; if ( ctsStatusReg & EVNT ) { index = Dominant_Element ( groupOfElements , & measCnt [ 0 ] ) ; if ( index == 0 ) { position = measCnt [ 0 ] + measCnt [ groupOfElements -> numElements - 1 ] + measCnt [ 1 ] ; } else if ( index == ( groupOfElements -> numElements - 1 ) ) { position = measCnt [ index ] + measCnt [ 0 ] + measCnt [ index - 1 ] ; } else { position = measCnt [ index ] + measCnt [ index + 1 ] + measCnt [ index - 1 ] ; } if ( position > groupOfElements -> sensorThreshold ) { position = index * ( groupOfElements -> points / groupOfElements -> numElements ) ; position += ( groupOfElements -> points / groupOfElements -> numElements ) / 2 ; if ( index == 0 ) { position += ( measCnt [ 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; position -= ( measCnt [ groupOfElements -> numElements - 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; if ( position < 0 ) { position = position + ( int16_t ) groupOfElements -> points ; position ++ ; } } else if ( index == ( groupOfElements -> numElements - 1 ) ) { position += ( measCnt [ 0 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; position -= ( measCnt [ index - 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; if ( position >= groupOfElements -> points ) { position = position - ( int16_t ) groupOfElements -> points ; } } else { position += ( measCnt [ index + 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; position -= ( measCnt [ index - 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; } if ( ( position > groupOfElements -> points ) || position < 0 ) { position = ILLEGAL_SLIDER_WHEEL_POSITION ; } } else { position = ILLEGAL_SLIDER_WHEEL_POSITION ; } } #ifndef RAM_FOR_FLASH free ( measCnt ) ; #endif return position ; }
@brief   Determine the position on a wheel
 @param   groupOfElements Pointer to wheel
 @return  result position on wheel or illegal value if no touch
@brief Determine the position on a wheel @param groupOfElements Pointer to wheel @return result position on wheel or illegal value if no touch
// allocate memory for measurement //make measurement // Translate the EVNT flag from an element level EVNT to a sensor level EVNT. // The sensor must read at least 75% cumulative response before indicating a  // touch. // The index represents the element within the array with the highest return. //  // Special case of 1st element in slider, add 1st, last, and 2nd // Special case of Last element in slider, add last, 1st, and 2nd to last //index = Dominant_Element(groupOfElements, &measCnt[0]); // The index represents the element within the array with the highest return. //  // Special case of 1st element in slider, which only has one neighbor, measCnt[1] // measCnt is limited to maxResponse within dominantElement function // -1 goes to points // Special case of Last element in slider, which only has one neighbor, measCnt[x-1] or measCnt[numElements-1] // measCnt is limited to maxResponse within dominantElement function /* CTS: 4_25_2013: NEED TO VALIDATE */ /* In the case of the wheel the '0' and 'points' represent 1/2 segments and wrap around to form the 
               * complete segment.  In this case the number of segments = the number of points.  In this implementation
               * the range is 0 to points-1, but could easily be 1-points and thus provide a non-zero value so that the 
               * ILLEGAL_SLIDER_WHEEL_POSITION mechanism is not needed.  This however would not be consistent with the 
               * slider implementation.
               */ //              if(position > groupOfElements->points) //              position--; // points+1 goes to 0 /* CTS: 4_25_2013: NEED TO VALIDATE */