1 /*
2 * Copyright 2023 Google LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/kscan.h>
9 #include <zephyr/input/input.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/ztest.h>
12
13 static const struct device *kscan_dev = DEVICE_DT_GET(
14 DT_NODELABEL(kscan_input));
15 static const struct device *input_dev = DEVICE_DT_GET(
16 DT_NODELABEL(fake_input_device));
17
18 DEVICE_DT_DEFINE(DT_INST(0, vnd_input_device), NULL, NULL, NULL, NULL,
19 PRE_KERNEL_1, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
20
21 static struct {
22 uint32_t row;
23 uint32_t col;
24 bool pressed;
25 } last_cb_val;
26 static int callback_calls_count;
27
kscan_callback(const struct device * dev,uint32_t row,uint32_t col,bool pressed)28 static void kscan_callback(const struct device *dev, uint32_t row, uint32_t col,
29 bool pressed)
30 {
31 TC_PRINT("row = %u col = %u %d\n", row, col, pressed);
32
33 callback_calls_count++;
34 last_cb_val.row = row;
35 last_cb_val.col = col;
36 last_cb_val.pressed = pressed;
37 }
38
ZTEST(kscan_input,test_kscan_input)39 ZTEST(kscan_input, test_kscan_input)
40 {
41 kscan_config(kscan_dev, kscan_callback);
42 kscan_enable_callback(kscan_dev);
43
44 input_report_abs(input_dev, INPUT_ABS_X, 101, false, K_FOREVER);
45 zassert_equal(callback_calls_count, 0);
46
47 input_report_abs(input_dev, INPUT_ABS_Y, 102, false, K_FOREVER);
48 zassert_equal(callback_calls_count, 0);
49
50 input_report_key(input_dev, INPUT_BTN_TOUCH, 1, true, K_FOREVER);
51 zassert_equal(callback_calls_count, 1);
52 zassert_equal(last_cb_val.col, 101);
53 zassert_equal(last_cb_val.row, 102);
54 zassert_equal(last_cb_val.pressed, true);
55
56 input_report_abs(input_dev, INPUT_ABS_X, 103, true, K_FOREVER);
57 zassert_equal(callback_calls_count, 2);
58 zassert_equal(last_cb_val.col, 103);
59 zassert_equal(last_cb_val.row, 102);
60 zassert_equal(last_cb_val.pressed, true);
61
62 input_report_key(input_dev, INPUT_BTN_TOUCH, 0, true, K_FOREVER);
63 zassert_equal(callback_calls_count, 3);
64 zassert_equal(last_cb_val.col, 103);
65 zassert_equal(last_cb_val.row, 102);
66 zassert_equal(last_cb_val.pressed, false);
67 }
68
69 ZTEST_SUITE(kscan_input, NULL, NULL, NULL, NULL, NULL);
70