From f280b3c0f6888b53c018ab09ccc06ded57bdf177 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Mon, 28 Apr 2014 11:31:53 +0100 Subject: [PATCH] maa: change struct names to be more unique and fix allocs Signed-off-by: Brendan Le Foll --- api/gpio.h | 15 +++++----- api/i2c.h | 24 ++++++++-------- api/pwm.h | 24 ++++++++-------- examples/blink-io8.c | 15 +++++----- examples/cycle-pwm3.c | 17 ++++++----- examples/i2c_HMC5883L.c | 22 +++++++-------- src/gpio/gpio.c | 62 +++++++++++++++++++---------------------- src/i2c/i2c.c | 27 +++++++++--------- src/pwm/pwm.c | 47 ++++++++++++++++--------------- 9 files changed, 125 insertions(+), 128 deletions(-) diff --git a/api/gpio.h b/api/gpio.h index 379d02b..190b60c 100644 --- a/api/gpio.h +++ b/api/gpio.h @@ -27,16 +27,15 @@ typedef struct { int pinMap; char path[64]; FILE *value_fp; -} gpio_t; +} maa_gpio_context; typedef char gpio_mode_t[16]; typedef char gpio_dir_t[16]; -maa_result_t maa_gpio_init(gpio_t *gpio, int pin); -int maa_gpio_set(int pin); -void maa_gpio_mode(gpio_t *gpio, gpio_mode_t mode); -void maa_gpio_dir(gpio_t *gpio, gpio_dir_t dir); +maa_gpio_context* maa_gpio_init(int pin); +void maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode); +void maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir); -void maa_gpio_close(gpio_t *gpio); -int maa_gpio_read(gpio_t *gpio); -void maa_gpio_write(gpio_t *gpio, int value); +void maa_gpio_close(maa_gpio_context *dev); +int maa_gpio_read(maa_gpio_context *dev); +void maa_gpio_write(maa_gpio_context *dev, int value); diff --git a/api/i2c.h b/api/i2c.h index af7396d..a4332af 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -29,16 +29,16 @@ typedef struct { int hz; int fh; int addr; - gpio_t gpio; -} i2c_t; + maa_gpio_context gpio; +} 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 * * @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. * @@ -49,7 +49,7 @@ void maa_i2c_frequency(i2c_t* dev, int hz); * - WriteAddressed - the master is writing to this 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. * @@ -60,14 +60,14 @@ int maa_i2c_receive(i2c_t* dev); * 0 on success, * 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. * * @returns * 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. * @@ -78,7 +78,7 @@ int maa_i2c_read_byte(i2c_t* dev); * 0 on success, * 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. * @@ -88,7 +88,7 @@ int maa_i2c_write(i2c_t* dev, const char *data, int length); * '1' if an ACK was received, * '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. * @@ -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 * 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); diff --git a/api/pwm.h b/api/pwm.h index aed83b5..2dbdf51 100644 --- a/api/pwm.h +++ b/api/pwm.h @@ -26,9 +26,9 @@ typedef struct { int chipid, pin; 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 * @@ -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 * 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 * @@ -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 * 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 * * @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. * @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 * @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). * @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 * @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. * @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. * and 0 will disable the output. * @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. */ -void maa_pwm_close(pwm_t* pwm); +void maa_pwm_close(maa_pwm_context* pwm); diff --git a/examples/blink-io8.c b/examples/blink-io8.c index 63a7cd0..c3afa19 100644 --- a/examples/blink-io8.c +++ b/examples/blink-io8.c @@ -29,15 +29,16 @@ int main(int argc, char **argv) { - fprintf(stdout, "MAA Version: %d\n Starting Blinking on IO8", maa_get_version()); - gpio_t gpio; - maa_gpio_init(&gpio, 26); - maa_gpio_dir(&gpio, "out"); + fprintf(stdout, "MAA Version: %d\nStarting Blinking on IO8\n", + maa_get_version()); + maa_gpio_context* gpio; + gpio = maa_gpio_init(26); + maa_gpio_dir(gpio, "out"); - while (1){ - maa_gpio_write(&gpio, 0); + while (1) { + maa_gpio_write(gpio, 0); sleep(1); - maa_gpio_write(&gpio, 1); + maa_gpio_write(gpio, 1); sleep(1); } return 0; diff --git a/examples/cycle-pwm3.c b/examples/cycle-pwm3.c index 2330eca..f716cf9 100644 --- a/examples/cycle-pwm3.c +++ b/examples/cycle-pwm3.c @@ -29,21 +29,24 @@ int main () { - pwm_t pwm; - maa_pwm_init(&pwm, 0, 3); - maa_pwm_period_us(&pwm, 200); - maa_pwm_enable(&pwm, 1); + maa_pwm_context* pwm; + pwm = maa_pwm_init(0, 3); + if (pwm == NULL) { + return 1; + } + maa_pwm_period_us(pwm, 200); + maa_pwm_enable(pwm, 1); float value = 0.0f; - while(1) { + while (1) { value = value + 0.01f; - maa_pwm_write(&pwm, value); + maa_pwm_write(pwm, value); usleep(50000); if (value >= 1.0f) { value = 0.0f; } - float output = maa_pwm_read(&pwm); + float output = maa_pwm_read(pwm); } return 0; } diff --git a/examples/i2c_HMC5883L.c b/examples/i2c_HMC5883L.c index a59f447..653c550 100644 --- a/examples/i2c_HMC5883L.c +++ b/examples/i2c_HMC5883L.c @@ -76,31 +76,31 @@ #define SCALE_4_35_MG 4.35 int -main () +main(int argc, char **argv) { float direction = 0; int16_t x = 0, y = 0, z = 0; char rx_tx_buf[MAX_BUFFER_LENGTH]; - i2c_t i2c; - maa_i2c_init(&i2c); + maa_i2c_context *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[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[1] = HMC5883L_CONT_MODE; - maa_i2c_write(&i2c, rx_tx_buf, 2); + maa_i2c_write(i2c, rx_tx_buf, 2); for(;;) { - maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); - maa_i2c_write_byte(&i2c, HMC5883L_DATA_REG); + maa_i2c_address(i2c, HMC5883L_I2C_ADDR); + maa_i2c_write_byte(i2c, HMC5883L_DATA_REG); - maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); - maa_i2c_read(&i2c, rx_tx_buf, DATA_REG_SIZE); + maa_i2c_address(i2c, HMC5883L_I2C_ADDR); + 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] ; z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ; diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index 41a5708..93d1415 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -30,21 +30,22 @@ #include "gpio.h" static int -maa_gpio_get_valfp(gpio_t *gpio) +maa_gpio_get_valfp(maa_gpio_context *dev) { 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 0; } -maa_result_t -maa_gpio_init(gpio_t *gpio, int pin) +maa_gpio_context* +maa_gpio_init(int pin) { 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) { 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); fclose(export_f); } - gpio->pin = pin; - return MAA_SUCCESS; -} - -int -maa_gpio_set(int pin) -{ - //Stuff - return 0; + dev->pin = pin; + return dev; } void -maa_gpio_mode(gpio_t *gpio, gpio_mode_t mode) +maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode) { //gpio->pin } 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) { - gpio->value_fp = NULL; + if (dev->value_fp != NULL) { + dev->value_fp = NULL; } 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; if ((direction = fopen(filepath, "w")) == NULL) { @@ -84,44 +78,44 @@ maa_gpio_dir(gpio_t *gpio, gpio_dir_t dir) } else { fprintf(direction, dir); fclose(direction); - gpio->value_fp = NULL; + dev->value_fp = NULL; } } int -maa_gpio_read(gpio_t *gpio) +maa_gpio_read(maa_gpio_context *dev) { - if (gpio->value_fp == NULL) { - maa_gpio_get_valfp(gpio); + if (dev->value_fp == NULL) { + maa_gpio_get_valfp(dev); } - fseek(gpio->value_fp, SEEK_SET, 0); + fseek(dev->value_fp, SEEK_SET, 0); char buffer[2]; - fread(buffer, 2, 1, gpio->value_fp); - fseek(gpio->value_fp, SEEK_SET, 0); + fread(buffer, 2, 1, dev->value_fp); + fseek(dev->value_fp, SEEK_SET, 0); return atoi(buffer); } void -maa_gpio_write(gpio_t *gpio, int value) +maa_gpio_write(maa_gpio_context *dev, int value) { - if (gpio->value_fp == NULL) { - maa_gpio_get_valfp(gpio); + if (dev->value_fp == NULL) { + maa_gpio_get_valfp(dev); } - fseek(gpio->value_fp, SEEK_SET, 0); - fprintf(gpio->value_fp, "%d", value); - fseek(gpio->value_fp, SEEK_SET, 0); + fseek(dev->value_fp, SEEK_SET, 0); + fprintf(dev->value_fp, "%d", value); + fseek(dev->value_fp, SEEK_SET, 0); } void -maa_gpio_close(gpio_t *gpio) +maa_gpio_close(maa_gpio_context *dev) { FILE *unexport_f; if ((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) { fprintf(stderr, "Failed to open unexport for writing!\n"); } else { - fprintf(unexport_f, "%d", gpio->pin); + fprintf(unexport_f, "%d", dev->pin); fclose(unexport_f); } } diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index 10d2d92..400c39c 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -25,13 +25,12 @@ #include "i2c.h" #include "smbus.h" -maa_result_t -maa_i2c_init(i2c_t* dev) +maa_i2c_context* +maa_i2c_init() { - // maa allocates the memory for *dev - dev = malloc(sizeof *dev); - if (!dev) - return MAA_ERROR_NO_RESOURCES; + maa_i2c_context* dev = (maa_i2c_context*) malloc(sizeof(maa_i2c_context)); + if (dev == NULL) + return NULL; // Galileo only has one I2C master which should be /dev/i2c-0 // reliability is a fickle friend! @@ -42,19 +41,19 @@ maa_i2c_init(i2c_t* dev) } void -maa_i2c_frequency(i2c_t* dev, int hz) +maa_i2c_frequency(maa_i2c_context* dev, int hz) { dev->hz = hz; } int -maa_i2c_receive(i2c_t* dev) +maa_i2c_receive(maa_i2c_context* dev) { return -1; } 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() if (read(dev->fh, data, length) == length) { @@ -64,7 +63,7 @@ maa_i2c_read(i2c_t* dev, char *data, int length) } int -maa_i2c_read_byte(i2c_t* dev) +maa_i2c_read_byte(maa_i2c_context* dev) { int byte; byte = i2c_smbus_read_byte(dev->fh); @@ -75,7 +74,7 @@ maa_i2c_read_byte(i2c_t* dev) } 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) { 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 -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) { fprintf(stderr, "Failed to write to I2CSlave slave\n"); @@ -95,7 +94,7 @@ maa_i2c_write_byte(i2c_t* dev, int data) } void -maa_i2c_address(i2c_t* dev, int addr) +maa_i2c_address(maa_i2c_context* dev, int addr) { dev->addr = addr; if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) { @@ -104,7 +103,7 @@ maa_i2c_address(i2c_t* dev, int addr) } void -maa_i2c_stop(i2c_t* dev) +maa_i2c_stop(maa_i2c_context* dev) { free(dev); } diff --git a/src/pwm/pwm.c b/src/pwm/pwm.c index 05e8380..9ed1528 100644 --- a/src/pwm/pwm.c +++ b/src/pwm/pwm.c @@ -27,7 +27,7 @@ #include "pwm.h" static int -maa_pwm_setup_duty_fp(pwm_t* 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 +39,7 @@ maa_pwm_setup_duty_fp(pwm_t* dev) } static void -maa_pwm_write_period(pwm_t* dev, int period) +maa_pwm_write_period(maa_pwm_context* dev, int period) { FILE *period_f; char bu[64]; @@ -53,7 +53,7 @@ maa_pwm_write_period(pwm_t* dev, int period) } 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) { maa_pwm_setup_duty_fp(dev); @@ -64,7 +64,7 @@ maa_pwm_write_duty(pwm_t* dev, int duty) } static int -maa_pwm_get_period(pwm_t* dev) +maa_pwm_get_period(maa_pwm_context* dev) { FILE *period_f; char bu[64]; @@ -81,7 +81,7 @@ maa_pwm_get_period(pwm_t* dev) } static int -maa_pwm_get_duty(pwm_t* dev) +maa_pwm_get_duty(maa_pwm_context* dev) { if (dev->duty_fp == NULL) { maa_pwm_setup_duty_fp(dev); @@ -92,12 +92,13 @@ maa_pwm_get_duty(pwm_t* dev) return atoi(output); } -maa_result_t -maa_pwm_init(pwm_t* dev, int chipin, int pin) +maa_pwm_context* +maa_pwm_init(int chipin, int pin) { - dev = malloc(sizeof *dev); - if (!dev) - return MAA_ERROR_NO_RESOURCES; + maa_pwm_context* dev = (maa_pwm_context*) malloc(sizeof(maa_pwm_context)); + if (dev == NULL) + return NULL; + dev->chipid = chipin; dev->pin = pin; @@ -107,67 +108,67 @@ maa_pwm_init(pwm_t* dev, int chipin, int pin) if ((export_f = fopen(buffer, "w")) == NULL) { fprintf(stderr, "Failed to open export for writing!\n"); - return MAA_ERROR_INVALID_HANDLE; + free(dev); + return NULL; } else { fprintf(export_f, "%d", dev->pin); fclose(export_f); maa_pwm_setup_duty_fp(dev); } - - return MAA_SUCCESS; + return dev; } 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)); } 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); return output; } 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); } 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); } 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); } 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); } 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); } 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); } void -maa_pwm_enable(pwm_t* dev, int enable) +maa_pwm_enable(maa_pwm_context* dev, int enable) { int status; if (enable != 0) { @@ -189,7 +190,7 @@ maa_pwm_enable(pwm_t* dev, int enable) } void -maa_pwm_close(pwm_t* dev) +maa_pwm_close(maa_pwm_context* dev) { maa_pwm_enable(dev, 0); FILE *unexport_f;