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