1 Reply Latest reply on Dec 18, 2017 5:35 PM by rzzh

    Low power CapSense design for guard sensor detection.

    user_498719592

      Hi Everyone,

       

      For my case, I am using the CE214022 to create a low power Capsense design, and add a widget to detect a liquid stream for disable the touch, but seems this guard detection were failed, do you have any idea that where I can put the guard detection process into my code? The main code is as following, thanks!

      int main()
      {    
          /* Variable to hold current button status */
          uint32 buttonStatus = OFF;        
      
          
          /* Variable to hold the current device state 
          *  State machine starts with sensor scan state after power-up
          */
          DEVICE_STATE currentState = SENSOR_SCAN; 
          
          /* This variable is used to indicate the current power mode */
          SCAN_MODE deviceScanMode = FAST_SCAN_MODE;
          
          /* This variable is used to implement a software counter. If the value 
          *  of this counter is greater than SCANMODE_TIMEOUT_VALUE, it indicates that the button sensor 
          *  was inactive for more than 3s. 
          */
          uint16 softCounter = RESET;
      
          /* Enable global interrupts. */ 
          CyGlobalIntEnable;    
      #if TUNER
          /* Initialize I2C component for CapSense tuner */
          EZI2C_Start();
          
          /* Set up communication data buffer to CapSense data structure to 
          *  expose to I2C master at primary slave address request        
          */
          EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam),\
                               (uint8 *)&CapSense_dsRam);
          CapSense_Start(); /* Initialize Component */
          CapSense_ScanAllWidgets(); /* Scan all widgets */
          for(;;)
          {
              /* Do this only when a scan is done */
              if(CapSense_NOT_BUSY == CapSense_IsBusy())
              {
                  CapSense_ProcessAllWidgets(); /* Process all widgets */
                  CapSense_RunTuner(); /* To sync with Tuner application */
                  if (CapSense_IsAnyWidgetActive()) /* Scan result verification */
                  {
                  /* add custom tasks to execute when touch detected */
                  }
                  CapSense_ScanAllWidgets(); /* Start next scan */
              }
          }
      #else
          RX_ISR_StartEx(uart_int);
          /* Initialize the CapSense CSD block */
          CapSense_Start();
          
          /* Configure CapSense parameters for button widget to scan them individually */
          CapSense_SetupWidget(CapSense_BUTTON_WDGT_ID);
          //CapSense_SetupWidget(CapSense_GUARDSENSOR_WDGT_ID);
                                  
          /* Watchdog is used to control the loop time in this project and watchdog
          *  is set to generate interrupt at every LOOP_TIME_FASTSCANMODE in fast scan mode  
          *  and at LOOP_TIME_SLOWSCANMODE in slow scan mode
          */
          WDT_Start();
          
          /* Put the num 5 cap raw data to srand*/
          srand(CapSense_BUTTON_SNS5_RAW0_VALUE);
          
          //show_preamble();
          UART_Start();
      
          all_led_on();
          initialiseFlag = TRUE;
          
          needPreamble = PREAMBLE_ON;
          set_mode(ACTIVE_MODE);
          
          for(;;)
          {
              /* Switch between sensor-scan -> wait-for-scan -> process -> sleep states */
              switch(currentState)
              {
                  case SENSOR_SCAN:                 
                      /* Initiate new scan only if the CapSense hardware is idle */
                      if(CapSense_NOT_BUSY == CapSense_IsBusy())
                      {   
                          
                          /* In fast scan mode, left button and right button is scanned individually */
                          if(deviceScanMode == FAST_SCAN_MODE)
                          {
                              /* Scan the sensors which are in the button widget i.e left button and right button */
                              CapSense_ScanAllWidgets();
                          }
                          /* In slow scan mode, only ganged sensor is scanned. Since we have only one ganged sensor,
                          *  the sensor is always connected to AMUXBUS in this mode.
                          *  This avoids time taken to connect the sensor to AMUXBUS every time the ganged sensor is scanned. 
                          */
                          else
                          {
                              CapSense_CSDScanExt();
                          } 
                          /* Put CPU to sleep while sensor scanning is in progress */
                          currentState = WAIT_FOR_SCAN_COMPLETE;                    
                      }
                  break;
      
                  case WAIT_FOR_SCAN_COMPLETE:
                      /* Device is in CPU Sleep until CapSense scanning is complete or
                      *  device is woken-up by either CapSense interrupt or I2C interrupt 
                      */
                      /* Disable interrupts, so that ISR is not serviced while
                      *  checking for CapSense scan status.
                      */
                      interruptState = CyEnterCriticalSection();
                                  
                      /* Check if CapSense scanning is complete */
                      if(CapSense_NOT_BUSY != CapSense_IsBusy())
                      {
                          /* If CapSense scannning is in progress, put CPU to sleep */
                          CySysPmSleep();
                      }
                      /* If CapSense scanning is complete, process the CapSense data */
                      else
                      {
                          currentState = PROCESS_DATA;
                      }
                      /* Enable interrupts for servicing ISR */
                      CyExitCriticalSection(interruptState);
                  break;
              
                  case PROCESS_DATA:
                      /* Set next state to SLEEP */
                      currentState = SLEEP;
                      
                      /* Clear the variable that is used for both the button status */
                      buttonStatus = CLEAR;
                      
                      if(deviceScanMode == FAST_SCAN_MODE)
                      {
                          
                          /* Update baseline and sensor status for both the button sensors */
                          CapSense_ProcessWidgetExt(CapSense_BUTTON_WDGT_ID, CapSense_PROCESS_CUSTOM);
                          
                          /* If button widget is reported active, process the 
                          *  button active status 
                          */
                          if(CapSense_IsWidgetActive(CapSense_BUTTON_WDGT_ID) && ((0x01 << CapSense_GUARDSENSOR_WDGT_ID) != CapSense_WDGT_STATUS0_VALUE))
                          {
      
                              //if(!CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS12_ID))
                              //{
                              //preamble();
                              /* Set left button state */
                              if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS0_ID))
                              {
                                  buttonStatus |= NUM0_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS1_ID))
                              {
                                  buttonStatus |= NUM1_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS2_ID))
                              {
                                  buttonStatus |= NUM2_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS3_ID))
                              {
                                  buttonStatus |= NUM3_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS4_ID))
                              {
                                  buttonStatus |= NUM4_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS5_ID))
                              {
                                  buttonStatus |= NUM5_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS6_ID))
                              {
                                  buttonStatus |= NUM6_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS7_ID))
                              {
                                  buttonStatus |= NUM7_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS8_ID))
                              {
                                  buttonStatus |= NUM8_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS9_ID))
                              {
                                  buttonStatus |= NUM9_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS10_ID))
                              {
                                  buttonStatus |= ENTER_BUTTON_MASK;
                              }
                              /* Set right button state */
                              else if(CapSense_IsSensorActive(CapSense_BUTTON_WDGT_ID, CapSense_BUTTON_SNS11_ID))
                              {
                                  buttonStatus |= LOCK_BUTTON_MASK;
                              }
                              
                              
                              /* Control RGB LED based on button status */
                              LED_Control(buttonStatus);
                              
                              /* Reset the software counter if any button is active. */
                              softCounter = RESET;
                              
                          }
                          /* If none of the buttons are active, execute below code */
                          else
                          {
                              /* Increment the software counter every LOOP_TIME_FASTSCANMODE if button 
                              *  touch is not detected. 
                              */
                              softCounter++;  
                              
                              LED_Control(buttonStatus);
      
                              /* Update previous button status which is saved in LED_Control API */
                              //LED_Control(buttonStatus);
                              /*
                              if((softCounter >= LED_OFF_DELAY_VALUE) && (prevButtonStatus != OFF))
                              {
                                  switch(prevButtonStatus)
                                  {
                                      case NUM0_BUTTON_MASK:
                                          Num0_Write(0);
                                          break;
                                      default:
                                          all_led_on();
                                          break;
                                  }
                                  prevButtonStatus = OFF;
                                  
                              }
                              */
                              /* If finger is not on sensor for SCANMODE_TIMEOUT_VALUE, switch off the 
                              *  LEDs and switch mode to slow scan mode to reduce power consumption 
                              */
                              if(softCounter >= SCANMODE_TIMEOUT_VALUE)
                              {   
                                  /* Watchdog is configured to generate interrupt at LOOP_TIME_SLOWSCANMODE */
                                  watchdogMatchValue = WDT_TIMEOUT_SLOW_SCAN;
                                  
                                  initialiseFlag = FALSE;
                                  
                                  /* Set mode to slow scan mode to scan sensors at LOOP_TIME_SLOWSCANMODE */
                                  deviceScanMode = SLOW_SCAN_MODE;
                                  
                                  /* Configure ganged sensor and connect it to AMUXBUS */
                                  CapSense_CSDSetupWidgetExt(CapSense_GANGEDBUTTON_WDGT_ID, CapSense_GANGEDBUTTON_SNS0_ID);
                                  
                                  
      
                              }  
                          }
                      }   
                      /* If deviceScanMode is SLOW_SCAN_MODE, perform the following tasks */    
                      else if(deviceScanMode == SLOW_SCAN_MODE)
                      {   
                          /* Update baselines of ganged button widget */
                          CapSense_ProcessWidget(CapSense_GANGEDBUTTON_WDGT_ID);
                          
                          /* If ganged widget is active in slow scan mode move to 
                          *  fast scan mode where sensors are scanned individually at LOOP_TIME_FASTSCANMODE
                          */
                          
                          if(CapSense_IsWidgetActive(CapSense_GANGEDBUTTON_WDGT_ID))
                          {
                              deviceScanMode = CHECK_GUARD;
                              currentState = SENSOR_SCAN;
                              CapSense_CSDSetupWidgetExt(CapSense_GUARDSENSOR_WDGT_ID, CapSense_GUARDSENSOR_SNS0_ID);
                          }
                      }
                      else
                      {
                          CapSense_ProcessWidget(CapSense_GUARDSENSOR_WDGT_ID);
                          if(!CapSense_IsWidgetActive(CapSense_GUARDSENSOR_WDGT_ID))
                          {
                              //CapSense_CSDSetupWidgetExt(CapSense_GUARDSENSOR_WDGT_ID, CapSense_GUARDSENSOR_SNS0_ID);
                              //CapSense_CSDScanExt();
                              //CapSense_SetupWidget(CapSense_GUARDSENSOR_WDGT_ID);
                              //CapSense_ProcessWidget(CapSense_GUARDSENSOR_WDGT_ID);
                              //if(CapSense_IsWidgetActive(CapSense_GUARDSENSOR_WDGT_ID))
                              //{
                              
                              currentState = SENSOR_SCAN;
                              
                              /* Set watchdog match value to fast scan mode */
                              watchdogMatchValue = WDT_TIMEOUT_FAST_SCAN;      
                              
                              /* Change the device mode to fast scan mode to provide fast touch response */
                              deviceScanMode = FAST_SCAN_MODE; 
                              
                              /* Configure button widgets parameters for scanning in fast scan mode */
                              CapSense_SetupWidget(CapSense_BUTTON_WDGT_ID);
                              //CapSense_SetupWidget(CapSense_GUARDSENSOR_WDGT_ID);
                          }
                          else
                          {
                              currentState = SENSOR_SCAN;
                              deviceScanMode = SLOW_SCAN_MODE;
                              CapSense_CSDSetupWidgetExt(CapSense_GANGEDBUTTON_WDGT_ID, CapSense_GANGEDBUTTON_SNS0_ID);
                          }
                      }
                      if(UARTRX_Buf)
                      {
                          switch(UARTRX_Buf)
                          {
                              case 0x61:
                              case 0x62:
                                  needPreamble = UARTRX_Buf;
                                  break;
                              case 0x63:
                                  set_mode(SETTING_MODE);
                                  break;
                              case 0x66:
                                  softCounter = SCANMODE_TIMEOUT_VALUE;
                                  break;
                              case 0x68:
                                  break;
                              case 0x6B:
                                  break;
                              case 0x6C:
                                  break;
                              default:
                                  break;
                          }
                          UARTRX_Buf = 0;
                      }
                  break;
                  
                  case SLEEP:             
                      /* In fast scan mode, put the device to only CPU sleep as TCPMW block is ON
                      *  to drive RGB LEDs and it cannot work in deep-sleep mode
                      */
                      if(deviceScanMode == FAST_SCAN_MODE)
                      {   
                          CySysPmSleep();
                      }
                      /* In slow scan mode, device can be put to deep-sleep as TCPWM is off */
                      else
                      {
                          //Send_UART(0x6A);
                          if(doorlockState != SLEEP_MODE)
                          {
                              Send_UART(0x6A);
                              set_mode(SLEEP_MODE);
                          }
                          EnterDeepSleepLowPowerMode();
                      }
                      /* Start scanning the sensors only if interrupt occured due to WDT.
                         Interrupt can also occur due to I2C interrupt while tuner is running.
                         In such cases, sensor is not scanned until WDT interrupt has occured
                      */
                      if(wdtInterruptOccured)
                      {
                          /* Set state to scan sensor after device wakes up from sleep */
                          currentState = SENSOR_SCAN;
                          
                          wdtInterruptOccured = FALSE;  
                          
                          /* Calibrate the ILO to ensure accurate scan intervals */
                          CalibrateWdtMatchValue();
                      }
                  break;
                  
                  default:
                      /*******************************************************************
                       * Unknown power mode state. Unexpected situation.
                       ******************************************************************/
                      CYASSERT(0);
                  break;
              } 
          }
      #endif
      }