From: Brendan Le Foll Date: Sun, 27 Apr 2014 22:11:31 +0000 (+0100) Subject: maa: change the linking of maa and make gpio functions match maa_ spec X-Git-Tag: v0.2.0~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ed0a1f60853ea209ed7918843e83870799ca6899;p=contrib%2Fmraa.git maa: change the linking of maa and make gpio functions match maa_ spec Signed-off-by: Brendan Le Foll --- diff --git a/api/gpio.h b/api/gpio.h index e3035da..379d02b 100644 --- a/api/gpio.h +++ b/api/gpio.h @@ -20,8 +20,9 @@ #include -typedef struct gpio_struct -{ +#include "maa.h" + +typedef struct { int pin; int pinMap; char path[64]; @@ -31,11 +32,11 @@ typedef struct gpio_struct typedef char gpio_mode_t[16]; typedef char gpio_dir_t[16]; -void gpio_init(gpio_t *gpio, int pin); -int gpio_set(int pin); -void gpio_mode(gpio_t *gpio, gpio_mode_t mode); -void gpio_dir(gpio_t *gpio, gpio_dir_t dir); +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); -void gpio_close(gpio_t *gpio); -int gpio_read(gpio_t *gpio); -void gpio_write(gpio_t *gpio, int value); +void maa_gpio_close(gpio_t *gpio); +int maa_gpio_read(gpio_t *gpio); +void maa_gpio_write(gpio_t *gpio, int value); diff --git a/api/i2c.h b/api/i2c.h index c9f806b..af7396d 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -21,6 +21,8 @@ #include #include #include + +#include "maa.h" #include "gpio.h" typedef struct { @@ -30,7 +32,7 @@ typedef struct { gpio_t gpio; } i2c_t; -int maa_i2c_init(i2c_t* dev); +maa_result_t maa_i2c_init(i2c_t* dev); /** Set the frequency of the I2C interface * diff --git a/api/maa.h b/api/maa.h index f6d90a3..f430a93 100644 --- a/api/maa.h +++ b/api/maa.h @@ -24,10 +24,21 @@ #pragma once -#include "i2c.h" -#include "gpio.h" -#include "pwm.h" - #define MAA_LIBRARY_VERSION 1 -int maa_get_version(); +typedef enum { + MAA_SUCCESS = 0, + MAA_ERROR_FEATURE_NOT_IMPLEMENTED = 1, + MAA_ERROR_FEATURE_NOT_SUPPORTED = 2, + MAA_ERROR_INVALID_VERBOSITY_LEVEL = 3, + MAA_ERROR_INVALID_PARAMETER = 4, + MAA_ERROR_INVALID_HANDLE = 5, + MAA_ERROR_NO_RESOURCES = 6, + MAA_ERROR_INVALID_RESOURCE = 7, + MAA_ERROR_INVALID_QUEUE_TYPE = 8, + MAA_ERROR_NO_DATA_AVAILABLE = 9, + + MAA_ERROR_UNSPECIFIED = 99 +} maa_result_t; + +maa_result_t maa_get_version(); diff --git a/api/pwm.h b/api/pwm.h index e3fe6a8..aed83b5 100644 --- a/api/pwm.h +++ b/api/pwm.h @@ -21,12 +21,14 @@ #include #include +#include "maa.h" + typedef struct { int chipid, pin; FILE *duty_fp; } pwm_t; -int maa_pwm_init(pwm_t* pwm, int chipin, int pin); +maa_result_t maa_pwm_init(pwm_t* pwm, int chipin, int pin); /** Set the ouput duty-cycle percentage, as a float * diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index f281341..9da7162 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,9 +1,11 @@ add_executable (i2c_HMC5883L i2c_HMC5883L.c) add_executable (hellomaa hellomaa.c) add_executable (cycle-pwm3 cycle-pwm3.c) +add_executable (blink-io8 blink-io8.c) include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include) target_link_libraries (hellomaa maa) target_link_libraries (i2c_HMC5883L maa m) target_link_libraries (cycle-pwm3 maa) +target_link_libraries (blink-io8 maa) diff --git a/examples/blink-io8.c b/examples/blink-io8.c index b24413d..63a7cd0 100644 --- a/examples/blink-io8.c +++ b/examples/blink-io8.c @@ -24,20 +24,20 @@ #include "stdio.h" -#include "maa.h" +#include "gpio.h" int main(int argc, char **argv) { - fprintf(stdout, "MAA Version: %d\n Starting Blinking on IO8", get_version()); + fprintf(stdout, "MAA Version: %d\n Starting Blinking on IO8", maa_get_version()); gpio_t gpio; - gpio_init(&gpio, 26); - gpio_dir(&gpio, "out"); + maa_gpio_init(&gpio, 26); + maa_gpio_dir(&gpio, "out"); while (1){ - gpio_write(&gpio, 0); + maa_gpio_write(&gpio, 0); sleep(1); - 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 d853947..2330eca 100644 --- a/examples/cycle-pwm3.c +++ b/examples/cycle-pwm3.c @@ -24,7 +24,7 @@ #include -#include "maa.h" +#include "pwm.h" int main () diff --git a/examples/i2c_HMC5883L.c b/examples/i2c_HMC5883L.c index e474ff4..a59f447 100644 --- a/examples/i2c_HMC5883L.c +++ b/examples/i2c_HMC5883L.c @@ -22,7 +22,7 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "maa.h" +#include "i2c.h" #include "math.h" #define MAX_BUFFER_LENGTH 6 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1ecb7d6..26c1975 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,9 +5,9 @@ include_directories( set (maa_LIB_HEADERS ${PROJECT_SOURCE_DIR}/api/maa.h + ${PROJECT_SOURCE_DIR}/api/gpio.h ${PROJECT_SOURCE_DIR}/api/i2c.h ${PROJECT_SOURCE_DIR}/api/pwm.h - ${PROJECT_SOURCE_DIR}/api/gpio.h ${PROJECT_SOURCE_DIR}/include/smbus.hpp ${PROJECT_SOURCE_DIR}/include/smbus.h ) @@ -21,9 +21,9 @@ set (maa_LIB_KERNEL set (maa_LIB_SRCS ${PROJECT_SOURCE_DIR}/src/maa.c + ${PROJECT_SOURCE_DIR}/src/gpio/gpio.c ${PROJECT_SOURCE_DIR}/src/i2c/i2c.c ${PROJECT_SOURCE_DIR}/src/i2c/smbus.c - ${PROJECT_SOURCE_DIR}/src/gpio/gpio.c ${PROJECT_SOURCE_DIR}/src/pwm/pwm.c ) diff --git a/src/gpio/gpio.c b/src/gpio/gpio.c index ed7a7fb..41a5708 100644 --- a/src/gpio/gpio.c +++ b/src/gpio/gpio.c @@ -29,60 +29,57 @@ #include "gpio.h" -#ifdef __cplusplus -extern "C" { -#endif - static int -gpio_get_valfp(gpio_t *gpio) +maa_gpio_get_valfp(gpio_t *gpio) { char bu[64]; sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin); - if((gpio->value_fp = fopen(bu, "r+b")) == NULL) { + if ((gpio->value_fp = fopen(bu, "r+b")) == NULL) { return 1; } return 0; } -void -gpio_init(gpio_t *gpio, int pin) +maa_result_t +maa_gpio_init(gpio_t *gpio, int pin) { FILE *export_f; - 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"); } else { fprintf(export_f, "%d", pin); fclose(export_f); } gpio->pin = pin; + return MAA_SUCCESS; } int -gpio_set(int pin) +maa_gpio_set(int pin) { //Stuff return 0; } void -gpio_mode(gpio_t *gpio, gpio_mode_t mode) +maa_gpio_mode(gpio_t *gpio, gpio_mode_t mode) { //gpio->pin } void -gpio_dir(gpio_t *gpio, gpio_dir_t dir) +maa_gpio_dir(gpio_t *gpio, gpio_dir_t dir) { - if(gpio->value_fp != NULL) { + if (gpio->value_fp != NULL) { gpio->value_fp = NULL; } char filepath[64]; snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin); FILE *direction; - if((direction = fopen(filepath, "w")) == NULL) { + if ((direction = fopen(filepath, "w")) == NULL) { fprintf(stderr, "Failed to open direction for writing!\n"); } else { fprintf(direction, dir); @@ -92,10 +89,10 @@ gpio_dir(gpio_t *gpio, gpio_dir_t dir) } int -gpio_read(gpio_t *gpio) +maa_gpio_read(gpio_t *gpio) { - if(gpio->value_fp == NULL) { - gpio_get_valfp(gpio); + if (gpio->value_fp == NULL) { + maa_gpio_get_valfp(gpio); } fseek(gpio->value_fp, SEEK_SET, 0); char buffer[2]; @@ -105,10 +102,10 @@ gpio_read(gpio_t *gpio) } void -gpio_write(gpio_t *gpio, int value) +maa_gpio_write(gpio_t *gpio, int value) { - if(gpio->value_fp == NULL) { - gpio_get_valfp(gpio); + if (gpio->value_fp == NULL) { + maa_gpio_get_valfp(gpio); } fseek(gpio->value_fp, SEEK_SET, 0); fprintf(gpio->value_fp, "%d", value); @@ -117,18 +114,14 @@ gpio_write(gpio_t *gpio, int value) } void -gpio_close(gpio_t *gpio) +maa_gpio_close(gpio_t *gpio) { 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"); } else { fprintf(unexport_f, "%d", gpio->pin); fclose(unexport_f); } } - -#ifdef __cplusplus -} -#endif diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index a14bf40..db32484 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -25,16 +25,20 @@ #include "i2c.h" #include "smbus.h" -int +maa_result_t maa_i2c_init(i2c_t* dev) { // maa allocates the memory for *dev dev = malloc(sizeof *dev); + if (!dev) + return MAA_ERROR_NO_RESOURCES; + // Galileo only has one I2C master which should be /dev/i2c-0 // reliability is a fickle friend! if ((dev->fh = open("/dev/i2c-0", O_RDWR)) < 1) { fprintf(stderr, "Failed to open requested i2c port"); } + return MAA_SUCCESS; } void diff --git a/src/maa.c b/src/maa.c index 3f93634..1c85a7f 100644 --- a/src/maa.c +++ b/src/maa.c @@ -24,7 +24,7 @@ #include "maa.h" -int +maa_result_t maa_get_version() { return MAA_LIBRARY_VERSION; diff --git a/src/maa.i b/src/maa.i index 0a7e1b2..9b55009 100644 --- a/src/maa.i +++ b/src/maa.i @@ -1,8 +1,11 @@ %{ #include "maa.h" + #include "gpio.h" + #include "pwm.h" + #include "i2c.h" %} %include "maa.h" -%include "i2c.h" %include "gpio.h" %include "pwm.h" +%include "i2c.h" diff --git a/src/pwm/pwm.c b/src/pwm/pwm.c index 1795e34..05e8380 100644 --- a/src/pwm/pwm.c +++ b/src/pwm/pwm.c @@ -91,10 +91,13 @@ maa_pwm_get_duty(pwm_t* dev) fseek(dev->duty_fp, SEEK_SET, 0); return atoi(output); } -int + +maa_result_t maa_pwm_init(pwm_t* dev, int chipin, int pin) { dev = malloc(sizeof *dev); + if (!dev) + return MAA_ERROR_NO_RESOURCES; dev->chipid = chipin; dev->pin = pin; @@ -104,13 +107,14 @@ 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; } else { fprintf(export_f, "%d", dev->pin); fclose(export_f); maa_pwm_setup_duty_fp(dev); } - return 0; + return MAA_SUCCESS; } void