Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

touchAttachInterrupt() userfunc parameter should not be void #4321

Closed
ericgu opened this issue Sep 2, 2020 · 8 comments · Fixed by #6194
Closed

touchAttachInterrupt() userfunc parameter should not be void #4321

ericgu opened this issue Sep 2, 2020 · 8 comments · Fixed by #6194
Assignees
Labels
Area: Peripherals API Relates to peripheral's APIs.

Comments

@ericgu
Copy link

ericgu commented Sep 2, 2020

I'm working on some code that uses the touch subsystem and the arduino HAL.

I can call touchAttachInterrupt() multiple times for different pins. The underlying code in __touchISR() knows which routine to call for a specific pin, but it doesn't pass any pin information on.

This forces me to write something like:
touchAttachInterrupt(4, Touch0Interrupt, 20);
...
touchAttachment(32, Touch9Interrupt, 20);

touchAttachInterrupt() should store the pin number away at the same time it stores the function pointer away and then pass it on the callback. That would let me write a routine like:

void MyTouchHandler(int pinNumber)
{

}

and handle all the touch pins with a single chunk of generic code.

@sansillusion
Copy link

Not sure as I'm not much of a programmer but would something like that work ? :
touchAttachInterrupt(4, TouchInterrupt(0), 20);
touchAttachment(32, TouchInterrupt(9), 20);

@ericgu
Copy link
Author

ericgu commented Sep 3, 2020

Unfortunately, that does not work, but you did get me thinking that I can probably do something with the C++ lambda support. Something like:

int pin = 32;
touchAttachInterrupt(pin, pin{MyInterruptHandler(pin);});

assuming I have my lambda syntax correct. That's not great but it's better than what I had planned from a syntax perspective, though the overhead might be worse...

Edit: there should be brackets around the second "pin".

@ericgu
Copy link
Author

ericgu commented Sep 3, 2020

Lambdas do work but not in the format I used; you can't pass lambdas that do captures to function pointers, so they aren't really any better than my initial implementation.

@stale
Copy link

stale bot commented Nov 2, 2020

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Nov 2, 2020
@stale
Copy link

stale bot commented Nov 19, 2020

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Nov 19, 2020
@Leo-C
Copy link

Leo-C commented Feb 24, 2021

Hi,
I believe that function esp_sleep_get_touchpad_wakeup_status() can help you.

Interrupt handler can not get a parameter indicating pin touched, but calling function above as first instruction, you'll have same information

Leonardo

@SuGlider
Copy link
Collaborator

New Touch API will have this function touchAttachInterruptArg(...)

/*
 * Set cycles that measurement operation takes
 * The result from touchRead, threshold and detection
 * accuracy depend on these values. Defaults are
 * 0x1000 for measure and 0x1000 for sleep.
 * With default values touchRead takes 0.5ms
 * */
void touchSetCycles(uint16_t measure, uint16_t sleep);

/*
 * Read touch pad (values close to 0 mean touch detected)
 * You can use this method to chose a good threshold value
 * to use as value for touchAttachInterrupt
 * */
uint32_t touchRead(uint8_t pin);

/*
 * Set function to be called if touch pad value falls (ESP32)
 * below the given threshold / rises (ESP32-S2/S3) by given increment (threshold). 
 * Use touchRead to determine a proper threshold between touched and untouched state
 * */
void touchAttachInterrupt(uint8_t pin, void (*userFunc)(void), uint32_t threshold);
void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold);
void touchDetachInterrupt(uint8_t pin);

/*
 * Specific functions to ESP32 
 * Tells the dirver if it shall activate the ISR if the sensor is Lower or Higher than the Threshold
 * Default if Lower.
 **/
void touchInterruptSetThresholdDirection(bool mustbeLower);

@VojtechBartoska VojtechBartoska added Area: Peripherals API Relates to peripheral's APIs. and removed Status: Stale Issue is stale stage (outdated/stuck) labels Jan 26, 2022
me-no-dev pushed a commit that referenced this issue Feb 3, 2022
Summary

Touch Sensor refactoring to be based on IDF 4.4.
Adds support to ESP32S2 and future ESP32S3.

Adds some new APIs:

For all chips:

void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold);
This function allows the user to add and pass a void* parameter to the ISR user callback.

void touchDetachInterrupt(uint8_t pin);
This function detaches ISR call back for the touch pad pin.

Only ESP32 chip

