diff options
author | Boyeon <boyeon.son@samsung.com> | 2019-08-27 14:07:42 +0900 |
---|---|---|
committer | Boyeon <boyeon.son@samsung.com> | 2019-08-27 14:08:03 +0900 |
commit | 942978cbc1511935f12567efa3d74ae743cfa00e (patch) | |
tree | 734e63b9041aa031cf66526817b196e6a382fa0f | |
parent | 60da2c184339f24a368d6e7753776611276c95b9 (diff) | |
download | st-things-light-942978cbc1511935f12567efa3d74ae743cfa00e.tar.gz st-things-light-942978cbc1511935f12567efa3d74ae743cfa00e.tar.bz2 st-things-light-942978cbc1511935f12567efa3d74ae743cfa00e.zip |
Sync resouce codes
Change-Id: I2a3ea9563904ee146737d49c0dcd84d56863289e
-rwxr-xr-x | inc/resource/resource_infrared_motion.h | 13 | ||||
-rwxr-xr-x | inc/resource/resource_led.h | 6 | ||||
-rwxr-xr-x | src/resource/resource_infrared_motion.c | 122 | ||||
-rwxr-xr-x | src/resource/resource_led.c | 26 |
4 files changed, 85 insertions, 82 deletions
diff --git a/inc/resource/resource_infrared_motion.h b/inc/resource/resource_infrared_motion.h index 60565a7..115c634 100755 --- a/inc/resource/resource_infrared_motion.h +++ b/inc/resource/resource_infrared_motion.h @@ -17,21 +17,8 @@ #ifndef __RESOURCE_INFRARED_MOTION_H__ #define __RESOURCE_INFRARED_MOTION_H__ -typedef enum { - MOTION_HANDLE_ERROR_NONE = 0, /**< Successful */ - MOTION_HANDLE_ERROR_NOT_OPEN, - MOTION_HANDLE_ERROR_INVALID_PIN -} resource_infrared_motion_handle_error_e; - typedef void (*resource_infrared_motion_interrupted_cb) (uint32_t motion_value, void *user_data); -typedef struct resource_infrared_motion_interrupted_data_s { - resource_infrared_motion_interrupted_cb interrupted_cb; - void *interrupted_cb_user_data; - uint32_t motion_value; - int is_called_first_time; -} resource_infrared_motion_interrupted_data; - /** * @brief Reads value of gpio connected infrared motion sensor (HC-SR501) * @param[in] pin_num The gpio pin number for the infrared motion sensor diff --git a/inc/resource/resource_led.h b/inc/resource/resource_led.h index 2a49a34..4adf22f 100755 --- a/inc/resource/resource_led.h +++ b/inc/resource/resource_led.h @@ -17,12 +17,6 @@ #ifndef __RESOURCE_LED_H__ #define __RESOURCE_LED_H__ -typedef enum { - LED_HANDLE_ERROR_NONE = 0, /**< Successful */ - LED_HANDLE_ERROR_NOT_OPEN, - LED_HANDLE_ERROR_INVALID_PIN -} resource_led_handle_error_e; - /** * @brief Writes value of gpio connected led light * @param[in] pin_num The gpio pin number for the led light diff --git a/src/resource/resource_infrared_motion.c b/src/resource/resource_infrared_motion.c index 650494a..574c58f 100755 --- a/src/resource/resource_infrared_motion.c +++ b/src/resource/resource_infrared_motion.c @@ -15,16 +15,26 @@ */ #include <peripheral_io.h> -#include <stdlib.h> - #include "resource/resource_infrared_motion.h" #include "log.h" +enum { + MOTION_HANDLE_ERROR_NONE = 0, /**< Successful */ + MOTION_HANDLE_ERROR_NOT_OPEN, + MOTION_HANDLE_ERROR_INVALID_PIN +} motion_handle_error_e; + +struct resource_im_interrupted_data { + resource_infrared_motion_interrupted_cb interrupted_cb; + void *interrupted_cb_user_data; + uint32_t motion_value; +}; + static peripheral_gpio_h g_sensor_h = NULL; static int g_pin_num = -1; -static resource_infrared_motion_interrupted_data *g_interrupted_data = NULL; +static struct resource_im_interrupted_data g_interrupted_data; -int _resource_validate_infrared_motion(int pin_num) +static int _resource_validate_infrared_motion(int pin_num) { int ret = MOTION_HANDLE_ERROR_NONE; @@ -38,7 +48,7 @@ int _resource_validate_infrared_motion(int pin_num) return ret; } -int resource_open_infrared_motion(int pin_num) +static int resource_open_infrared_motion(int pin_num) { peripheral_gpio_h temp = NULL; @@ -65,18 +75,21 @@ int resource_open_infrared_motion(int pin_num) int resource_read_infrared_motion(int pin_num, uint32_t *out_value) { int ret = PERIPHERAL_ERROR_NONE; - - if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_NOT_OPEN) { - ret = resource_open_infrared_motion(pin_num); - retv_if(ret < 0, -1); - } - if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_INVALID_PIN) { - _E("Invalid pin number."); - return -1; + int ret_valid = MOTION_HANDLE_ERROR_NONE; + + ret_valid = _resource_validate_infrared_motion(pin_num); + if (ret_valid != MOTION_HANDLE_ERROR_NONE) { + if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) { + ret = resource_open_infrared_motion(pin_num); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) { + _E("Invalid pin number."); + return -1; + } } ret = peripheral_gpio_read(g_sensor_h, out_value); - retv_if(ret < 0, -1); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); return 0; } @@ -87,85 +100,86 @@ void resource_close_infrared_motion(void) _I("Infrared Motion Sensor is finishing..."); + if (g_interrupted_data.interrupted_cb) { + peripheral_gpio_unset_interrupted_cb(g_sensor_h); + g_interrupted_data.interrupted_cb = NULL; + g_interrupted_data.interrupted_cb_user_data = NULL; + g_interrupted_data.motion_value = 0; + } + peripheral_gpio_close(g_sensor_h); - if (g_interrupted_data != NULL) { - free(g_interrupted_data); - g_interrupted_data = NULL; - } g_sensor_h = NULL; g_pin_num = -1; } void _resoucre_motion_interrupted_cb (peripheral_gpio_h gpio, peripheral_error_e error, void *user_data) { - int ret = PERIPHERAL_ERROR_NONE; - if (!g_sensor_h) return; - if (g_interrupted_data->is_called_first_time) { - ret = peripheral_gpio_read(g_sensor_h, &g_interrupted_data->motion_value); - if (ret) return; + g_interrupted_data.motion_value = !g_interrupted_data.motion_value; - g_interrupted_data->is_called_first_time = 0; - } else { - // toggle g_interrupted_data->motion_value - g_interrupted_data->motion_value = !g_interrupted_data->motion_value; - } - - g_interrupted_data->interrupted_cb(g_interrupted_data->motion_value, g_interrupted_data->interrupted_cb_user_data); + g_interrupted_data.interrupted_cb(g_interrupted_data.motion_value, g_interrupted_data.interrupted_cb_user_data); } int resource_set_interrupted_cb_infrared_motion(int pin_num, resource_infrared_motion_interrupted_cb interrupted_cb, void *user_data) { int ret = PERIPHERAL_ERROR_NONE; - if (g_interrupted_data == NULL) { - g_interrupted_data = calloc(1, sizeof(resource_infrared_motion_interrupted_data)); - } + int ret_valid = MOTION_HANDLE_ERROR_NONE; - if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_NOT_OPEN) { + ret_valid = _resource_validate_infrared_motion(pin_num); + if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) { ret = resource_open_infrared_motion(pin_num); - retv_if(ret < 0, -1); - } - if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_INVALID_PIN) { + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) { _E("Invalid pin number."); return -1; } - g_interrupted_data->interrupted_cb = interrupted_cb; - g_interrupted_data->interrupted_cb_user_data = user_data; - g_interrupted_data->motion_value = 0; - g_interrupted_data->is_called_first_time = 1; - ret = peripheral_gpio_set_edge_mode(g_sensor_h, PERIPHERAL_GPIO_EDGE_BOTH); - retv_if(ret < 0, -1); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + + g_interrupted_data.motion_value = 0; + ret = peripheral_gpio_read(g_sensor_h, &g_interrupted_data.motion_value); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); - ret = peripheral_gpio_set_interrupted_cb(g_sensor_h, _resoucre_motion_interrupted_cb, g_interrupted_data); - retv_if(ret < 0, -1); + ret = peripheral_gpio_set_interrupted_cb(g_sensor_h, _resoucre_motion_interrupted_cb, &g_interrupted_data); + goto_if(ret != PERIPHERAL_ERROR_NONE, ERROR); + + g_interrupted_data.interrupted_cb = interrupted_cb; + g_interrupted_data.interrupted_cb_user_data = user_data; return 0; + +ERROR: + _E("failed to read gpio"); + peripheral_gpio_unset_interrupted_cb(g_sensor_h); + g_interrupted_data.interrupted_cb = NULL; + g_interrupted_data.interrupted_cb_user_data = NULL; + g_interrupted_data.motion_value = 0; + return -1; } int resource_unset_interrupted_cb_infrared_motion(int pin_num) { int ret = PERIPHERAL_ERROR_NONE; + int ret_valid = MOTION_HANDLE_ERROR_NONE; - if (g_interrupted_data != NULL) { - free(g_interrupted_data); - g_interrupted_data = NULL; - } - - if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_NOT_OPEN) { + ret_valid = _resource_validate_infrared_motion(pin_num); + if (ret_valid == MOTION_HANDLE_ERROR_NOT_OPEN) { _E("No open handle."); return -1; - } - else if (_resource_validate_infrared_motion(pin_num) == MOTION_HANDLE_ERROR_INVALID_PIN) { + } else if (ret_valid == MOTION_HANDLE_ERROR_INVALID_PIN) { _E("Invalid pin number."); return -1; } ret = peripheral_gpio_unset_interrupted_cb(g_sensor_h); - retv_if(ret < 0, -1); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + + g_interrupted_data.interrupted_cb = NULL; + g_interrupted_data.interrupted_cb_user_data = NULL; + g_interrupted_data.motion_value = 0; return 0; } diff --git a/src/resource/resource_led.c b/src/resource/resource_led.c index cc4dca6..4d5d15d 100755 --- a/src/resource/resource_led.c +++ b/src/resource/resource_led.c @@ -15,10 +15,15 @@ */ #include <peripheral_io.h> - #include "resource/resource_led.h" #include "log.h" +typedef enum { + LED_HANDLE_ERROR_NONE = 0, /**< Successful */ + LED_HANDLE_ERROR_NOT_OPEN, + LED_HANDLE_ERROR_INVALID_PIN +} led_handle_error_e; + static peripheral_gpio_h g_sensor_h = NULL; static int g_pin_num = -1; @@ -75,14 +80,17 @@ void resource_close_led(void) int resource_write_led(int pin_num, int write_value) { int ret = PERIPHERAL_ERROR_NONE; - - if (_resource_validate_led(pin_num) == LED_HANDLE_ERROR_NOT_OPEN) { - ret = resource_open_led(pin_num); - retv_if(ret < 0, -1); - } - if (_resource_validate_led(pin_num) == LED_HANDLE_ERROR_INVALID_PIN) { - _E("Invalid pin number."); - return -1; + int ret_valid = LED_HANDLE_ERROR_NONE; + + ret_valid = _resource_validate_led(pin_num); + if (ret_valid != LED_HANDLE_ERROR_NONE) { + if (ret_valid == LED_HANDLE_ERROR_NOT_OPEN) { + ret = resource_open_led(pin_num); + retv_if(ret != PERIPHERAL_ERROR_NONE, -1); + } else if (ret_valid == LED_HANDLE_ERROR_INVALID_PIN) { + _E("Invalid pin number."); + return -1; + } } ret = peripheral_gpio_write(g_sensor_h, write_value); |