mraa  1.7.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Macros 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 "types.hpp"
29 #include <stdexcept>
30 
31 namespace mraa
32 {
33 
41 class Pwm
42 {
43  public:
53  Pwm(int pin, bool owner = true, int chipid = -1)
54  {
55  if (chipid == -1) {
56  m_pwm = mraa_pwm_init(pin);
57  } else {
58  m_pwm = mraa_pwm_init_raw(chipid, pin);
59  }
60 
61  if (m_pwm == NULL) {
62  throw std::invalid_argument("Error initialising PWM on pin");
63  }
64 
65  if (!owner) {
66  mraa_pwm_owner(m_pwm, 0);
67  }
68  }
69 
76  Pwm(void* pwm_context)
77  {
78  m_pwm = (mraa_pwm_context) pwm_context;
79  if (m_pwm == NULL) {
80  throw std::invalid_argument("Invalid PWM context");
81  }
82  }
86  ~Pwm()
87  {
88  mraa_pwm_close(m_pwm);
89  }
99  Result
100  write(float percentage)
101  {
102  return (Result) mraa_pwm_write(m_pwm, percentage);
103  }
112  float
114  {
115  return mraa_pwm_read(m_pwm);
116  }
123  Result
124  period(float period)
125  {
126  return (Result) mraa_pwm_period(m_pwm, period);
127  }
134  Result
135  period_ms(int ms)
136  {
137  return (Result) mraa_pwm_period_ms(m_pwm, ms);
138  }
145  Result
146  period_us(int us)
147  {
148  return (Result) mraa_pwm_period_us(m_pwm, us);
149  }
156  Result
157  pulsewidth(float seconds)
158  {
159  return (Result) mraa_pwm_pulsewidth(m_pwm, seconds);
160  }
167  Result
169  {
170  return (Result) mraa_pwm_pulsewidth_ms(m_pwm, ms);
171  }
178  Result
180  {
181  return (Result) mraa_pwm_pulsewidth_us(m_pwm, us);
182  }
190  Result
192  {
193  return (Result) mraa_pwm_enable(m_pwm, enable);
194  }
200  int
202  {
203  return mraa_pwm_get_max_period(m_pwm);
204  }
210  int
212  {
213  return mraa_pwm_get_min_period(m_pwm);
214  }
215 
216  private:
217  mraa_pwm_context m_pwm;
218 };
219 }
Result
Definition: types.hpp:199
mraa_result_t mraa_pwm_period(mraa_pwm_context dev, float seconds)
Result period_ms(int ms)
Definition: pwm.hpp:135
mraa_pwm_context mraa_pwm_init(int pin)
Pulse Width Modulation module.
mraa_pwm_context mraa_pwm_init_raw(int chipid, int pin)
Result period(float period)
Definition: pwm.hpp:124
mraa_result_t mraa_pwm_period_ms(mraa_pwm_context dev, int ms)
int max_period()
Definition: pwm.hpp:201
int min_period()
Definition: pwm.hpp:211
Result period_us(int us)
Definition: pwm.hpp:146
int mraa_pwm_get_min_period(mraa_pwm_context dev)
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 mraa_pwm_enable(mraa_pwm_context dev, int enable)
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)
Result pulsewidth(float seconds)
Definition: pwm.hpp:157
Result write(float percentage)
Definition: pwm.hpp:100
int mraa_pwm_get_max_period(mraa_pwm_context dev)
float read()
Definition: pwm.hpp:113
mraa_result_t mraa_pwm_pulsewidth(mraa_pwm_context dev, float seconds)
Result pulsewidth_us(int us)
Definition: pwm.hpp:179
Pwm(int pin, bool owner=true, int chipid=-1)
Definition: pwm.hpp:53
Result pulsewidth_ms(int ms)
Definition: pwm.hpp:168
mraa_result_t mraa_pwm_close(mraa_pwm_context dev)
~Pwm()
Definition: pwm.hpp:86
Pwm(void *pwm_context)
Definition: pwm.hpp:76
Result enable(bool enable)
Definition: pwm.hpp:191
mraa_result_t mraa_pwm_owner(mraa_pwm_context dev, mraa_boolean_t owner)
API to Pulse Width Modulation.
Definition: pwm.hpp:41