#define DEFAULT_BI_LED_RED 17
#define DEFAULT_BI_LED_GREEN 27
+
#define CONFIG_KEY_RPI_BI_LED_RED "bi.red"
#define CONFIG_KEY_RPI_BI_LED_GREEN "bi.green"
#define CONFIG_KEY_RPI_USE_BI_LED "bi.use"
-#define DEFAULT_RGB_LED_RED 12
-#define DEFAULT_RGB_LED_GREEN 13
-#define DEFAULT_RGB_LED_BLUE 24
-#define CONFIG_KEY_RPI_RGB_LED_RED "rgb.red"
-#define CONFIG_KEY_RPI_RGB_LED_GREEN "rgb.green"
-#define CONFIG_KEY_RPI_RGB_LED_BLUE "rgb.blue"
-#define CONFIG_KEY_RPI_USE_RGB_LED "rgb.use"
-
-#define DEFAULT_RGB_PCA_R 4
-#define DEFAULT_RGB_PCA_G 5
-#define DEFAULT_RGB_PCA_B 10
+
+#define CONFIG_KEY_RPI_RGB_GPIO_R "rgb.gpio.red"
+#define CONFIG_KEY_RPI_RGB_GPIO_G "rgb.gpio.green"
+#define CONFIG_KEY_RPI_RGB_GPIO_B "rgb.gpio.blue"
+#define CONFIG_KEY_RPI_USE_GPIO_RGB "rgb.gpio.use"
+
+#define CONFIG_KEY_RPI_RGB_L2C_R "rgb.l2c.red"
+#define CONFIG_KEY_RPI_RGB_L2C_G "rgb.l2c.green"
+#define CONFIG_KEY_RPI_RGB_L2C_B "rgb.l2c.blue"
+#define CONFIG_KEY_RPI_USE_L2C_RGB "rgb.l2c.use"
+
+
+#define DEFAULT_RGB_GPIO_R 14
+#define DEFAULT_RGB_GPIO_G 15
+#define DEFAULT_RGB_GPIO_B 18
+
+#define DEFAULT_RGB_L2C_R 8
+#define DEFAULT_RGB_L2C_G 9
+#define DEFAULT_RGB_L2C_B 10
#define CONFIG_GRP_RPI "Rpi.led"
typedef struct _led_s {
peripheral_gpio_h gpio_bi_led[2];
- peripheral_pwm_h pwn_rgb_led[3];
- led_color_e current_color;
+ peripheral_gpio_h rgb_gpio[3];
+ bi_led_color_e current_color;
+
+
+ bool current_rgb_gpio_color[3];
+ int current_rgb_l2c_color[3];
int use_bi_led;
- int use_rgb_led;
+ int use_rgb_gpio;
+ int use_rgb_l2c;
} led_s;
static led_s s_info = {
.current_color = LED_COLOR_NONE,
+ .current_rgb_gpio_color = { 0, },
+ .current_rgb_l2c_color = { 0, },
0, };
#define CHECK_GPIO_ERROR(pin, ret) \
return ; \
}
+#define RGB_TO_REGISTER(val) ((unsigned)((float)val / 255.0f * 4095u))
-static inline void _led_set(led_color_e color)
+static inline void _led_bi_set(bi_led_color_e color)
{
int red;
int green;
break;
}
- _D("Colors: %d %d", red, green);
-
int ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_RED], red);
ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
- peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
+ ret = peripheral_gpio_write(s_info.gpio_bi_led[LED_COLOR_GREEN], green);
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ _D("BI: [%d, %d]", red, green);
+}
+
+static inline void _rgb_gpio_set(bool red, bool green, bool blue)
+{
+ int ret = peripheral_gpio_write(s_info.rgb_gpio[0], red);
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ ret = peripheral_gpio_write(s_info.rgb_gpio[1], green);
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ ret = peripheral_gpio_write(s_info.rgb_gpio[2], blue);
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ _D("RGB GPIO: [%d, %d, %d]", red, green, blue);
+}
+
+static inline void _rgb_l2c_set(bool red, bool green, bool blue)
+{
+//#define R 51
+//#define G 231
+//#define B 157
+
+//#define R 88
+//#define G 51
+//#define B 231
+
+
+ int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_R, 0, RGB_TO_REGISTER(red));
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_G, 0, RGB_TO_REGISTER(green));
ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_L2C_B, 0, RGB_TO_REGISTER(blue));
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+
+ _D("RGB L2C: [%d, %d, %d] -> [%d, %d, %d]", red, green, blue, RGB_TO_REGISTER(red), RGB_TO_REGISTER(green), RGB_TO_REGISTER(blue));
+}
+
+static gboolean _restore_bi_color_cb(gpointer data)
+{
+ _led_bi_set(s_info.current_color);
+ return false;
+}
+
+static gboolean _restore_rgb_gpio_color_cb(gpointer data)
+{
+ _rgb_gpio_set(s_info.current_rgb_gpio_color[0], s_info.current_rgb_gpio_color[1], s_info.current_rgb_gpio_color[2]);
+ return false;
}
-static gboolean _restore_color_cb(gpointer data)
+static gboolean _restore_rgb_l2c_color_cb(gpointer data)
{
- _led_set(s_info.current_color);
+ _rgb_l2c_set(s_info.current_rgb_l2c_color[0], s_info.current_rgb_l2c_color[1], s_info.current_rgb_l2c_color[2]);
return false;
}
}
-//static peripheral_pwm_h _init_pwm(int default_pwm_pin, char *key)
-//{
-// peripheral_pwm_h pwm;
-// int pin;
-//
-// bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_pwm_pin, &pin);
-// _D("PWN: %d, DUTY: %u, period: %u", pin, (unsigned)2e6, (unsigned)2e7);
-//
-// if (modified) {
-// config_save();
-// }
-//
-// int ret = peripheral_pwm_open(1, pin, &pwm);
-// retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-//
-// ret = peripheral_pwm_set_period(pwm, (unsigned)2e7);
-// retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-//
-// ret = peripheral_pwm_set_duty_cycle(pwm, (unsigned)2e6);
-// retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-//
-// ret = peripheral_pwm_set_enabled(pwm, true);
-// retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret, NULL);
-//
-//
-//
-// return pwm;
-//}
-
-//static void _init_rgb_led(void)
-//{
-// bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_RGB_LED, 1, &s_info.use_rgb_led);
-// if (modified) {
-// config_save();
-// }
-//
-// if (s_info.use_rgb_led) {
-// s_info.pwn_rgb_led[LED_COLOR_RED] = _init_pwm(DEFAULT_RGB_LED_RED, CONFIG_KEY_RPI_RGB_LED_RED);
-// s_info.pwn_rgb_led[LED_COLOR_GREEN] = _init_pwm(DEFAULT_RGB_LED_GREEN, CONFIG_KEY_RPI_RGB_LED_GREEN);
-// s_info.pwn_rgb_led[LED_COLOR_BLUE] = _init_pwm(DEFAULT_RGB_LED_BLUE, CONFIG_KEY_RPI_RGB_LED_BLUE);
-//
-//
-// peripheral_pwm_set_duty_cycle(s_info.pwn_rgb_led[LED_COLOR_RED], (unsigned)1e7);
-// }
-//}
-
static void _init_bi_led(void)
{
bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_BI_LED, 1, &s_info.use_bi_led);
}
}
-static void _init_pca(void)
+static void _init_rgb_gpio_led(void)
{
- int ret;
-// int ret = resource_pca9685_init(DEFAULT_RGB_PCA_R, 1000);
-// ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
-////
-// ret = resource_pca9685_init(DEFAULT_RGB_PCA_G, 1000);
-// ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
-//
-// ret = resource_pca9685_init(DEFAULT_RGB_PCA_B, 1000);
-// ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
-//
- ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_R, 0, 0);
- ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+ bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_GPIO_RGB, 1, &s_info.use_rgb_gpio);
+ if (modified) {
+ config_save();
+ }
- ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_G, 0, 255);
+ if (!s_info.use_rgb_gpio) {
+ _D("RGB GPIO is turned off");
+ return;
+ }
+
+ s_info.rgb_gpio[0] = _init_gpio(DEFAULT_RGB_GPIO_R, CONFIG_KEY_RPI_RGB_GPIO_R);
+ s_info.rgb_gpio[1] = _init_gpio(DEFAULT_RGB_GPIO_G, CONFIG_KEY_RPI_RGB_GPIO_G);
+ s_info.rgb_gpio[2] = _init_gpio(DEFAULT_RGB_GPIO_B, CONFIG_KEY_RPI_RGB_GPIO_B);
+}
+
+static void _init_rgb_l2c_led(void)
+{
+ bool modified = config_get_int_with_default(CONFIG_GRP_RPI, CONFIG_KEY_RPI_USE_L2C_RGB, 1, &s_info.use_rgb_l2c);
+ if (modified) {
+ config_save();
+ }
+
+ if (!s_info.use_rgb_l2c) {
+ _D("RGB L2C is turned off");
+ return;
+ }
+
+ int ret = resource_pca9685_init(DEFAULT_RGB_L2C_R, 60);
ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
- ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_B, 0, 0);
+ ret = resource_pca9685_init(DEFAULT_RGB_L2C_G, 60);
ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
-// peripheral_i2c_h g_i2c_h;
-// ret = peripheral_i2c_open(1, 0x40, &g_i2c_h);
-// if (ret != PERIPHERAL_ERROR_NONE) {
-// _E("failed to open pca9685-[bus:%d, addr:%d]", RPI3_I2C_BUS, PCA9685_ADDRESS);
-// return;
-// }
+ ret = resource_pca9685_init(DEFAULT_RGB_L2C_B, 60);
+ ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
}
void resource_led_init(void)
{
_D("Initialize Led");
_init_bi_led();
-// _init_rgb_led();
- _init_pca();
+ _init_rgb_gpio_led();
+ _init_rgb_l2c_led();
}
void resource_led_destroy(void)
peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_RED]);
peripheral_gpio_close(s_info.gpio_bi_led[LED_COLOR_GREEN]);
- resource_pca9685_fini(DEFAULT_RGB_PCA_R);
- resource_pca9685_fini(DEFAULT_RGB_PCA_G);
- resource_pca9685_fini(DEFAULT_RGB_PCA_B);
+ resource_pca9685_fini(DEFAULT_RGB_L2C_R);
+ resource_pca9685_fini(DEFAULT_RGB_L2C_G);
+ resource_pca9685_fini(DEFAULT_RGB_L2C_B);
}
-void resource_led_set(led_color_e color)
+void resource_bi_led_set(bi_led_color_e color)
{
_D("Set led to: %d", color);
if (color == s_info.current_color) {
}
s_info.current_color = color;
- _led_set(color);
+ _led_bi_set(color);
+ peripheral_gpio_write(s_info.rgb_gpio[LED_COLOR_RED], 1);
+}
- int ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_R, 0, 0);
- ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+void resource_bi_led_blink(bi_led_color_e color, unsigned timeout)
+{
+ if (color == s_info.current_color) {
+ return;
+ }
- ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_G, 0, 255);
- ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+ _led_bi_set(color);
+ g_timeout_add(timeout, _restore_bi_color_cb, NULL);
+}
- ret = resource_pca9685_set_value_to_channel(DEFAULT_RGB_PCA_B, 0, 0);
- ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
+void resource_rgb_gpio_set(bool red, bool green, bool blue)
+{
+ if (red == s_info.current_rgb_gpio_color[0] &&
+ green == s_info.current_rgb_gpio_color[1] &&
+ blue == s_info.current_rgb_gpio_color[2]) {
+ return;
+ }
+
+
+ s_info.current_rgb_gpio_color[0] = red;
+ s_info.current_rgb_gpio_color[1] = green;
+ s_info.current_rgb_gpio_color[2] = blue;
+
+ _rgb_gpio_set(red, green, blue);
}
-void resource_led_blink(led_color_e color, unsigned timeout)
+void resource_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout)
{
- if (color == s_info.current_color) {
+ if (red == s_info.current_rgb_gpio_color[0] &&
+ green == s_info.current_rgb_gpio_color[1] &&
+ blue == s_info.current_rgb_gpio_color[2]) {
+ return;
+ }
+
+ _rgb_gpio_set(red, green, blue);
+ g_timeout_add(timeout, _restore_rgb_gpio_color_cb, NULL);
+}
+
+void resource_rgb_l2c_set(bool red, bool green, bool blue)
+{
+ if (red == s_info.current_rgb_l2c_color[0] &&
+ green == s_info.current_rgb_l2c_color[1] &&
+ blue == s_info.current_rgb_l2c_color[2]) {
+ return;
+ }
+
+
+ s_info.current_rgb_l2c_color[0] = red;
+ s_info.current_rgb_l2c_color[1] = green;
+ s_info.current_rgb_l2c_color[2] = blue;
+
+ _rgb_l2c_set(red, green, blue);
+}
+
+void resource_rgb_l2c_blink(bool red, bool green, bool blue, unsigned timeout)
+{
+ if (red == s_info.current_rgb_l2c_color[0] &&
+ green == s_info.current_rgb_l2c_color[1] &&
+ blue == s_info.current_rgb_l2c_color[2]) {
return;
}
- _led_set(color);
- g_timeout_add(timeout, _restore_color_cb, NULL);
+ _rgb_l2c_set(red, green, blue);
+ g_timeout_add(timeout, _restore_rgb_l2c_color_cb, NULL);
}