summaryrefslogtreecommitdiff
path: root/peripheral/libmraa/api/mraa/pwm.hpp
blob: a449a4394a4a770da3b534eb21a16b48cd0715ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
 * Copyright (c) 2014 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#pragma once

#include "pwm.h"
#include "types.hpp"
#include <stdexcept>

namespace mraa
{

/**
 * @brief API to Pulse Width Modulation
 *
 * This file defines the PWM interface for libmraa
 *
 * @snippet Pwm3-cycle.cpp Interesting
 */
class Pwm
{
  public:
    /**
     * instanciates a PWM object on a pin
     *
     * @param pin the pin number used on your board
     * @param owner if you are the owner of the pin the destructor will
     * @param chipid the pwmchip to use, use only in raw mode
     * unexport the pin from sysfs, default behaviour is you are the owner
     * if the pinmapper exported it
     */
    Pwm(int pin, bool owner = true, int chipid = -1)
    {
        if (chipid == -1) {
            m_pwm = mraa_pwm_init(pin);
        } else {
            m_pwm = mraa_pwm_init_raw(chipid, pin);
        }

        if (m_pwm == NULL) {
            throw std::invalid_argument("Error initialising PWM on pin");
        }

        if (!owner) {
            mraa_pwm_owner(m_pwm, 0);
        }
    }
    /**
     * Pwm destructor
     */
    ~Pwm()
    {
        mraa_pwm_close(m_pwm);
    }
    /**
     * Set the output duty-cycle percentage, as a float
     *
     * @param percentage A floating-point value representing percentage of
     * output. The value should lie between 0.0f (representing on 0%) and
     * 1.0f Values above or below this range will be set at either 0.0f or
     * 1.0f
     * @return Result of operation
     */
    Result
    write(float percentage)
    {
        return (Result) mraa_pwm_write(m_pwm, percentage);
    }
    /**
     * Read the ouput duty-cycle percentage, as a float
     *
     * @return A floating-point value representing percentage of
     * output. The value should lie between 0.0f (representing on 0%) and
     * 1.0f Values above or below this range will be set at either 0.0f or
     * 1.0f
     */
    float
    read()
    {
        return mraa_pwm_read(m_pwm);
    }
    /**
     * Set the PWM period as seconds represented in a float
     *
     * @param period Period represented as a float in seconds
     * @return Result of operation
     */
    Result
    period(float period)
    {
        return (Result) mraa_pwm_period(m_pwm, period);
    }
    /**
     * Set period, milliseconds
     *
     * @param ms milliseconds for period
     * @return Result of operation
     */
    Result
    period_ms(int ms)
    {
        return (Result) mraa_pwm_period_ms(m_pwm, ms);
    }
    /**
     * Set period, microseconds
     *
     * @param us microseconds as period
     * @return Result of operation
     */
    Result
    period_us(int us)
    {
        return (Result) mraa_pwm_period_us(m_pwm, us);
    }
    /**
     * Set pulsewidth, As represnted by seconds in a (float)
     *
     * @param seconds The duration of a pulse
     * @return Result of operation
     */
    Result
    pulsewidth(float seconds)
    {
        return (Result) mraa_pwm_pulsewidth(m_pwm, seconds);
    }
    /**
     * Set pulsewidth, milliseconds
     *
     * @param ms milliseconds for pulsewidth
     * @return Result of operation
     */
    Result
    pulsewidth_ms(int ms)
    {
        return (Result) mraa_pwm_pulsewidth_ms(m_pwm, ms);
    }
    /**
     * The pulsewidth, microseconds
     *
     * @param us microseconds for pulsewidth
     * @return Result of operation
     */
    Result
    pulsewidth_us(int us)
    {
        return (Result) mraa_pwm_pulsewidth_us(m_pwm, us);
    }
    /**
     * Set the enable status of the PWM pin. None zero will assume on with
     * output being driven and 0 will disable the output
     *
     * @param enable enable status of pin
     * @return Result of operation
     */
    Result
    enable(bool enable)
    {
        if (enable)
            return (Result) mraa_pwm_enable(m_pwm, 1);
        else
            return (Result) mraa_pwm_enable(m_pwm, 0);
    }
    /**
     * Set the period and duty of a PWM object.
     *
     * @param period represented in ms.
     * @param duty represnted in ms as float.
     * @return Result of operation
     */
    Result
    config_ms(int period, float duty)
    {
        return (Result) mraa_pwm_config_ms(m_pwm, period, duty);
    }
    /**
     * Set the period and duty (percent) of a PWM object.
     *
     * @param period as represented in ms.
     * @param duty percentage i.e. 50% = 0.5f
     * @return Result of operation
     */
    Result
    config_percent(int period, float duty)
    {
        return (Result) mraa_pwm_config_percent(m_pwm, period, duty);
    }
    /**
     * Get the maximum pwm period in us
     *
     * @return max pwm in us
     */
    int
    max_period()
    {
        return mraa_pwm_get_max_period();
    }
    /**
     * Get the minimum pwm period in us
     *
     * @return min pwm in us
     */
    int
    min_period()
    {
        return mraa_pwm_get_min_period();
    }

  private:
    mraa_pwm_context m_pwm;
};
}