Skip to content

Commit 60a4b50

Browse files
committed
added test for touch peripheral
1 parent 86577bf commit 60a4b50

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

tests/touch/cfg.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"targets": [
3+
{
4+
"name": "esp32",
5+
"fqbn":["espressif:esp32:esp32:PSRAM=enabled,PartitionScheme=huge_app,FlashMode=dio"]
6+
},
7+
{
8+
"name": "esp32s2",
9+
"fqbn": ["espressif:esp32:esp32s2:PSRAM=enabled,PartitionScheme=huge_app"]
10+
},
11+
{
12+
"name": "esp32s3",
13+
"fqbn": ["espressif:esp32:esp32s3:PSRAM=opi,USBMode=default,PartitionScheme=huge_app"]
14+
}
15+
]
16+
}

tests/touch/test_touch.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_touch(dut):
2+
dut.expect_unity_test_output(timeout=240)

tests/touch/touch.ino

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include <unity.h>
2+
#include "driver/touch_pad.h"
3+
4+
#if CONFIG_IDF_TARGET_ESP32
5+
6+
#define TEST_TOUCH_CHANNEL (9)
7+
static touch_pad_t touch_list[TEST_TOUCH_CHANNEL] = {
8+
TOUCH_PAD_NUM0,
9+
//TOUCH_PAD_NUM1 is GPIO0, for download.
10+
TOUCH_PAD_NUM2,
11+
TOUCH_PAD_NUM3,
12+
TOUCH_PAD_NUM4,
13+
TOUCH_PAD_NUM5,
14+
TOUCH_PAD_NUM6,
15+
TOUCH_PAD_NUM7,
16+
TOUCH_PAD_NUM8,
17+
TOUCH_PAD_NUM9
18+
};
19+
20+
uint8_t TOUCH_GPIOS[] = {4,2,15,13,12,14,27,33,32};
21+
22+
#define NO_TOUCH_GPIO 25
23+
24+
#define RELEASED_VALUE 80 //80+ read value to pass test
25+
#define PRESSED_VALUE 20 //20- read value to pass test
26+
#define INTERRUPT_THRESHOLD 40
27+
28+
#else //ESP32S2 and ESP32S3
29+
30+
#define TEST_TOUCH_CHANNEL (12) //14
31+
static touch_pad_t touch_list[TEST_TOUCH_CHANNEL] = {
32+
TOUCH_PAD_NUM1,
33+
TOUCH_PAD_NUM2,
34+
TOUCH_PAD_NUM3,
35+
TOUCH_PAD_NUM4,
36+
TOUCH_PAD_NUM5,
37+
TOUCH_PAD_NUM6,
38+
TOUCH_PAD_NUM7,
39+
TOUCH_PAD_NUM8,
40+
TOUCH_PAD_NUM9,
41+
TOUCH_PAD_NUM10,
42+
TOUCH_PAD_NUM11,
43+
TOUCH_PAD_NUM12
44+
//TOUCH_PAD_NUM13, //Wrong reading
45+
//TOUCH_PAD_NUM14
46+
};
47+
48+
uint8_t TOUCH_GPIOS[] = {1,2,3,4,5,6,7,8,9,10,11,12/*,13,14*/};
49+
50+
#define NO_TOUCH_GPIO 17
51+
52+
#if CONFIG_IDF_TARGET_ESP32S2
53+
#define RELEASED_VALUE 8500 //8500- read value to pass test
54+
#define PRESSED_VALUE 42000 //40000+ read value to pass test
55+
#define INTERRUPT_THRESHOLD 30000
56+
#elif CONFIG_IDF_TARGET_ESP32S3
57+
#define RELEASED_VALUE 25000 //25000- read value to pass test
58+
#define PRESSED_VALUE 100000 //150000+ read value to pass test
59+
#define INTERRUPT_THRESHOLD 80000
60+
#endif
61+
62+
#endif
63+
64+
bool touch1detected = false;
65+
bool touch2detected = false;
66+
67+
void gotTouch1() {
68+
touch1detected = true;
69+
}
70+
71+
void gotTouch2() {
72+
touch2detected = true;
73+
}
74+
75+
/*
76+
* Change the slope to get larger value from touch sensor.
77+
*/
78+
static void test_press_fake(touch_pad_t pad_num) {
79+
touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_1, TOUCH_PAD_TIE_OPT_DEFAULT);
80+
}
81+
82+
/*
83+
* Change the slope to get smaller value from touch sensor.
84+
*/
85+
static void test_release_fake(touch_pad_t pad_num) {
86+
touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
87+
}
88+
89+
90+
/* These functions are intended to be called before and after each test. */
91+
void setUp(void) {
92+
93+
}
94+
95+
void tearDown(void) {
96+
for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
97+
test_release_fake(touch_list[i]);
98+
}
99+
delay(100);
100+
}
101+
102+
/*
103+
* Test Touch read on all available channels - compare values if reading is right
104+
*/
105+
void test_touch_read(void) {
106+
107+
//TEST RELEASE STATE
108+
for (int i = 0; i < sizeof(TOUCH_GPIOS); i++) {
109+
#ifdef CONFIG_IDF_TARGET_ESP32
110+
TEST_ASSERT_GREATER_THAN(RELEASED_VALUE, touchRead(TOUCH_GPIOS[i]));
111+
#else
112+
TEST_ASSERT_LESS_THAN(RELEASED_VALUE, touchRead(TOUCH_GPIOS[i]));
113+
#endif
114+
}
115+
116+
// TEST PRESS STATE
117+
for (int j = 0; j < TEST_TOUCH_CHANNEL; j++) {
118+
test_press_fake(touch_list[j]);
119+
}
120+
delay(100);
121+
122+
for (int k = 0; k < sizeof(TOUCH_GPIOS); k++) {
123+
#ifdef CONFIG_IDF_TARGET_ESP32
124+
TEST_ASSERT_LESS_THAN(PRESSED_VALUE,touchRead(TOUCH_GPIOS[k]));
125+
#else
126+
TEST_ASSERT_GREATER_THAN(PRESSED_VALUE, touchRead(TOUCH_GPIOS[k]));
127+
#endif
128+
}
129+
}
130+
131+
void test_touch_interrtupt(void) {
132+
133+
touchAttachInterrupt(TOUCH_GPIOS[0], gotTouch1, INTERRUPT_THRESHOLD);
134+
touchAttachInterrupt(TOUCH_GPIOS[1], gotTouch2, INTERRUPT_THRESHOLD);
135+
136+
test_press_fake(touch_list[0]);
137+
test_press_fake(touch_list[1]);
138+
139+
delay(300);
140+
141+
touchDetachInterrupt(TOUCH_GPIOS[0]);
142+
touchDetachInterrupt(TOUCH_GPIOS[1]);
143+
144+
TEST_ASSERT_TRUE(touch1detected);
145+
TEST_ASSERT_TRUE(touch2detected);
146+
}
147+
148+
void test_touch_errors(void) {
149+
150+
TEST_ASSERT_FALSE(touchRead(NO_TOUCH_GPIO));
151+
}
152+
153+
154+
void setup() {
155+
Serial.begin(115200);
156+
while (!Serial) {
157+
;
158+
}
159+
160+
UNITY_BEGIN();
161+
RUN_TEST(test_touch_read);
162+
RUN_TEST(test_touch_interrtupt);
163+
RUN_TEST(test_touch_errors);
164+
UNITY_END();
165+
}
166+
167+
void loop() {
168+
169+
}

0 commit comments

Comments
 (0)