|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/sys/util.h> |
| 8 | +#include <zephyr/device.h> |
| 9 | +#include <zephyr/drivers/gpio/gpio_emul.h> |
| 10 | +#include <zephyr/kernel.h> |
| 11 | +#include <zephyr/input/input.h> |
| 12 | +#include <zephyr/input/input_renesas_rx_ctsu.h> |
| 13 | + |
| 14 | +#ifdef CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG |
| 15 | +#include "qe_touch_config.h" |
| 16 | +#endif /* CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG */ |
| 17 | + |
| 18 | +static const struct device *const test_touch_dev = DEVICE_DT_GET(DT_NODELABEL(ctsu)); |
| 19 | +#define BUTTON_1_IDX DT_NODE_CHILD_IDX(DT_NODELABEL(onboard_button_1)) |
| 20 | +#define BUTTON_2_IDX DT_NODE_CHILD_IDX(DT_NODELABEL(onboard_button_2)) |
| 21 | +#define SLIDER_IDX DT_NODE_CHILD_IDX(DT_NODELABEL(onboard_slider)) |
| 22 | + |
| 23 | +#define STACKSIZE 1024 |
| 24 | +#define PRIORITY 7 |
| 25 | +#define LED0_NODE DT_NODELABEL(testled0) |
| 26 | +#define LED1_NODE DT_NODELABEL(testled1) |
| 27 | +#define LED2_NODE DT_NODELABEL(testled2) |
| 28 | +#define LED3_NODE DT_NODELABEL(testled3) |
| 29 | +#define NUM_TEST_LED 4 |
| 30 | + |
| 31 | +#define LED_ON 1 |
| 32 | +#define LED_OFF 0 |
| 33 | + |
| 34 | +#define DEBOUNCE_TIMES 3 |
| 35 | +#define DEBOUNCE_INTERVAL_MS (500 / DEBOUNCE_TIMES) |
| 36 | + |
| 37 | +enum { |
| 38 | + TOUCH_BUTTON_1 = 10, |
| 39 | + TOUCH_BUTTON_2 = 11, |
| 40 | + TOUCH_SLIDER = 1, |
| 41 | +}; |
| 42 | + |
| 43 | +static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios); |
| 44 | +static const struct gpio_dt_spec led1 = GPIO_DT_SPEC_GET(LED1_NODE, gpios); |
| 45 | +static const struct gpio_dt_spec led2 = GPIO_DT_SPEC_GET(LED2_NODE, gpios); |
| 46 | +static const struct gpio_dt_spec led3 = GPIO_DT_SPEC_GET(LED3_NODE, gpios); |
| 47 | +static const struct gpio_dt_spec *leds[] = {&led0, &led1, &led2, &led3}; |
| 48 | +static volatile int led_state; |
| 49 | +static volatile int leds_on_off[NUM_TEST_LED] = {0}; |
| 50 | +static volatile int current_main_led; |
| 51 | + |
| 52 | +static volatile int event_count[DT_CHILD_NUM_STATUS_OKAY(DT_NODELABEL(ctsu))] = {0}; |
| 53 | +static uint16_t last_code; |
| 54 | +static volatile int32_t last_val[DT_CHILD_NUM_STATUS_OKAY(DT_NODELABEL(ctsu))]; |
| 55 | +static struct k_sem btn1_sem; |
| 56 | +static struct k_sem btn2_sem; |
| 57 | +static struct k_sem sldr_sem; |
| 58 | +static struct k_sem render_sem; |
| 59 | + |
| 60 | +static void sample_setup(void) |
| 61 | +{ |
| 62 | + gpio_pin_configure_dt(&led0, GPIO_OUTPUT_INACTIVE); |
| 63 | + gpio_pin_configure_dt(&led1, GPIO_OUTPUT_INACTIVE); |
| 64 | + gpio_pin_configure_dt(&led2, GPIO_OUTPUT_INACTIVE); |
| 65 | + gpio_pin_configure_dt(&led3, GPIO_OUTPUT_INACTIVE); |
| 66 | + |
| 67 | + k_sem_init(&btn1_sem, 0, 1); |
| 68 | + k_sem_init(&btn2_sem, 0, 1); |
| 69 | + k_sem_init(&sldr_sem, 0, 1); |
| 70 | + k_sem_init(&render_sem, 0, 1); |
| 71 | + |
| 72 | +#ifdef CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG |
| 73 | + int ret = renesas_rx_ctsu_group_configure( |
| 74 | + test_touch_dev, (struct renesas_rx_ctsu_touch_cfg *)&g_qe_touch_instance_config01); |
| 75 | + |
| 76 | + if (ret < 0) { |
| 77 | + printk("Failed to configure QE Touch: %d\n", ret); |
| 78 | + return; |
| 79 | + } |
| 80 | +#endif /* CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG */ |
| 81 | +} |
| 82 | + |
| 83 | +void button1_task(void) |
| 84 | +{ |
| 85 | + |
| 86 | + while (1) { |
| 87 | + k_sem_take(&btn1_sem, K_FOREVER); |
| 88 | + if (led_state == 0) { |
| 89 | + /** Current leds are OFF, do nothing */ |
| 90 | + } else { |
| 91 | + if (led_state >= 15) { |
| 92 | + led_state = 1; |
| 93 | + } else { |
| 94 | + led_state++; |
| 95 | + } |
| 96 | + } |
| 97 | + k_sem_give(&render_sem); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void button2_task(void) |
| 102 | +{ |
| 103 | + int prev_state = 0; |
| 104 | + |
| 105 | + while (1) { |
| 106 | + k_sem_take(&btn2_sem, K_FOREVER); |
| 107 | + if (led_state == 0) { |
| 108 | + /** Current led state is OFF */ |
| 109 | + if (prev_state == 0) { |
| 110 | + /** First time power on */ |
| 111 | + led_state = 1; |
| 112 | + } else { |
| 113 | + /** Restore prev_state */ |
| 114 | + led_state = prev_state; |
| 115 | + } |
| 116 | + } else { |
| 117 | + prev_state = led_state; |
| 118 | + led_state = 0; |
| 119 | + } |
| 120 | + k_sem_give(&render_sem); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void slider_task(void) |
| 125 | +{ |
| 126 | + while (1) { |
| 127 | + k_sem_take(&sldr_sem, K_FOREVER); |
| 128 | + if (led_state == 0) { |
| 129 | + continue; |
| 130 | + } |
| 131 | + int curr_val = last_val[SLIDER_IDX]; |
| 132 | + |
| 133 | + if (curr_val != TOUCH_OFF_VALUE) { |
| 134 | + if (curr_val == 100) { |
| 135 | + curr_val = 99; |
| 136 | + } |
| 137 | + current_main_led = 3 - (curr_val / 25); |
| 138 | + } |
| 139 | + k_sem_give(&render_sem); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +void render_task(void) |
| 144 | +{ |
| 145 | + while (1) { |
| 146 | + k_sem_take(&render_sem, K_FOREVER); |
| 147 | + for (int i = 0; i < NUM_TEST_LED; i++) { |
| 148 | + leds_on_off[(current_main_led + i) % 4] = |
| 149 | + ((led_state >> i) & 0x1) ? LED_ON : LED_OFF; |
| 150 | + } |
| 151 | + |
| 152 | + for (int i = 0; i < NUM_TEST_LED; i++) { |
| 153 | + gpio_pin_set_dt(leds[i], leds_on_off[i]); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +static void test_touch_keys_cb_handler(struct input_event *evt, void *user_data) |
| 159 | +{ |
| 160 | + switch (evt->code) { |
| 161 | + case TOUCH_BUTTON_1: |
| 162 | + if (evt->value == 0) { |
| 163 | + printk("Button 1 released\n"); |
| 164 | + if (last_val[BUTTON_1_IDX] != 0) { |
| 165 | + k_sem_give(&btn1_sem); |
| 166 | + } |
| 167 | + } else { |
| 168 | + event_count[BUTTON_1_IDX]++; |
| 169 | + printk("Button 1 pressed %d time(s)\n", event_count[BUTTON_1_IDX]); |
| 170 | + } |
| 171 | + last_val[BUTTON_1_IDX] = evt->value; |
| 172 | + break; |
| 173 | + |
| 174 | + case TOUCH_BUTTON_2: |
| 175 | + if (evt->value == 0) { |
| 176 | + printk("Button 2 released\n"); |
| 177 | + if (last_val[BUTTON_2_IDX] != 0) { |
| 178 | + k_sem_give(&btn2_sem); |
| 179 | + } |
| 180 | + } else { |
| 181 | + event_count[BUTTON_2_IDX]++; |
| 182 | + printk("Button 2 pressed %d time(s)\n", event_count[BUTTON_2_IDX]); |
| 183 | + } |
| 184 | + last_val[BUTTON_2_IDX] = evt->value; |
| 185 | + break; |
| 186 | + |
| 187 | + case TOUCH_SLIDER: |
| 188 | + if (evt->value == TOUCH_OFF_VALUE) { |
| 189 | + printk("Slider released\n"); |
| 190 | + if (last_val[SLIDER_IDX] != TOUCH_OFF_VALUE) { |
| 191 | + } |
| 192 | + } else { |
| 193 | + if (last_val[SLIDER_IDX] == TOUCH_OFF_VALUE) { |
| 194 | + event_count[SLIDER_IDX]++; |
| 195 | + printk("Slider pressed\n"); |
| 196 | + } |
| 197 | + printk("Position: %d\n", evt->value); |
| 198 | + } |
| 199 | + k_sem_give(&sldr_sem); |
| 200 | + last_val[SLIDER_IDX] = evt->value; |
| 201 | + break; |
| 202 | + |
| 203 | + default: |
| 204 | + break; |
| 205 | + } |
| 206 | + last_code = evt->code; |
| 207 | +} |
| 208 | +INPUT_CALLBACK_DEFINE(test_touch_dev, test_touch_keys_cb_handler, NULL); |
| 209 | + |
| 210 | +K_THREAD_DEFINE(button1_id, STACKSIZE, button1_task, NULL, NULL, NULL, PRIORITY, 0, 0); |
| 211 | +K_THREAD_DEFINE(button2_id, STACKSIZE, button2_task, NULL, NULL, NULL, PRIORITY, 0, 0); |
| 212 | +K_THREAD_DEFINE(slider_id, STACKSIZE, slider_task, NULL, NULL, NULL, PRIORITY, 0, 0); |
| 213 | +K_THREAD_DEFINE(render_id, STACKSIZE, render_task, NULL, NULL, NULL, PRIORITY, 0, 0); |
| 214 | + |
| 215 | +int main(void) |
| 216 | +{ |
| 217 | + sample_setup(); |
| 218 | + printk("On board CTSU components sample started!\n"); |
| 219 | + printk("Press on touch nodes for sample\n"); |
| 220 | + return 0; |
| 221 | +} |
0 commit comments