2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "resource/resource_servo_motor.h"
20 #define ARTIK_PWM_CHIPID 0
21 #define ARTIK_PWM_PIN 2
23 #define MILLI_SECOND (1000000)
25 peripheral_pwm_h g_pwm_h = NULL;
27 static int _get_duty_cycle(door_state_e mode, int *duty_cycle)
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
34 _E("get_duty_cycle unknown mode=[%d]", mode);
38 _I("get_duty_cycle mode=[%d:%s] : duty_cycle [%d] ms",
40 mode == DOOR_STATE_OPENED ? "OPENED " :
41 (mode == DOOR_STATE_CLOSE ? "CLOSE " :
42 (mode == DOOR_STATE_CLOSING ? "CLOSING" : "OPENING")),
47 peripheral_error_e resource_motor_driving(door_state_e mode)
49 peripheral_error_e ret = PERIPHERAL_ERROR_NONE;
51 int chip = ARTIK_PWM_CHIPID; // Chip 0
52 int pin = ARTIK_PWM_PIN; // Pin 2
54 int period = 20 * MILLI_SECOND;
58 ret = _get_duty_cycle(mode, &duty_cycle);
60 _E("_get_duty_cycle unknown mode=[%d]", mode);
61 return PERIPHERAL_ERROR_INVALID_PARAMETER;
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);
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);
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);
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);
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);