CCS/CapTouch_Tuning/Library/CTS_Layer.c
/***************************************************************************/
/**
 * @brief   Measure the change in capacitance of the Sensor
 * 
 *          This function measures the change in capacitance of each element
 *          within a sensor and updates the baseline tracking in the event that
 *          no change exceeds the detection threshold.
 *          The order of the elements within the Sensor structure is arbitrary 
 *          but must be consistent between the application and configuration. 
 *          The first element in the array (deltaCnt) corresponds to the first 
 *          element within the Sensor structure.
 * @param   groupOfElements Pointer to Sensor structure to be measured
 * @param   deltaCnt Address to where the measurements are to be written
 * @return  none
 ******************************************************************************/
@brief   Measure the change in capacitance of the Sensor

This function measures the change in capacitance of each element
within a sensor and updates the baseline tracking in the event that
no change exceeds the detection threshold.
The order of the elements within the Sensor structure is arbitrary
but must be consistent between the application and configuration.
The first element in the array (deltaCnt) corresponds to the first
element within the Sensor structure.
@param   groupOfElements Pointer to Sensor structure to be measured
@param   deltaCnt Address to where the measurements are to be written
@return  none
@brief Measure the change in capacitance of the Sensor This function measures the change in capacitance of each element within a sensor and updates the baseline tracking in the event that no change exceeds the detection threshold . The order of the elements within the Sensor structure is arbitrary but must be consistent between the application and configuration . The first element in the array ( deltaCnt ) corresponds to the first element within the Sensor structure . @param groupOfElements Pointer to Sensor structure to be measured @param deltaCnt Address to where the measurements are to be written @return none
void TI_CAPT_Custom(const struct Sensor* groupOfElements, uint16_t * deltaCnt)
{ 
    uint8_t j;
    uint16_t tempCnt, remainder;
    ctsStatusReg &= ~ EVNT;
    TI_CAPT_Raw(groupOfElements, &deltaCnt[0]); 
    for (j = 0; j < (groupOfElements->numElements); j++)
    {  
        tempCnt = deltaCnt[j];
        if(deltaCnt[j])
        {
        if(((ctsStatusReg & DOI_MASK)
        		&& (groupOfElements->halDefinition & RO_MASK))
            ||
         ((!(ctsStatusReg & DOI_MASK))
        		&& (!(groupOfElements->halDefinition & RO_MASK))))
        { 
            if(baseCnt[j+groupOfElements->baseOffset] < deltaCnt[j])            
            {
                deltaCnt[j] = 0;
                if(((groupOfElements->arrayPtr[j])->threshold)
                &&
                (baseCnt[j+groupOfElements->baseOffset]
                    +((groupOfElements->arrayPtr[j])->threshold/2) < tempCnt))
                {
                    tempCnt = baseCnt[j+groupOfElements->baseOffset]
                                +((groupOfElements->arrayPtr[j])->threshold)/2;
                }
            }
            else
            {
                deltaCnt[j] = baseCnt[j+groupOfElements->baseOffset]
                                  - deltaCnt[j];
            }
        }
        if(((!(ctsStatusReg & DOI_MASK))
        		&& (groupOfElements->halDefinition & RO_MASK))
           ||
             ((ctsStatusReg & DOI_MASK)
            	&& (!(groupOfElements->halDefinition & RO_MASK))))
        { 
            if(baseCnt[j+groupOfElements->baseOffset] > deltaCnt[j])            
            {
                deltaCnt[j] = 0;
                if(((groupOfElements->arrayPtr[j])->threshold)
                &&
                (baseCnt[j+groupOfElements->baseOffset]
                     -((groupOfElements->arrayPtr[j])->threshold/2) > tempCnt))
                {
                    tempCnt = baseCnt[j+groupOfElements->baseOffset]
                                -((groupOfElements->arrayPtr[j])->threshold)/2;
                }
            }
            else       
            {
                deltaCnt[j] = deltaCnt[j]
                              - baseCnt[j+groupOfElements->baseOffset];
            }         
        }
        if (deltaCnt[j]==0)
        { 
        	remainder = 0;
			switch ((ctsStatusReg & TRADOI_VSLOW))
			{
			case TRADOI_FAST:
					tempCnt = tempCnt/2;
					baseCnt[j+groupOfElements->baseOffset]
					= (baseCnt[j+groupOfElements->baseOffset]/2);
					break;
			case TRADOI_MED:
					tempCnt = tempCnt/4;
					baseCnt[j+groupOfElements->baseOffset]
					= 3*(baseCnt[j+groupOfElements->baseOffset]/4);
					break;
			case TRADOI_SLOW:
				  remainder = 0x003F & baseCnt[j+groupOfElements->baseOffset];
				  remainder = remainder * 63;
				  remainder += 0x003F & tempCnt;           
				  remainder = remainder >> 6;
				  tempCnt = tempCnt/64;
				  baseCnt[j+groupOfElements->baseOffset]
				  = 63*(baseCnt[j+groupOfElements->baseOffset]/64);
					break;
			case TRADOI_VSLOW:
                remainder = 0x007F & baseCnt[j+groupOfElements->baseOffset];
                remainder = remainder * 127;
                remainder += 0x007F & tempCnt;
                remainder = remainder >> 7;
				tempCnt = tempCnt/128;
				baseCnt[j+groupOfElements->baseOffset]
				= 127*(baseCnt[j+groupOfElements->baseOffset]/128);
			break;
			}
		    tempCnt += remainder;
            baseCnt[j+groupOfElements->baseOffset] += tempCnt;
            if(groupOfElements->halDefinition & RO_MASK)
            {
                baseCnt[j+groupOfElements->baseOffset]++;
            }
            else
            {
                baseCnt[j+groupOfElements->baseOffset]--;
            }
        }
        else if(deltaCnt[j]<(groupOfElements->arrayPtr[j])->threshold 
		         && !(ctsStatusReg & PAST_EVNT))
        {    
        	remainder = 1;
            switch ((ctsStatusReg & TRIDOI_FAST))
            {
            case TRIDOI_VSLOW:
                tempCnt = 0;
				break;
			case TRIDOI_SLOW:
				remainder = 2;
			    tempCnt = 0;
			    break;
			case TRIDOI_MED:
				tempCnt = tempCnt/4;
				baseCnt[j+groupOfElements->baseOffset] = 3*(baseCnt[j+groupOfElements->baseOffset]/4);
				break;
			case TRIDOI_FAST:
				tempCnt = tempCnt/2;
				baseCnt[j+groupOfElements->baseOffset] = (baseCnt[j+groupOfElements->baseOffset]/2);
				break;
			}
          baseCnt[j+groupOfElements->baseOffset] += tempCnt;
          if(groupOfElements->halDefinition & RO_MASK)
          {
              baseCnt[j+groupOfElements->baseOffset]-= remainder;
          }
          else
          {
              baseCnt[j+groupOfElements->baseOffset]+= remainder;
          }
        }
        else if(deltaCnt[j]>=(groupOfElements->arrayPtr[j])->threshold)
        {
          ctsStatusReg |= EVNT;
          ctsStatusReg |= PAST_EVNT;
        }
        }
    }
    if(!(ctsStatusReg & EVNT))
    {
      ctsStatusReg &= ~PAST_EVNT;
    }
}
void TI_CAPT_Custom ( const struct Sensor * groupOfElements , uint16_t * deltaCnt ) { uint8_t j ; uint16_t tempCnt , remainder ; ctsStatusReg &= ~ EVNT ; TI_CAPT_Raw ( groupOfElements , & deltaCnt [ 0 ] ) ; for ( j = 0 ; j < ( groupOfElements -> numElements ) ; j ++ ) { tempCnt = deltaCnt [ j ] ; if ( deltaCnt [ j ] ) { if ( ( ( ctsStatusReg & DOI_MASK ) && ( groupOfElements -> halDefinition & RO_MASK ) ) || ( ( ! ( ctsStatusReg & DOI_MASK ) ) && ( ! ( groupOfElements -> halDefinition & RO_MASK ) ) ) ) { if ( baseCnt [ j + groupOfElements -> baseOffset ] < deltaCnt [ j ] ) { deltaCnt [ j ] = 0 ; if ( ( ( groupOfElements -> arrayPtr [ j ] ) -> threshold ) && ( baseCnt [ j + groupOfElements -> baseOffset ] + ( ( groupOfElements -> arrayPtr [ j ] ) -> threshold / 2 ) < tempCnt ) ) { tempCnt = baseCnt [ j + groupOfElements -> baseOffset ] + ( ( groupOfElements -> arrayPtr [ j ] ) -> threshold ) / 2 ; } } else { deltaCnt [ j ] = baseCnt [ j + groupOfElements -> baseOffset ] - deltaCnt [ j ] ; } } if ( ( ( ! ( ctsStatusReg & DOI_MASK ) ) && ( groupOfElements -> halDefinition & RO_MASK ) ) || ( ( ctsStatusReg & DOI_MASK ) && ( ! ( groupOfElements -> halDefinition & RO_MASK ) ) ) ) { if ( baseCnt [ j + groupOfElements -> baseOffset ] > deltaCnt [ j ] ) { deltaCnt [ j ] = 0 ; if ( ( ( groupOfElements -> arrayPtr [ j ] ) -> threshold ) && ( baseCnt [ j + groupOfElements -> baseOffset ] - ( ( groupOfElements -> arrayPtr [ j ] ) -> threshold / 2 ) > tempCnt ) ) { tempCnt = baseCnt [ j + groupOfElements -> baseOffset ] - ( ( groupOfElements -> arrayPtr [ j ] ) -> threshold ) / 2 ; } } else { deltaCnt [ j ] = deltaCnt [ j ] - baseCnt [ j + groupOfElements -> baseOffset ] ; } } if ( deltaCnt [ j ] == 0 ) { remainder = 0 ; switch ( ( ctsStatusReg & TRADOI_VSLOW ) ) { case TRADOI_FAST : tempCnt = tempCnt / 2 ; baseCnt [ j + groupOfElements -> baseOffset ] = ( baseCnt [ j + groupOfElements -> baseOffset ] / 2 ) ; break ; case TRADOI_MED : tempCnt = tempCnt / 4 ; baseCnt [ j + groupOfElements -> baseOffset ] = 3 * ( baseCnt [ j + groupOfElements -> baseOffset ] / 4 ) ; break ; case TRADOI_SLOW : remainder = 0x003F & baseCnt [ j + groupOfElements -> baseOffset ] ; remainder = remainder * 63 ; remainder += 0x003F & tempCnt ; remainder = remainder >> 6 ; tempCnt = tempCnt / 64 ; baseCnt [ j + groupOfElements -> baseOffset ] = 63 * ( baseCnt [ j + groupOfElements -> baseOffset ] / 64 ) ; break ; case TRADOI_VSLOW : remainder = 0x007F & baseCnt [ j + groupOfElements -> baseOffset ] ; remainder = remainder * 127 ; remainder += 0x007F & tempCnt ; remainder = remainder >> 7 ; tempCnt = tempCnt / 128 ; baseCnt [ j + groupOfElements -> baseOffset ] = 127 * ( baseCnt [ j + groupOfElements -> baseOffset ] / 128 ) ; break ; } tempCnt += remainder ; baseCnt [ j + groupOfElements -> baseOffset ] += tempCnt ; if ( groupOfElements -> halDefinition & RO_MASK ) { baseCnt [ j + groupOfElements -> baseOffset ] ++ ; } else { baseCnt [ j + groupOfElements -> baseOffset ] -- ; } } else if ( deltaCnt [ j ] < ( groupOfElements -> arrayPtr [ j ] ) -> threshold && ! ( ctsStatusReg & PAST_EVNT ) ) { remainder = 1 ; switch ( ( ctsStatusReg & TRIDOI_FAST ) ) { case TRIDOI_VSLOW : tempCnt = 0 ; break ; case TRIDOI_SLOW : remainder = 2 ; tempCnt = 0 ; break ; case TRIDOI_MED : tempCnt = tempCnt / 4 ; baseCnt [ j + groupOfElements -> baseOffset ] = 3 * ( baseCnt [ j + groupOfElements -> baseOffset ] / 4 ) ; break ; case TRIDOI_FAST : tempCnt = tempCnt / 2 ; baseCnt [ j + groupOfElements -> baseOffset ] = ( baseCnt [ j + groupOfElements -> baseOffset ] / 2 ) ; break ; } baseCnt [ j + groupOfElements -> baseOffset ] += tempCnt ; if ( groupOfElements -> halDefinition & RO_MASK ) { baseCnt [ j + groupOfElements -> baseOffset ] -= remainder ; } else { baseCnt [ j + groupOfElements -> baseOffset ] += remainder ; } } else if ( deltaCnt [ j ] >= ( groupOfElements -> arrayPtr [ j ] ) -> threshold ) { ctsStatusReg |= EVNT ; ctsStatusReg |= PAST_EVNT ; } } } if ( ! ( ctsStatusReg & EVNT ) ) { ctsStatusReg &= ~ PAST_EVNT ; } }
@brief   Measure the change in capacitance of the Sensor
@brief Measure the change in capacitance of the Sensor
// measure group of sensors /*
        	 * Interested in a decrease in counts. Either the decrease
        	 * represents an increase in capacitance (a touch) with the
        	 * RO method, or the decrease represents a decrease in capacitance
        	 * (release) with the fRO or RC methods.
        	 */ /*
                 * The measured value is greater than the baseline therefore
                 * no detection logic is needed.  The measured value is
                 * preserved in tempCnt and is used for baseline updates.
                 */ /*
                	 * When the threshold is valid (non-calibration state),
                	 * limit the measurement to the baseline + threshold/2.
                	 */ /*
                 * deltaCnt now represents the magnitude of change relative to
                 * the baseline.
                 */ /*
        	 * Interested in an increase in counts. Either the increase
        	 * represents a decrease in capacitance (a release) with the
        	 * RO method, or the increase represents a increase in capacitance
        	 * (touch) with the fRO or RC methods.
        	 */ /*
                 * The measured value is less than the baseline therefore
                 * no detection logic is needed.  The measured value is
                 * preserved in tempCnt and is used for baseline updates.
                 */ /*
                	 * When the threshold is valid (non-calibration state),
                	 * limit the measurement to the baseline - threshold/2.
                	 */ /*
                 * deltaCnt now represents the magnitude of change relative to
                 * the baseline.
                 */ // This section updates the baseline capacitance************************ // if delta counts is 0, then the change in capacitance was opposite // the direction of interest.  The baseCnt[i] is updated with the // saved tempCnt value for the current index value 'i'. //Fast //Medium //slow /* Calculate remainder associated with (x + 63*y)/64 */ //very slow /* Calculate remainder associated with (x+127*y)/128 */ /* Base_Capacitance = (Measured_Capacitance/Z)
		                        + Y*(Base_Capacitance/Z) */ /* In the case that DOI is set and */ /*  If the RO_MASK is set then the direction of interest is
			     *  decreasing (counts decrease with capacitance) and therefore
			     *  movement against the direction of interest would be an
			     *  increase: increment.
			     */ /*  RO_MASK is not set and therefore a decrease is against
			     *  the direction of interest: decrement
			     */ /* deltaCnt is either 0, less than threshold, or greater than 
		   threshold, never negative. */ //if delta counts is positive but less than threshold, //slow //medium //fast /*
           *  Base_Capacitance = (Measured_Capacitance/Z) +
           *  Y*(Base_Capacitance/Z)
           */ /*  If the RO_MASK is set then the direction of interest is
			     *  decreasing (counts decrease with capacitance) and
			     *  therefore movement in the direction of interest would
			     *  be a decrease: decrement.
			     */ /*  RO_MASK is not set and therefore an increase is in the
			     *  direction of interest: increment
			     */ //if delta counts above the threshold, event has occurred // end of for-loop