Fix in led colors when a new lap event is detected
[apps/native/gear-racing-car.git] / src / resource / resource_led.c
index df11e7c..1de1ea1 100644 (file)
@@ -84,7 +84,7 @@ static led_s s_info = {
 
 #define RGB_TO_REGISTER(val) ((unsigned)((float)val / 255.0f * 4095u))
 
-static inline void _led_bi_set(bi_led_color_e color)
+static inline void _led_bi_set(bi_led_color_e color, bool store_current_color)
 {
        int red;
        int green;
@@ -94,6 +94,15 @@ static inline void _led_bi_set(bi_led_color_e color)
                return;
        }
 
+//     if (color == s_info.current_color) {
+//             _D("No change in bi led");
+//             return;
+//     }
+
+       if (store_current_color) {
+               s_info.current_color = color;
+       }
+
        switch (color) {
                case LED_COLOR_RED:
                        red = 1;
@@ -118,13 +127,25 @@ static inline void _led_bi_set(bi_led_color_e color)
        _D("BI: [%d, %d]", red, green);
 }
 
-static inline void _rgb_gpio_set(bool red, bool green, bool blue)
+static inline void _rgb_gpio_set(bool red, bool green, bool blue, bool store_current_color)
 {
        if (!s_info.use_rgb_gpio) {
                _D("gpio rgb led is turned OFF");
                return;
        }
 
+//     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]) {
+//             _D("No change in gpio led");
+//             return;
+//     }
+
+       if (store_current_color) {
+               s_info.current_rgb_gpio_color[0] = red;
+               s_info.current_rgb_gpio_color[1] = green;
+               s_info.current_rgb_gpio_color[2] = blue;
+       }
        _D("RGB GPIO: [%d, %d, %d]", red, green, blue);
 
        int ret = peripheral_gpio_write(s_info.rgb_gpio[0], red);
@@ -137,7 +158,7 @@ static inline void _rgb_gpio_set(bool red, bool green, bool blue)
        ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret);
 }
 
-static inline void _rgb_l2c_set(int red, int green, int blue)
+static inline void _rgb_l2c_set(int red, int green, int blue, bool store_current_color)
 {
        FUNCTION_START;
        if (!s_info.use_rgb_l2c) {
@@ -145,6 +166,19 @@ static inline void _rgb_l2c_set(int red, int green, int blue)
                return;
        }
 
+//     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]) {
+//             _D("No change in l2c led");
+//             return;
+//     }
+
+       if (store_current_color) {
+               s_info.current_rgb_l2c_color[0] = red;
+               s_info.current_rgb_l2c_color[1] = green;
+               s_info.current_rgb_l2c_color[2] = blue;
+       }
+
        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);
 
@@ -160,20 +194,20 @@ static inline void _rgb_l2c_set(int red, int green, int blue)
 
 static gboolean _restore_bi_color_cb(gpointer data)
 {
-       _led_bi_set(s_info.current_color);
+       _led_bi_set(s_info.current_color, false);
        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]);
+       _rgb_gpio_set(s_info.current_rgb_gpio_color[0], s_info.current_rgb_gpio_color[1], s_info.current_rgb_gpio_color[2], false);
        return false;
 }
 
 static gboolean _restore_rgb_l2c_color_cb(gpointer data)
 {
        FUNCTION_START;
-       _rgb_l2c_set(s_info.current_rgb_l2c_color[0], s_info.current_rgb_l2c_color[1], s_info.current_rgb_l2c_color[2]);
+       _rgb_l2c_set(s_info.current_rgb_l2c_color[0], s_info.current_rgb_l2c_color[1], s_info.current_rgb_l2c_color[2], false);
        FUNCTION_END;
        return false;
 }
@@ -301,127 +335,47 @@ void resource_led_destroy(void)
        peripheral_gpio_close(s_info.rgb_gpio[1]);
        peripheral_gpio_close(s_info.rgb_gpio[2]);
 
-       resource_led_rgb_l2c_set(0, 0, 0);
+       _rgb_l2c_set(0, 0, 0, true);
 
        resource_pca9685_fini(DEFAULT_RGB_L2C_R);
        resource_pca9685_fini(DEFAULT_RGB_L2C_G);
        resource_pca9685_fini(DEFAULT_RGB_L2C_B);
 }
 
-void resource_led_bi_set(bi_led_color_e color)
-{
-       _D("Set led to: %d", color);
-       if (color == s_info.current_color) {
-               _D("No change in bi led");
-               return;
-       }
-
-       s_info.current_color = color;
-       _led_bi_set(color);
-}
-
-void resource_led_bi_blink(bi_led_color_e color, unsigned timeout)
-{
-       if (color == s_info.current_color) {
-               _D("No change in bi led");
-               return;
-       }
-
-       _led_bi_set(color);
-       g_timeout_add(timeout, _restore_bi_color_cb, NULL);
-}
-
-void resource_led_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]) {
-               _D("No change in gpio led");
-               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_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout)
-{
-       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]) {
-               _D("No change in gpio led");
-               return;
-       }
-
-       _rgb_gpio_set(red, green, blue);
-       g_timeout_add(timeout, _restore_rgb_gpio_color_cb, NULL);
-}
-
-void resource_led_rgb_l2c_set(int red, int green, int 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]) {
-               _D("No change in l2c led");
-               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_led_rgb_l2c_blink(int red, int green, int 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]) {
-               _D("No change in l2c led");
-               return;
-       }
-
-       _rgb_l2c_set(red, green, blue);
-       g_timeout_add(timeout, _restore_rgb_l2c_color_cb, NULL);
-}
-
-void resource_led_set_rgb_colors(char *key,
+static void _set_rgb(char *key, bool store_current,
                int def_r_3bit, int def_g_3bit, int def_b_3bit,
                int def_r_24bit, int def_g_24bit, int def_b_24bit,
                bi_led_color_e bi_led)
 {
-       FUNCTION_START;
-
        int red, green, blue;
        char final_key[PATH_MAX];
 
        snprintf(final_key, PATH_MAX, "3bit.%s", key);
        bool modified = config_get_rgb_with_default("Rpi.led", final_key, def_r_3bit, def_g_3bit, def_b_3bit, &red, &green, &blue);
-       resource_led_rgb_gpio_set(red, green, blue);
-
-       _D("%s: 3bit color: [%d, %d, %d]", final_key, red, green, blue);
+       _rgb_gpio_set(red, green, blue, store_current);
 
        snprintf(final_key, PATH_MAX, "24bit.%s", key);
        modified |= config_get_rgb_with_default("Rpi.led", final_key, def_r_24bit, def_g_24bit, def_b_24bit, &red, &green, &blue);
-       resource_led_rgb_l2c_set(red, green, blue);
-
-       _D("%s: 24bit color: [%d, %d, %d]", final_key, red, green, blue);
+       _rgb_l2c_set(red, green, blue, store_current);
 
        int value;
        snprintf(final_key, PATH_MAX, "bicolor.%s", key);
        modified |= config_get_int_with_default("Rpi.led", final_key, bi_led, &value);
-       resource_led_bi_set(value);
+       _led_bi_set(value, store_current);
 
        if (modified) {
                config_save();
        }
+}
+
+void resource_led_set_rgb_colors(char *key,
+               int def_r_3bit, int def_g_3bit, int def_b_3bit,
+               int def_r_24bit, int def_g_24bit, int def_b_24bit,
+               bi_led_color_e bi_led)
+{
+       FUNCTION_START;
 
-       _D("%s: bicolor: [%d]", final_key, value);
+       _set_rgb(key, true, def_r_3bit, def_g_3bit, def_b_3bit, def_r_24bit, def_g_24bit, def_b_24bit, bi_led);
 
        FUNCTION_END;
 }
@@ -433,8 +387,7 @@ void resource_led_blink_rgb_colors(char *key,
 {
        FUNCTION_START;
 
-       resource_led_set_rgb_colors(key, def_r_3bit, def_g_3bit, def_b_3bit, def_r_24bit, def_g_24bit, def_b_24bit, bi_led);
-
+       _set_rgb(key, false, def_r_3bit, def_g_3bit, def_b_3bit, def_r_24bit, def_g_24bit, def_b_24bit, bi_led);
        g_timeout_add(timeout, _restore_rgb_color_cb, NULL);