Private
Public Access
2
0

maa: change struct names to be more unique and fix allocs

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-04-28 11:31:53 +01:00
parent 00fd42b186
commit f280b3c0f6
9 changed files with 125 additions and 128 deletions

View File

@@ -27,16 +27,15 @@ typedef struct {
int pinMap; int pinMap;
char path[64]; char path[64];
FILE *value_fp; FILE *value_fp;
} gpio_t; } maa_gpio_context;
typedef char gpio_mode_t[16]; typedef char gpio_mode_t[16];
typedef char gpio_dir_t[16]; typedef char gpio_dir_t[16];
maa_result_t maa_gpio_init(gpio_t *gpio, int pin); maa_gpio_context* maa_gpio_init(int pin);
int maa_gpio_set(int pin); void maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode);
void maa_gpio_mode(gpio_t *gpio, gpio_mode_t mode); void maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir);
void maa_gpio_dir(gpio_t *gpio, gpio_dir_t dir);
void maa_gpio_close(gpio_t *gpio); void maa_gpio_close(maa_gpio_context *dev);
int maa_gpio_read(gpio_t *gpio); int maa_gpio_read(maa_gpio_context *dev);
void maa_gpio_write(gpio_t *gpio, int value); void maa_gpio_write(maa_gpio_context *dev, int value);

View File

@@ -29,16 +29,16 @@ typedef struct {
int hz; int hz;
int fh; int fh;
int addr; int addr;
gpio_t gpio; maa_gpio_context gpio;
} i2c_t; } maa_i2c_context;
maa_result_t maa_i2c_init(i2c_t* dev); maa_i2c_context* maa_i2c_init();
/** Set the frequency of the I2C interface /** Set the frequency of the I2C interface
* *
* @param hz The bus frequency in hertz * @param hz The bus frequency in hertz
*/ */
void maa_i2c_frequency(i2c_t* dev, int hz); void maa_i2c_frequency(maa_i2c_context* dev, int hz);
/** Checks to see if this I2C Slave has been addressed. /** Checks to see if this I2C Slave has been addressed.
* *
@@ -49,7 +49,7 @@ void maa_i2c_frequency(i2c_t* dev, int hz);
* - WriteAddressed - the master is writing to this slave * - WriteAddressed - the master is writing to this slave
* - WriteGeneral - the master is writing to all slave * - WriteGeneral - the master is writing to all slave
*/ */
int maa_i2c_receive(i2c_t* dev); int maa_i2c_receive(maa_i2c_context* dev);
/** Read from an I2C master. /** Read from an I2C master.
* *
@@ -60,14 +60,14 @@ int maa_i2c_receive(i2c_t* dev);
* 0 on success, * 0 on success,
* non-0 otherwise * non-0 otherwise
*/ */
int maa_i2c_read(i2c_t* dev, char *data, int length); int maa_i2c_read(maa_i2c_context* dev, char *data, int length);
/** Read a single byte from an I2C master. /** Read a single byte from an I2C master.
* *
* @returns * @returns
* the byte read * the byte read
*/ */
int maa_i2c_read_byte(i2c_t* dev); int maa_i2c_read_byte(maa_i2c_context* dev);
/** Write to an I2C master. /** Write to an I2C master.
* *
@@ -78,7 +78,7 @@ int maa_i2c_read_byte(i2c_t* dev);
* 0 on success, * 0 on success,
* non-0 otherwise * non-0 otherwise
*/ */
int maa_i2c_write(i2c_t* dev, const char *data, int length); int maa_i2c_write(maa_i2c_context* dev, const char *data, int length);
/** Write a single byte to an I2C master. /** Write a single byte to an I2C master.
* *
@@ -88,7 +88,7 @@ int maa_i2c_write(i2c_t* dev, const char *data, int length);
* '1' if an ACK was received, * '1' if an ACK was received,
* '0' otherwise * '0' otherwise
*/ */
int maa_i2c_write_byte(i2c_t* dev, int data); int maa_i2c_write_byte(maa_i2c_context* dev, int data);
/** Sets the I2C slave address. /** Sets the I2C slave address.
* *
@@ -96,8 +96,8 @@ int maa_i2c_write_byte(i2c_t* dev, int data);
* signifcant bit). If set to 0, the slave will only respond to the * signifcant bit). If set to 0, the slave will only respond to the
* general call address. * general call address.
*/ */
void maa_i2c_address(i2c_t* dev, int address); void maa_i2c_address(maa_i2c_context* dev, int address);
/** De-inits an i2c_t device /** De-inits an maa_i2c_context device
*/ */
void maa_i2c_stop(i2c_t* dev); void maa_i2c_stop(maa_i2c_context* dev);

