From 60a4b50121805e8b3a7186e09198e584c92d66f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 31 Oct 2022 12:11:24 +0100
Subject: [PATCH 1/6] added test for touch peripheral

---
 tests/touch/cfg.json      |  16 ++++
 tests/touch/test_touch.py |   2 +
 tests/touch/touch.ino     | 169 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 187 insertions(+)
 create mode 100644 tests/touch/cfg.json
 create mode 100644 tests/touch/test_touch.py
 create mode 100644 tests/touch/touch.ino

diff --git a/tests/touch/cfg.json b/tests/touch/cfg.json
new file mode 100644
index 00000000000..6e9e8143ffd
--- /dev/null
+++ b/tests/touch/cfg.json
@@ -0,0 +1,16 @@
+{
+  "targets": [
+    {
+      "name": "esp32",
+      "fqbn":["espressif:esp32:esp32:PSRAM=enabled,PartitionScheme=huge_app,FlashMode=dio"]
+    },
+    {
+      "name": "esp32s2",
+      "fqbn": ["espressif:esp32:esp32s2:PSRAM=enabled,PartitionScheme=huge_app"]
+    },
+    {
+      "name": "esp32s3",
+      "fqbn": ["espressif:esp32:esp32s3:PSRAM=opi,USBMode=default,PartitionScheme=huge_app"]
+    }
+  ]
+}
diff --git a/tests/touch/test_touch.py b/tests/touch/test_touch.py
new file mode 100644
index 00000000000..dc80b6dfaa4
--- /dev/null
+++ b/tests/touch/test_touch.py
@@ -0,0 +1,2 @@
+def test_touch(dut):
+    dut.expect_unity_test_output(timeout=240)
diff --git a/tests/touch/touch.ino b/tests/touch/touch.ino
new file mode 100644
index 00000000000..06c5963d267
--- /dev/null
+++ b/tests/touch/touch.ino
@@ -0,0 +1,169 @@
+#include <unity.h>
+#include "driver/touch_pad.h"
+
+#if CONFIG_IDF_TARGET_ESP32
+
+#define TEST_TOUCH_CHANNEL   (9)
+static touch_pad_t touch_list[TEST_TOUCH_CHANNEL] = {
+    TOUCH_PAD_NUM0,
+    //TOUCH_PAD_NUM1 is GPIO0, for download.
+    TOUCH_PAD_NUM2,
+    TOUCH_PAD_NUM3,
+    TOUCH_PAD_NUM4,
+    TOUCH_PAD_NUM5,
+    TOUCH_PAD_NUM6,
+    TOUCH_PAD_NUM7,
+    TOUCH_PAD_NUM8,
+    TOUCH_PAD_NUM9
+};
+
+uint8_t TOUCH_GPIOS[] = {4,2,15,13,12,14,27,33,32};
+
+#define NO_TOUCH_GPIO 25
+
+#define RELEASED_VALUE        80 //80+ read value to pass test
+#define PRESSED_VALUE         20 //20- read value to pass test
+#define INTERRUPT_THRESHOLD   40
+
+#else //ESP32S2 and ESP32S3
+
+#define TEST_TOUCH_CHANNEL   (12) //14
+static touch_pad_t touch_list[TEST_TOUCH_CHANNEL] = {
+    TOUCH_PAD_NUM1,
+    TOUCH_PAD_NUM2,
+    TOUCH_PAD_NUM3,
+    TOUCH_PAD_NUM4,
+    TOUCH_PAD_NUM5,
+    TOUCH_PAD_NUM6,
+    TOUCH_PAD_NUM7,
+    TOUCH_PAD_NUM8,
+    TOUCH_PAD_NUM9,
+    TOUCH_PAD_NUM10,
+    TOUCH_PAD_NUM11,
+    TOUCH_PAD_NUM12
+    //TOUCH_PAD_NUM13, //Wrong reading
+    //TOUCH_PAD_NUM14
+};
+
+uint8_t TOUCH_GPIOS[] = {1,2,3,4,5,6,7,8,9,10,11,12/*,13,14*/};
+
+#define NO_TOUCH_GPIO 17
+
+#if CONFIG_IDF_TARGET_ESP32S2
+  #define RELEASED_VALUE        8500 //8500- read value to pass test
+  #define PRESSED_VALUE         42000 //40000+ read value to pass test
+  #define INTERRUPT_THRESHOLD   30000
+#elif CONFIG_IDF_TARGET_ESP32S3
+  #define RELEASED_VALUE        25000 //25000- read value to pass test
+  #define PRESSED_VALUE         100000 //150000+ read value to pass test
+  #define INTERRUPT_THRESHOLD   80000
+#endif
+
+#endif
+
+bool touch1detected = false;
+bool touch2detected = false;
+
+void gotTouch1() {
+  touch1detected = true;
+}
+
+void gotTouch2() {
+  touch2detected = true;
+}
+
+/*
+ * Change the slope to get larger value from touch sensor.
+ */
+static void test_press_fake(touch_pad_t pad_num) {
+  touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_1, TOUCH_PAD_TIE_OPT_DEFAULT);
+}
+
+/*
+ * Change the slope to get smaller value from touch sensor.
+ */
+static void test_release_fake(touch_pad_t pad_num) {
+  touch_pad_set_cnt_mode(pad_num, TOUCH_PAD_SLOPE_7, TOUCH_PAD_TIE_OPT_DEFAULT);
+}
+
+
+/* These functions are intended to be called before and after each test. */
+void setUp(void) {
+  
+}
+
+void tearDown(void) {
+  for (int i = 0; i < TEST_TOUCH_CHANNEL; i++) {
+          test_release_fake(touch_list[i]);
+  }
+  delay(100);
+}
+  
+/*
+ * Test Touch read on all available channels - compare values if reading is right
+ */
+void test_touch_read(void) {
+
+  //TEST RELEASE STATE
+  for (int i = 0; i < sizeof(TOUCH_GPIOS); i++) {
+        #ifdef CONFIG_IDF_TARGET_ESP32
+          TEST_ASSERT_GREATER_THAN(RELEASED_VALUE, touchRead(TOUCH_GPIOS[i]));
+        #else
+          TEST_ASSERT_LESS_THAN(RELEASED_VALUE, touchRead(TOUCH_GPIOS[i]));
+        #endif
+  }
+
+  // TEST PRESS STATE
+  for (int j = 0; j < TEST_TOUCH_CHANNEL; j++) {
+          test_press_fake(touch_list[j]);
+  }
+  delay(100);
+  
+  for (int k = 0; k < sizeof(TOUCH_GPIOS); k++) {
+        #ifdef CONFIG_IDF_TARGET_ESP32
+          TEST_ASSERT_LESS_THAN(PRESSED_VALUE,touchRead(TOUCH_GPIOS[k]));
+        #else
+          TEST_ASSERT_GREATER_THAN(PRESSED_VALUE, touchRead(TOUCH_GPIOS[k]));
+        #endif
+  }
+}
+
+void test_touch_interrtupt(void) {
+ 
+  touchAttachInterrupt(TOUCH_GPIOS[0], gotTouch1, INTERRUPT_THRESHOLD);
+  touchAttachInterrupt(TOUCH_GPIOS[1], gotTouch2, INTERRUPT_THRESHOLD);
+
+  test_press_fake(touch_list[0]);
+  test_press_fake(touch_list[1]);
+
+  delay(300);
+  
+  touchDetachInterrupt(TOUCH_GPIOS[0]);
+  touchDetachInterrupt(TOUCH_GPIOS[1]);
+  
+  TEST_ASSERT_TRUE(touch1detected);
+  TEST_ASSERT_TRUE(touch2detected);
+}
+
+void test_touch_errors(void) {
+  
+  TEST_ASSERT_FALSE(touchRead(NO_TOUCH_GPIO));
+}
+
+
+void setup() {
+  Serial.begin(115200);
+  while (!Serial) {
+    ;
+  }
+  
+  UNITY_BEGIN();
+  RUN_TEST(test_touch_read);
+  RUN_TEST(test_touch_interrtupt);
+  RUN_TEST(test_touch_errors);
+  UNITY_END();
+}
+
+void loop() {
+
+}

