7 term/Microprocessor-Systems-and-Tools/Lab 6/libs/CTS_Layer.c
/***************************************************************************/
/**
 * @brief   Determine the position on a slider
 * @param   groupOfElements Pointer to slider
 * @return  result position on slider or illegal value if no touch
 ******************************************************************************/
@brief   Determine the position on a slider
@param   groupOfElements Pointer to slider
@return  result position on slider or illegal value if no touch
@brief Determine the position on a slider @param groupOfElements Pointer to slider @return result position on slider or illegal value if no touch
uint16_t TI_CAPT_Slider(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[1];
        }
        else if(index == (groupOfElements->numElements -1))
        {
          position = measCnt[groupOfElements->numElements -1]
                   + measCnt[groupOfElements->numElements -2];
        }
        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)
            {
              if(measCnt[1])
              {
                  position += (measCnt[1]
                                  *(groupOfElements->points
                                		/groupOfElements->numElements))/100;
              }
              else
              {
                  position = (measCnt[0]
                                  *(groupOfElements->points
                		                /groupOfElements->numElements)/2)/100;
              }
            }
            else if(index == (groupOfElements->numElements -1))
            {
              if(measCnt[index-1])
              {
                  position -= (measCnt[index-1]
                                   *(groupOfElements->points
                                		 /groupOfElements->numElements))/100;
              }
              else
              {
                  position = groupOfElements->points;
                  position -= (measCnt[index]
                                  *(groupOfElements->points
                                		/groupOfElements->numElements)/2)/100;
              }
            }
            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_Slider ( 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 [ 1 ] ; } else if ( index == ( groupOfElements -> numElements - 1 ) ) { position = measCnt [ groupOfElements -> numElements - 1 ] + measCnt [ groupOfElements -> numElements - 2 ] ; } 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 ) { if ( measCnt [ 1 ] ) { position += ( measCnt [ 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; } else { position = ( measCnt [ 0 ] * ( groupOfElements -> points / groupOfElements -> numElements ) / 2 ) / 100 ; } } else if ( index == ( groupOfElements -> numElements - 1 ) ) { if ( measCnt [ index - 1 ] ) { position -= ( measCnt [ index - 1 ] * ( groupOfElements -> points / groupOfElements -> numElements ) ) / 100 ; } else { position = groupOfElements -> points ; position -= ( measCnt [ index ] * ( groupOfElements -> points / groupOfElements -> numElements ) / 2 ) / 100 ; } } 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 slider
 @param   groupOfElements Pointer to slider
 @return  result position on slider or illegal value if no touch
@brief Determine the position on a slider @param groupOfElements Pointer to slider @return result position on slider or illegal value if no touch
// allocate memory for measurement //make measurement // Use EVNT flag to determine if slider was touched. // The EVNT flag is a global variable and managed within the TI_CAPT_Custom // function. // 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 // Determine if sensor threshold criteria is met // calculate position // Special case of 1st element in slider, which only has one  // neighbor, measCnt[1]. measCnt is limited to maxResponse  // within dominantElement function /* Calculate position based upon measCnt[0] */ // Special case of Last element in slider, which only has one  // neighbor, measCnt[x-1] or measCnt[numElements-1]