View File

@@ -26,9 +26,9 @@
typedef struct { typedef struct {
int chipid, pin; int chipid, pin;
FILE *duty_fp; FILE *duty_fp;
} pwm_t; } maa_pwm_context;
maa_result_t maa_pwm_init(pwm_t* pwm, int chipin, int pin); maa_pwm_context* maa_pwm_init(int chipin, int pin);
/** Set the ouput duty-cycle percentage, as a float /** Set the ouput duty-cycle percentage, as a float
* *
@@ -36,7 +36,7 @@ maa_result_t maa_pwm_init(pwm_t* pwm, int chipin, int pin);
* The value should lie between 0.0f (representing on 0%) and 1.0f * 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. * Values above or below this range will be set at either 0.0f or 1.0f.
*/ */
void maa_pwm_write(pwm_t* pwm, float percentage); void maa_pwm_write(maa_pwm_context* pwm, float percentage);
/** Read the ouput duty-cycle percentage, as a float /** Read the ouput duty-cycle percentage, as a float
* *
@@ -44,45 +44,45 @@ void maa_pwm_write(pwm_t* pwm, float percentage);
* The value should lie between 0.0f (representing on 0%) and 1.0f * 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. * Values above or below this range will be set at either 0.0f or 1.0f.
*/ */
float maa_pwm_read(pwm_t* pwm); float maa_pwm_read(maa_pwm_context* pwm);
/** Set the PWM period as seconds represented in a float /** Set the PWM period as seconds represented in a float
* *
* @param seconds Peroid represented as a float in seconds. * @param seconds Peroid represented as a float in seconds.
*/ */
void maa_pwm_period(pwm_t* pwm, float seconds); void maa_pwm_period(maa_pwm_context* pwm, float seconds);
/** Set period. milli-oseconds. /** Set period. milli-oseconds.
* @param ms milli-seconds for period. * @param ms milli-seconds for period.
*/ */
void maa_pwm_period_ms(pwm_t* pwm, int ms); void maa_pwm_period_ms(maa_pwm_context* pwm, int ms);
/** Set period. microseconds /** Set period. microseconds
* @param ns microseconds as period. * @param ns microseconds as period.
*/ */
void maa_pwm_period_us(pwm_t* pwm, int us); void maa_pwm_period_us(maa_pwm_context* pwm, int us);
/** Set pulsewidth, As represnted by seconds in a (float). /** Set pulsewidth, As represnted by seconds in a (float).
* @param seconds The duration of a pulse * @param seconds The duration of a pulse
*/ */
void maa_pwm_pulsewidth(pwm_t* pwm, float seconds); void maa_pwm_pulsewidth(maa_pwm_context* pwm, float seconds);
/** Set pulsewidth. Milliseconds /** Set pulsewidth. Milliseconds
* @param ms milliseconds for pulsewidth. * @param ms milliseconds for pulsewidth.
*/ */
void maa_pwm_pulsewidth_ms(pwm_t* pwm, int ms); void maa_pwm_pulsewidth_ms(maa_pwm_context* pwm, int ms);
/** Set pulsewidth, microseconds. /** Set pulsewidth, microseconds.
* @param us microseconds for pulsewidth. * @param us microseconds for pulsewidth.
*/ */
void maa_pwm_pulsewidth_us(pwm_t* pwm, int us); void 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. /** Set the enable status of the PWM pin. None zero will assume on with output being driven.
* and 0 will disable the output. * and 0 will disable the output.
* @param enable enable status of pin * @param enable enable status of pin
*/ */
void maa_pwm_enable(pwm_t* pwm, int enable); void maa_pwm_enable(maa_pwm_context* pwm, int enable);
/** Close and unexport the PWM pin. /** Close and unexport the PWM pin.
*/ */
void maa_pwm_close(pwm_t* pwm); void maa_pwm_close(maa_pwm_context* pwm);

View File

