/*! \fn     touchClearCurrentDetections(void)
*   \brief  Clear interrupt line for detections
*/
void touchClearCurrentDetections(void)
{
    uint8_t temp_uint;
    readDataFromTS(REG_AT42QT_SLIDER_POS, &temp_uint);
    readDataFromTS(REG_AT42QT_DET_STAT, &temp_uint);    
    readDataFromTS(REG_AT42QT_KEY_STAT1, &temp_uint);
    readDataFromTS(REG_AT42QT_KEY_STAT2, &temp_uint);
}
/*! \fn     touchWaitForButtonsReleased(void)
*   \brief  Wait for the user to remove his finger from the buttons
*/
void touchWaitForButtonsReleased(void)
{
    uint8_t keys_detection_status;
    
    do 
    {
        readDataFromTS(REG_AT42QT_DET_STAT, &keys_detection_status);
    }
    while (keys_detection_status & (AT42QT2120_SDET_MASK | AT42QT2120_TDET_MASK));
}
/*! \fn     touchWaitForWheelReleased(void)
*   \brief  Wait for the user to remove his finger from the wheel
*/
void mooltipass_touch_sensing::touchWaitForWheelReleased(void)
{
    uint8_t keys_detection_status;

    do
    {
        readDataFromTS(REG_AT42QT_DET_STAT, &keys_detection_status);
    }
    while (keys_detection_status & AT42QT2120_SDET_MASK);
}
Exemplo n.º 4
0
void afterTouchInitTests(void)
{
    //#define TEST_TS
    #ifdef TEST_TS
    uint8_t temp_byte;
    uint16_t temp_uint = 0;
    RET_TYPE temp_ret_type = RETURN_RIGHT_PRESSED;
    
    activityDetectedRoutine();
    oledWriteActiveBuffer();
    activateProxDetection();
    while(!(temp_ret_type & RETURN_LEFT_PRESSED))
    {
        if (temp_ret_type != RETURN_NO_CHANGE)
        {
            oledSetXY(0,0);
            readDataFromTS(REG_AT42QT_SLIDER_POS, &temp_byte);
            printf("POS: %02X\r\n", temp_byte);
            readDataFromTS(REG_AT42QT_DET_STAT, &temp_byte);
            printf("DET STAT: %02X\r\n", temp_byte);
            readDataFromTS(REG_AT42QT_KEY_STAT1, &temp_byte);
            printf("DET1: %02X\r\n", temp_byte);
            readDataFromTS(REG_AT42QT_KEY_STAT2, &temp_byte);
            printf("DET2: %02X\r\n", temp_byte);
            printf("counter: %04X\r\n", temp_uint++);
        }
        temp_ret_type = touchDetectionRoutine();     
    }
    activateGuardKey();
    launchCalibrationCycle();
    while(1)
    {
        if (temp_ret_type != RETURN_NO_CHANGE)
        {            
            oledSetXY(0,0);
            readDataFromTS(REG_AT42QT_SLIDER_POS, &temp_byte);
            printf("POS: %02X\r\n", temp_byte);
            readDataFromTS(REG_AT42QT_DET_STAT, &temp_byte);
            printf("DET STAT: %02X\r\n", temp_byte);
            readDataFromTS(REG_AT42QT_KEY_STAT1, &temp_byte);
            printf("DET1: %02X\r\n", temp_byte);
            readDataFromTS(REG_AT42QT_KEY_STAT2, &temp_byte);
            printf("DET2: %02X\r\n", temp_byte);
            printf("counter: %04X\r\n", temp_uint++);
        }
        temp_ret_type = touchDetectionRoutine();
    }
    #endif
}
/*! \fn     getTouchedButton()
*   \brief  Find which button is touched
*   \return LEFT_BUTTON or RIGHT_BUTTON
*/
RET_TYPE getTouchedButton(void)
{
    uint8_t temp_byte;
    
    readDataFromTS(AT42QT2120_ADDR, REG_AT42QT_KEY_STAT2, &temp_byte);
    
    if (temp_byte & 0x02)
    {
        return LEFT_BUTTON;
    } 
    else
    {
        return RIGHT_BUTTON;
    }
}
/*! \fn     isButtonTouched()
*   \brief  Check if the a touch button is touched
*   \return RETURN_OK or RETURN_NOK
*/
RET_TYPE isButtonTouched(void)
{
    uint8_t temp_byte;
    
    readDataFromTS(AT42QT2120_ADDR, REG_AT42QT_DET_STAT, &temp_byte);
    
    if ((temp_byte & 0x01) && !(temp_byte & 0x02))
    {
        return RETURN_OK;
    } 
    else
    {
        return RETURN_NOK;
    }
}
/*! \fn     checkTSPres()
*   \brief  Check that the AT42QT2120 is here
*   \return RETURN_OK or RETURN_NOK
*/
static inline RET_TYPE checkTSPres(void)
{
    RET_TYPE temp_return;
    uint8_t temp_byte;

    temp_return = readDataFromTS(REG_AT42QT_CHIP_ID, &temp_byte);
    if (temp_return != RETURN_OK)
    {
        return temp_return;
    }
    else if(temp_byte != AT42QT2120_ID)
    {
        return RETURN_NOK;
    }
    else
    {
        return RETURN_OK;
    }
}
/*! \fn     checkTSPres()
*   \brief  Check that the AT42QT2120 is here
*   \return RETURN_OK or RETURN_NOK
*/
uint8_t mooltipass_touch_sensing::checkTSPres(void)
{
    uint8_t temp_return;
    uint8_t temp_byte;

    temp_return = readDataFromTS(REG_AT42QT_CHIP_ID, &temp_byte);
    if (temp_return != RETURN_OK)
    {
        return temp_return;
    }
    else if(temp_byte != AT42QT2120_ID)
    {
        return RETURN_NOK;
    }
    else
    {
        return RETURN_OK;
    }
}
/*! \fn     touchDetectionRoutine(uint8_t led_mask)
*   \brief  Touch detection routine
*   \param  led_mask    Mask containing which LEDs to switchoff
*   \return Touch detection result (see touch_detect_return_t)
*/
RET_TYPE touchDetectionRoutine(uint8_t led_mask)
{
    RET_TYPE return_val = RETURN_NO_CHANGE;
    uint8_t keys_detection_status;
    uint8_t led_states[NB_KEYS];
    uint8_t temp_bool = FALSE;
    uint8_t temp_uint;
    
    // Set the LEDs on by default
    memset((void*)led_states, AT42QT2120_OUTPUT_H_VAL, NB_KEYS);
    
    // Switch them off depending on mask    
    for (temp_uint = 0; temp_uint < NB_KEYS; temp_uint++)
    {
        if (led_mask & (1 << temp_uint))
        {
            led_states[temp_uint] = AT42QT2120_OUTPUT_L_VAL;
        }
    }
    
    if (isTouchChangeDetected())
    {
        // Set temp bool to TRUE
        temp_bool = TRUE;
        
        // Read detection status register
        readDataFromTS(REG_AT42QT_DET_STAT, &keys_detection_status);
        
        // Unused byte that needs to be read        
        readDataFromTS(REG_AT42QT_KEY_STAT1, &temp_uint);
        
        // If wheel is touched
        if (keys_detection_status & AT42QT2120_SDET_MASK)
        {
            // Get position and update global var
            readDataFromTS(REG_AT42QT_SLIDER_POS, &last_raw_wheel_position);
            
            // Update LED states
            led_states[getWheelTouchDetectionQuarter()] = AT42QT2120_OUTPUT_L_VAL;
            return_val |= RETURN_WHEEL_PRESSED;
        }
        else
        {
            return_val |= RETURN_WHEEL_RELEASED;
        }

        // Read button touched register
        readDataFromTS(REG_AT42QT_KEY_STAT2, &temp_uint);
        
        // If one button is touched
        if ((keys_detection_status & AT42QT2120_TDET_MASK) && !(keys_detection_status & AT42QT2120_SDET_MASK))
        {
            if (temp_uint & 0x02)
            {
                // Left button
                led_states[TOUCHPOS_LEFT] = AT42QT2120_OUTPUT_L_VAL;
                return_val |= RETURN_LEFT_PRESSED;
                return_val |= RETURN_RIGHT_RELEASED;
            }
            else if(temp_uint & 0x08)
            {
                // Right button
                led_states[TOUCHPOS_RIGHT] = AT42QT2120_OUTPUT_L_VAL;
                return_val |= RETURN_RIGHT_PRESSED;
                return_val |= RETURN_LEFT_RELEASED;
            }
            else
            {
                return_val |= RETURN_PROX_DETECTION;
            }
        }
        else
        {
            return_val |= RETURN_PROX_RELEASED;
            return_val |= RETURN_LEFT_RELEASED;
            return_val |= RETURN_RIGHT_RELEASED;
        }
        
        // Switch on cathode if activity
        if (return_val & TOUCH_PRESS_MASK)
        {
            activityDetectedRoutine();
        }
        
        // Touch inhibit logic
        if ((return_val & TOUCH_PRESS_MASK) == 0)
        {
            touch_inhibit = FALSE;
        } 
        else if (touch_inhibit == TRUE)
        {
            return_val = RETURN_NO_CHANGE;
        }
    }    
    
    // If there's a touch change or led mask has changed
    if ((temp_bool == TRUE) || (led_mask != last_led_mask))
    {
        last_led_mask = led_mask;
        writeDataToTS(LEFT_LED_REGISTER, led_states[TOUCHPOS_LEFT]);
        writeDataToTS(RIGHT_LED_REGISTER, led_states[TOUCHPOS_RIGHT]);
        writeDataToTS(WHEEL_TLEFT_LED_REGISTER, led_states[TOUCHPOS_WHEEL_TLEFT]);
        writeDataToTS(WHEEL_TRIGHT_LED_REGISTER, led_states[TOUCHPOS_WHEEL_TRIGHT]);
        writeDataToTS(WHEEL_BLEFT_LED_REGISTER, led_states[TOUCHPOS_WHEEL_BLEFT]);
        writeDataToTS(WHEEL_BRIGHT_LED_REGISTER,  led_states[TOUCHPOS_WHEEL_BRIGHT]);
        // In some rare cases LED state changes can create detections. In that case we add a small delay
        timerBasedDelayMs(2);
        touchClearCurrentDetections();
    }
    
    return return_val;   
}
/*! \fn     touchDetectionRoutine(uint8_t led_mask)
*   \brief  Touch detection routine
*   \param  led_mask    Mask containing which LEDs to switchoff
*   \return Touch detection result (see touch_detect_return_t)
*/
uint8_t mooltipass_touch_sensing::touchDetectionRoutine(uint8_t led_mask)
{
    uint8_t return_val = RETURN_NO_CHANGE;
    uint8_t keys_detection_status;
    uint8_t led_states[NB_KEYS];
    uint8_t temp_bool = false;
    uint8_t temp_uint;

    // Set the LEDs on by default
    memset((void*)led_states, AT42QT2120_OUTPUT_H_VAL, NB_KEYS);

    // Switch them off depending on mask
    for (temp_uint = 0; temp_uint < NB_KEYS; temp_uint++)
    {
        if (led_mask & (1 << temp_uint))
        {
            led_states[temp_uint] = AT42QT2120_OUTPUT_L_VAL;
        }
    }

    if (isTouchChangeDetected())
    {
        // Set temp bool to true
        temp_bool = true;

        // Read detection status register
        readDataFromTS(REG_AT42QT_DET_STAT, &keys_detection_status);

        // Unused byte that needs to be read
        readDataFromTS(REG_AT42QT_KEY_STAT1, &temp_uint);

        // If wheel is touched
        if (keys_detection_status & AT42QT2120_SDET_MASK)
        {
            // Get position and update global var
            readDataFromTS(REG_AT42QT_SLIDER_POS, &last_raw_wheel_position);

            // Update LED states
            led_states[getWheelTouchDetectionQuarter()] = AT42QT2120_OUTPUT_L_VAL;
            return_val |= RETURN_WHEEL_PRESSED;
        }
        else
        {
            return_val |= RETURN_WHEEL_RELEASED;
        }

        // Read button touched register
        readDataFromTS(REG_AT42QT_KEY_STAT2, &temp_uint);

        // If one button is touched
        if ((keys_detection_status & AT42QT2120_TDET_MASK) && !(keys_detection_status & AT42QT2120_SDET_MASK))
        {
            if (temp_uint & 0x02)
            {
                // Left button
                led_states[TOUCHPOS_LEFT] = AT42QT2120_OUTPUT_L_VAL;
                return_val |= RETURN_LEFT_PRESSED;
                return_val |= RETURN_RIGHT_RELEASED;
            }
            else if(temp_uint & 0x08)
            {
                // Right button
                led_states[TOUCHPOS_RIGHT] = AT42QT2120_OUTPUT_L_VAL;
                return_val |= RETURN_RIGHT_PRESSED;
                return_val |= RETURN_LEFT_RELEASED;
            }
            else
            {
                return_val |= RETURN_PROX_DETECTION;
            }
        }
        else
        {
            return_val |= RETURN_PROX_RELEASED;
            return_val |= RETURN_LEFT_RELEASED;
            return_val |= RETURN_RIGHT_RELEASED;
        }

        // Switch on cathode if activity
        if (return_val & TOUCH_PRESS_MASK)
        {
            //activityDetectedRoutine();
        }
    }

    // If there's a touch change or led mask has changed
    if ((temp_bool == true) || (led_mask != last_led_mask))
    {
        last_led_mask = led_mask;
        writeDataToTS(LEFT_LED_REGISTER, led_states[TOUCHPOS_LEFT]);
        writeDataToTS(RIGHT_LED_REGISTER, led_states[TOUCHPOS_RIGHT]);
        writeDataToTS(WHEEL_TLEFT_LED_REGISTER, led_states[TOUCHPOS_WHEEL_TLEFT]);
        writeDataToTS(WHEEL_TRIGHT_LED_REGISTER, led_states[TOUCHPOS_WHEEL_TRIGHT]);
        writeDataToTS(WHEEL_BLEFT_LED_REGISTER, led_states[TOUCHPOS_WHEEL_BLEFT]);
        writeDataToTS(WHEEL_BRIGHT_LED_REGISTER,  led_states[TOUCHPOS_WHEEL_BRIGHT]);
    }

    return return_val;
}