1 /*
2  * Copyright (c) 2018 Jan Van Winkel <jan.van_winkel@dxplore.eu>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/devicetree.h>
9 #include <zephyr/drivers/display.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <lvgl.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <zephyr/kernel.h>
15 #include <lvgl_input_device.h>
16 
17 #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
18 #include <zephyr/logging/log.h>
19 LOG_MODULE_REGISTER(app);
20 
21 static uint32_t count;
22 
23 #ifdef CONFIG_GPIO
24 static struct gpio_dt_spec button_gpio = GPIO_DT_SPEC_GET_OR(
25 		DT_ALIAS(sw0), gpios, {0});
26 static struct gpio_callback button_callback;
27 
button_isr_callback(const struct device * port,struct gpio_callback * cb,uint32_t pins)28 static void button_isr_callback(const struct device *port,
29 				struct gpio_callback *cb,
30 				uint32_t pins)
31 {
32 	ARG_UNUSED(port);
33 	ARG_UNUSED(cb);
34 	ARG_UNUSED(pins);
35 
36 	count = 0;
37 }
38 #endif /* CONFIG_GPIO */
39 
40 #ifdef CONFIG_LV_Z_ENCODER_INPUT
41 static const struct device *lvgl_encoder =
42 	DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_lvgl_encoder_input));
43 #endif /* CONFIG_LV_Z_ENCODER_INPUT */
44 
45 #ifdef CONFIG_LV_Z_KEYPAD_INPUT
46 static const struct device *lvgl_keypad =
47 	DEVICE_DT_GET(DT_COMPAT_GET_ANY_STATUS_OKAY(zephyr_lvgl_keypad_input));
48 #endif /* CONFIG_LV_Z_KEYPAD_INPUT */
49 
lv_btn_click_callback(lv_event_t * e)50 static void lv_btn_click_callback(lv_event_t *e)
51 {
52 	ARG_UNUSED(e);
53 
54 	count = 0;
55 }
56 
main(void)57 int main(void)
58 {
59 	char count_str[11] = {0};
60 	const struct device *display_dev;
61 	lv_obj_t *hello_world_label;
62 	lv_obj_t *count_label;
63 
64 	display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
65 	if (!device_is_ready(display_dev)) {
66 		LOG_ERR("Device not ready, aborting test");
67 		return 0;
68 	}
69 
70 #ifdef CONFIG_GPIO
71 	if (gpio_is_ready_dt(&button_gpio)) {
72 		int err;
73 
74 		err = gpio_pin_configure_dt(&button_gpio, GPIO_INPUT);
75 		if (err) {
76 			LOG_ERR("failed to configure button gpio: %d", err);
77 			return 0;
78 		}
79 
80 		gpio_init_callback(&button_callback, button_isr_callback,
81 				   BIT(button_gpio.pin));
82 
83 		err = gpio_add_callback(button_gpio.port, &button_callback);
84 		if (err) {
85 			LOG_ERR("failed to add button callback: %d", err);
86 			return 0;
87 		}
88 
89 		err = gpio_pin_interrupt_configure_dt(&button_gpio,
90 						      GPIO_INT_EDGE_TO_ACTIVE);
91 		if (err) {
92 			LOG_ERR("failed to enable button callback: %d", err);
93 			return 0;
94 		}
95 	}
96 #endif /* CONFIG_GPIO */
97 
98 #ifdef CONFIG_LV_Z_ENCODER_INPUT
99 	lv_obj_t *arc;
100 	lv_group_t *arc_group;
101 
102 	arc = lv_arc_create(lv_scr_act());
103 	lv_obj_align(arc, LV_ALIGN_CENTER, 0, -15);
104 	lv_obj_set_size(arc, 150, 150);
105 
106 	arc_group = lv_group_create();
107 	lv_group_add_obj(arc_group, arc);
108 	lv_indev_set_group(lvgl_input_get_indev(lvgl_encoder), arc_group);
109 #endif /* CONFIG_LV_Z_ENCODER_INPUT */
110 
111 #ifdef CONFIG_LV_Z_KEYPAD_INPUT
112 	lv_obj_t *btn_matrix;
113 	lv_group_t *btn_matrix_group;
114 	static const char *const btnm_map[] = {"1", "2", "3", "4", ""};
115 
116 	btn_matrix = lv_btnmatrix_create(lv_scr_act());
117 	lv_obj_align(btn_matrix, LV_ALIGN_CENTER, 0, 70);
118 	lv_btnmatrix_set_map(btn_matrix, (const char **)btnm_map);
119 	lv_obj_set_size(btn_matrix, 100, 50);
120 
121 	btn_matrix_group = lv_group_create();
122 	lv_group_add_obj(btn_matrix_group, btn_matrix);
123 	lv_indev_set_group(lvgl_input_get_indev(lvgl_keypad), btn_matrix_group);
124 #endif /* CONFIG_LV_Z_KEYPAD_INPUT */
125 
126 	if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN) || IS_ENABLED(CONFIG_LV_Z_POINTER_INPUT)) {
127 		lv_obj_t *hello_world_button;
128 
129 		hello_world_button = lv_btn_create(lv_scr_act());
130 		lv_obj_align(hello_world_button, LV_ALIGN_CENTER, 0, -15);
131 		lv_obj_add_event_cb(hello_world_button, lv_btn_click_callback, LV_EVENT_CLICKED,
132 				    NULL);
133 		hello_world_label = lv_label_create(hello_world_button);
134 	} else {
135 		hello_world_label = lv_label_create(lv_scr_act());
136 	}
137 
138 	lv_label_set_text(hello_world_label, "Hello world!");
139 	lv_obj_align(hello_world_label, LV_ALIGN_CENTER, 0, 0);
140 
141 	count_label = lv_label_create(lv_scr_act());
142 	lv_obj_align(count_label, LV_ALIGN_BOTTOM_MID, 0, 0);
143 
144 	lv_task_handler();
145 	display_blanking_off(display_dev);
146 
147 	while (1) {
148 		if ((count % 100) == 0U) {
149 			sprintf(count_str, "%d", count/100U);
150 			lv_label_set_text(count_label, count_str);
151 		}
152 		lv_task_handler();
153 		++count;
154 		k_sleep(K_MSEC(10));
155 	}
156 }
157