pwm: Updated API
[contrib/mraa.git] / api / pwm.h
1 /*
2  * Originally from mbed Microcontroller Library
3  * Copyright (c) 2006-2013 ARM Limited
4  * Copyright (c) 2014 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #pragma once
20
21 /** @file
22  *
23  * This file defines the pwm interface for libmaa
24  *
25  */
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 #include <stdio.h>
32 #include <fcntl.h>
33
34 #include "maa.h"
35
36 /**
37  * A strucutre representing a PWM pin
38  */
39 typedef struct {
40     /*@{*/
41     int pin; /**< the pin number, as known to the os. */
42     int chipid; /**< the chip id, which the pwm resides */
43     FILE *duty_fp; /**< File pointer to duty file */
44     /*@}*/
45 } maa_pwm_context;
46
47 /** Initialise pwm_context, uses board mapping.
48  *
49  * @param pin The PWM PIN.
50  *
51  * @return maa_pwm_context The returned initialised pwm context
52  */
53 maa_pwm_context* maa_pwm_init(int pin);
54
55 /** Initialise pwm_context, raw mode.
56  *
57  * @param chipid The chip inwhich the PWM is under in SYSFS
58  * @param pin The PWM PIN.
59  *
60  * @return maa_pwm_context The returned initialised pwm context
61  */
62 maa_pwm_context* maa_pwm_init_raw(int chipid, int pin);
63
64 /** Set the ouput duty-cycle percentage, as a float
65  *
66  * @param pwm The PWM context to use.
67  * @param percentage A floating-point value representing percentage of output.
68  *    The value should lie between 0.0f (representing on 0%) and 1.0f
69  *    Values above or below this range will be set at either 0.0f or 1.0f.
70  *
71  * @return maa_result_t the maa result.
72  */
73 maa_result_t maa_pwm_write(maa_pwm_context* pwm, float percentage);
74
75 /** Read the ouput duty-cycle percentage, as a float
76  *
77  * @param pwm The PWM context to use.
78  * @return percentage A floating-point value representing percentage of output.
79  *    The value should lie between 0.0f (representing on 0%) and 1.0f
80  *    Values above or below this range will be set at either 0.0f or 1.0f.
81  */
82 float maa_pwm_read(maa_pwm_context* pwm);
83
84 /** Set the PWM period as seconds represented in a float
85  *
86  * @param pwm The PWM context to use.
87  * @param seconds Peroid represented as a float in seconds.
88  *
89  * @return maa_result_t the maa result.
90  */
91 maa_result_t maa_pwm_period(maa_pwm_context* pwm, float seconds);
92
93 /** Set period. milli-oseconds.
94  *
95  * @param pwm The PWM context to use.
96  * @param ms milli-seconds for period.
97  *
98  * @return maa_result_t the maa result.
99  */
100 maa_result_t maa_pwm_period_ms(maa_pwm_context* pwm, int ms);
101
102 /** Set period. microseconds
103  *
104  * @param pwm The PWM context to use.
105  * @param ns microseconds as period.
106  *
107  * @return maa_result_t the maa result.
108  */
109 maa_result_t maa_pwm_period_us(maa_pwm_context* pwm, int us);
110
111 /** Set pulsewidth, As represnted by seconds in a (float).
112  *
113  * @param pwm The PWM context to use.
114  * @param seconds The duration of a pulse
115  *
116  * @return maa_result_t the maa result.
117  */
118 maa_result_t maa_pwm_pulsewidth(maa_pwm_context* pwm, float seconds);
119
120 /** Set pulsewidth. Milliseconds
121  *
122  * @param pwm The PWM context to use.
123  * @param ms milliseconds for pulsewidth.
124  *
125  * @return maa_result_t the maa result.
126  */
127 maa_result_t maa_pwm_pulsewidth_ms(maa_pwm_context* pwm, int ms);
128
129 /** Set pulsewidth, microseconds.
130  *
131  * @param pwm The PWM context to use.
132  * @param us microseconds for pulsewidth.
133  *
134  * @return maa_result_t the maa result.
135  */
136 maa_result_t maa_pwm_pulsewidth_us(maa_pwm_context* pwm, int us);
137
138 /** Set the enable status of the PWM pin. None zero will assume on with output being driven.
139  *   and 0 will disable the output.
140  *
141  * @param pwm The PWM context to use.
142  * @param enable enable status of pin
143  *
144  * @return maa_result_t the maa result.
145  */
146 maa_result_t maa_pwm_enable(maa_pwm_context* pwm, int enable);
147
148 /** Close and unexport the PWM pin.
149  * 
150  * @param pwm The PWM context to use.
151  *
152  * @return maa_result_t the maa result.
153  */
154 maa_result_t maa_pwm_close(maa_pwm_context* pwm);
155
156 #ifdef __cplusplus
157 }
158 #endif