mraa  0.7.1
Low Level Skeleton Library for Communication on GNU/Linux platforms
pwm.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@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 #pragma once
26 
27 #include "pwm.h"
28 #include <stdexcept>
29 
30 namespace mraa
31 {
32 
40 class Pwm
41 {
42  public:
52  Pwm(int pin, bool owner = true, int chipid = -1)
53  {
54  if (chipid == -1) {
55  m_pwm = mraa_pwm_init(pin);
56  } else {
57  m_pwm = mraa_pwm_init_raw(chipid, pin);
58  }
59 
60  if (m_pwm == NULL) {
61  throw std::invalid_argument("Error initialising PWM on pin");
62  }
63 
64  if (!owner) {
65  mraa_pwm_owner(m_pwm, 0);
66  }
67  }
71  ~Pwm()
72  {
73  mraa_pwm_close(m_pwm);
74  }
85  write(float percentage)
86  {
87  return mraa_pwm_write(m_pwm, percentage);
88  }
97  float
98  read()
99  {
100  return mraa_pwm_read(m_pwm);
101  }
109  period(float period)
110  {
111  return mraa_pwm_period(m_pwm, period);
112  }
120  period_ms(int ms)
121  {
122  return mraa_pwm_period_ms(m_pwm, ms);
123  }
131  period_us(int us)
132  {
133  return mraa_pwm_period_us(m_pwm, us);
134  }
142  pulsewidth(float seconds)
143  {
144  return mraa_pwm_pulsewidth(m_pwm, seconds);
145  }
154  {
155  return mraa_pwm_pulsewidth_ms(m_pwm, ms);
156  }
165  {
166  return mraa_pwm_pulsewidth_us(m_pwm, us);
167  }
177  {
178  if (enable)
179  return mraa_pwm_enable(m_pwm, 1);
180  else
181  return mraa_pwm_enable(m_pwm, 0);
182  }
191  config_ms(int period, float duty)
192  {
193  return mraa_pwm_config_ms(m_pwm, period, duty);
194  }
203  config_percent(int period, float duty)
204  {
205  return mraa_pwm_config_percent(m_pwm, period, duty);
206  }
212  int
214  {
215  return mraa_pwm_get_max_period();
216  }
222  int
224  {
225  return mraa_pwm_get_min_period();
226  }
227 
228  private:
229  mraa_pwm_context m_pwm;
230 };
231 }
mraa_result_t period(float period)
Definition: pwm.hpp:109
mraa_result_t mraa_pwm_period(mraa_pwm_context dev, float seconds)
mraa_pwm_context mraa_pwm_init(int pin)
Pulse Width Modulation module.
int mraa_pwm_get_min_period()
mraa_result_t pulsewidth_ms(int ms)
Definition: pwm.hpp:153
mraa_pwm_context mraa_pwm_init_raw(int chipid, int pin)
mraa_result_t mraa_pwm_config_ms(mraa_pwm_context dev, int period, float duty)
mraa_result_t pulsewidth_us(int us)
Definition: pwm.hpp:164
mraa_result_t mraa_pwm_period_ms(mraa_pwm_context dev, int ms)
int max_period()
Definition: pwm.hpp:213
int mraa_pwm_get_max_period()
int min_period()
Definition: pwm.hpp:223
mraa_result_t config_percent(int period, float duty)
Definition: pwm.hpp:203
mraa_result_t pulsewidth(float seconds)
Definition: pwm.hpp:142
mraa_result_t mraa_pwm_pulsewidth_ms(mraa_pwm_context dev, int ms)
mraa_result_t mraa_pwm_pulsewidth_us(mraa_pwm_context dev, int us)
mraa_result_t period_ms(int ms)
Definition: pwm.hpp:120
mraa_result_t mraa_pwm_enable(mraa_pwm_context dev, int enable)
mraa_result_t config_ms(int period, float duty)
Definition: pwm.hpp:191
float mraa_pwm_read(mraa_pwm_context dev)
mraa_result_t mraa_pwm_period_us(mraa_pwm_context dev, int us)
mraa_result_t mraa_pwm_write(mraa_pwm_context dev, float percentage)
float read()
Definition: pwm.hpp:98
mraa_result_t mraa_pwm_pulsewidth(mraa_pwm_context dev, float seconds)
mraa_result_t write(float percentage)
Definition: pwm.hpp:85
Definition: aio.hpp:30
Pwm(int pin, bool owner=true, int chipid=-1)
Definition: pwm.hpp:52
mraa_result_t
Definition: types.h:184
mraa_result_t enable(bool enable)
Definition: pwm.hpp:176
mraa_result_t period_us(int us)
Definition: pwm.hpp:131
mraa_result_t mraa_pwm_close(mraa_pwm_context dev)
~Pwm()
Definition: pwm.hpp:71
mraa_result_t mraa_pwm_config_percent(mraa_pwm_context dev, int period, float duty)
mraa_result_t mraa_pwm_owner(mraa_pwm_context dev, mraa_boolean_t owner)
API to Pulse Width Modulation.
Definition: pwm.hpp:40