From 5f168a472c159df99a7acbe5fa8f4ed4e036ea53 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 31 Oct 2022 12:33:55 +0100
Subject: [PATCH 2/6] removed cfg.json

---
 tests/touch/cfg.json | 16 ----------------
 1 file changed, 16 deletions(-)
 delete mode 100644 tests/touch/cfg.json

diff --git a/tests/touch/cfg.json b/tests/touch/cfg.json
deleted file mode 100644
index 6e9e8143ffd..00000000000
--- a/tests/touch/cfg.json
+++ /dev/null
@@ -1,16 +0,0 @@
-{
-  "targets": [
-    {
-      "name": "esp32",
-      "fqbn":["espressif:esp32:esp32:PSRAM=enabled,PartitionScheme=huge_app,FlashMode=dio"]
-    },
-    {
-      "name": "esp32s2",
-      "fqbn": ["espressif:esp32:esp32s2:PSRAM=enabled,PartitionScheme=huge_app"]
-    },
-    {
-      "name": "esp32s3",
-      "fqbn": ["espressif:esp32:esp32s3:PSRAM=opi,USBMode=default,PartitionScheme=huge_app"]
-    }
-  ]
-}

From abc9f8f3cc3909694d98c1dbc54d302ea6bc6d18 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 31 Oct 2022 12:34:15 +0100
Subject: [PATCH 3/6] pass test for unsupported chips

