Page 1 of 1

Capacitive touch example?

Posted: Sun Jan 22, 2017 3:15 am
by dspau1
Hi, I'm looking for a code example for capacitive touch functions - read current value, attach interrupt handler with threshold, etc.

The only example I have found so far is in the Arduino build https://github.com/espressif/arduino-esp32/issues/112, which exposes touchSetCycles, touchRead, touchAttachInterrupt, however this has not helped me figure out the native API.

Anyone got this working yet?

Thanks!

Re: Capacitive touch example?

Posted: Sun Jan 22, 2017 4:58 am
by kolban
I'm not a great fan of pointing someone to a header file and saying "there it is" ... I much prefer some guidance ... but thankfully this one looks simple enough. Apparently there is a touch pad driver and there is a header for it here.

https://github.com/espressif/esp-idf/bl ... ouch_pad.h

There only appears to be a couple of APIs and they look simple enough. The really good news is that if you read down to the bottom of the header there appear to be examples included. Have a look through this header and have a bash. If you have further questions or experiences that cause problems, post back to this thread and we'll pick it up again.

Re: Capacitive touch example?

Posted: Sun Jan 22, 2017 6:28 am
by dspau1
Hi Neil, thanks for the reply... I'm ok with the first part after some head scratching re parameter names:

Code: Select all

 *   touch_pad_init();//only init one time
 *   touch_pad_config(0,300);//set the intr threshold,use touch_pad_read to determine this threshold 
 *   touch_pad_isr_handler_register(rtc_intr,NULL, 0, NULL)
However I have not a clue what the callback function does. Is this level of complexity really necessary to get a reference to the triggering touch pad and read its value? Is there a simple way to add a separate callback function to each touch pad or do all pads have to reference the same callback function?

Code: Select all

void rtc_intr(void * arg)
 *   {
 *       uint32_t pad_intr = READ_PERI_REG(SARADC_SAR_TOUCH_CTRL2_REG) & 0x3ff;
 *       uint8_t i = 0;
 *       uint32_t rtc_intr = READ_PERI_REG(RTC_CNTL_INT_ST_REG);
 *       WRITE_PERI_REG(RTC_CNTL_INT_CLR_REG, rtc_intr);
 *       SET_PERI_REG_MASK(SARADC_SAR_TOUCH_CTRL2_REG, SARADC_TOUCH_MEAS_EN_CLR);
 *       if (rtc_intr & RTC_CNTL_TOUCH_INT_ST) {
 *           for (i = 0; i < TOUCH_PAD_MAX; ++i) {
 *               if ((pad_intr >> i) & 0x01) {
 *                   ets_printf("touch pad intr %u\n",i);
 *               }
 *           }
 *       }
 *  }

Re: Capacitive touch example?

Posted: Sun Jan 22, 2017 7:14 am
by WiFive
There is only one rtc interrupt so it has to be shared (similar to gpio interrupt). You could implement an isr service like gpio to have separate callbacks per pin. Arduino already does this so you could use that code.

Re: Capacitive touch example?

Posted: Sun Jan 22, 2017 3:11 pm
by dspau1
Ok thanks, I'll take a look at the arduino code.

My expectation was that idf would abstract away most of the uglyness - having to read and write registers directly etc. Is this a symptom of the early stage of development, or am I misunderstanding the purpose of idf?

Re: Capacitive touch example?

Posted: Sun Jan 22, 2017 6:38 pm
by WiFive
Yes this is probably not final version of the driver. Registers are beautiful though ;)

Re: Capacitive touch example?

Posted: Mon Jan 23, 2017 4:49 pm
by enitalp
i used the arduino code in my ISP-IDF project with a test code :
#define BRACELET_ON GPIO_NUM_27

while(true)
{
int l_Value = touchRead(BRACELET_ON);
if (l_Value<20)
{
l_NbTouchFrame++;
}
else
{
l_NbTouchFrame = 0;
}
if (l_NbTouchFrame > 4)
{
digitalWrite(LED_TOUCH,1);
}
else
{
digitalWrite(LED_TOUCH,0);
}
}

copied the esp32-hal-touch.c and gpio.c and .h from the esp32 arduino fix one or two compile errors. and it worked

Re: Capacitive touch example?

Posted: Sun Jan 29, 2017 4:51 am
by dspau1
Any official word on touch driver updates for esp-idf v2? I see in the v2 release notes under "New drivers" is "touch pad"...

My vote would be for a touch pad ISR service similar to GPIO.