swig: add unexport() calls to be used by destructors in object api
[contrib/mraa.git] / src / pwm / pwm.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <stdlib.h>
26
27 #include "pwm.h"
28
29 static int
30 maa_pwm_setup_duty_fp(maa_pwm_context* dev)
31 {
32     char bu[64];
33     sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin);
34
35     if ((dev->duty_fp = fopen(bu, "r+b")) == NULL) {
36         return 1;
37     }
38     return 0;
39 }
40
41 static maa_result_t
42 maa_pwm_write_period(maa_pwm_context* dev, int period)
43 {
44     FILE *period_f;
45     char bu[64];
46     sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
47
48     if ((period_f = fopen(bu, "r+b")) == NULL) {
49         fprintf(stderr, "Failed to open period for writing!\n");
50         return MAA_ERROR_INVALID_RESOURCE;
51     }
52     fprintf(period_f, "%d", period);
53     fclose(period_f);
54     if (ferror(period_f) != 0)
55         return MAA_ERROR_INVALID_RESOURCE;
56     return MAA_SUCCESS;
57 }
58
59 static maa_result_t
60 maa_pwm_write_duty(maa_pwm_context* dev, int duty)
61 {
62     if (dev->duty_fp == NULL) {
63         maa_pwm_setup_duty_fp(dev);
64     }
65     fprintf(dev->duty_fp, "%d", duty);
66     rewind(dev->duty_fp);
67     fflush(dev->duty_fp);
68      if (ferror(dev->duty_fp) != 0)
69         return MAA_ERROR_INVALID_RESOURCE;
70     return MAA_SUCCESS;
71 }
72
73 static int
74 maa_pwm_get_period(maa_pwm_context* dev)
75 {
76     FILE *period_f;
77     char bu[64];
78     char output[16];
79
80     sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
81     if ((period_f = fopen(bu, "rb")) == NULL) {
82         fprintf(stderr, "Failed to open period for reading!\n");
83         return 0;
84     }
85     fgets(output, 16, period_f);
86     fclose(period_f);
87     return atoi(output);
88 }
89
90 static int
91 maa_pwm_get_duty(maa_pwm_context* dev)
92 {
93     if (dev->duty_fp == NULL) {
94         maa_pwm_setup_duty_fp(dev);
95     }
96     char output[16];
97     fgets(output, 16, dev->duty_fp);
98     fseek(dev->duty_fp, SEEK_SET, 0);
99     return atoi(output);
100 }
101
102 maa_pwm_context*
103 maa_pwm_init(int pin) {
104     //TODO
105     return maa_pwm_init_raw(0, pin);
106 }
107
108 maa_pwm_context*
109 maa_pwm_init_raw(int chipin, int pin)
110 {
111     maa_pwm_context* dev = (maa_pwm_context*) malloc(sizeof(maa_pwm_context));
112     if (dev == NULL)
113         return NULL;
114
115     dev->chipid = chipin;
116     dev->pin = pin;
117
118     FILE *export_f;
119     char buffer[64];
120     snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/export", dev->chipid);
121
122     if ((export_f = fopen(buffer, "w")) == NULL) {
123         fprintf(stderr, "Failed to open export for writing!\n");
124         free(dev);
125         return NULL;
126     } else {
127         fprintf(export_f, "%d", dev->pin);
128         fclose(export_f);
129         maa_pwm_setup_duty_fp(dev);
130     }
131     return dev;
132 }
133
134 maa_result_t
135 maa_pwm_write(maa_pwm_context* dev, float percentage)
136 {
137    return maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
138 }
139
140 float
141 maa_pwm_read(maa_pwm_context* dev)
142 {
143     float output = maa_pwm_get_duty(dev) / (float) maa_pwm_get_period(dev);
144     return output;
145 }
146
147 maa_result_t
148 maa_pwm_period(maa_pwm_context* dev, float seconds)
149 {
150     return maa_pwm_period_ms(dev, seconds*1000);
151 }
152
153 maa_result_t
154 maa_pwm_period_ms(maa_pwm_context* dev, int ms)
155 {
156     return maa_pwm_period_us(dev, ms*1000);
157 }
158
159 maa_result_t
160 maa_pwm_period_us(maa_pwm_context* dev, int us)
161 {
162     return maa_pwm_write_period(dev, us*1000);
163 }
164
165 maa_result_t
166 maa_pwm_pulsewidth(maa_pwm_context* dev, float seconds)
167 {
168     return maa_pwm_pulsewidth_ms(dev, seconds*1000);
169 }
170
171 maa_result_t
172 maa_pwm_pulsewidth_ms(maa_pwm_context* dev, int ms)
173 {
174     return maa_pwm_pulsewidth_us(dev, ms*1000);
175 }
176
177 maa_result_t
178 maa_pwm_pulsewidth_us(maa_pwm_context* dev, int us)
179 {
180     return maa_pwm_write_duty(dev, us*1000);
181 }
182
183 maa_result_t
184 maa_pwm_enable(maa_pwm_context* dev, int enable)
185 {
186     int status;
187     if (enable != 0) {
188         status = 1;
189     } else {
190         status = enable;
191     }
192     FILE *enable_f;
193     char bu[64];
194     sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/enable", dev->chipid, dev->pin);
195
196     if ((enable_f = fopen(bu, "w")) == NULL) {
197         fprintf(stderr, "Failed to open enable for writing!\n");
198         return MAA_ERROR_INVALID_RESOURCE;
199     }
200     fprintf(enable_f, "%d", status);
201     fclose(enable_f);
202     if (ferror(enable_f) != 0)
203         return MAA_ERROR_INVALID_RESOURCE;
204     return MAA_SUCCESS;
205 }
206
207 maa_result_t
208 maa_pwm_unexport(maa_pwm_context* dev)
209 {
210     // disable pwm before unexporting
211     maa_pwm_enable(dev, 0);
212     FILE *unexport_f;
213     char buffer[64];
214     snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/unexport", dev->chipid);
215
216     if ((unexport_f = fopen(buffer, "w")) == NULL) {
217         fprintf(stderr, "Failed to open unexport for writing!\n");
218         return MAA_ERROR_INVALID_RESOURCE;
219     }
220     fprintf(unexport_f, "%d", dev->pin);
221     fclose(unexport_f);
222     if (ferror(unexport_f) != 0)
223         return MAA_ERROR_INVALID_RESOURCE;
224 }
225
226 maa_result_t
227 maa_pwm_close(maa_pwm_context* dev)
228 {
229     maa_pwm_unexport(dev);
230     free(dev);
231     return MAA_SUCCESS;
232 }