|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | +#include <zephyr/device.h> |
| 9 | +#include <zephyr/drivers/gpio.h> |
| 10 | +#include <zephyr/input/input.h> |
| 11 | +#include <zephyr/input/input_renesas_ra_ctsu.h> |
| 12 | +#include <zephyr/sys/printk.h> |
| 13 | + |
| 14 | +#ifdef CONFIG_INPUT_RENESAS_RA_QE_TOUCH_CFG |
| 15 | +#include "qe_touch_config.h" |
| 16 | +#endif /* CONFIG_INPUT_RENESAS_RA_QE_TOUCH_CFG*/ |
| 17 | + |
| 18 | +#define LED_MATRIX DT_NODELABEL(rtk0eg0019b01002bj_led_matrix) |
| 19 | +#define BUTTON1 DT_NODELABEL(rtk0eg0019b01002bj_bt1) |
| 20 | +#define BUTTON2 DT_NODELABEL(rtk0eg0019b01002bj_bt2) |
| 21 | +#define BUTTON3 DT_NODELABEL(rtk0eg0019b01002bj_bt3) |
| 22 | +#define SLIDER DT_NODELABEL(rtk0eg0019b01002bj_slider) |
| 23 | +#define WHEEL DT_NODELABEL(rtk0eg0019b01002bj_wheel) |
| 24 | + |
| 25 | +#define QE_TOUCH_CFG1 DT_CHILD(DT_NODELABEL(rtk0eg0019b01002bj_ctsu), group1) |
| 26 | +#define QE_TOUCH_CFG2 DT_CHILD(DT_NODELABEL(rtk0eg0019b01002bj_ctsu), group2) |
| 27 | +#define QE_TOUCH_CFG3 DT_CHILD(DT_NODELABEL(rtk0eg0019b01002bj_ctsu), group3) |
| 28 | + |
| 29 | +#define NUM_ROWS DT_PROP(LED_MATRIX, num_rows) |
| 30 | +#define NUM_COLS DT_PROP(LED_MATRIX, num_cols) |
| 31 | + |
| 32 | +#define LED_MATRIX_ROWS(idx, nodeid) GPIO_DT_SPEC_GET_BY_IDX(nodeid, led_row_gpios, idx) |
| 33 | +#define LED_MATRIX_COLS(idx, nodeid) GPIO_DT_SPEC_GET_BY_IDX(nodeid, led_col_gpios, idx) |
| 34 | + |
| 35 | +#define BUTTON_LED_NUM (3U) |
| 36 | + |
| 37 | +#define SLIDER_LED_NUM (5U) |
| 38 | +#define SLIDER_RESOLUTION (100) |
| 39 | + |
| 40 | +#define WHEEL_LED_NUM (8U) |
| 41 | +#define WHEEL_RESOLUTION_DEGREE (360) |
| 42 | + |
| 43 | +static const struct gpio_dt_spec led_row[NUM_ROWS] = { |
| 44 | + LISTIFY(NUM_ROWS, LED_MATRIX_ROWS, (,), LED_MATRIX), |
| 45 | +}; |
| 46 | + |
| 47 | +static const struct gpio_dt_spec led_col[NUM_COLS] = { |
| 48 | + LISTIFY(NUM_COLS, LED_MATRIX_COLS, (,), LED_MATRIX), |
| 49 | +}; |
| 50 | + |
| 51 | +static const unsigned int wheel_leds_lut[WHEEL_LED_NUM] = {0, 1, 2, 3, 4, 5, 6, 7}; |
| 52 | +static const unsigned int slider_leds_lut[SLIDER_LED_NUM] = {8, 9, 10, 11, 12}; |
| 53 | +static const unsigned int button_leds_lut[BUTTON_LED_NUM] = {13, 14, 15}; |
| 54 | + |
| 55 | +static void rtk0eg0019b01002bj_led_output(unsigned int led_idx, bool on) |
| 56 | +{ |
| 57 | + /* Refer to RTK0EG0022S01001BJ - Design Package for the Touch Application board layout */ |
| 58 | + static const unsigned int led_map[NUM_ROWS * NUM_COLS][2] = { |
| 59 | + {0, 0}, {1, 0}, {2, 0}, {3, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, |
| 60 | + {0, 3}, {1, 3}, {2, 3}, {3, 3}, {0, 2}, {1, 2}, {2, 2}, {3, 2}, |
| 61 | + }; |
| 62 | + const struct gpio_dt_spec *p_led_row = &led_row[led_map[led_idx][0]]; |
| 63 | + const struct gpio_dt_spec *p_led_col = &led_col[led_map[led_idx][1]]; |
| 64 | + |
| 65 | + gpio_pin_set_dt(p_led_row, on ? 1 : 0); |
| 66 | + gpio_pin_set_dt(p_led_col, on ? 1 : 0); |
| 67 | +} |
| 68 | + |
| 69 | +static void rtk0eg0019b01002bj_led_init(void) |
| 70 | +{ |
| 71 | + for (int i = 0; i < NUM_ROWS; i++) { |
| 72 | + gpio_pin_configure_dt(&led_row[i], GPIO_OUTPUT_INACTIVE); |
| 73 | + } |
| 74 | + |
| 75 | + for (int i = 0; i < NUM_COLS; i++) { |
| 76 | + gpio_pin_configure_dt(&led_col[i], GPIO_OUTPUT_INACTIVE); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +static void blink_leds(unsigned int duration_ms) |
| 81 | +{ |
| 82 | + for (int i = 0; i < 5; i++) { |
| 83 | + /* Turn all LEDs ON */ |
| 84 | + for (int j = 0; j < NUM_ROWS * NUM_COLS; j++) { |
| 85 | + rtk0eg0019b01002bj_led_output(j, true); |
| 86 | + } |
| 87 | + k_msleep(duration_ms); |
| 88 | + |
| 89 | + /* Turn all LEDs OFF */ |
| 90 | + for (int j = 0; j < NUM_ROWS * NUM_COLS; j++) { |
| 91 | + rtk0eg0019b01002bj_led_output(j, false); |
| 92 | + } |
| 93 | + k_msleep(duration_ms); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +static inline unsigned int wheel_get_current_step(unsigned int value) |
| 98 | +{ |
| 99 | + return (value * WHEEL_LED_NUM) / WHEEL_RESOLUTION_DEGREE; |
| 100 | +} |
| 101 | + |
| 102 | +static inline unsigned int slider_get_current_step(unsigned int value) |
| 103 | +{ |
| 104 | + return (value * SLIDER_LED_NUM) / SLIDER_RESOLUTION; |
| 105 | +} |
| 106 | + |
| 107 | +static void rtk0eg0019b01002bj_evt_handler(struct input_event *evt, void *user_data) |
| 108 | +{ |
| 109 | + unsigned int led_idx; |
| 110 | + |
| 111 | + /* Set all LEDs to OFF */ |
| 112 | + for (int i = 0; i < NUM_ROWS * NUM_COLS; i++) { |
| 113 | + rtk0eg0019b01002bj_led_output(i, false); |
| 114 | + } |
| 115 | + |
| 116 | + switch (evt->code) { |
| 117 | + case INPUT_KEY_0: { |
| 118 | + led_idx = button_leds_lut[0]; |
| 119 | + break; |
| 120 | + } |
| 121 | + case INPUT_KEY_1: { |
| 122 | + led_idx = button_leds_lut[1]; |
| 123 | + break; |
| 124 | + } |
| 125 | + case INPUT_KEY_2: { |
| 126 | + led_idx = button_leds_lut[2]; |
| 127 | + break; |
| 128 | + } |
| 129 | + case INPUT_ABS_WHEEL: { |
| 130 | + led_idx = wheel_get_current_step(evt->value) + wheel_leds_lut[0]; |
| 131 | + break; |
| 132 | + } |
| 133 | + case INPUT_ABS_THROTTLE: { |
| 134 | + led_idx = slider_get_current_step(evt->value) + slider_leds_lut[0]; |
| 135 | + break; |
| 136 | + } |
| 137 | + default: |
| 138 | + /* Unexpected event */ |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + rtk0eg0019b01002bj_led_output(led_idx, true); |
| 143 | +} |
| 144 | + |
| 145 | +INPUT_CALLBACK_DEFINE_NAMED(DEVICE_DT_GET(BUTTON1), rtk0eg0019b01002bj_evt_handler, NULL, button1); |
| 146 | +INPUT_CALLBACK_DEFINE_NAMED(DEVICE_DT_GET(BUTTON2), rtk0eg0019b01002bj_evt_handler, NULL, button2); |
| 147 | +INPUT_CALLBACK_DEFINE_NAMED(DEVICE_DT_GET(BUTTON3), rtk0eg0019b01002bj_evt_handler, NULL, button3); |
| 148 | +INPUT_CALLBACK_DEFINE_NAMED(DEVICE_DT_GET(SLIDER), rtk0eg0019b01002bj_evt_handler, NULL, slider); |
| 149 | +INPUT_CALLBACK_DEFINE_NAMED(DEVICE_DT_GET(WHEEL), rtk0eg0019b01002bj_evt_handler, NULL, wheel); |
| 150 | + |
| 151 | +int main(void) |
| 152 | +{ |
| 153 | +#ifdef CONFIG_INPUT_RENESAS_RA_QE_TOUCH_CFG |
| 154 | + const struct device *qe_touch_cfg1 = DEVICE_DT_GET(QE_TOUCH_CFG1); |
| 155 | + const struct device *qe_touch_cfg2 = DEVICE_DT_GET(QE_TOUCH_CFG2); |
| 156 | + const struct device *qe_touch_cfg3 = DEVICE_DT_GET(QE_TOUCH_CFG3); |
| 157 | + int ret; |
| 158 | +#endif |
| 159 | + rtk0eg0019b01002bj_led_init(); |
| 160 | + |
| 161 | + /* Blink all leds 5 times, each time is 200ms */ |
| 162 | + blink_leds(200); |
| 163 | + |
| 164 | +#ifdef CONFIG_INPUT_RENESAS_RA_QE_TOUCH_CFG |
| 165 | + ret = renesas_ra_ctsu_group_configure( |
| 166 | + qe_touch_cfg1, (struct renesas_ra_ctsu_touch_cfg *)&g_qe_touch_instance_config01); |
| 167 | + if (ret < 0) { |
| 168 | + printk("Failed to configure QE Touch Group 1: %d\n", ret); |
| 169 | + return ret; |
| 170 | + } |
| 171 | + |
| 172 | + ret = renesas_ra_ctsu_group_configure( |
| 173 | + qe_touch_cfg2, (struct renesas_ra_ctsu_touch_cfg *)&g_qe_touch_instance_config02); |
| 174 | + if (ret < 0) { |
| 175 | + printk("Failed to configure QE Touch Group 2: %d\n", ret); |
| 176 | + return ret; |
| 177 | + } |
| 178 | + |
| 179 | + ret = renesas_ra_ctsu_group_configure( |
| 180 | + qe_touch_cfg3, (struct renesas_ra_ctsu_touch_cfg *)&g_qe_touch_instance_config03); |
| 181 | + if (ret < 0) { |
| 182 | + printk("Failed to configure QE Touch Group 3: %d\n", ret); |
| 183 | + return ret; |
| 184 | + } |
| 185 | +#endif |
| 186 | + |
| 187 | + printk("rtk0eg0019b01002bj sample started\n"); |
| 188 | + return 0; |
| 189 | +} |
0 commit comments