|
1 | 1 | /*
|
2 |
| - * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | *
|
4 | 4 | * SPDX-License-Identifier: Apache-2.0
|
5 | 5 | */
|
|
8 | 8 | #include <stdlib.h>
|
9 | 9 | #include "freertos/FreeRTOS.h"
|
10 | 10 | #include "freertos/task.h"
|
11 |
| -#include "driver/ledc.h" |
12 | 11 | #include "esp_log.h"
|
13 | 12 | #include "unity.h"
|
| 13 | +#include "touch_sensor_lowlevel.h" |
14 | 14 | #include "touch_proximity_sensor.h"
|
15 | 15 |
|
16 | 16 | const static char *TAG = "touch-proximity-sensor-test";
|
17 | 17 |
|
18 | 18 | #define TEST_MEMORY_LEAK_THRESHOLD (-200)
|
19 |
| -#define IO_BUZZER_CTRL 36 |
20 |
| -#define LEDC_LS_TIMER LEDC_TIMER_1 |
21 |
| -#define LEDC_LS_CH0_CHANNEL LEDC_CHANNEL_0 |
22 |
| -#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE |
23 | 19 |
|
24 |
| -static bool s_buzzer_driver_installed = false; |
25 | 20 | static touch_proximity_handle_t s_touch_proximity_sensor = NULL;
|
26 | 21 |
|
27 | 22 | static size_t before_free_8bit;
|
28 | 23 | static size_t before_free_32bit;
|
29 | 24 |
|
30 |
| -static esp_err_t buzzer_driver_install(gpio_num_t buzzer_pin) |
31 |
| -{ |
32 |
| - ledc_timer_config_t ledc_timer = { |
33 |
| - .duty_resolution = LEDC_TIMER_13_BIT, //Resolution of PWM duty |
34 |
| - .freq_hz = 5000, //Frequency of PWM signal |
35 |
| - .speed_mode = LEDC_LS_MODE, //Timer mode |
36 |
| - .timer_num = LEDC_LS_TIMER, //Timer index |
37 |
| - .clk_cfg = LEDC_AUTO_CLK, //Auto select the source clock (REF_TICK or APB_CLK or RTC_8M_CLK) |
38 |
| - }; |
39 |
| - TEST_ESP_OK(ledc_timer_config(&ledc_timer)); |
40 |
| - ledc_channel_config_t ledc_channel = { |
41 |
| - .channel = LEDC_LS_CH0_CHANNEL, |
42 |
| - .duty = 4096, |
43 |
| - .gpio_num = buzzer_pin, |
44 |
| - .speed_mode = LEDC_LS_MODE, |
45 |
| - .hpoint = 0, |
46 |
| - .timer_sel = LEDC_LS_TIMER //Let timer associate with LEDC channel (Timer1) |
47 |
| - }; |
48 |
| - TEST_ESP_OK(ledc_channel_config(&ledc_channel)); |
49 |
| - TEST_ESP_OK(ledc_fade_func_install(0)); |
50 |
| - TEST_ESP_OK(ledc_set_duty(ledc_channel.speed_mode, ledc_channel.channel, 0)); |
51 |
| - TEST_ESP_OK(ledc_update_duty(ledc_channel.speed_mode, ledc_channel.channel)); |
52 |
| - return ESP_OK; |
53 |
| -} |
54 |
| - |
55 |
| -static void buzzer_set_voice(bool en) |
56 |
| -{ |
57 |
| - uint32_t freq = en ? 5000 : 0; |
58 |
| - ledc_set_duty(LEDC_LS_MODE, LEDC_LS_CH0_CHANNEL, freq); |
59 |
| - ledc_update_duty(LEDC_LS_MODE, LEDC_LS_CH0_CHANNEL); |
60 |
| -} |
61 |
| - |
62 |
| -static void example_proxi_callback(uint32_t channel, proxi_evt_t event, void *cb_arg) |
| 25 | +static void example_proxi_callback(uint32_t channel, proxi_state_t event, void *cb_arg) |
63 | 26 | {
|
64 | 27 | switch (event) {
|
65 |
| - case PROXI_EVT_ACTIVE: |
66 |
| - buzzer_set_voice(1); |
| 28 | + case PROXI_STATE_ACTIVE: |
67 | 29 | ESP_LOGI(TAG, "CH%"PRIu32", active!", channel);
|
68 | 30 | break;
|
69 |
| - case PROXI_EVT_INACTIVE: |
70 |
| - buzzer_set_voice(0); |
| 31 | + case PROXI_STATE_INACTIVE: |
71 | 32 | ESP_LOGI(TAG, "CH%"PRIu32", inactive!", channel);
|
72 | 33 | break;
|
73 | 34 | default:
|
74 | 35 | break;
|
75 | 36 | }
|
76 | 37 | }
|
77 | 38 |
|
78 |
| -TEST_CASE("touch proximity sensor APIs test", "[touch_proximity_sensor][API]") |
| 39 | +TEST_CASE("touch proximity sensor loop get test", "[touch_proximity_sensor][loop]") |
79 | 40 | {
|
80 |
| - proxi_config_t config = (proxi_config_t)DEFAULTS_PROX_CONFIGS(); |
81 |
| - config.channel_num = 1; |
82 |
| - config.meas_count = 50; |
83 |
| - config.channel_list[0] = TOUCH_PAD_NUM8; |
84 |
| - config.threshold_p[0] = 0.004; |
85 |
| - config.threshold_n[0] = 0.004; |
86 |
| - config.noise_p = 0.001; |
87 |
| - config.debounce_p = 1; |
88 |
| - esp_err_t ret = touch_proximity_sensor_create(&config, &s_touch_proximity_sensor, &example_proxi_callback, NULL); |
89 |
| - if (ret != ESP_OK) { |
90 |
| - ESP_LOGE(TAG, "touch proximity sense create failed"); |
| 41 | + uint32_t channel_list[] = {TOUCH_PAD_NUM8}; |
| 42 | + float channel_threshold[] = {0.004f}; |
| 43 | + touch_proxi_config_t config = { |
| 44 | + .channel_num = 1, |
| 45 | + .channel_list = channel_list, |
| 46 | + .channel_threshold = channel_threshold, |
| 47 | + .debounce_times = 2, |
| 48 | + .skip_lowlevel_init = false, |
| 49 | + }; |
| 50 | + |
| 51 | + // Test create |
| 52 | + TEST_ESP_OK(touch_proximity_sensor_create(&config, &s_touch_proximity_sensor, NULL, NULL)); |
| 53 | + TEST_ASSERT_NOT_NULL(s_touch_proximity_sensor); |
| 54 | + |
| 55 | + int counter = 0; |
| 56 | + while (counter++ < 100) { |
| 57 | + uint32_t data; |
| 58 | + proxi_state_t state; |
| 59 | + TEST_ESP_OK(touch_proximity_sensor_handle_events(s_touch_proximity_sensor)); |
| 60 | + TEST_ESP_OK(touch_proximity_sensor_get_state(s_touch_proximity_sensor, TOUCH_PAD_NUM8, &state)); |
| 61 | + TEST_ESP_OK(touch_proximity_sensor_get_data(s_touch_proximity_sensor, TOUCH_PAD_NUM8, &data)); |
| 62 | + printf("CH%d, state: %d, data: %"PRIu32"\n", TOUCH_PAD_NUM8, state, data); |
| 63 | + vTaskDelay(20 / portTICK_PERIOD_MS); |
91 | 64 | }
|
92 |
| - touch_proximity_sensor_start(s_touch_proximity_sensor); |
93 |
| - vTaskDelay(300 / portTICK_PERIOD_MS); |
94 |
| - touch_proximity_sensor_stop(s_touch_proximity_sensor); |
95 |
| - vTaskDelay(200 / portTICK_PERIOD_MS); |
96 |
| - touch_proximity_sensor_delete(s_touch_proximity_sensor); |
| 65 | + |
| 66 | + // Test delete |
| 67 | + TEST_ESP_OK(touch_proximity_sensor_delete(s_touch_proximity_sensor)); |
| 68 | + s_touch_proximity_sensor = NULL; |
97 | 69 | }
|
98 | 70 |
|
99 |
| -TEST_CASE("touch proximity sensor create & start test", "[ignore][touch_proximity_sensor][create & start]") |
| 71 | +TEST_CASE("touch proximity sensor callback test", "[touch_proximity_sensor][callback]") |
100 | 72 | {
|
101 |
| - if (s_buzzer_driver_installed == false) { |
102 |
| - if (buzzer_driver_install(IO_BUZZER_CTRL) == ESP_OK) { |
103 |
| - s_buzzer_driver_installed = true; |
104 |
| - } else { |
105 |
| - ESP_LOGW(TAG, "Buzzer driver install failed, skipping test"); |
106 |
| - } |
107 |
| - } |
108 |
| - proxi_config_t config = (proxi_config_t)DEFAULTS_PROX_CONFIGS(); |
109 |
| - config.channel_num = 1; |
110 |
| - config.meas_count = 50; |
111 |
| - config.channel_list[0] = TOUCH_PAD_NUM8; |
112 |
| - config.threshold_p[0] = 0.004; |
113 |
| - config.threshold_n[0] = 0.004; |
114 |
| - config.noise_p = 0.001; |
115 |
| - config.debounce_p = 1; |
116 |
| - esp_err_t ret = touch_proximity_sensor_create(&config, &s_touch_proximity_sensor, &example_proxi_callback, NULL); |
117 |
| - if (ret != ESP_OK) { |
118 |
| - ESP_LOGE(TAG, "touch proximity sense create failed"); |
| 73 | + uint32_t channel_list[] = {TOUCH_PAD_NUM8}; |
| 74 | + float channel_threshold[] = {0.004f}; |
| 75 | + touch_proxi_config_t config = { |
| 76 | + .channel_num = 1, |
| 77 | + .channel_list = channel_list, |
| 78 | + .channel_threshold = channel_threshold, |
| 79 | + .debounce_times = 2, |
| 80 | + .skip_lowlevel_init = false, |
| 81 | + }; |
| 82 | + |
| 83 | + TEST_ESP_OK(touch_proximity_sensor_create(&config, &s_touch_proximity_sensor, &example_proxi_callback, NULL)); |
| 84 | + |
| 85 | + ESP_LOGI(TAG, "touch proximity sensor started - approach the touch pad to test proximity detection"); |
| 86 | + for (int i = 0; i < 100; i++) { |
| 87 | + touch_proximity_sensor_handle_events(s_touch_proximity_sensor); |
| 88 | + vTaskDelay(20 / portTICK_PERIOD_MS); |
119 | 89 | }
|
120 |
| - touch_proximity_sensor_start(s_touch_proximity_sensor); |
121 |
| - vTaskDelay(200 / portTICK_PERIOD_MS); |
122 |
| - ESP_LOGI(TAG, "touch proximity sensor has started! when you approach the touch sub-board, the buzzer will sound."); |
| 90 | + |
| 91 | + TEST_ESP_OK(touch_proximity_sensor_delete(s_touch_proximity_sensor)); |
| 92 | + s_touch_proximity_sensor = NULL; |
123 | 93 | }
|
124 | 94 |
|
125 |
| -TEST_CASE("touch proximity sensor stop & delete test", "[ignore][touch_proximity_sensor][stop & delete]") |
| 95 | +TEST_CASE("touch proximity sensor skip lowlevel init", "[touch_proximity_sensor][loop]") |
126 | 96 | {
|
127 |
| - touch_proximity_sensor_stop(s_touch_proximity_sensor); |
128 |
| - vTaskDelay(200 / portTICK_PERIOD_MS); |
129 |
| - touch_proximity_sensor_delete(s_touch_proximity_sensor); |
| 97 | + // First initialize the low level touch sensor |
| 98 | + uint32_t channel_list[] = {TOUCH_PAD_NUM8}; |
| 99 | + touch_lowlevel_type_t channel_type[] = {TOUCH_LOWLEVEL_TYPE_PROXIMITY}; |
| 100 | + touch_lowlevel_config_t low_config = { |
| 101 | + .channel_num = 1, |
| 102 | + .channel_list = channel_list, |
| 103 | + .channel_type = channel_type, |
| 104 | + .proximity_count = CONFIG_TOUCH_PROXIMITY_MEAS_COUNT, |
| 105 | + }; |
| 106 | + TEST_ESP_OK(touch_sensor_lowlevel_create(&low_config)); |
| 107 | + |
| 108 | + // Now create two proximity sensors that use the same touch hardware |
| 109 | + touch_proximity_handle_t sensor1 = NULL; |
| 110 | + touch_proximity_handle_t sensor2 = NULL; |
| 111 | + |
| 112 | + float threshold1[] = {0.008f}; |
| 113 | + float threshold2[] = {0.01f}; // Different threshold for testing |
| 114 | + |
| 115 | + // Configure first sensor |
| 116 | + touch_proxi_config_t config1 = { |
| 117 | + .channel_num = 1, |
| 118 | + .channel_list = channel_list, |
| 119 | + .channel_threshold = threshold1, |
| 120 | + .debounce_times = 2, |
| 121 | + .skip_lowlevel_init = true, // Skip since we already initialized |
| 122 | + }; |
| 123 | + |
| 124 | + // Configure second sensor |
| 125 | + touch_proxi_config_t config2 = { |
| 126 | + .channel_num = 1, |
| 127 | + .channel_list = channel_list, |
| 128 | + .channel_threshold = threshold2, |
| 129 | + .debounce_times = 3, // Different debounce for testing |
| 130 | + .skip_lowlevel_init = true, |
| 131 | + }; |
| 132 | + |
| 133 | + // Create both sensors |
| 134 | + TEST_ESP_OK(touch_proximity_sensor_create(&config1, &sensor1, NULL, NULL)); |
| 135 | + TEST_ESP_OK(touch_proximity_sensor_create(&config2, &sensor2, NULL, NULL)); |
| 136 | + TEST_ASSERT_NOT_NULL(sensor1); |
| 137 | + TEST_ASSERT_NOT_NULL(sensor2); |
| 138 | + TEST_ESP_OK(touch_sensor_lowlevel_start()); |
| 139 | + |
| 140 | + // Test both sensors for a while |
| 141 | + for (int i = 0; i < 100; i++) { |
| 142 | + uint32_t data1, data2; |
| 143 | + proxi_state_t state1, state2; |
| 144 | + |
| 145 | + // Handle events and get data from both sensors |
| 146 | + TEST_ESP_OK(touch_proximity_sensor_handle_events(sensor1)); |
| 147 | + TEST_ESP_OK(touch_proximity_sensor_handle_events(sensor2)); |
| 148 | + |
| 149 | + // Get data from both sensors |
| 150 | + TEST_ESP_OK(touch_proximity_sensor_get_data(sensor1, TOUCH_PAD_NUM8, &data1)); |
| 151 | + TEST_ESP_OK(touch_proximity_sensor_get_data(sensor2, TOUCH_PAD_NUM8, &data2)); |
| 152 | + |
| 153 | + // Get states from both sensors |
| 154 | + TEST_ESP_OK(touch_proximity_sensor_get_state(sensor1, TOUCH_PAD_NUM8, &state1)); |
| 155 | + TEST_ESP_OK(touch_proximity_sensor_get_state(sensor2, TOUCH_PAD_NUM8, &state2)); |
| 156 | + |
| 157 | + // Both sensors should get similar raw data since they use the same hardware |
| 158 | + TEST_ASSERT_UINT32_WITHIN(100, data1, data2); |
| 159 | + |
| 160 | + // States might differ due to different thresholds and debounce settings |
| 161 | + printf("Sensor1: data=%"PRIu32", state=%d; Sensor2: data=%"PRIu32", state=%d\n", |
| 162 | + data1, state1, data2, state2); |
| 163 | + |
| 164 | + vTaskDelay(20 / portTICK_PERIOD_MS); |
| 165 | + } |
| 166 | + |
| 167 | + // Clean up in reverse order |
| 168 | + TEST_ESP_OK(touch_proximity_sensor_delete(sensor2)); |
| 169 | + TEST_ESP_OK(touch_proximity_sensor_delete(sensor1)); |
| 170 | + |
| 171 | + // Finally clean up the low level touch sensor |
| 172 | + TEST_ESP_OK(touch_sensor_lowlevel_stop()); |
| 173 | + TEST_ESP_OK(touch_sensor_lowlevel_delete()); |
130 | 174 | }
|
131 | 175 |
|
132 | 176 | static void check_leak(size_t before_free, size_t after_free, const char *type)
|
|
0 commit comments