ESP32 Capacitive Touch Sensor


#1

Hello. I was finally able to implement something in ulisp, it’s not much but it’s important to me. For the esp32 implement the function touchRead(GPIO);
indent preformatted text by 4 spaces

const char string230[] PROGMEM = "touchread";

const char doc221[] PROGMEM = "(touchread pin)\n";
{ string230, fn_touchread, 0x11, doc221 },

object *fn_touchread (object *args, object *env) {
  (void) env;
  int pin;
  object *arg = first(args);
  if (keywordp(arg)) pin = checkkeyword(TOUCHREAD, arg);
  else {
    pin = checkinteger(TOUCHREAD, arg);
    checktouchread(pin);
  }
  return number(touchRead(pin));
}

y el checkeo de los pines todavia incompleto
void checktouchread (int pin) {
#if defined(ESP8266)
  if (pin!=17) error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ESP32)
if (!(pin==0 || pin==2 || pin==3 ||(pin>=4 && pin<=6) ))
      error(TOUCHREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_FEATHER_ESP32)
  if (!(pin==4 || (pin>=12 && pin<=15) || (pin>=25 && pin<=27) || (pin>=32 && pin<=36) || pin==39))
    error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
  if (!(pin==8 || (pin>=14 && pin<=18))) error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2)
  if (!((pin>=5 && pin<=9) || (pin>=16 && pin<=18))) error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
  if (!((pin>=0 && pin<=1) || (pin>=3 && pin<=5))) error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_FEATHERS2) | defined(ARDUINO_ESP32S2_DEV)
  if (!((pin>=1 && pin<=20))) error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_ESP32C3_DEV)
  if (!((pin>=0 && pin<=5))) error(ANALOGREAD, PSTR("invalid pin"), number(pin));
#elif defined(ARDUINO_ESP32S3_DEV)
if (!(pin==0 || pin==2 || pin==3 || (pin>=4 && pin<=9 ))
    error(TOUCHREAD, PSTR("invalid pin"), number(pin));
  
#endif
}

How can I do so that when I install a new version of ulisp I don’t have to write all these changes again.
Thanks


#2

Hello,

Maybe adding a new function will work for you. Please check here: http://www.ulisp.com/show?19Q4

Best Regards


#3

I’ve added formatting to your program.

How can I do so that when I install a new version of uLisp I don’t have to write all these changes again.

Currently there isn’t an easy way to do this. You’d need to add it again to a new version.


#4

Thanks.
I am going to map the pins of the other esp32 boards well so that the checktouchread() function works on all boards with these capabilities


#5

All the esp32 are mapped with their touch pins.

   void checktouchread (int pin) {
    #if defined(ESP8266)
         error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ESP32)
    if (!(pin==0 || pin==2 || pin==3 ||(pin>=4 && pin<=6) ))error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_FEATHER_ESP32)
      if (!(pin>=12 && pin<=15) || pin==27 || pin==32 || pin==33) error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_ADAFRUIT_FEATHER_ESP32S2)
      if (!((pin>=8 && pin<=13)|| (pin>=3 && pin<=6)))error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32S2)
      if (!((pin>=5 && pin<=9))) error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_ADAFRUIT_QTPY_ESP32C3)
       error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_FEATHERS2) | defined(ARDUINO_ESP32S2_DEV)
      if (!( pin==1 || pin==3 || (pin>=5 && pin<=13))) error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_ESP32C3_DEV)
      error(TOUCHREAD, PSTR("invalid pin"), number(pin));
    #elif defined(ARDUINO_ESP32S3_DEV)
    if (!(pin==0 || pin==2 || pin==3 || (pin>=4 && pin<=9 ))
        error(TOUCHREAD, PSTR("invalid pin"), number(pin));
      
    #endif
    }