---
 tests/touch/touch.ino | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/tests/touch/touch.ino b/tests/touch/touch.ino
index 06c5963d267..9009696b69c 100644
--- a/tests/touch/touch.ino
+++ b/tests/touch/touch.ino
@@ -1,4 +1,8 @@
 #include <unity.h>
+#include "soc/soc_caps.h"
+
+#if SOC_TOUCH_SENSOR_NUM > 0
+
 #include "driver/touch_pad.h"
 
 #if CONFIG_IDF_TARGET_ESP32
@@ -150,7 +154,6 @@ void test_touch_errors(void) {
   TEST_ASSERT_FALSE(touchRead(NO_TOUCH_GPIO));
 }
 
-
 void setup() {
   Serial.begin(115200);
   while (!Serial) {
@@ -167,3 +170,25 @@ void setup() {
 void loop() {
 
 }
+
+#endif
+//PASS TEST for UNSUPPORTED CHIPS
+
+void test_pass(void){
+  TEST_ASSERT_EQUAL(1, 1);
+}
+
+void setup() {
+  Serial.begin(115200);
+  while (!Serial) {
+    ;
+  }
+  
+  UNITY_BEGIN();
+  RUN_TEST(test_pass);
+  UNITY_END();
+}
+
+void loop() {
+
+}

From da53070d1d53304d162bd719b9abc98239d1299a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 31 Oct 2022 12:44:45 +0100
Subject: [PATCH 4/6] fixed condition

---
 tests/touch/touch.ino | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tests/touch/touch.ino b/tests/touch/touch.ino
index 9009696b69c..e14b2c25a92 100644
--- a/tests/touch/touch.ino
+++ b/tests/touch/touch.ino
@@ -171,7 +171,7 @@ void loop() {
 
 }
 
-#endif
+#else
 //PASS TEST for UNSUPPORTED CHIPS
 
 void test_pass(void){
@@ -192,3 +192,5 @@ void setup() {
 void loop() {
 
 }
+
+#endif

From 230c19a7ad72f1e861d8c198fb50709643503a8f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Mon, 31 Oct 2022 13:30:39 +0100
Subject: [PATCH 5/6] changed released value for S2

---
 tests/touch/touch.ino | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/touch/touch.ino b/tests/touch/touch.ino
index e14b2c25a92..8cd666a5fe4 100644
--- a/tests/touch/touch.ino
+++ b/tests/touch/touch.ino
@@ -54,7 +54,7 @@ uint8_t TOUCH_GPIOS[] = {1,2,3,4,5,6,7,8,9,10,11,12/*,13,14*/};
 #define NO_TOUCH_GPIO 17
 
 #if CONFIG_IDF_TARGET_ESP32S2
-  #define RELEASED_VALUE        8500 //8500- read value to pass test
+  #define RELEASED_VALUE        10000 //10000- read value to pass test
   #define PRESSED_VALUE         42000 //40000+ read value to pass test
   #define INTERRUPT_THRESHOLD   30000
 #elif CONFIG_IDF_TARGET_ESP32S3

From 0435809e6630b8b837868103ecdc535e35c9b201 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Procha=CC=81zka?=
 <90197375+P-R-O-C-H-Y@users.noreply.github.com>
Date: Wed, 2 Nov 2022 12:41:53 +0100
Subject: [PATCH 6/6] add new chip error

---
 tests/touch/touch.ino | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tests/touch/touch.ino b/tests/touch/touch.ino
index 8cd666a5fe4..d250f179d87 100644
--- a/tests/touch/touch.ino
+++ b/tests/touch/touch.ino
@@ -61,6 +61,8 @@ uint8_t TOUCH_GPIOS[] = {1,2,3,4,5,6,7,8,9,10,11,12/*,13,14*/};
   #define RELEASED_VALUE        25000 //25000- read value to pass test
   #define PRESSED_VALUE         100000 //150000+ read value to pass test
   #define INTERRUPT_THRESHOLD   80000
+#else
+  #error Test not currently supported on this chip. Please adjust and try again!
 #endif
 
 #endif