823ca4df776cfecdd176c0ec24d48b9d20f3e9c2
[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 "st_things.h"
18 #include "log.h"
19 #include "resource/resource_servo_motor.h"
20
21 #define ARTIK_PWM_CHIPID        0
22 #define ARTIK_PWM_PIN           2
23
24 #define MILLI_SECOND    (1000000)
25
26 peripheral_pwm_h g_pwm_h = NULL;
27
28 static int _get_duty_cycle(door_state_e mode, int *duty_cycle)
29 {
30         if ((mode == DOOR_STATE_OPENED) || (mode == DOOR_STATE_OPENING))
31                 *duty_cycle = 2 * MILLI_SECOND;         // CLOCKWISE
32         else if ((mode == DOOR_STATE_CLOSE) || (mode == DOOR_STATE_CLOSING))
33                 *duty_cycle = 1 * MILLI_SECOND;         // COUNTER-CLOCKWISE
34         else {
35                 ERR("get_duty_cycle unknown mode=[%d]", mode);
36                 return -1;
37         }
38
39         INFO("get_duty_cycle mode=[%d:%s] : duty_cycle [%d] ms",
40                                                         mode,
41                                                         mode == DOOR_STATE_OPENED ? "OPENED " :
42                                                         (mode == DOOR_STATE_CLOSE ? "CLOSE  " :
43                                                         (mode == DOOR_STATE_CLOSING ? "CLOSING" : "OPENING")),
44                                                         *duty_cycle/(1000));
45
46         return 0;
47 }
48 peripheral_error_e resource_motor_driving(door_state_e mode)
49 {
50         peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
51
52         int chip = ARTIK_PWM_CHIPID;    // Chip 0
53         int pin  = ARTIK_PWM_PIN;               // Pin 2
54
55         int period = 20 * MILLI_SECOND;
56         int duty_cycle = 0;
57         bool enable = true;
58
59         ret = _get_duty_cycle(mode, &duty_cycle);
60         if (ret != 0) {
61                 ERR("_get_duty_cycle unknown mode=[%d]", mode);
62                 return PERIPHERAL_ERROR_INVALID_PARAMETER;
63         }
64
65         if (g_pwm_h == NULL){
66                 // Opening a PWM Handle : The chip and pin parameters required for this function must be set
67                 if ((ret = peripheral_pwm_open(chip, pin, &g_pwm_h)) != PERIPHERAL_ERROR_NONE ) {
68                         ERR("peripheral_pwm_open() failed!![%d]", ret);
69                         return ret;
70                 }
71         }
72
73         // Setting the Period : sets the period to 20 milliseconds. The unit is nanoseconds
74         if ((ret = peripheral_pwm_set_period(g_pwm_h, period)) != PERIPHERAL_ERROR_NONE) {
75                 ERR("peripheral_pwm_set_period() failed!![%d]", ret);
76                 return ret;
77         }
78
79         // Setting the Duty Cycle : sets the duty cycle to 1~2 milliseconds. The unit is nanoseconds
80         if ((ret = peripheral_pwm_set_duty_cycle(g_pwm_h, duty_cycle)) != PERIPHERAL_ERROR_NONE) {
81                 ERR("peripheral_pwm_set_duty_cycle() failed!![%d]", ret);
82                 return ret;
83         }
84
85         // Enabling Repetition
86         if ((ret = peripheral_pwm_set_enabled(g_pwm_h, enable)) != PERIPHERAL_ERROR_NONE) {
87                 ERR("peripheral_pwm_set_enabled() failed!![%d]", ret);
88                 return ret;
89         }
90
91         if (g_pwm_h != NULL) {
92                 // Closing a PWM Handle : close a PWM handle that is no longer used,
93                 if ((ret = peripheral_pwm_close(g_pwm_h)) != PERIPHERAL_ERROR_NONE ) {
94                         ERR("peripheral_pwm_close() failed!![%d]", ret);
95                         return ret;
96                 }
97                 g_pwm_h = NULL;
98         }
99
100         return ret;
101 }