1 /*
2  * Copyright 2023 Google LLC
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <zephyr/device.h>
9 #include <zephyr/input/input.h>
10 #include <zephyr/input/input_kbd_matrix.h>
11 #include <zephyr/shell/shell.h>
12 #include <zephyr/shell/shell_dummy.h>
13 
14 static const struct input_kbd_matrix_common_config test_cfg = {
15 	.row_size = INPUT_KBD_MATRIX_ROW_BITS,
16 	.col_size = 4,
17 };
18 
19 DEVICE_DEFINE(kbd_matrix, "kbd-matrix", NULL, NULL,
20 	     NULL,  &test_cfg,
21 	     POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);
22 
report_matrix_entry(int row,int col,int val)23 static void report_matrix_entry(int row, int col, int val)
24 {
25 	const struct device *dev = &DEVICE_NAME_GET(kbd_matrix);
26 
27 	input_report_abs(dev, INPUT_ABS_X, col, false, K_FOREVER);
28 	input_report_abs(dev, INPUT_ABS_Y, row, false, K_FOREVER);
29 	input_report_key(dev, INPUT_BTN_TOUCH, val, true, K_FOREVER);
30 }
31 
main(void)32 int main(void)
33 {
34 	const struct shell *sh = shell_backend_dummy_get_ptr();
35 	int err;
36 
37 	err = shell_execute_cmd(sh, "input kbd_matrix_state_dump kbd-matrix");
38 	if (err) {
39 		printf("Failed to execute the shell command: %d\n", err);
40 	}
41 
42 	report_matrix_entry(0, 0, 1);
43 	report_matrix_entry(4, 0, 1);
44 	report_matrix_entry(1, 1, 1);
45 	report_matrix_entry(2, 2, 1);
46 
47 	report_matrix_entry(0, 0, 0);
48 	report_matrix_entry(4, 0, 0);
49 	report_matrix_entry(1, 1, 0);
50 	report_matrix_entry(2, 2, 0);
51 
52 	report_matrix_entry(3, 3, 1);
53 	report_matrix_entry(3, 3, 0);
54 
55 #if CONFIG_INPUT_KBD_MATRIX_16_BIT_ROW
56 	report_matrix_entry(12, 0, 1);
57 	report_matrix_entry(12, 0, 0);
58 #endif
59 
60 	err = shell_execute_cmd(sh, "input kbd_matrix_state_dump off");
61 	if (err) {
62 		printf("Failed to execute the shell command: %d\n", err);
63 	}
64 
65 	return 0;
66 }
67