0f9a9bf238f8c1a116d7a716c30df1586414bdd0
[platform/upstream/iotjs.git] / tools / src / module / iotjs_module_pwm.h
1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16
17 #ifndef IOTJS_MODULE_PWM_H
18 #define IOTJS_MODULE_PWM_H
19
20 #include "iotjs_def.h"
21 #include "iotjs_objectwrap.h"
22 #include "iotjs_reqwrap.h"
23
24
25 typedef enum {
26   kPwmOpOpen,
27   kPwmOpSetDutyCycle,
28   kPwmOpSetPeriod,
29   kPwmOpSetFrequency,
30   kPwmOpSetEnable,
31   kPwmOpClose,
32 } PwmOp;
33
34
35 typedef struct {
36   iotjs_jobjectwrap_t jobjectwrap;
37
38 #if defined(__linux__)
39   int chip;
40   iotjs_string_t device;
41 #elif defined(__NUTTX__)
42   int device_fd;
43 #endif
44   int pin;
45   double duty_cycle;
46   double period;
47   bool enable;
48 } IOTJS_VALIDATED_STRUCT(iotjs_pwm_t);
49
50
51 typedef bool (*pwm_func_ptr)(iotjs_pwm_t*);
52
53
54 typedef struct {
55   pwm_func_ptr caller;
56   bool result;
57   PwmOp op;
58 } iotjs_pwm_reqdata_t;
59
60
61 typedef struct {
62   iotjs_reqwrap_t reqwrap;
63   uv_work_t req;
64   iotjs_pwm_reqdata_t req_data;
65   iotjs_pwm_t* pwm_instance;
66 } IOTJS_VALIDATED_STRUCT(iotjs_pwm_reqwrap_t);
67
68
69 #define THIS iotjs_pwm_reqwrap_t* pwm_reqwrap
70
71 iotjs_pwm_reqwrap_t* iotjs_pwm_reqwrap_from_request(uv_work_t* req);
72 iotjs_pwm_reqdata_t* iotjs_pwm_reqwrap_data(THIS);
73
74 iotjs_pwm_t* iotjs_pwm_instance_from_reqwrap(THIS);
75
76 #undef THIS
77
78
79 #define PWM_WORKER_INIT                                                     \
80   iotjs_pwm_reqwrap_t* req_wrap = iotjs_pwm_reqwrap_from_request(work_req); \
81   iotjs_pwm_reqdata_t* req_data = iotjs_pwm_reqwrap_data(req_wrap);         \
82   iotjs_pwm_t* pwm = iotjs_pwm_instance_from_reqwrap(req_wrap);
83
84
85 void iotjs_pwm_open_worker(uv_work_t* work_req);
86
87 bool iotjs_pwm_set_period(iotjs_pwm_t* pwm);
88 bool iotjs_pwm_set_dutycycle(iotjs_pwm_t* pwm);
89 bool iotjs_pwm_set_enable(iotjs_pwm_t* pwm);
90 bool iotjs_pwm_close(iotjs_pwm_t* pwm);
91
92
93 #endif /* IOTJS_MODULE_PWM_H */