@@ -29,15 +29,16 @@
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
fprintf(stdout, "MAA Version: %d\n Starting Blinking on IO8", maa_get_version()); fprintf(stdout, "MAA Version: %d\nStarting Blinking on IO8\n",
gpio_t gpio; maa_get_version());
maa_gpio_init(&gpio, 26); maa_gpio_context* gpio;
maa_gpio_dir(&gpio, "out"); gpio = maa_gpio_init(26);
maa_gpio_dir(gpio, "out");
while (1) { while (1) {
maa_gpio_write(&gpio, 0); maa_gpio_write(gpio, 0);
sleep(1); sleep(1);
maa_gpio_write(&gpio, 1); maa_gpio_write(gpio, 1);
sleep(1); sleep(1);
} }
return 0; return 0;

View File

@@ -29,21 +29,24 @@
int int
main () main ()
{ {
pwm_t pwm; maa_pwm_context* pwm;
maa_pwm_init(&pwm, 0, 3); pwm = maa_pwm_init(0, 3);
maa_pwm_period_us(&pwm, 200); if (pwm == NULL) {
maa_pwm_enable(&pwm, 1); return 1;
}
maa_pwm_period_us(pwm, 200);
maa_pwm_enable(pwm, 1);
float value = 0.0f; float value = 0.0f;
while (1) { while (1) {
value = value + 0.01f; value = value + 0.01f;
maa_pwm_write(&pwm, value); maa_pwm_write(pwm, value);
usleep(50000); usleep(50000);
if (value >= 1.0f) { if (value >= 1.0f) {
value = 0.0f; value = 0.0f;
} }
float output = maa_pwm_read(&pwm); float output = maa_pwm_read(pwm);
} }
return 0; return 0;
} }

View File

