-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathesp32_touch_example.cpp
82 lines (73 loc) · 2.74 KB
/
esp32_touch_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/** @file esp32_touch_example.cpp
* ********************** ESP32Touch basic example ****************************
*
* This registers three touch button inputs switching on/off and toggling
* an LED connected to GPIO 2 (on-board blue LED on many popular boards).
*
* All of the operation takes place asynchronously via user-defined
* callback functions, which can be any type compatible with a C++
* std::function object, i.e. plain global functions, static and non-static
* class or instance members or C++11 lambda expressions.
* The callbacks must have a signature of void(void).
*
*
* For the sensor input pins, please again note that the touch API uses
* a different numbering scheme than the standard GPIO numbers.
* E.g. touch button no. 0 is GPIO 4.
*
* I can also recommend the ESP32 pinout reference and reserved pins info from:
* https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
*
* GPIOs used in this example: <p>
* GPIO2: LED output, tied to GND via 220...470 Ohms <br>
* GPIO12: Touch sensor input for OFF <br>
* GPIO13: Touch sensor input for ON <br>
* GPIO15: Touch sensor input for toggling </p>
*
* Repository at: https://github.com/ul-gh/ESP32Touch
* 2020-02-25 Ulrich Lukas
*/
#include <Arduino.h>
#include <esp32_touch.hpp>
// LED pin
constexpr uint8_t led_pin = 2; // GPIO 2
// Touch button GPIO pins
constexpr int touch_io_off = 5; // GPIO 12
constexpr int touch_io_on = 4; // GPIO 13
constexpr int touch_io_toggle = 3; // GPIO 15
// Touch button touch detection threshold in percent of the
// calibration-time (i.e. idle-state) sensor readout value.
constexpr uint8_t touch_threshold = 92;
// Standard serial port setup
constexpr unsigned long serial_baudrate = 115200;
// Instantiate touch button driver
ESP32Touch buttons{};
// Define callback functions
void enable_led() {
Serial.println("Switching ON the LED!");
digitalWrite(led_pin, HIGH);
}
void disable_led() {
Serial.println("Switching the LED off!");
digitalWrite(led_pin, LOW);
}
void setup() {
// Enable serial output
Serial.begin(serial_baudrate);
// Setup LED GPIO pin
pinMode(led_pin, OUTPUT);
// Register above callback functions with the touch button driver
buttons.configure_input(touch_io_on, touch_threshold, enable_led);
buttons.configure_input(touch_io_off, touch_threshold, disable_led);
// You can also directly register a lambda:
buttons.configure_input(touch_io_toggle, touch_threshold, []() {
Serial.println("Toggling the LED!");
bool current_led_state = (bool)digitalRead(led_pin);
digitalWrite(led_pin, !current_led_state);
});
// Call this once and you are done..
buttons.begin();
}
void loop() {
// All touch button code runs asynchronously, you can do anything here.
}