void touchInterruptSetThresholdDirection(bool mustbeLower);
This function allows the user to set if the ISR callback will be activated when the touch sensor readings are lower or higher than the threshold defined. See example TouchButton.ino.

Only ESP32-S2 and ESP32-S3 chips

bool touchInterruptGetLastStatus(uint8_t pin);
This function reports if the touch pad pin is touched or untouched. It can be used with ISR to identify when it is touched and untouched (released). See example TouchButtonV2.ino.

Impact

None. ll original APIs and examples now run on ESP32 and ESP32-S2.

Related links

Fix #6095
Fix #6034
Fix #5799
Fix #5745
Fix #5527
Fix #5493
Fix #4321
Fix #4044
Fix #2722
Fix #2625
Jason2866 added a commit to Jason2866/arduino-esp32 that referenced this issue Feb 4, 2022
* Fix wdt OTA update

* Fix ESP32-Solo WDT on HTTP OTA update

Co-authored-by: Theo Arends <11044339+arendst@users.noreply.github.com>

* Fix ESP32-Solo WDT on HTTP OTA update

* Fix ESP32-Solo WDT on HTTP OTA update

Co-authored-by: Theo Arends <11044339+arendst@users.noreply.github.com>

* IDF master c69f0ec32

* Fix WDT only for solo core

* IDF master 58022f859

* IDF master 606557b48

* IDF master 8131d6f46

* IDF master 59aa60d52

* IDF master 83956ebba

* IDF master 83956ebbae

* IDF master b1c3ee71c5

* IDF master 2c49af9e75

* IDF master eb1a66e7c9

* IDF master a20df743f1

* Fix build compilation due to changes in the HW_TIMER's structs

* Fix compilation warnings and errors with USB

* Update USBCDC.cpp

* Update CMakeLists.txt

* IDF master b63ec47238

* Update libarduino_tinyusb.a

* Update HWCDC.cpp

* IDF master 5f38b766a8

* IDF master b1f851b8f8

* IDF master 61299f879e

* Delete tools/sdk/esp32/include/coap directory

* Delete tools/sdk/esp32c3/include/coap directory

* Delete tools/sdk/esp32s2/include/coap directory

* solve conflicts

* force Dynamic Buffer instead of Static ones

* Remove debug if

* Resolve conflicts

* Revert espressif#5861

* add GPIO defines for C3

* Edit sd_diskio to check card status

* Update CMakeLists.txt

* Delete libraries/RainMaker directory

* Update README.md

* Update README.md

* Revert "RMT refactor"

* Fixes Lib Builder compiling errors

* Fixes Lib Builder compiling errors

* Revert "Revert "RMT refactor""

* Update esp32-hal-rmt.c

* Update esp32-hal-rmt.c

* Update esp32-hal-rmt.c

* Update esp32-hal-rmt.c

* Update esp32-hal-gpio.c

* Bugfix of ff_sd_status

* Fix I2C Slave Compile

I2C Slave currently doesn't compile for projects where Arduino is an IDF component.  This adds missing conditionals.

* add missing c3 define

when disable HAL lock is set

* add ESP32C3 `esp32_adc2gpio`

* Fixes UART MODBUS and Loopback issue

* Update esp32-hal-uart.c

* Revert "Fixing interrupts in LEDC (espressif#6160)"

This reverts commit a593206.

* Update esp32-hal.h

* use Tasmota github

* Use Tasmota platform for CI

* tasmota eaptool v3.2

* add missing c3 define

* enable SPI hal for S3

* revert enable spi for S3

* Update Esp.cpp

* Update Esp.cpp

* fix S3 uart gpios

