pwm: Completed functions:
[contrib/mraa.git] / api / pwm.h
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  *
4  * Copyright © 2014 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #pragma once
27
28 #include <stdio.h>
29 #include <fcntl.h>
30
31 namespace maa {
32
33 /** A PWM object, used for interacting with PWM output.
34  *
35  * Example:
36  * @code
37  * // Set up PWM object then cycle percentage 0-100.
38  *
39  * #include "maa.h"
40  *
41  * PWM pwm(3);
42  *
43  * int main() {
44  *     pwm.period_us(7968750i); //Max Galileo Rev D
45  *     pwm.enable(1);
46  *
47  *     float value = 0;
48  *     while(1) {
49  *         pwm.write(value);
50  *         sleep(0.5);
51  *         if(value == 1.0) {
52  *             value = 0;
53  *         } else {
54  *             value = value +0.1;
55  *         }
56  *     }
57  * }
58  * @endcode
59  */
60 class PWM {
61
62 private:
63     int chipid, pin;
64     FILE *duty_fp;
65
66     void write_period(int period);
67     void write_duty(int duty);
68     int setup_duty_fp();
69     int get_period();
70     int get_duty();
71
72 public:
73
74     /** Create an PWM object
75      *
76      *  @param chipid The chip in which the following pin is on.
77      *  @param pin The PWM channel to operate on
78      */
79     PWM(int chipid, int pin);
80
81     /** Set the ouput duty-cycle percentage, as a float
82      *
83      *  @param percentage A floating-point value representing percentage of output.
84      *    The value should lie between 0.0f (representing on 0%) and 1.0f
85      *    Values above or below this range will be set at either 0.0f or 1.0f.
86      */
87     void write(float percentage);
88
89     /** Read the ouput duty-cycle percentage, as a float
90      *
91      *  @return percentage A floating-point value representing percentage of output.
92      *    The value should lie between 0.0f (representing on 0%) and 1.0f
93      *    Values above or below this range will be set at either 0.0f or 1.0f.
94      */
95     float read();
96
97     /** Set the PWM period as seconds represented in a float
98      *
99      *  @param seconds Peroid represented as a float in seconds.
100      */
101     void period(float seconds);
102
103     /** Set period. milli-oseconds.
104      *  @param ms milli-seconds for period.
105      */
106     void period_ms(int ms);
107
108     /** Set period. microseconds
109      *  @param ns microseconds as period.
110      */
111     void period_us(int us);
112
113     /** Set pulsewidth, As represnted by seconds in a (float).
114      *  @param seconds The duration of a pulse
115      */
116     void pulsewidth(float seconds);
117
118      /** Set pulsewidth. Milliseconds
119      *  @param ms milliseconds for pulsewidth.
120      */
121     void pulsewidth_ms(int ms);
122
123       /** Set pulsewidth, microseconds.
124      *  @param us microseconds for pulsewidth.
125      */
126     void pulsewidth_us(int us);
127
128     /** Set the enable status of the PWM pin. None zero will assume on with output being driven.
129      *   and 0 will disable the output.
130      *  @param enable enable status of pin
131      */
132     void enable(int enable);
133
134      /** Close and unexport the PWM pin.
135      */
136     void close();
137
138 };
139 }