Skip to content

Commit 14d1753

Browse files
committed
samples: subsys: display: lvgl: Add button to reset counter
Connect the sw0 alias to reset the counter. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent 57d2614 commit 14d1753

File tree

1 file changed

+49
-2
lines changed
  • samples/subsys/display/lvgl/src

1 file changed

+49
-2
lines changed

samples/subsys/display/lvgl/src/main.c

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
*/
66

77
#include <zephyr/device.h>
8+
#include <zephyr/devicetree.h>
89
#include <zephyr/drivers/display.h>
10+
#include <zephyr/drivers/gpio.h>
911
#include <lvgl.h>
1012
#include <stdio.h>
1113
#include <string.h>
@@ -15,9 +17,28 @@
1517
#include <zephyr/logging/log.h>
1618
LOG_MODULE_REGISTER(app);
1719

20+
static uint32_t count;
21+
22+
#ifdef CONFIG_GPIO
23+
static struct gpio_dt_spec button_gpio = GPIO_DT_SPEC_GET_OR(
24+
DT_ALIAS(sw0), gpios, {0});
25+
static struct gpio_callback button_callback;
26+
27+
static void button_isr_callback(const struct device *port,
28+
struct gpio_callback *cb,
29+
uint32_t pins)
30+
{
31+
ARG_UNUSED(port);
32+
ARG_UNUSED(cb);
33+
ARG_UNUSED(pins);
34+
35+
count = 0;
36+
}
37+
#endif
38+
1839
void main(void)
1940
{
20-
uint32_t count = 0U;
41+
int err;
2142
char count_str[11] = {0};
2243
const struct device *display_dev;
2344
lv_obj_t *hello_world_label;
@@ -29,6 +50,32 @@ void main(void)
2950
return;
3051
}
3152

53+
#ifdef CONFIG_GPIO
54+
if (device_is_ready(button_gpio.port)) {
55+
err = gpio_pin_configure_dt(&button_gpio, GPIO_INPUT);
56+
if (err) {
57+
LOG_ERR("failed to configure button gpio: %d", err);
58+
return;
59+
}
60+
61+
gpio_init_callback(&button_callback, button_isr_callback,
62+
BIT(button_gpio.pin));
63+
64+
err = gpio_add_callback(button_gpio.port, &button_callback);
65+
if (err) {
66+
LOG_ERR("failed to add button callback: %d", err);
67+
return;
68+
}
69+
70+
err = gpio_pin_interrupt_configure_dt(&button_gpio,
71+
GPIO_INT_EDGE_TO_ACTIVE);
72+
if (err) {
73+
LOG_ERR("failed to enable button callback: %d", err);
74+
return;
75+
}
76+
}
77+
#endif
78+
3279
if (IS_ENABLED(CONFIG_LV_Z_POINTER_KSCAN)) {
3380
lv_obj_t *hello_world_button;
3481

@@ -54,7 +101,7 @@ void main(void)
54101
lv_label_set_text(count_label, count_str);
55102
}
56103
lv_task_handler();
57-
k_sleep(K_MSEC(10));
58104
++count;
105+
k_sleep(K_MSEC(10));
59106
}
60107
}

0 commit comments

Comments
 (0)