Private
Public Access
2
0

pwm.hpp: Add C++ wrapper around Pwm

* maa_pwm_context becomes an opaque pointer
* C++ wrapper class Pwm
* swig now uses C++ wrapper Pwm to generate API

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-05-16 11:53:19 +01:00
parent 8a03e61f43
commit f0763844fa
6 changed files with 130 additions and 106 deletions

View File

@@ -39,16 +39,7 @@ extern "C" {
#include "maa.h"
/**
* A strucutre representing a PWM pin
*/
typedef struct {
/*@{*/
int pin; /**< the pin number, as known to the os. */
int chipid; /**< the chip id, which the pwm resides */
FILE *duty_fp; /**< File pointer to duty file */
/*@}*/
} maa_pwm_context;
typedef struct _pwm* maa_pwm_context;
/** Initialise pwm_context, uses board mapping.
*
@@ -56,7 +47,7 @@ typedef struct {
*
* @return maa_pwm_context The returned initialised pwm context
*/
maa_pwm_context* maa_pwm_init(int pin);
maa_pwm_context maa_pwm_init(int pin);
/** Initialise pwm_context, raw mode.
*
@@ -65,7 +56,7 @@ maa_pwm_context* maa_pwm_init(int pin);
*
* @return maa_pwm_context The returned initialised pwm context
*/
maa_pwm_context* maa_pwm_init_raw(int chipid, int pin);
maa_pwm_context maa_pwm_init_raw(int chipid, int pin);
/** Set the ouput duty-cycle percentage, as a float
*
@@ -76,7 +67,7 @@ maa_pwm_context* maa_pwm_init_raw(int chipid, int pin);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_write(maa_pwm_context* pwm, float percentage);
maa_result_t maa_pwm_write(maa_pwm_context pwm, float percentage);
/** Read the ouput duty-cycle percentage, as a float
*
@@ -85,7 +76,7 @@ maa_result_t maa_pwm_write(maa_pwm_context* pwm, float percentage);
* 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 maa_pwm_read(maa_pwm_context* pwm);
float maa_pwm_read(maa_pwm_context pwm);
/** Set the PWM period as seconds represented in a float
*
@@ -94,7 +85,7 @@ float maa_pwm_read(maa_pwm_context* pwm);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_period(maa_pwm_context* pwm, float seconds);
maa_result_t maa_pwm_period(maa_pwm_context pwm, float seconds);
/** Set period, milli-oseconds.
*
@@ -103,7 +94,7 @@ maa_result_t maa_pwm_period(maa_pwm_context* pwm, float seconds);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_period_ms(maa_pwm_context* pwm, int ms);
maa_result_t maa_pwm_period_ms(maa_pwm_context pwm, int ms);
/** Set period, microseconds
*
@@ -112,7 +103,7 @@ maa_result_t maa_pwm_period_ms(maa_pwm_context* pwm, int ms);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_period_us(maa_pwm_context* pwm, int us);
maa_result_t maa_pwm_period_us(maa_pwm_context pwm, int us);
/** Set pulsewidth, As represnted by seconds in a (float).
*
@@ -121,7 +112,7 @@ maa_result_t maa_pwm_period_us(maa_pwm_context* pwm, int us);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_pulsewidth(maa_pwm_context* pwm, float seconds);
maa_result_t maa_pwm_pulsewidth(maa_pwm_context pwm, float seconds);
/** Set pulsewidth, milliseconds
*
@@ -130,7 +121,7 @@ maa_result_t maa_pwm_pulsewidth(maa_pwm_context* pwm, float seconds);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_pulsewidth_ms(maa_pwm_context* pwm, int ms);
maa_result_t maa_pwm_pulsewidth_ms(maa_pwm_context pwm, int ms);
/** Set pulsewidth, microseconds.
*
@@ -139,7 +130,7 @@ maa_result_t maa_pwm_pulsewidth_ms(maa_pwm_context* pwm, int ms);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_pulsewidth_us(maa_pwm_context* pwm, int us);
maa_result_t maa_pwm_pulsewidth_us(maa_pwm_context pwm, int us);
/** Set the enable status of the PWM pin. None zero will assume on with output being driven.
* and 0 will disable the output.
@@ -149,7 +140,7 @@ maa_result_t maa_pwm_pulsewidth_us(maa_pwm_context* pwm, int us);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_enable(maa_pwm_context* pwm, int enable);
maa_result_t maa_pwm_enable(maa_pwm_context pwm, int enable);
/** Unexport the PWM context (maa_pwm_close() will call this function)
*
@@ -157,7 +148,7 @@ maa_result_t maa_pwm_enable(maa_pwm_context* pwm, int enable);
*
* @return maa result type.
*/
maa_result_t maa_pwm_unexport(maa_pwm_context* pwm);
maa_result_t maa_pwm_unexport(maa_pwm_context pwm);
/** Close and unexport the PWM pin.
*
@@ -165,7 +156,7 @@ maa_result_t maa_pwm_unexport(maa_pwm_context* pwm);
*
* @return maa_result_t the maa result.
*/
maa_result_t maa_pwm_close(maa_pwm_context* pwm);
maa_result_t maa_pwm_close(maa_pwm_context pwm);
#ifdef __cplusplus
}

82
api/pwm.hpp Normal file
View File

@@ -0,0 +1,82 @@
/*
* 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
/** @file
*
* This file defines the pwm C++ interface for libmaa
*
*/
#include "pwm.h"
namespace maa {
class Pwm {
public:
Pwm(int pin, int chipid=-1) {
if (chipid == -1)
m_pwm = maa_pwm_init(pin);
else
m_pwm = maa_pwm_init_raw(pin, chipid);
}
~Pwm() {
maa_pwm_close(m_pwm);
}
maa_result_t write(float percentage) {
return maa_pwm_write(m_pwm, percentage);
}
float read() {
return maa_pwm_read(m_pwm);
}
maa_result_t period(float period) {
return maa_pwm_period(m_pwm, period);
}
maa_result_t period_ms(int ms) {
return maa_pwm_period_ms(m_pwm, ms);
}
maa_result_t period_us(int us) {
return maa_pwm_period_us(m_pwm, us);
}
maa_result_t pulsewidth(float seconds) {
return maa_pwm_pulsewidth(m_pwm, seconds);
}
maa_result_t pulsewidth_ms(int ms) {
return maa_pwm_pulsewidth_ms(m_pwm, ms);
}
maa_result_t pulsewidth_us(int us) {
return maa_pwm_pulsewidth_us(m_pwm, us);
}
maa_result_t enable(bool enable) {
if (enable)
return maa_pwm_enable(m_pwm, 1);
else
return maa_pwm_enable(m_pwm, 0);
}
private:
maa_pwm_context m_pwm;
};
}