1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/gpio.h>
10 #include <zephyr/drivers/gpio/gpio_sx1509b.h>
11 
12 #define NUMBER_OF_LEDS 3
13 #define GREEN_LED DT_GPIO_PIN(DT_NODELABEL(led0), gpios)
14 #define BLUE_LED DT_GPIO_PIN(DT_NODELABEL(led1), gpios)
15 #define RED_LED DT_GPIO_PIN(DT_NODELABEL(led2), gpios)
16 
17 enum sx1509b_color { sx1509b_red = 0, sx1509b_green, sx1509b_blue };
18 
19 const struct device *sx1509b_dev;
20 static struct k_work_delayable rgb_work;
21 static uint8_t which_color = sx1509b_red;
22 static uint8_t iterator;
23 
24 static const gpio_pin_t rgb_pins[] = {
25 	RED_LED,
26 	GREEN_LED,
27 	BLUE_LED,
28 };
29 
rgb_work_handler(struct k_work * work)30 static void rgb_work_handler(struct k_work *work)
31 {
32 	if (which_color == sx1509b_red) {
33 		sx1509b_led_intensity_pin_set(sx1509b_dev, RED_LED, iterator);
34 
35 		iterator++;
36 
37 		if (iterator >= 255) {
38 			sx1509b_led_intensity_pin_set(sx1509b_dev, RED_LED, 0);
39 			which_color = sx1509b_green;
40 			iterator = 0;
41 		}
42 	} else if (which_color == sx1509b_green) {
43 		sx1509b_led_intensity_pin_set(sx1509b_dev, GREEN_LED, iterator);
44 
45 		iterator++;
46 
47 		if (iterator >= 255) {
48 			sx1509b_led_intensity_pin_set(sx1509b_dev, GREEN_LED,
49 						      0);
50 			which_color = sx1509b_blue;
51 			iterator = 0;
52 		}
53 	} else if (which_color == sx1509b_blue) {
54 		sx1509b_led_intensity_pin_set(sx1509b_dev, BLUE_LED, iterator);
55 
56 		iterator++;
57 
58 		if (iterator >= 255) {
59 			sx1509b_led_intensity_pin_set(sx1509b_dev, BLUE_LED, 0);
60 			which_color = sx1509b_red;
61 			iterator = 0;
62 		}
63 	}
64 
65 	k_work_schedule(k_work_delayable_from_work(work), K_MSEC(10));
66 }
67 
main(void)68 int main(void)
69 {
70 	int err;
71 
72 	printk("SX1509B intensity sample\n");
73 
74 	sx1509b_dev = DEVICE_DT_GET(DT_NODELABEL(sx1509b));
75 
76 	if (!device_is_ready(sx1509b_dev)) {
77 		printk("sx1509b: device not ready.\n");
78 		return 0;
79 	}
80 
81 	for (int i = 0; i < NUMBER_OF_LEDS; i++) {
82 		err = sx1509b_led_intensity_pin_configure(sx1509b_dev,
83 							  rgb_pins[i]);
84 
85 		if (err) {
86 			printk("Error configuring pin for LED intensity\n");
87 		}
88 	}
89 
90 	k_work_init_delayable(&rgb_work, rgb_work_handler);
91 	k_work_schedule(&rgb_work, K_NO_WAIT);
92 	return 0;
93 }
94