#include <stdio.h>
-typedef struct gpio_struct
-{
+#include "maa.h"
+
+typedef struct {
int pin;
int pinMap;
char path[64];
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);
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
+
+#include "maa.h"
#include "gpio.h"
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
*
#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();
#include <stdio.h>
#include <fcntl.h>
+#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
*
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)
#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;
#include <unistd.h>
-#include "maa.h"
+#include "pwm.h"
int
main ()
* 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
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
)
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
)
#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);
}
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];
}
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);
}
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
#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
#include "maa.h"
-int
+maa_result_t
maa_get_version()
{
return MAA_LIBRARY_VERSION;
%{
#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"
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;
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