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:
37
api/pwm.h
37
api/pwm.h
@@ -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
82
api/pwm.hpp
Normal 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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -30,7 +30,7 @@ int
|
||||
main ()
|
||||
{
|
||||
maa_init();
|
||||
maa_pwm_context* pwm;
|
||||
maa_pwm_context pwm;
|
||||
pwm = maa_pwm_init(3);
|
||||
if (pwm == NULL) {
|
||||
return 1;
|
||||
|
||||
@@ -9,6 +9,7 @@ set (maa_LIB_HEADERS
|
||||
${PROJECT_SOURCE_DIR}/api/i2c.h
|
||||
${PROJECT_SOURCE_DIR}/api/i2c.hpp
|
||||
${PROJECT_SOURCE_DIR}/api/pwm.h
|
||||
${PROJECT_SOURCE_DIR}/api/pwm.hpp
|
||||
${PROJECT_SOURCE_DIR}/api/spi.h
|
||||
${PROJECT_SOURCE_DIR}/api/aio.h
|
||||
${PROJECT_SOURCE_DIR}/api/aio.hpp
|
||||
|
||||
65
src/maa.i
65
src/maa.i
@@ -1,7 +1,7 @@
|
||||
%{
|
||||
#include "maa.h"
|
||||
#include "gpio.h"
|
||||
#include "pwm.h"
|
||||
#include "pwm.hpp"
|
||||
#include "i2c.hpp"
|
||||
#include "spi.h"
|
||||
#include "aio.hpp"
|
||||
@@ -130,68 +130,7 @@ typedef struct {
|
||||
|
||||
#### PWM ####
|
||||
|
||||
%rename(Pwm) maa_pwm_context;
|
||||
|
||||
%ignore duty_fp;
|
||||
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;
|
||||
|
||||
%nodefault maa_pwm_context;
|
||||
%extend maa_pwm_context {
|
||||
maa_pwm_context(int pin)
|
||||
{
|
||||
return maa_pwm_init(pin);
|
||||
}
|
||||
maa_pwm_context(int chipid, int pin)
|
||||
{
|
||||
return maa_pwm_init_raw(chipid, pin);
|
||||
}
|
||||
~maa_pwm_context()
|
||||
{
|
||||
maa_pwm_unexport($self);
|
||||
}
|
||||
int write(float percentage)
|
||||
{
|
||||
return maa_pwm_write($self, percentage);
|
||||
}
|
||||
int read()
|
||||
{
|
||||
return maa_pwm_read($self);
|
||||
}
|
||||
int period(float seconds)
|
||||
{
|
||||
return maa_pwm_period($self, seconds);
|
||||
}
|
||||
int period_ms(int ms)
|
||||
{
|
||||
return maa_pwm_period_ms($self, ms);
|
||||
}
|
||||
int period_us(int us)
|
||||
{
|
||||
return maa_pwm_period_us($self, us);
|
||||
}
|
||||
int pulsewidth(float seconds)
|
||||
{
|
||||
return maa_pwm_pulsewidth($self, seconds);
|
||||
}
|
||||
int pulsewidth_ms(int ms)
|
||||
{
|
||||
return maa_pwm_pulsewidth($self, ms);
|
||||
}
|
||||
int pulsewidth_us(int us)
|
||||
{
|
||||
return maa_pwm_pulsewidth($self, us);
|
||||
}
|
||||
int enable(int enable)
|
||||
{
|
||||
return maa_pwm_enable($self, enable);
|
||||
}
|
||||
}
|
||||
%include "pwm.hpp"
|
||||
|
||||
#### SPI ####
|
||||
|
||||
|
||||
@@ -26,8 +26,19 @@
|
||||
|
||||
#include "pwm.h"
|
||||
|
||||
/**
|
||||
* A strucutre representing a PWM pin
|
||||
*/
|
||||
struct _pwm {
|
||||
/*@{*/
|
||||
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 */
|
||||
/*@}*/
|
||||
};
|
||||
|
||||
static int
|
||||
maa_pwm_setup_duty_fp(maa_pwm_context* dev)
|
||||
maa_pwm_setup_duty_fp(maa_pwm_context dev)
|
||||
{
|
||||
char bu[64];
|
||||
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin);
|
||||
@@ -39,7 +50,7 @@ maa_pwm_setup_duty_fp(maa_pwm_context* dev)
|
||||
}
|
||||
|
||||
static maa_result_t
|
||||
maa_pwm_write_period(maa_pwm_context* dev, int period)
|
||||
maa_pwm_write_period(maa_pwm_context dev, int period)
|
||||
{
|
||||
FILE *period_f;
|
||||
char bu[64];
|
||||
@@ -57,7 +68,7 @@ maa_pwm_write_period(maa_pwm_context* dev, int period)
|
||||
}
|
||||
|
||||
static maa_result_t
|
||||
maa_pwm_write_duty(maa_pwm_context* dev, int duty)
|
||||
maa_pwm_write_duty(maa_pwm_context dev, int duty)
|
||||
{
|
||||
if (dev->duty_fp == NULL) {
|
||||
maa_pwm_setup_duty_fp(dev);
|
||||
@@ -71,7 +82,7 @@ maa_pwm_write_duty(maa_pwm_context* dev, int duty)
|
||||
}
|
||||
|
||||
static int
|
||||
maa_pwm_get_period(maa_pwm_context* dev)
|
||||
maa_pwm_get_period(maa_pwm_context dev)
|
||||
{
|
||||
FILE *period_f;
|
||||
char bu[64];
|
||||
@@ -88,7 +99,7 @@ maa_pwm_get_period(maa_pwm_context* dev)
|
||||
}
|
||||
|
||||
static int
|
||||
maa_pwm_get_duty(maa_pwm_context* dev)
|
||||
maa_pwm_get_duty(maa_pwm_context dev)
|
||||
{
|
||||
if (dev->duty_fp == NULL) {
|
||||
maa_pwm_setup_duty_fp(dev);
|
||||
@@ -99,7 +110,7 @@ maa_pwm_get_duty(maa_pwm_context* dev)
|
||||
return atoi(output);
|
||||
}
|
||||
|
||||
maa_pwm_context*
|
||||
maa_pwm_context
|
||||
maa_pwm_init(int pin) {
|
||||
maa_pin_t* pinm = maa_check_pwm(pin);
|
||||
if (pinm == NULL)
|
||||
@@ -110,10 +121,10 @@ maa_pwm_init(int pin) {
|
||||
return maa_pwm_init_raw(chip,pinn);
|
||||
}
|
||||
|
||||
maa_pwm_context*
|
||||
maa_pwm_context
|
||||
maa_pwm_init_raw(int chipin, int pin)
|
||||
{
|
||||
maa_pwm_context* dev = (maa_pwm_context*) malloc(sizeof(maa_pwm_context));
|
||||
maa_pwm_context dev = (maa_pwm_context) malloc(sizeof(struct _pwm));
|
||||
if (dev == NULL)
|
||||
return NULL;
|
||||
|
||||
@@ -137,56 +148,56 @@ maa_pwm_init_raw(int chipin, int pin)
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_write(maa_pwm_context* dev, float percentage)
|
||||
maa_pwm_write(maa_pwm_context dev, float percentage)
|
||||
{
|
||||
return maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
|
||||
}
|
||||
|
||||
float
|
||||
maa_pwm_read(maa_pwm_context* dev)
|
||||
maa_pwm_read(maa_pwm_context dev)
|
||||
{
|
||||
float output = maa_pwm_get_duty(dev) / (float) maa_pwm_get_period(dev);
|
||||
return output;
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_period(maa_pwm_context* dev, float seconds)
|
||||
maa_pwm_period(maa_pwm_context dev, float seconds)
|
||||
{
|
||||
return maa_pwm_period_ms(dev, seconds*1000);
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_period_ms(maa_pwm_context* dev, int ms)
|
||||
maa_pwm_period_ms(maa_pwm_context dev, int ms)
|
||||
{
|
||||
return maa_pwm_period_us(dev, ms*1000);
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_period_us(maa_pwm_context* dev, int us)
|
||||
maa_pwm_period_us(maa_pwm_context dev, int us)
|
||||
{
|
||||
return maa_pwm_write_period(dev, us*1000);
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_pulsewidth(maa_pwm_context* dev, float seconds)
|
||||
maa_pwm_pulsewidth(maa_pwm_context dev, float seconds)
|
||||
{
|
||||
return maa_pwm_pulsewidth_ms(dev, seconds*1000);
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_pulsewidth_ms(maa_pwm_context* dev, int ms)
|
||||
maa_pwm_pulsewidth_ms(maa_pwm_context dev, int ms)
|
||||
{
|
||||
return maa_pwm_pulsewidth_us(dev, ms*1000);
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_pulsewidth_us(maa_pwm_context* dev, int us)
|
||||
maa_pwm_pulsewidth_us(maa_pwm_context dev, int us)
|
||||
{
|
||||
return maa_pwm_write_duty(dev, us*1000);
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_enable(maa_pwm_context* dev, int enable)
|
||||
maa_pwm_enable(maa_pwm_context dev, int enable)
|
||||
{
|
||||
int status;
|
||||
if (enable != 0) {
|
||||
@@ -210,7 +221,7 @@ maa_pwm_enable(maa_pwm_context* dev, int enable)
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_unexport(maa_pwm_context* dev)
|
||||
maa_pwm_unexport(maa_pwm_context dev)
|
||||
{
|
||||
// disable pwm before unexporting
|
||||
maa_pwm_enable(dev, 0);
|
||||
@@ -230,7 +241,7 @@ maa_pwm_unexport(maa_pwm_context* dev)
|
||||
}
|
||||
|
||||
maa_result_t
|
||||
maa_pwm_close(maa_pwm_context* dev)
|
||||
maa_pwm_close(maa_pwm_context dev)
|
||||
{
|
||||
maa_pwm_unexport(dev);
|
||||
free(dev);
|
||||
|
||||
Reference in New Issue
Block a user