From 2a8e514c48ee8365fd3d94ebcf7d8d6ff004ccba Mon Sep 17 00:00:00 2001 From: Michal Skorupinski Date: Wed, 31 Oct 2018 13:45:30 +0100 Subject: Added two color led to indicate the app's status Change-Id: I8a74f7ea0cae9aaaeb15dc1d185cca2ed7198151 Signed-off-by: Michal Skorupinski --- src/app.c | 6 ++ src/cloud/cloud_communication.c | 3 + src/lap_counter/lap_counter.c | 5 +- src/resource/resource_lap_counter.c | 4 +- src/resource/resource_led.c | 144 ++++++++++++++++++++++++++++++++++++ 5 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/resource/resource_led.c (limited to 'src') diff --git a/src/app.c b/src/app.c index 30d36c4..9a693f6 100644 --- a/src/app.c +++ b/src/app.c @@ -317,6 +317,8 @@ static void _initialize_components(app_data *ad) message_manager_init(); controller_connection_manager_listen(); lap_counter_init(); + resource_led_init(); + _D("Car settings: Stering[%d +/- %d] Engine[%d - %d], CamAzimuth[%d +/- %d], CamElev[%d - %d]", s_info.stering_center, s_info.stering_range, @@ -366,6 +368,8 @@ 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_led_set(LED_COLOR_RED); + return true; } @@ -387,12 +391,14 @@ static void service_app_control(app_control_h app_control, void *data) static void service_app_terminate(void *data) { app_data *ad = data; + resource_led_set(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); resource_set_servo_motor_value(s_info.azimuth_pin, (AZIMUTH_MIN + AZIMUTH_MAX) / 2); resource_lap_counter_destroy(); + resource_led_destroy(); if (ad->idle_h) g_source_remove(ad->idle_h); diff --git a/src/cloud/cloud_communication.c b/src/cloud/cloud_communication.c index 634773e..effc0b0 100644 --- a/src/cloud/cloud_communication.c +++ b/src/cloud/cloud_communication.c @@ -25,6 +25,7 @@ #include "log.h" #include "config.h" #include "net-util.h" +#include "resource/resource_led.h" typedef struct communication_data_ { gboolean is_initialized; @@ -117,9 +118,11 @@ static void post_response_cb(request_result_e result, void *user_data) { if (result == SUCCESS) { _I("POST SUCCESS"); + resource_led_set(LED_COLOR_GREEN); } else { _I("POST FAILURE"); + resource_led_set(LED_COLOR_RED); } } diff --git a/src/lap_counter/lap_counter.c b/src/lap_counter/lap_counter.c index aa9746d..b52bd8a 100644 --- a/src/lap_counter/lap_counter.c +++ b/src/lap_counter/lap_counter.c @@ -22,6 +22,7 @@ #include #include #include "cloud/cloud_communication.h" +#include "resource/resource_led.h" #define MIN_LAP_TIME 5 #define MAX_NAME_LENGTH 256 @@ -98,6 +99,7 @@ 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); + return lap; } @@ -105,12 +107,13 @@ void lap_counter_get_lap_time() { struct timespec timestamp; int ret = clock_gettime(CLOCK_MONOTONIC, ×tamp); - retv_error_message(ret != 0, ret); + ret_error_message(ret != 0, ret); if (s_info.last_timestamp.tv_nsec != 0 || s_info.last_timestamp.tv_sec != 0) { _calculate_lap_time(&s_info.last_timestamp, ×tamp); } else { _D("Initial lap"); + resource_led_blink(LED_COLOR_NONE, 1000); } s_info.last_timestamp.tv_sec = timestamp.tv_sec; diff --git a/src/resource/resource_lap_counter.c b/src/resource/resource_lap_counter.c index e6d5029..72aac8a 100644 --- a/src/resource/resource_lap_counter.c +++ b/src/resource/resource_lap_counter.c @@ -21,6 +21,7 @@ #include #include "log.h" #include "lap_counter/lap_counter.h" +#include "resource/resource_led.h" #include "config.h" #define DEFAULT_LAP_GPIO 26 @@ -54,7 +55,6 @@ static void _gpio_interrupted_cb(peripheral_gpio_h gpio, peripheral_error_e erro lap_counter_get_lap_time(); } - s_info.gpio_prev_value = gpio_value; } @@ -72,7 +72,7 @@ void resource_lap_counter_init(void) } ret = peripheral_gpio_open(pin, &s_info.gpio); - retv_error_message(ret != PERIPHERAL_ERROR_NONE, ret); + ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret); ret = peripheral_gpio_set_direction(s_info.gpio, PERIPHERAL_GPIO_DIRECTION_IN); CHECK_GPIO_ERROR(s_info.gpio, ret); diff --git a/src/resource/resource_led.c b/src/resource/resource_led.c new file mode 100644 index 0000000..e112615 --- /dev/null +++ b/src/resource/resource_led.c @@ -0,0 +1,144 @@ +/* +* Copyright (c) 2018 Samsung Electronics Co., Ltd. +* +* Licensed under the Flora License, Version 1.1 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://floralicense.org/license/ +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include +#include +#include +#include +#include +#include "log.h" +#include "resource/resource_led.h" +#include "config.h" + +#define DEFAULT_LED_RED 17 +#define DEFAULT_LED_GREEN 27 +#define CONFIG_KEY_RPI_PIN_LED_RED "led.red" +#define CONFIG_KEY_RPI_PIN_LED_GREEN "led.green" +#define CONFIG_GRP_RPI "Rpi" + +typedef struct _led_s { + peripheral_gpio_h gpio_red; + peripheral_gpio_h gpio_green; + led_color_e current_color; +} led_s; + +static led_s s_info = { 0, }; + +#define CHECK_GPIO_ERROR(pin, ret) \ + if (ret != PERIPHERAL_ERROR_NONE) { \ + peripheral_gpio_close(pin); \ + _E("GPIO ERROR: %s, %s[%d]", get_error_message(ret), __FILE__, __LINE__); \ + return ; \ + } + + +static inline void _led_set(led_color_e color) +{ + int red; + int green; + + + switch (color) { + case LED_COLOR_RED: + red = 1; + green = 0; + break; + case LED_COLOR_GREEN: + red = 0; + green = 1; + break; + default: + red = 0; + green = 0; + break; + } + + int ret = peripheral_gpio_write(s_info.gpio_red, red); + CHECK_GPIO_ERROR(s_info.gpio_red, ret); + + peripheral_gpio_write(s_info.gpio_green, green); + CHECK_GPIO_ERROR(s_info.gpio_green, ret); +} + +static gboolean _restore_color_cb(gpointer data) +{ + _led_set(s_info.current_color); + return false; +} + +static peripheral_gpio_h _init_gpio(int default_gpio, char *key) +{ + peripheral_gpio_h gpio; +// int ret = PERIPHERAL_ERROR_NONE; + int pin = 0; + bool modified = config_get_int_with_default(CONFIG_GRP_RPI, key, default_gpio, &pin); + + _D("gpio: %d", pin); + + if (modified) { + config_save(); + } + + peripheral_gpio_open(pin, &gpio); +// ret_error_message(ret != PERIPHERAL_ERROR_NONE, ret); + + peripheral_gpio_set_direction(gpio, PERIPHERAL_GPIO_DIRECTION_OUT_INITIALLY_LOW); +// CHECK_GPIO_ERROR(gpio, ret); + + return gpio; + +} + +void resource_led_init(void) +{ + _D("Initialize Led"); + + s_info.gpio_red = _init_gpio(DEFAULT_LED_RED, CONFIG_KEY_RPI_PIN_LED_RED); + s_info.gpio_green = _init_gpio(DEFAULT_LED_GREEN, CONFIG_KEY_RPI_PIN_LED_GREEN); + + + int ret = peripheral_gpio_write(s_info.gpio_green, 1); + CHECK_GPIO_ERROR(s_info.gpio_green, ret); +} + +void resource_led_destroy(void) +{ + peripheral_gpio_close(s_info.gpio_red); + peripheral_gpio_close(s_info.gpio_green); +} + +void resource_led_set(led_color_e color) +{ + _D("Set led to: %d", color); + if (color == s_info.current_color) { + return; + } + + s_info.current_color = color; + _led_set(color); +} + +void resource_led_blink(led_color_e color, unsigned timeout) +{ + if (color == s_info.current_color) { + return; + } + + _led_set(color); + g_timeout_add(timeout, _restore_color_cb, NULL); +} + -- cgit v1.2.3