* fix S3 flash image base (espressif#6208)

* Added my new Unexpected Maker ESP32-S3 boards (espressif#6211)

* Fixes UART1 and UART2 default pins for ESP32-S3 (espressif#6202)

Summary

This PR fixes an issue with UART1 default pins.
When using pins RX_1 = 18 and TX_1 = 17, UART1 will display a Break Error on those pins when they are floting (not connected).

It also defines RX_2 = 19 and TX_2 = 20 as default pins for UART2.

Impact

The deaulf pins may look different from the original pinout diagram.

* Fix - SD mount issue (espressif#6162)

* sdSelectCard longer timeout for sdWait

* GO_IDLE_STATE command ignores sdWait fail

* fix: restoring handshake timeout (espressif#6165) (espressif#6166)

* Use 8.4.0+2021r2-patch2 toolchains for CI (espressif#6184)

with platformio

* Fixes onReceive deadlock (espressif#6201)

* added explanatory comments to WebServer.h (espressif#6204)

* Add conditional include to WiFiProv.h (espressif#6192)

Resolves issue espressif#6171

* Resolve WString TODO (espressif#6190)

Resolve TODO (XXX) by logging warning message.

* Allow HTTPCLIENT_1_1_COMPATIBLE to be disabled (espressif#6200)

Allow a user to disable the HTTPCLIENT_1_1_COMPATIBLE flag from the command line, or whichever means available.

* Update RequestHandlersImpl.h (espressif#6179)

With LittleFS the `fs.exists(path)` returns true also on folders. A `isDirectory()` call is required to set _isFile to false on directories.
This enables serving all files from a folder like : `server->serveStatic("/", LittleFS, "/", cacheHeader.c_str());
        File f = fs.open(path);
        _isFile = (f && (! f.isDirectory()));

* Refactor the CI scripts (espressif#6191)

The reason behind this refactoring is that all of the sketch related functions can (and will) be used for other purposes.

Build in the sketch directory: This will make it easy to handle artifacts after the build.
Separate sketch related functions from IDE installation script. This is the main commit.
Create a separate job for the Cmake check. This check was part of one of the Linux build. I believe that it's not the best place for such a check.
Checking for the skip landmarks and validity of the the sketch directory were already done by count_sketches.

* Fixes UART_1 TX Pin setup

When passing the PR to this branch, it seems that the change to this line was forgotten.

* Fix random CaptivePortal.ino crashes (espressif#6206)

CaptivePortal.ino example did randomly crash for me ... so I start investigate ;-)

Decoding stack results
0x4016faea: WiFiUDP::write(unsigned char const*, unsigned int) at C:\Users\knoeb\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.5\libraries\WiFi\src\WiFiUdp.cpp line 201
0x400d4a4a: DNSServer::replyWithIP() at C:\Users\knoeb\AppData\Local\Temp\arduino_build_486825\sketch\src\DNSServer\DNSServer.cpp line 187
0x400d4d01: DNSServer::processNextRequest() at C:\Users\knoeb\AppData\Local\Temp\arduino_build_486825\sketch\src\DNSServer\DNSServer.cpp line 117
0x400d3e81: loop() at D:\Drive\Dokumente\HTL_Lehrer\2021_22\Projekte\Stromzaehler_Patrick\arduino/arduino.ino line 1078
0x400dd545: loopTask(void*) at C:\Users\knoeb\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.5\cores\esp32\main.cpp line 37
0x4008a0de: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

I found with wireshark a possibility to trigger the crash on demand is:
nslookup 3.1o1osr0092ons87rp375p1pq8q066o8p56or1sqsps6rs17r4384q9748qr1r52.699p1r741q737393648s29917o45p16q50rn517rnsp73pp68p1q259s92693qp.s607408539s0p06p7559os0899866344r7qq7rpns960o9576q65.r5n94r5so9784pq1.i.03.s.sophosxl.net

The problem was that QNameLength is a signed byte and therefore its not possible to count up to 255. Additionally we need 256 bytes for the QName string to accommodate for the zero termination.

* Update HardwareSerial.cpp

* Update Esp.cpp

* Update Esp.cpp

* Bootloader is at 0x0000 for S3

* Initial SPI support and S3-Box variant

* Docs cleanup and version updated to 2.0.2 (espressif#6213)

* Docs cleanup and version updated to 2.0.2

* Removed issue template information

* Removed issue template file

* Added referecnces for the issue and feature request form

* Bootloader is at 0x0000 for S3 (espressif#6215)

* Update esp32-hal-cpu.c

* Update esp32-hal-spi.c

* Unnecessary operation removed from map() in WMath.cpp (espressif#6218)

* Unneccesary Operation Removed

(A) extra operation not needed and incorrect:
      wrong by 0.5 but happens to be thrown out

     ( delta * dividend + (divisor / 2) ) / divisor

        delta * dividend     divisor
    = ---------------- + -----------
        divisor                    2 * divisor

    = delta * dividend / divisor + 1/2

(B) check first before doing other computations

(C) changed to rise/run, easier for future maintainer
      since it's closer to equation of a line

(D) before: mult, shift, add, div, add
      now: mult, div, add

(E) error message easier to trace where thrown

* Update WMath.cpp

forgot to change variable name

* framebuffer location for no-psram boards (espressif#6219)

If board has no PSRAM, we need to set the framebuffer location to DRAM:
config.fb_location = CAMERA_FB_IN_DRAM;

* Update install-platformio-esp32.sh

* small adjustments for NVS and PSRAM init

* WIP: Initial support for PSRAM (QSPI and OPI)

* Fix replace() failing

* Touch Sensor IDF Refactoring (espressif#6194)

Summary

Touch Sensor refactoring to be based on IDF 4.4.
Adds support to ESP32S2 and future ESP32S3.

Adds some new APIs:

For all chips:

void touchAttachInterruptArg(uint8_t pin, void (*userFunc)(void*), void *arg, uint32_t threshold);
This function allows the user to add and pass a void* parameter to the ISR user callback.

void touchDetachInterrupt(uint8_t pin);
This function detaches ISR call back for the touch pad pin.

Only ESP32 chip

void touchInterruptSetThresholdDirection(bool mustbeLower);
This function allows the user to set if the ISR callback will be activated when the touch sensor readings are lower or higher than the threshold defined. See example TouchButton.ino.

Only ESP32-S2 and ESP32-S3 chips

bool touchInterruptGetLastStatus(uint8_t pin);
This function reports if the touch pad pin is touched or untouched. It can be used with ISR to identify when it is touched and untouched (released). See example TouchButtonV2.ino.

Impact

None. ll original APIs and examples now run on ESP32 and ESP32-S2.

Related links

Fix espressif#6095
Fix espressif#6034
Fix espressif#5799
Fix espressif#5745
Fix espressif#5527
Fix espressif#5493
Fix espressif#4321
Fix espressif#4044
Fix espressif#2722
Fix espressif#2625

* [FEATURE] Basic implementation of Arduino's I2S library (espressif#5304)

Basic version of Arduino's I2S library. We currently support only 16bps + 16000 Hz sampling frequency. Other bitrates and sample rates will print warning and continue to operate, however the resulting audio quality may be poor.
There will be further effort to fix these issues.

* Update CMakeLists.txt

* remove Rainmaker

* [I2S] Fix lib-builder error

* Update CMakeLists.txt

* try to find lib builder fail

* Update CMakeLists.txt

* Update idf_component.yml

Co-authored-by: Theo Arends <11044339+arendst@users.noreply.github.com>
Co-authored-by: me-no-dev <me-no-dev@users.noreply.github.com>
Co-authored-by: me-no-dev <hristo@espressif.com>
Co-authored-by: Jan Procházka <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Co-authored-by: Jason2866 <jason2866@github.com>
Co-authored-by: Rodrigo Garcia <Rodrigo.Garcia@espressif.com>
Co-authored-by: mrengineer7777 <44048235+mrengineer7777@users.noreply.github.com>
Co-authored-by: Unexpected Maker <seon@unexpectedmaker.com>
Co-authored-by: Vlasta Hajek <vlastimil.hajek@bonitoo.io>
Co-authored-by: Enes Şimşek <62627966+h-enes-simsek@users.noreply.github.com>
Co-authored-by: Clemens Kirchgatterer <clemens@1541.org>
Co-authored-by: Matthias Hertel <mathertel@hotmail.com>
Co-authored-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
Co-authored-by: Patrick Behal <5472845+im-pro-at@users.noreply.github.com>
Co-authored-by: Pedro Minatel <pminatel@gmail.com>
Co-authored-by: IanSC <8453489+IanSC@users.noreply.github.com>
Co-authored-by: René Bohne <rene.bohne@gmail.com>
Co-authored-by: s-hadinger <49731213+s-hadinger@users.noreply.github.com>
Co-authored-by: Tomáš Pilný <34927466+PilnyTomas@users.noreply.github.com>
@jdeltoft
Copy link

Does this new touchAttachInterruptArg() api support wake up from deep sleep? Or is that only supported with touchAttachInterrupt().

I have this for setup:

void setup() {
  touch_pad_init();

  for(int i=0; i<TOUCH_PIN_COUNT; i++) {
    touchAttachInterruptArg(pinNames[i], gotTouch, &pinIds[i], capTouchThreshold);
    touchSleepWakeUpEnable(pinNames[i], 500);
  }

  esp_sleep_enable_touchpad_wakeup();
}

And then if I call esp_deep_sleep_start(); I lose my serial connection and never seem to wake up from any touch. If I just remove this deep sleep call, everything works perfect for the 5 cap touch inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Peripherals API Relates to peripheral's APIs.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants