From: Michal Skorupinski Date: Thu, 22 Nov 2018 17:11:09 +0000 (+0100) Subject: Config for different led colors displaying the car states X-Git-Url: http://review.tizen.org/git/?p=apps%2Fnative%2Fgear-racing-car.git;a=commitdiff_plain;h=75c6b45209b082337612dde08adde5d866161135 Config for different led colors displaying the car states Change-Id: I468803b99a35b608ace3579d02265058c7a49e0f Signed-off-by: Michal Skorupinski --- diff --git a/inc/config.h b/inc/config.h index 366cee7..7189479 100644 --- a/inc/config.h +++ b/inc/config.h @@ -159,5 +159,8 @@ int config_remove_group(const char *group); bool config_get_string_or_set_default(char *group, char *key, char *default_value, char **value); bool config_get_int_with_default(char *group, char *key, int default_value, int *value); +bool config_get_rgb_with_default(char *group, char *key, + int default_r, int default_g, int default_b, + int *red, int *green, int *blue); #endif /* end of include guard: CONFIG_H */ diff --git a/inc/resource/resource_led.h b/inc/resource/resource_led.h index d134c07..7e31fe2 100644 --- a/inc/resource/resource_led.h +++ b/inc/resource/resource_led.h @@ -28,11 +28,19 @@ typedef enum _led_color_e { void resource_led_init(void); void resource_led_destroy(void); -void resource_bi_led_set(bi_led_color_e color); -void resource_bi_led_blink(bi_led_color_e color, unsigned timeout); -void resource_rgb_gpio_set(bool red, bool green, bool blue); -void resource_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout); -void resource_rgb_l2c_set(int red, int green, int blue); -void resource_rgb_l2c_blink(int red, int green, int blue, unsigned timeout); +void resource_led_bi_set(bi_led_color_e color); +void resource_led_bi_blink(bi_led_color_e color, unsigned timeout); +void resource_led_rgb_gpio_set(bool red, bool green, bool blue); +void resource_led_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout); +void resource_led_rgb_l2c_set(int red, int green, int blue); +void resource_led_rgb_l2c_blink(int red, int green, int blue, unsigned timeout); +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); +void resource_led_blink_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, int timeout); #endif /* RESOURCE_RESOURCE_LED_H_ */ diff --git a/src/app.c b/src/app.c index 753b412..8c4f0bd 100644 --- a/src/app.c +++ b/src/app.c @@ -74,6 +74,14 @@ #define AZIMUTH_MIN 200 #define AZIMUTH_MAX 700 +#define CONFIG_LED_STATE_KEY_INIT "init" +#define CONFIG_DEFAULT_LED_3BIT_INIT 1, 1, 0 +#define CONFIG_DEFAULT_LED_24BIT_INIT 255, 32, 0 + +#define CONFIG_LED_STATE_KEY_OFF "off" +#define CONFIG_DEFAULT_LED_3BIT_OFF 0, 0, 0 +#define CONFIG_DEFAULT_LED_24BIT_OFF 0, 0, 0 + enum { DIR_STATE_S, DIR_STATE_F, @@ -276,7 +284,7 @@ static void _initialize_config() modified |= config_get_string_or_set_default(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid, &id); g_free(uuid); - modified |= config_get_string_or_set_default(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati", &name); + modified |= config_get_string_or_set_default(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Pink Car", &name); modified |= config_get_int_with_default(CONFIG_GRP_STEERING, CONFIG_KEY_STEERING_CENTER, s_info.stering_center, &s_info.stering_center); modified |= config_get_int_with_default(CONFIG_GRP_STEERING, CONFIG_KEY_STEERING_RANGE, s_info.stering_range, &s_info.stering_range); @@ -368,10 +376,10 @@ static bool service_app_create(void *data) controller_connection_manager_set_command_received_cb(__command_received_cb); controller_connection_manager_set_user_name_received_cb(__user_name_received_cb); - resource_bi_led_set(LED_COLOR_RED); - resource_rgb_gpio_set(0, 0, 1); - resource_rgb_l2c_set(0, 0, 255); - + resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_INIT, + CONFIG_DEFAULT_LED_3BIT_INIT, + CONFIG_DEFAULT_LED_24BIT_INIT, + LED_COLOR_RED); return true; } @@ -394,9 +402,10 @@ static void service_app_control(app_control_h app_control, void *data) static void service_app_terminate(void *data) { app_data *ad = data; - resource_bi_led_set(LED_COLOR_NONE); - resource_rgb_gpio_set(0, 0, 0); - resource_rgb_l2c_set(0, 0, 0); + resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_OFF, + CONFIG_DEFAULT_LED_3BIT_OFF, + CONFIG_DEFAULT_LED_24BIT_OFF, + LED_COLOR_NONE); resource_set_servo_motor_value(s_info.stering_pin, STERING_SERVO_CENTER); resource_set_servo_motor_value(s_info.elevation_pin, ELEVATION_MIN); diff --git a/src/cloud/cloud_communication.c b/src/cloud/cloud_communication.c index 482057f..5a183ff 100644 --- a/src/cloud/cloud_communication.c +++ b/src/cloud/cloud_communication.c @@ -27,6 +27,14 @@ #include "net-util.h" #include "resource/resource_led.h" +#define CONFIG_LED_STATE_KEY_READY "ready" +#define CONFIG_DEFAULT_LED_3BIT_READY 1, 0, 1 +#define CONFIG_DEFAULT_LED_24BIT_READY 215, 15, 96 + +#define CONFIG_LED_STATE_KEY_FAIL "fail" +#define CONFIG_DEFAULT_LED_3BIT_FAIL 1, 0, 0 +#define CONFIG_DEFAULT_LED_24BIT_FAIL 255, 0, 0 + typedef struct communication_data_ { gboolean is_initialized; gboolean is_running; @@ -118,15 +126,17 @@ static void post_response_cb(request_result_e result, void *user_data) { if (result == SUCCESS) { _I("POST SUCCESS"); - resource_bi_led_set(LED_COLOR_GREEN); - resource_rgb_gpio_set(0, 1, 0); - resource_rgb_l2c_set(0, 255, 0); + resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_READY, + CONFIG_DEFAULT_LED_3BIT_READY, + CONFIG_DEFAULT_LED_24BIT_READY, + LED_COLOR_GREEN); } else { _I("POST FAILURE"); - resource_bi_led_set(LED_COLOR_RED); - resource_rgb_gpio_set(1, 0, 0); - resource_rgb_l2c_set(255, 0, 0); + resource_led_set_rgb_colors(CONFIG_LED_STATE_KEY_FAIL, + CONFIG_DEFAULT_LED_3BIT_FAIL, + CONFIG_DEFAULT_LED_24BIT_FAIL, + LED_COLOR_RED); } } diff --git a/src/config.c b/src/config.c index ecf730b..cb6ecaf 100644 --- a/src/config.c +++ b/src/config.c @@ -260,3 +260,28 @@ bool config_get_int_with_default(char *group, char *key, int default_value, int return modified; } + +bool config_get_rgb_with_default(char *group, char *key, + int default_r, int default_g, int default_b, + int *red, int *green, int *blue) +{ + bool modified = false; + int color; + + if (config_get_int(group, key, &color) != 0) { + + color = (default_r & 0xFF) << 16; + color += (default_g & 0xFF) << 8; + color += (default_b & 0xFF) << 0; + + config_set_int(group, key, color); + + modified = true; + } + + *red = (color >> 16) & 0xFF; + *green = (color >> 8) & 0xFF; + *blue = (color >> 0) & 0xFF; + + return modified; +} diff --git a/src/lap_counter/lap_counter.c b/src/lap_counter/lap_counter.c index 347c72f..0c67fd2 100644 --- a/src/lap_counter/lap_counter.c +++ b/src/lap_counter/lap_counter.c @@ -27,6 +27,14 @@ #define MIN_LAP_TIME 5 #define MAX_NAME_LENGTH 256 +#define CONFIG_LED_STATE_KEY_LAP "lap" +#define CONFIG_DEFAULT_LED_3BIT_LAP 0, 1, 0 +#define CONFIG_DEFAULT_LED_24BIT_LAP 0, 255, 0 + +#define CONFIG_LED_STATE_KEY_FIRST_LAP "first.lap" +#define CONFIG_DEFAULT_LED_3BIT_FIRST_LAP 0, 0, 1 +#define CONFIG_DEFAULT_LED_24BIT_FIRST_LAP 0, 0, 255 + typedef struct lap_counter_data { char *user_name; struct timespec last_timestamp; @@ -99,6 +107,11 @@ static inline struct timespec _calculate_lap_time(struct timespec *prev, struct _D("----------------------------------------------"); cloud_communication_post_lap(lap.tv_sec * 1e3 + lap.tv_nsec / 1e6, s_info.user_name); + resource_led_blink_rgb_colors(CONFIG_LED_STATE_KEY_LAP, + CONFIG_DEFAULT_LED_3BIT_LAP, + CONFIG_DEFAULT_LED_24BIT_LAP, + LED_COLOR_NONE, + 1000); return lap; } @@ -113,9 +126,12 @@ void lap_counter_get_lap_time() _calculate_lap_time(&s_info.last_timestamp, ×tamp); } else { _D("Initial lap"); - resource_bi_led_blink(LED_COLOR_RED, 1000); - resource_gpio_rgb_blink(1, 0, 1, 1000); - resource_rgb_l2c_blink(255, 0, 255, 1000); + + resource_led_blink_rgb_colors(CONFIG_LED_STATE_KEY_FIRST_LAP, + CONFIG_DEFAULT_LED_3BIT_FIRST_LAP, + CONFIG_DEFAULT_LED_24BIT_FIRST_LAP, + LED_COLOR_NONE, + 1000); } s_info.last_timestamp.tv_sec = timestamp.tv_sec; diff --git a/src/resource/resource_led.c b/src/resource/resource_led.c index 9c40a5d..71bd003 100644 --- a/src/resource/resource_led.c +++ b/src/resource/resource_led.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -173,6 +174,15 @@ static gboolean _restore_rgb_l2c_color_cb(gpointer data) return false; } +static gboolean _restore_rgb_color_cb(gpointer data) +{ + _restore_bi_color_cb(data); + _restore_rgb_gpio_color_cb(data); + _restore_rgb_l2c_color_cb(data); + + return false; +} + static peripheral_gpio_h _init_gpio(int default_gpio, char *key) { peripheral_gpio_h gpio; @@ -274,14 +284,14 @@ void resource_led_destroy(void) peripheral_gpio_close(s_info.rgb_gpio[1]); peripheral_gpio_close(s_info.rgb_gpio[2]); - resource_rgb_l2c_set(0, 0, 0); + resource_led_rgb_l2c_set(0, 0, 0); resource_pca9685_fini(DEFAULT_RGB_L2C_R); resource_pca9685_fini(DEFAULT_RGB_L2C_G); resource_pca9685_fini(DEFAULT_RGB_L2C_B); } -void resource_bi_led_set(bi_led_color_e color) +void resource_led_bi_set(bi_led_color_e color) { _D("Set led to: %d", color); if (color == s_info.current_color) { @@ -292,7 +302,7 @@ void resource_bi_led_set(bi_led_color_e color) _led_bi_set(color); } -void resource_bi_led_blink(bi_led_color_e color, unsigned timeout) +void resource_led_bi_blink(bi_led_color_e color, unsigned timeout) { if (color == s_info.current_color) { return; @@ -302,7 +312,7 @@ void resource_bi_led_blink(bi_led_color_e color, unsigned timeout) g_timeout_add(timeout, _restore_bi_color_cb, NULL); } -void resource_rgb_gpio_set(bool red, bool green, bool blue) +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] && @@ -318,7 +328,7 @@ void resource_rgb_gpio_set(bool red, bool green, bool blue) _rgb_gpio_set(red, green, blue); } -void resource_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout) +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] && @@ -330,7 +340,7 @@ void resource_gpio_rgb_blink(bool red, bool green, bool blue, unsigned timeout) g_timeout_add(timeout, _restore_rgb_gpio_color_cb, NULL); } -void resource_rgb_l2c_set(int red, int green, int blue) +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] && @@ -345,7 +355,7 @@ void resource_rgb_l2c_set(int red, int green, int blue) _rgb_l2c_set(red, green, blue); } -void resource_rgb_l2c_blink(int red, int green, int blue, unsigned timeout) +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] && @@ -357,3 +367,43 @@ void resource_rgb_l2c_blink(int red, int green, int blue, unsigned timeout) g_timeout_add(timeout, _restore_rgb_l2c_color_cb, NULL); } +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) +{ + 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); + + 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); + + 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); + + if (modified) { + config_save(); + } + _D("%s: bicolor: [%d]", final_key, value); +} + +void resource_led_blink_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, int timeout) +{ + 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); + + g_timeout_add(timeout, _restore_rgb_color_cb, NULL); +}