mraa  0.6.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Pages
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 
39 class Pwm {
40  public:
50  Pwm(int pin, bool owner=true, int chipid=-1) {
51  if (chipid == -1) {
52  m_pwm = mraa_pwm_init(pin);
53  }
54  else {
55  m_pwm = mraa_pwm_init_raw(pin, chipid);
56  }
57 
58  if (m_pwm == NULL) {
59  throw std::invalid_argument("Error initialising PWM on pin");
60  }
61 
62  if (!owner) {
63  mraa_pwm_owner(m_pwm, 0);
64  }
65  }
69  ~Pwm() {
70  mraa_pwm_close(m_pwm);
71  }
81  mraa_result_t write(float percentage) {
82  return mraa_pwm_write(m_pwm, percentage);
83  }
92  float read() {
93  return mraa_pwm_read(m_pwm);
94  }
102  return mraa_pwm_period(m_pwm, period);
103  }
111  return mraa_pwm_period_ms(m_pwm, ms);
112  }
120  return mraa_pwm_period_us(m_pwm, us);
121  }
128  mraa_result_t pulsewidth(float seconds) {
129  return mraa_pwm_pulsewidth(m_pwm, seconds);
130  }
138  return mraa_pwm_pulsewidth_ms(m_pwm, ms);
139  }
147  return mraa_pwm_pulsewidth_us(m_pwm, us);
148  }
157  if (enable)
158  return mraa_pwm_enable(m_pwm, 1);
159  else
160  return mraa_pwm_enable(m_pwm, 0);
161  }
169  mraa_result_t config_ms(int period, float duty) {
170  return mraa_pwm_config_ms(m_pwm, period, duty);
171  }
180  return mraa_pwm_config_percent(m_pwm, period, duty);
181  }
182 
183  private:
184  mraa_pwm_context m_pwm;
185 };
186 
187 }
mraa_result_t period(float period)
Definition: pwm.hpp:101
mraa_result_t mraa_pwm_period(mraa_pwm_context dev, float seconds)
mraa_pwm_context mraa_pwm_init(int pin)
Pulse Width Modulation module.
mraa_result_t pulsewidth_ms(int ms)
Definition: pwm.hpp:137
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:146
mraa_result_t mraa_pwm_period_ms(mraa_pwm_context dev, int ms)
mraa_result_t config_percent(int period, float duty)
Definition: pwm.hpp:179
mraa_result_t pulsewidth(float seconds)
Definition: pwm.hpp:128
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:110
mraa_result_t mraa_pwm_enable(mraa_pwm_context dev, int enable)
mraa_result_t config_ms(int period, float duty)
Definition: pwm.hpp:169
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:92
mraa_result_t mraa_pwm_pulsewidth(mraa_pwm_context dev, float seconds)
mraa_result_t write(float percentage)
Definition: pwm.hpp:81
Pwm(int pin, bool owner=true, int chipid=-1)
Definition: pwm.hpp:50
mraa_result_t
Definition: types.h:145
mraa_result_t enable(bool enable)
Definition: pwm.hpp:156
mraa_result_t period_us(int us)
Definition: pwm.hpp:119
mraa_result_t mraa_pwm_close(mraa_pwm_context dev)
~Pwm()
Definition: pwm.hpp:69
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:39