1 /*
2 * Copyright (c) 2019 Henrik Brix Andersen <henrik@brixandersen.dk>
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/drivers/led.h>
8 #include <zephyr/drivers/kscan.h>
9 #include <zephyr/devicetree.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/logging/log.h>
12
13 LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
14
15 #define LED_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(holtek_ht16k33)
16 #define KEY_NODE DT_CHILD(LED_NODE, keyscan)
17
keyscan_callback(const struct device * dev,uint32_t row,uint32_t column,bool pressed)18 static void keyscan_callback(const struct device *dev, uint32_t row,
19 uint32_t column, bool pressed)
20 {
21 LOG_INF("Row %d, column %d %s", row, column,
22 pressed ? "pressed" : "released");
23 }
24
main(void)25 int main(void)
26 {
27 const struct device *const led = DEVICE_DT_GET(LED_NODE);
28 const struct device *const key = DEVICE_DT_GET(KEY_NODE);
29 int err;
30 int i;
31
32 if (!device_is_ready(led)) {
33 LOG_ERR("LED device not ready");
34 return 0;
35 }
36
37 if (!device_is_ready(key)) {
38 LOG_ERR("Keyscan device not ready");
39 return 0;
40 }
41
42 err = kscan_config(key, keyscan_callback);
43 if (err) {
44 LOG_ERR("Failed to add keyscan callback (err %d)", err);
45 }
46
47 while (1) {
48 LOG_INF("Iterating through all LEDs, turning them on "
49 "one-by-one");
50 for (i = 0; i < 128; i++) {
51 led_on(led, i);
52 k_sleep(K_MSEC(100));
53 }
54
55 for (i = 500; i <= 2000; i *= 2) {
56 LOG_INF("Blinking LEDs with a period of %d ms", i);
57 led_blink(led, 0, i / 2, i / 2);
58 k_msleep(10 * i);
59 }
60 led_blink(led, 0, 0, 0);
61
62 for (i = 100; i >= 0; i -= 10) {
63 LOG_INF("Setting LED brightness to %d%%", i);
64 led_set_brightness(led, 0, i);
65 k_sleep(K_MSEC(1000));
66 }
67
68 LOG_INF("Turning all LEDs off and restoring 100%% brightness");
69 for (i = 0; i < 128; i++) {
70 led_off(led, i);
71 }
72 led_set_brightness(led, 0, 100);
73 }
74 return 0;
75 }
76