@@ -76,31 +76,31 @@
#define SCALE_4_35_MG 4.35 #define SCALE_4_35_MG 4.35
int int
main () main(int argc, char **argv)
{ {
float direction = 0; float direction = 0;
int16_t x = 0, y = 0, z = 0; int16_t x = 0, y = 0, z = 0;
char rx_tx_buf[MAX_BUFFER_LENGTH]; char rx_tx_buf[MAX_BUFFER_LENGTH];
i2c_t i2c; maa_i2c_context *i2c;
maa_i2c_init(&i2c); i2c = maa_i2c_init();
maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); maa_i2c_address(i2c, HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_CONF_REG_B; rx_tx_buf[0] = HMC5883L_CONF_REG_B;
rx_tx_buf[1] = GA_1_3_REG; rx_tx_buf[1] = GA_1_3_REG;
maa_i2c_write(&i2c, rx_tx_buf, 2); maa_i2c_write(i2c, rx_tx_buf, 2);
maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); maa_i2c_address(i2c, HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_MODE_REG; rx_tx_buf[0] = HMC5883L_MODE_REG;
rx_tx_buf[1] = HMC5883L_CONT_MODE; rx_tx_buf[1] = HMC5883L_CONT_MODE;
maa_i2c_write(&i2c, rx_tx_buf, 2); maa_i2c_write(i2c, rx_tx_buf, 2);
for(;;) { for(;;) {
maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); maa_i2c_address(i2c, HMC5883L_I2C_ADDR);
maa_i2c_write_byte(&i2c, HMC5883L_DATA_REG); maa_i2c_write_byte(i2c, HMC5883L_DATA_REG);
maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); maa_i2c_address(i2c, HMC5883L_I2C_ADDR);
maa_i2c_read(&i2c, rx_tx_buf, DATA_REG_SIZE); maa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE);
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ; x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ;
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ; z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ;

View File

@@ -30,21 +30,22 @@
#include "gpio.h" #include "gpio.h"
static int static int
maa_gpio_get_valfp(gpio_t *gpio) maa_gpio_get_valfp(maa_gpio_context *dev)
{ {
char bu[64]; char bu[64];
sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin); sprintf(bu, "/sys/class/gpio/gpio%d/value", dev->pin);
if ((gpio->value_fp = fopen(bu, "r+b")) == NULL) { if ((dev->value_fp = fopen(bu, "r+b")) == NULL) {
return 1; return 1;
} }
return 0; return 0;
} }
maa_result_t maa_gpio_context*
maa_gpio_init(gpio_t *gpio, int pin) maa_gpio_init(int pin)
{ {
FILE *export_f; FILE *export_f;
maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
if ((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) { if ((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
fprintf(stderr, "Failed to open export for writing!\n"); fprintf(stderr, "Failed to open export for writing!\n");
@@ -52,31 +53,24 @@ maa_gpio_init(gpio_t *gpio, int pin)
fprintf(export_f, "%d", pin); fprintf(export_f, "%d", pin);
fclose(export_f); fclose(export_f);
} }
gpio->pin = pin; dev->pin = pin;
return MAA_SUCCESS; return dev;
}
int
maa_gpio_set(int pin)
{
//Stuff
return 0;
} }
void void
maa_gpio_mode(gpio_t *gpio, gpio_mode_t mode) maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
{ {
//gpio->pin //gpio->pin
} }
void void
maa_gpio_dir(gpio_t *gpio, gpio_dir_t dir) maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
{ {
if (gpio->value_fp != NULL) { if (dev->value_fp != NULL) {
gpio->value_fp = NULL; dev->value_fp = NULL;
} }
char filepath[64]; char filepath[64];
snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin); snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", dev->pin);
FILE *direction; FILE *direction;
if ((direction = fopen(filepath, "w")) == NULL) { if ((direction = fopen(filepath, "w")) == NULL) {
@@ -84,44 +78,44 @@ maa_gpio_dir(gpio_t *gpio, gpio_dir_t dir)
} else { } else {
fprintf(direction, dir); fprintf(direction, dir);
fclose(direction); fclose(direction);
gpio->value_fp = NULL; dev->value_fp = NULL;
} }
} }
int int
maa_gpio_read(gpio_t *gpio) maa_gpio_read(maa_gpio_context *dev)
{ {
if (gpio->value_fp == NULL) { if (dev->value_fp == NULL) {
maa_gpio_get_valfp(gpio); maa_gpio_get_valfp(dev);
} }
fseek(gpio->value_fp, SEEK_SET, 0); fseek(dev->value_fp, SEEK_SET, 0);
char buffer[2]; char buffer[2];
fread(buffer, 2, 1, gpio->value_fp); fread(buffer, 2, 1, dev->value_fp);
fseek(gpio->value_fp, SEEK_SET, 0); fseek(dev->value_fp, SEEK_SET, 0);
return atoi(buffer); return atoi(buffer);
} }
void void
maa_gpio_write(gpio_t *gpio, int value) maa_gpio_write(maa_gpio_context *dev, int value)
{ {
if (gpio->value_fp == NULL) { if (dev->value_fp == NULL) {
maa_gpio_get_valfp(gpio); maa_gpio_get_valfp(dev);
} }
fseek(gpio->value_fp, SEEK_SET, 0); fseek(dev->value_fp, SEEK_SET, 0);
fprintf(gpio->value_fp, "%d", value); fprintf(dev->value_fp, "%d", value);
fseek(gpio->value_fp, SEEK_SET, 0); fseek(dev->value_fp, SEEK_SET, 0);
} }
void void
maa_gpio_close(gpio_t *gpio) maa_gpio_close(maa_gpio_context *dev)
{ {
FILE *unexport_f; FILE *unexport_f;
if ((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) { if ((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
fprintf(stderr, "Failed to open unexport for writing!\n"); fprintf(stderr, "Failed to open unexport for writing!\n");
} else { } else {
fprintf(unexport_f, "%d", gpio->pin); fprintf(unexport_f, "%d", dev->pin);
fclose(unexport_f); fclose(unexport_f);
} }
} }

View File

@@ -25,13 +25,12 @@
#include "i2c.h" #include "i2c.h"
#include "smbus.h" #include "smbus.h"
maa_result_t maa_i2c_context*
maa_i2c_init(i2c_t* dev) maa_i2c_init()
{ {
// maa allocates the memory for *dev maa_i2c_context* dev = (maa_i2c_context*) malloc(sizeof(maa_i2c_context));
dev = malloc(sizeof *dev); if (dev == NULL)
if (!dev) return NULL;
return MAA_ERROR_NO_RESOURCES;
// Galileo only has one I2C master which should be /dev/i2c-0 // Galileo only has one I2C master which should be /dev/i2c-0
// reliability is a fickle friend! // reliability is a fickle friend!
@@ -42,19 +41,19 @@ maa_i2c_init(i2c_t* dev)
} }
void void
maa_i2c_frequency(i2c_t* dev, int hz) maa_i2c_frequency(maa_i2c_context* dev, int hz)
{ {
dev->hz = hz; dev->hz = hz;
} }
int int
maa_i2c_receive(i2c_t* dev) maa_i2c_receive(maa_i2c_context* dev)
{ {
return -1; return -1;
} }
int int
maa_i2c_read(i2c_t* dev, char *data, int length) maa_i2c_read(maa_i2c_context* dev, char *data, int length)
{ {
// this is the read(3) syscall not maa_i2c_read() // this is the read(3) syscall not maa_i2c_read()
if (read(dev->fh, data, length) == length) { if (read(dev->fh, data, length) == length) {
@@ -64,7 +63,7 @@ maa_i2c_read(i2c_t* dev, char *data, int length)
} }
int int
maa_i2c_read_byte(i2c_t* dev) maa_i2c_read_byte(maa_i2c_context* dev)
{ {
int byte; int byte;
byte = i2c_smbus_read_byte(dev->fh); byte = i2c_smbus_read_byte(dev->fh);
@@ -75,7 +74,7 @@ maa_i2c_read_byte(i2c_t* dev)
} }
int int
maa_i2c_write(i2c_t* dev, const char* data, int length) maa_i2c_write(maa_i2c_context* dev, const char* data, int length)
{ {
if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) { if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) {
fprintf(stderr, "Failed to write to I2CSlave slave\n"); fprintf(stderr, "Failed to write to I2CSlave slave\n");
@@ -85,7 +84,7 @@ maa_i2c_write(i2c_t* dev, const char* data, int length)
} }
int int
maa_i2c_write_byte(i2c_t* dev, int data) maa_i2c_write_byte(maa_i2c_context* dev, int data)
{ {
if (i2c_smbus_write_byte(dev->fh, data) < 0) { if (i2c_smbus_write_byte(dev->fh, data) < 0) {
fprintf(stderr, "Failed to write to I2CSlave slave\n"); fprintf(stderr, "Failed to write to I2CSlave slave\n");
@@ -95,7 +94,7 @@ maa_i2c_write_byte(i2c_t* dev, int data)
} }
void void
maa_i2c_address(i2c_t* dev, int addr) maa_i2c_address(maa_i2c_context* dev, int addr)
{ {
dev->addr = addr; dev->addr = addr;
if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) { if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
@@ -104,7 +103,7 @@ maa_i2c_address(i2c_t* dev, int addr)
} }
void void
maa_i2c_stop(i2c_t* dev) maa_i2c_stop(maa_i2c_context* dev)
{ {
free(dev); free(dev);
} }

View File

@@ -27,7 +27,7 @@
#include "pwm.h" #include "pwm.h"
static int static int
maa_pwm_setup_duty_fp(pwm_t* dev) maa_pwm_setup_duty_fp(maa_pwm_context* dev)
{ {
char bu[64]; char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin); sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin);
@@ -39,7 +39,7 @@ maa_pwm_setup_duty_fp(pwm_t* dev)
} }
static void static void
maa_pwm_write_period(pwm_t* dev, int period) maa_pwm_write_period(maa_pwm_context* dev, int period)
{ {
FILE *period_f; FILE *period_f;
char bu[64]; char bu[64];
@@ -53,7 +53,7 @@ maa_pwm_write_period(pwm_t* dev, int period)
} }
static void static void
maa_pwm_write_duty(pwm_t* dev, int duty) maa_pwm_write_duty(maa_pwm_context* dev, int duty)
{ {
if (dev->duty_fp == NULL) { if (dev->duty_fp == NULL) {
maa_pwm_setup_duty_fp(dev); maa_pwm_setup_duty_fp(dev);
@@ -64,7 +64,7 @@ maa_pwm_write_duty(pwm_t* dev, int duty)
} }
static int static int
maa_pwm_get_period(pwm_t* dev) maa_pwm_get_period(maa_pwm_context* dev)
{ {
FILE *period_f; FILE *period_f;
char bu[64]; char bu[64];
@@ -81,7 +81,7 @@ maa_pwm_get_period(pwm_t* dev)
} }
static int static int
maa_pwm_get_duty(pwm_t* dev) maa_pwm_get_duty(maa_pwm_context* dev)
{ {
if (dev->duty_fp == NULL) { if (dev->duty_fp == NULL) {
maa_pwm_setup_duty_fp(dev); maa_pwm_setup_duty_fp(dev);
@@ -92,12 +92,13 @@ maa_pwm_get_duty(pwm_t* dev)
return atoi(output); return atoi(output);
} }
maa_result_t maa_pwm_context*
maa_pwm_init(pwm_t* dev, int chipin, int pin) maa_pwm_init(int chipin, int pin)
{ {
dev = malloc(sizeof *dev); maa_pwm_context* dev = (maa_pwm_context*) malloc(sizeof(maa_pwm_context));
if (!dev) if (dev == NULL)
return MAA_ERROR_NO_RESOURCES; return NULL;
dev->chipid = chipin; dev->chipid = chipin;
dev->pin = pin; dev->pin = pin;
@@ -107,67 +108,67 @@ maa_pwm_init(pwm_t* dev, int chipin, int pin)
if ((export_f = fopen(buffer, "w")) == NULL) { if ((export_f = fopen(buffer, "w")) == NULL) {
fprintf(stderr, "Failed to open export for writing!\n"); fprintf(stderr, "Failed to open export for writing!\n");
return MAA_ERROR_INVALID_HANDLE; free(dev);
return NULL;
} else { } else {
fprintf(export_f, "%d", dev->pin); fprintf(export_f, "%d", dev->pin);
fclose(export_f); fclose(export_f);
maa_pwm_setup_duty_fp(dev); maa_pwm_setup_duty_fp(dev);
} }
return dev;
return MAA_SUCCESS;
} }
void void
maa_pwm_write(pwm_t* dev, float percentage) maa_pwm_write(maa_pwm_context* dev, float percentage)
{ {
maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev)); maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
} }
float float
maa_pwm_read(pwm_t* dev) maa_pwm_read(maa_pwm_context* dev)
{ {
float output = maa_pwm_get_duty(dev) / (float) maa_pwm_get_period(dev); float output = maa_pwm_get_duty(dev) / (float) maa_pwm_get_period(dev);
return output; return output;
} }
void void
maa_pwm_period(pwm_t* dev, float seconds) maa_pwm_period(maa_pwm_context* dev, float seconds)
{ {
maa_pwm_period_ms(dev, seconds*1000); maa_pwm_period_ms(dev, seconds*1000);
} }
void void
maa_pwm_period_ms(pwm_t* dev, int ms) maa_pwm_period_ms(maa_pwm_context* dev, int ms)
{ {
maa_pwm_period_us(dev, ms*1000); maa_pwm_period_us(dev, ms*1000);
} }
void void
maa_pwm_period_us(pwm_t* dev, int us) maa_pwm_period_us(maa_pwm_context* dev, int us)
{ {
maa_pwm_write_period(dev, us*1000); maa_pwm_write_period(dev, us*1000);
} }
void void
maa_pwm_pulsewidth(pwm_t* dev, float seconds) maa_pwm_pulsewidth(maa_pwm_context* dev, float seconds)
{ {
maa_pwm_pulsewidth_ms(dev, seconds*1000); maa_pwm_pulsewidth_ms(dev, seconds*1000);
} }
void void
maa_pwm_pulsewidth_ms(pwm_t* dev, int ms) maa_pwm_pulsewidth_ms(maa_pwm_context* dev, int ms)
{ {
maa_pwm_pulsewidth_us(dev, ms*1000); maa_pwm_pulsewidth_us(dev, ms*1000);
} }
void void
maa_pwm_pulsewidth_us(pwm_t* dev, int us) maa_pwm_pulsewidth_us(maa_pwm_context* dev, int us)
{ {
maa_pwm_write_duty(dev, us*1000); maa_pwm_write_duty(dev, us*1000);
} }
void void
maa_pwm_enable(pwm_t* dev, int enable) maa_pwm_enable(maa_pwm_context* dev, int enable)
{ {
int status; int status;
if (enable != 0) { if (enable != 0) {
@@ -189,7 +190,7 @@ maa_pwm_enable(pwm_t* dev, int enable)
} }
void void
maa_pwm_close(pwm_t* dev) maa_pwm_close(maa_pwm_context* dev)
{ {
maa_pwm_enable(dev, 0); maa_pwm_enable(dev, 0);
FILE *unexport_f; FILE *unexport_f;