tizen 5.0 migration
[apps/native/blind-motor.git] / src / resource / resource_servo_motor.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "log.h"
18 #include "resource/resource_servo_motor.h"
19
20 #define ARTIK_PWM_CHIPID        0
21 #define ARTIK_PWM_PIN           2
22
23 #define MILLI_SECOND    (1000000)
24
25 peripheral_pwm_h g_pwm_h = NULL;
26
27 static int _get_duty_cycle(door_state_e mode, int *duty_cycle)
28 {
29         if ((mode == DOOR_STATE_OPENED) || (mode == DOOR_STATE_OPENING))
30                 *duty_cycle = 2 * MILLI_SECOND;         // CLOCKWISE
31         else if ((mode == DOOR_STATE_CLOSE) || (mode == DOOR_STATE_CLOSING))
32                 *duty_cycle = 1 * MILLI_SECOND;         // COUNTER-CLOCKWISE
33         else {
34                 _E("get_duty_cycle unknown mode=[%d]", mode);
35                 return -1;
36         }
37
38         _I("get_duty_cycle mode=[%d:%s] : duty_cycle [%d] ms",
39                                                         mode,
40                                                         mode == DOOR_STATE_OPENED ? "OPENED " :
41                                                         (mode == DOOR_STATE_CLOSE ? "CLOSE  " :
42                                                         (mode == DOOR_STATE_CLOSING ? "CLOSING" : "OPENING")),
43                                                         *duty_cycle/(1000));
44
45         return 0;
46 }
47 peripheral_error_e resource_motor_driving(door_state_e mode)
48 {
49         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
50
51         int chip = ARTIK_PWM_CHIPID;    // Chip 0
52         int pin  = ARTIK_PWM_PIN;               // Pin 2
53
54         int period = 20 * MILLI_SECOND;
55         int duty_cycle = 0;
56         bool enable = true;
57
58         ret = _get_duty_cycle(mode, &duty_cycle);
59         if (ret != 0) {
60                 _E("_get_duty_cycle unknown mode=[%d]", mode);
61                 return PERIPHERAL_ERROR_INVALID_PARAMETER;
62         }
63
64         if (g_pwm_h == NULL){
65                 // Opening a PWM Handle : The chip and pin parameters required for this function must be set
66                 if ((ret = peripheral_pwm_open(chip, pin, &g_pwm_h)) != PERIPHERAL_ERROR_NONE ) {
67                         _E("peripheral_pwm_open() failed!![%d]", ret);
68                         return ret;
69                 }
70         }
71
72         // Setting the Period : sets the period to 20 milliseconds. The unit is nanoseconds
73         if ((ret = peripheral_pwm_set_period(g_pwm_h, period)) != PERIPHERAL_ERROR_NONE) {
74                 _E("peripheral_pwm_set_period() failed!![%d]", ret);
75                 return ret;
76         }
77
78         // Setting the Duty Cycle : sets the duty cycle to 1~2 milliseconds. The unit is nanoseconds
79         if ((ret = peripheral_pwm_set_duty_cycle(g_pwm_h, duty_cycle)) != PERIPHERAL_ERROR_NONE) {
80                 _E("peripheral_pwm_set_duty_cycle() failed!![%d]", ret);
81                 return ret;
82         }
83
84         // Enabling Repetition
85         if ((ret = peripheral_pwm_set_enabled(g_pwm_h, enable)) != PERIPHERAL_ERROR_NONE) {
86                 _E("peripheral_pwm_set_enabled() failed!![%d]", ret);
87                 return ret;
88         }
89
90         if (g_pwm_h != NULL) {
91                 // Closing a PWM Handle : close a PWM handle that is no longer used,
92                 if ((ret = peripheral_pwm_close(g_pwm_h)) != PERIPHERAL_ERROR_NONE ) {
93                         _E("peripheral_pwm_close() failed!![%d]", ret);
94                         return ret;
95                 }
96                 g_pwm_h = NULL;
97         }
98
99         return ret;
100 }