Private
Public Access
2
0

MAA version 0.2.0 moves to a standard C API

* Removed all C++ code and renamed all .cxx extensions to .c
* All functions are renamed to maa_ and modules are for example called maa_pwm
* Cmake can now 'make doc' using a Doxyfile.in to create documentation
* examples/ have been updated but swig generated API is untested

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-04-27 21:17:54 +01:00
parent 5a270191b5
commit a5a407e4b5
23 changed files with 389 additions and 736 deletions

View File

@@ -5,7 +5,6 @@ include_directories(
set (maa_LIB_HEADERS
${PROJECT_SOURCE_DIR}/api/maa.h
${PROJECT_SOURCE_DIR}/api/i2cslave.h
${PROJECT_SOURCE_DIR}/api/i2c.h
${PROJECT_SOURCE_DIR}/api/pwm.h
${PROJECT_SOURCE_DIR}/api/gpio.h
@@ -21,12 +20,11 @@ set (maa_LIB_KERNEL
)
set (maa_LIB_SRCS
${PROJECT_SOURCE_DIR}/src/maa.cxx
${PROJECT_SOURCE_DIR}/src/i2c/i2c.cxx
${PROJECT_SOURCE_DIR}/src/i2c/i2cslave.cxx
${PROJECT_SOURCE_DIR}/src/maa.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.cxx
${PROJECT_SOURCE_DIR}/src/pwm/pwm.c
)
add_library (maa SHARED ${maa_LIB_SRCS})

View File

@@ -22,55 +22,57 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "i2cslave.h"
#include "i2c.h"
#include "smbus.h"
using namespace maa;
I2CSlave::I2CSlave(unsigned int sda, unsigned int scl)
int
maa_i2c_init(i2c_t* dev)
{
// maa allocates the memory for *dev
dev = malloc(sizeof *dev);
// Galileo only has one I2C master which should be /dev/i2c-0
// reliability is a fickle friend!
if ((i2c_handle = open("/dev/i2c-0", O_RDWR)) < 1) {
if ((dev->fh = open("/dev/i2c-0", O_RDWR)) < 1) {
fprintf(stderr, "Failed to open requested i2c port");
}
}
void
I2CSlave::frequency(int hz)
maa_i2c_frequency(i2c_t* dev, int hz)
{
_hz = hz;
dev->hz = hz;
}
int
I2CSlave::receive(void)
maa_i2c_receive(i2c_t* dev)
{
return -1;
}
int
I2CSlave::read(char *data, int length)
maa_i2c_read(i2c_t* dev, char *data, int length)
{
// this is the read(3) syscall not I2CSlave::read()
if (::read(i2c_handle, data, length) == length) {
// this is the read(3) syscall not maa_i2c_read()
if (read(dev->fh, data, length) == length) {
return length;
}
return -1;
}
int
I2CSlave::read(void)
maa_i2c_read_byte(i2c_t* dev)
{
int byte;
if (byte = i2c_smbus_read_byte(i2c_handle) < 0) {
if (byte = i2c_smbus_read_byte(dev->fh) < 0) {
return -1;
}
return byte;
}
int
I2CSlave::write(const char *data, int length)
maa_i2c_write(i2c_t* dev, const char* data, int length)
{
if (i2c_smbus_write_i2c_block_data(i2c_handle, 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");
return -1;
}
@@ -78,9 +80,9 @@ I2CSlave::write(const char *data, int length)
}
int
I2CSlave::write(int data)
maa_i2c_write_byte(i2c_t* dev, int data)
{
if (i2c_smbus_write_byte(i2c_handle, data) < 0) {
if (i2c_smbus_write_byte(dev->fh, data) < 0) {
fprintf(stderr, "Failed to write to I2CSlave slave\n");
return -1;
}
@@ -88,15 +90,16 @@ I2CSlave::write(int data)
}
void
I2CSlave::address(int addr)
maa_i2c_address(i2c_t* dev, int addr)
{
_addr = addr;
if (ioctl(i2c_handle, I2C_SLAVE_FORCE, addr) < 0) {
dev->addr = addr;
if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) {
fprintf(stderr, "Failed to set slave address %d\n", addr);
}
}
void
I2CSlave::stop()
maa_i2c_stop(i2c_t* dev)
{
free(dev);
}

View File

@@ -1,93 +0,0 @@
/*
* Author: Brendan Le Foll
* 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.
*/
#include "i2c.h"
using namespace maa;
I2C::I2C(unsigned int sda, unsigned int scl)
{
// Galileo only has one I2C device which is always /dev/i2c-0
// reliability is a fickle friend!
if (i2c_handle = open("/dev/i2c-0", O_RDWR) < 1) {
fprintf(stderr, "Failed to open requested i2c port");
}
}
void
I2C::frequency(int hz)
{
_hz = hz;
}
int
I2C::read(int address, char *data, int length, bool repeated)
{
return 0;
}
int
I2C::read(int ack)
{
int byte;
if (byte = i2c_smbus_read_byte(i2c_handle) < 0) {
return -1;
}
return byte;
}
int
I2C::write(int address, const char *data, int length, bool repeated)
{
if (i2c_smbus_write_i2c_block_data(i2c_handle, data[0], length, (uint8_t*) data) < 0) {
fprintf(stderr, "Failed to write to I2C slave\n");
return -1;
}
return 0;
}
int
I2C::write(int data)
{
if (i2c_smbus_write_byte(i2c_handle, data) < 0) {
fprintf(stderr, "Failed to write to I2C slave\n");
return -1;
}
return 0;
}
void
I2C::start()
{
}
void
I2C::stop()
{
}
void
I2C::aquire()
{
}

View File

@@ -13,7 +13,6 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/..
)
SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(maajs.i PROPERTIES SWIG_FLAGS "-node")
SWIG_ADD_MODULE(maajs javascript ${maa_LIB_SRCS})

View File

@@ -24,19 +24,8 @@
#include "maa.h"
using namespace maa;
int
get_version()
maa_get_version()
{
return MAA_LIBRARY_VERSION;
}
int
make_a_conn()
{
maa::I2C i2c(28, 27);
int addr = 0x62;
char data[2];
i2c.read(addr, data, 2);
}

View File

@@ -4,6 +4,5 @@
%include "maa.h"
%include "i2c.h"
%include "i2cslave.h"
%include "gpio.h"
%include "pwm.h"

View File

@@ -8,4 +8,4 @@ Description: Low Level Skeleton Library for Communication
Version: @maa_VERSION_STRING@
Libs: -L${libdir} -lmaa
Cflags: -I${includedir}/maa -lpthread
Cflags: -I${includedir}/maa

202
src/pwm/pwm.c Normal file
View File

@@ -0,0 +1,202 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@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.
*/
#include <stdlib.h>
#include "pwm.h"
static int
maa_pwm_setup_duty_fp(pwm_t* dev)
{
char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", dev->chipid, dev->pin);
if ((dev->duty_fp = fopen(bu, "r+b")) == NULL) {
return 1;
}
return 0;
}
static void
maa_pwm_write_period(pwm_t* dev, int period)
{
FILE *period_f;
char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
if ((period_f = fopen(bu, "r+b")) == NULL) {
fprintf(stderr, "Failed to open period for writing!\n");
}
fprintf(period_f, "%d", period);
fclose(period_f);
}
static void
maa_pwm_write_duty(pwm_t* dev, int duty)
{
if (dev->duty_fp == NULL) {
maa_pwm_setup_duty_fp(dev);
}
fprintf(dev->duty_fp, "%d", duty);
rewind(dev->duty_fp);
fflush(dev->duty_fp);
}
static int
maa_pwm_get_period(pwm_t* dev)
{
FILE *period_f;
char bu[64];
char output[16];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", dev->chipid, dev->pin);
if ((period_f = fopen(bu, "rb")) == NULL) {
fprintf(stderr, "Failed to open period for reading!\n");
return 0;
}
fgets(output, 16, period_f);
fclose(period_f);
return atoi(output);
}
static int
maa_pwm_get_duty(pwm_t* dev)
{
if (dev->duty_fp == NULL) {
maa_pwm_setup_duty_fp(dev);
}
char output[16];
fgets(output, 16, dev->duty_fp);
fseek(dev->duty_fp, SEEK_SET, 0);
return atoi(output);
}
int
maa_pwm_init(pwm_t* dev, int chipin, int pin)
{
dev = malloc(sizeof *dev);
dev->chipid = chipin;
dev->pin = pin;
FILE *export_f;
char buffer[64];
snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/export", dev->chipid);
if ((export_f = fopen(buffer, "w")) == NULL) {
fprintf(stderr, "Failed to open export for writing!\n");
} else {
fprintf(export_f, "%d", dev->pin);
fclose(export_f);
maa_pwm_setup_duty_fp(dev);
}
return 0;
}
void
maa_pwm_write(pwm_t* dev, float percentage)
{
maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
}
float
maa_pwm_read(pwm_t* 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_ms(dev, seconds*1000);
}
void
maa_pwm_period_ms(pwm_t* dev, int ms)
{
maa_pwm_period_us(dev, ms*1000);
}
void
maa_pwm_period_us(pwm_t* dev, int us)
{
maa_pwm_write_period(dev, us*1000);
}
void
maa_pwm_pulsewidth(pwm_t* dev, float seconds)
{
maa_pwm_pulsewidth_ms(dev, seconds*1000);
}
void
maa_pwm_pulsewidth_ms(pwm_t* dev, int ms)
{
maa_pwm_pulsewidth_us(dev, ms*1000);
}
void
maa_pwm_pulsewidth_us(pwm_t* dev, int us)
{
maa_pwm_write_duty(dev, us*1000);
}
void
maa_pwm_enable(pwm_t* dev, int enable)
{
int status;
if (enable != 0) {
status = 1;
} else {
status = enable;
}
FILE *enable_f;
char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/enable", dev->chipid, dev->pin);
if ((enable_f = fopen(bu, "w")) == NULL) {
fprintf(stderr, "Failed to open export for writing!\n");
} else {
fprintf(enable_f, "%d", status);
fclose(enable_f);
}
//Do Something
}
void
maa_pwm_close(pwm_t* dev)
{
maa_pwm_enable(dev, 0);
FILE *unexport_f;
char buffer[64];
snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/unexport", dev->chipid);
if ((unexport_f = fopen(buffer, "w")) == NULL) {
fprintf(stderr, "Failed to open unexport for writing!\n");
} else {
fprintf(unexport_f, "%d", dev->pin);
fclose(unexport_f);
}
free(dev);
}

View File

@@ -1,204 +0,0 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@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.
*/
#include <stdlib.h>
#include "pwm.h"
using namespace maa;
PWM::PWM(int chipin, int pinin)
{
chipid = chipin;
pin = pinin;
FILE *export_f;
char buffer[64];
snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/export", chipid);
if((export_f = fopen(buffer, "w")) == NULL) {
fprintf(stderr, "Failed to open export for writing!\n");
} else {
fprintf(export_f, "%d", pin);
fclose(export_f);
setup_duty_fp();
}
}
void
PWM::write(float percentage)
{
write_duty(percentage*get_period());
}
float
PWM::read()
{
float output = get_duty() / (float) get_period();
return output;
}
void
PWM::period(float seconds)
{
period_ms(seconds*1000);
}
void
PWM::period_ms(int ms)
{
period_us(ms*1000);
}
void
PWM::period_us(int us)
{
write_period(us*1000);
}
void
PWM::pulsewidth(float seconds)
{
pulsewidth_ms(seconds*1000);
}
void
PWM::pulsewidth_ms(int ms)
{
pulsewidth_us(ms*1000);
}
void
PWM::pulsewidth_us(int us)
{
write_duty(us*1000);
}
void
PWM::enable(int enable)
{
int status;
if(enable != 0) {
status = 1;
} else {
status = enable;
}
FILE *enable_f;
char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/enable", chipid, pin);
if((enable_f = fopen(bu, "w")) == NULL) {
fprintf(stderr, "Failed to open export for writing!\n");
} else {
fprintf(enable_f, "%d", status);
fclose(enable_f);
}
//Do Something
}
void
PWM::close()
{
enable(0);
FILE *unexport_f;
char buffer[64];
snprintf(buffer, 64, "/sys/class/pwm/pwmchip%d/unexport", chipid);
if((unexport_f = fopen(buffer, "w")) == NULL) {
fprintf(stderr, "Failed to open unexport for writing!\n");
} else {
fprintf(unexport_f, "%d", pin);
fclose(unexport_f);
}
}
void
PWM::write_period(int period)
{
FILE *period_f;
char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", chipid, pin);
if((period_f = fopen(bu, "r+b")) == NULL) {
fprintf(stderr, "Failed to open period for writing!\n");
} else {
fprintf(period_f, "%d", period);
fclose(period_f);
}
}
void
PWM::write_duty(int duty)
{
if(duty_fp == NULL) {
setup_duty_fp();
}
fprintf(duty_fp, "%d", duty);
rewind(duty_fp);
fflush(duty_fp);
}
int
PWM::setup_duty_fp()
{
char bu[64];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/duty_cycle", chipid, pin);
if((duty_fp = fopen(bu, "r+b")) == NULL) {
return 1;
} else {
return 0;
}
return 1;
}
int
PWM::get_period()
{
FILE *period_f;
char bu[64];
char output[16];
sprintf(bu, "/sys/class/pwm/pwmchip%d/pwm%d/period", chipid, pin);
if((period_f = fopen(bu, "rb")) == NULL) {
fprintf(stderr, "Failed to open period for reading!\n");
return 0;
} else {
fgets(output, 16, period_f);
fclose(period_f);
return atoi(output);
}
}
int
PWM::get_duty()
{
if(duty_fp == NULL) {
setup_duty_fp();
}
char output[16];
fgets(output, 16, duty_fp);
fseek(duty_fp, SEEK_SET, 0);
return atoi(output);
}

View File

@@ -6,7 +6,5 @@ include_directories(
${PYTHON_INCLUDE_DIRS}
)
SET_SOURCE_FILES_PROPERTIES(pymaa.i PROPERTIES CPLUSPLUS ON)
SWIG_ADD_MODULE(pymaa python pymaa.i ${maa_LIB_SRCS})
SWIG_LINK_LIBRARIES(pymaa ${PYTHON_LIBRARIES})