From a5a407e4b589be8e4e9ef5a1e1c1daf06cdcbea0 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Sun, 27 Apr 2014 21:17:54 +0100 Subject: [PATCH] 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 --- CMakeLists.txt | 21 ++- README | 5 +- api/gpio.h | 8 - api/i2c.h | 167 +++++++++------------ api/i2cslave.h | 154 ------------------- api/maa.h | 3 +- api/pwm.h | 146 +++++++----------- examples/CMakeLists.txt | 8 +- examples/{blink-io8.cxx => blink-io8.c} | 0 examples/{cycle-pwm3.cxx => cycle-pwm3.c} | 11 +- examples/{hellomaa.cxx => hellomaa.c} | 2 +- examples/{i2c_HMC5883L.cxx => i2c_HMC5883L.c} | 19 +-- include/smbus.hpp | 8 - src/CMakeLists.txt | 8 +- src/i2c/{i2cslave.cxx => i2c.c} | 45 +++--- src/i2c/i2c.cxx | 93 ------------ src/javascript/CMakeLists.txt | 1 - src/{maa.cxx => maa.c} | 13 +- src/maa.i | 1 - src/maa.pc.cmake | 2 +- src/pwm/pwm.c | 202 +++++++++++++++++++++++++ src/pwm/pwm.cxx | 204 -------------------------- src/python/CMakeLists.txt | 2 - 23 files changed, 388 insertions(+), 735 deletions(-) delete mode 100644 api/i2cslave.h rename examples/{blink-io8.cxx => blink-io8.c} (100%) rename examples/{cycle-pwm3.cxx => cycle-pwm3.c} (88%) rename examples/{hellomaa.cxx => hellomaa.c} (94%) rename examples/{i2c_HMC5883L.cxx => i2c_HMC5883L.c} (88%) delete mode 100644 include/smbus.hpp rename src/i2c/{i2cslave.cxx => i2c.c} (66%) delete mode 100644 src/i2c/i2c.cxx rename src/{maa.cxx => maa.c} (89%) create mode 100644 src/pwm/pwm.c delete mode 100644 src/pwm/pwm.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index f54e3de..f956e0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,15 +5,15 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall") set(maa_VERSION_MAJOR 0) -set(maa_VERSION_MINOR 1) -set(maa_VERSION_PATCH 1) +set(maa_VERSION_MINOR 2) +set(maa_VERSION_PATCH 0) set(maa_VERSION_STRING ${maa_VERSION_MAJOR}.${maa_VERSION_MINOR}.${maa_VERSION_PATCH}) set (SWIG_EXECUTABLE /usr/bin/swig) -FIND_PACKAGE (SWIG REQUIRED) -INCLUDE (${SWIG_USE_FILE}) +find_package (SWIG REQUIRED) +include (${SWIG_USE_FILE}) -SET (CMAKE_SWIG_FLAGS "") +set (CMAKE_SWIG_FLAGS "") option (test "Build all tests." OFF) @@ -24,3 +24,14 @@ if (test) enable_testing () add_subdirectory (tests) endif () + +# add a target to generate API documentation with Doxygen +find_package (Doxygen) +if (DOXYGEN_FOUND) + configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY) + add_custom_target (doc + ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Generating API documentation with Doxygen" VERBATIM + ) +endif (DOXYGEN_FOUND) diff --git a/README b/README index a2d35ac..7f29274 100644 --- a/README +++ b/README @@ -1,6 +1,6 @@ MAA - Low Level Skeleton Library for Communication on Intel platforms -Library for C/C++ to interface with Galileo & other Intel platforms over: +Library in C/C++ to interface with Galileo & other Intel platforms over: - I2C - SPI @@ -8,7 +8,8 @@ Library for C/C++ to interface with Galileo & other Intel platforms over: - PWM - AIO -In a structured and sane API with port nanmes/numbering that match boards. +In a structured and sane API with port nanmes/numbering that match boards & +with bindings to javascript & python. The intent is to make it easier for developers and sensor manufacturers to map their sensors & actuators on top of supported hardware and to allow control of diff --git a/api/gpio.h b/api/gpio.h index 18eb77e..e3035da 100644 --- a/api/gpio.h +++ b/api/gpio.h @@ -20,10 +20,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - typedef struct gpio_struct { int pin; @@ -43,7 +39,3 @@ void 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); - -#ifdef __cplusplus -} -#endif diff --git a/api/i2c.h b/api/i2c.h index b2acbec..c9f806b 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -18,119 +18,84 @@ #pragma once +#include #include #include +#include "gpio.h" -#include "smbus.hpp" +typedef struct { + int hz; + int fh; + int addr; + gpio_t gpio; +} i2c_t; -namespace maa { +int maa_i2c_init(i2c_t* dev); -/** An I2C Master, used for communicating with I2C slave devices +/** Set the frequency of the I2C interface * - * Example: - * @code - * // Read from I2C slave at address 0x62 + * @param hz The bus frequency in hertz + */ +void maa_i2c_frequency(i2c_t* dev, int hz); + +/** Checks to see if this I2C Slave has been addressed. * - * #include "maa.h" + * @returns + * A status indicating if the device has been addressed, and how + * - NoData - the slave has not been addressed + * - ReadAddressed - the master has requested a read from this slave + * - WriteAddressed - the master is writing to this slave + * - WriteGeneral - the master is writing to all slave + */ +int maa_i2c_receive(i2c_t* dev); + +/** Read from an I2C master. * - * I2C i2c(p28, p27); + * @param data pointer to the byte array to read data in to + * @param length maximum number of bytes to read * - * int main() { - * int address = 0x62; - * char data[2]; - * i2c.read(address, data, 2); - * } - * @endcode + * @returns + * 0 on success, + * non-0 otherwise */ -class I2C { - -public: - enum RxStatus { - NoData, - MasterGeneralCall, - MasterWrite, - MasterRead - }; - - enum Acknowledge { - NoACK = 0, - ACK = 1 - }; +int maa_i2c_read(i2c_t* dev, char *data, int length); - /** Create an I2C Master interface, connected to the specified pins - * - * @param sda I2C data line pin - * @param scl I2C clock line pin - */ - I2C(unsigned int sda, unsigned int scl); - - /** Set the frequency of the I2C interface - * - * @param hz The bus frequency in hertz - */ - void frequency(int hz); - - /** Read from an I2C slave - * - * Performs a complete read transaction. The bottom bit of - * the address is forced to 1 to indicate a read. - * - * @param address 8-bit I2C slave address [ addr | 1 ] - * @param data Pointer to the byte-array to read data in to - * @param length Number of bytes to read - * @param repeated Repeated start, true - don't send stop at end - * - * @returns - * 0 on success (ack), - * non-0 on failure (nack) - */ - int read(int address, char *data, int length, bool repeated = false); - - /** Read a single byte from the I2C bus - * - * @param ack indicates if the byte is to be acknowledged (1 = acknowledge) - * - * @returns - * the byte read - */ - int read(int ack); - - /** Write to an I2C slave - * - * Performs a complete write transaction. The bottom bit of - * the address is forced to 0 to indicate a write. - * - * @param address 8-bit I2C slave address [ addr | 0 ] - * @param data Pointer to the byte-array data to send - * @param length Number of bytes to send - * @param repeated Repeated start, true - do not send stop at end - * - * @returns - * 0 on success (ack), - * non-0 on failure (nack) - */ - int write(int address, const char *data, int length, bool repeated = false); +/** Read a single byte from an I2C master. + * + * @returns + * the byte read + */ +int maa_i2c_read_byte(i2c_t* dev); - /** Write single byte out on the I2C bus - * @param data data to write out on bus - * - * @returns - * '1' if an ACK was received, - * '0' otherwise - */ - int write(int data); +/** Write to an I2C master. + * + * @param data pointer to the byte array to be transmitted + * @param length the number of bytes to transmite + * + * @returns + * 0 on success, + * non-0 otherwise + */ +int maa_i2c_write(i2c_t* dev, const char *data, int length); - /** Creates a start condition on the I2C bus - */ - void start(void); +/** Write a single byte to an I2C master. + * + * @data the byte to write + * + * @returns + * '1' if an ACK was received, + * '0' otherwise + */ +int maa_i2c_write_byte(i2c_t* dev, int data); - /** Creates a stop condition on the I2C bus - */ - void stop(void); +/** Sets the I2C slave address. + * + * @param address The address to set for the slave (ignoring the least + * 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); -protected: - void aquire(); - int _hz; - int i2c_handle; -}; -} +/** De-inits an i2c_t device + */ +void maa_i2c_stop(i2c_t* dev); diff --git a/api/i2cslave.h b/api/i2cslave.h deleted file mode 100644 index f717247..0000000 --- a/api/i2cslave.h +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Originally from mbed Microcontroller Library - * Copyright (c) 2006-2013 ARM Limited - * Copyright (c) 2014 Intel Corporation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include -#include "smbus.hpp" -#include "gpio.h" - -namespace maa { - -/** An I2C Slave, used for communicating with an I2C Master device - * - * Example: - * @code - * // Simple I2C responder - * #include - * - * I2CSlave slave(p9, p10); - * - * int main() { - * char buf[10]; - * char msg[] = "Slave!"; - * - * slave.address(0xA0); - * while (1) { - * int i = slave.receive(); - * switch (i) { - * case I2CSlave::ReadAddressed: - * slave.write(msg, strlen(msg) + 1); // Includes null char - * break; - * case I2CSlave::WriteGeneral: - * slave.read(buf, 10); - * printf("Read G: %s\n", buf); - * break; - * case I2CSlave::WriteAddressed: - * slave.read(buf, 10); - * printf("Read A: %s\n", buf); - * break; - * } - * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer - * } - * } - * @endcode - */ -class I2CSlave { - -public: - enum RxStatus { - NoData = 0, - ReadAddressed = 1, - WriteGeneral = 2, - WriteAddressed = 3 - }; - - /** Create an I2C Slave interface, connected to the specified pins. - * - * @param sda I2C data line pin - * @param scl I2C clock line pin - */ - I2CSlave(unsigned int sda, unsigned int scl); - - /** Set the frequency of the I2C interface - * - * @param hz The bus frequency in hertz - */ - void frequency(int hz); - - /** Checks to see if this I2C Slave has been addressed. - * - * @returns - * A status indicating if the device has been addressed, and how - * - NoData - the slave has not been addressed - * - ReadAddressed - the master has requested a read from this slave - * - WriteAddressed - the master is writing to this slave - * - WriteGeneral - the master is writing to all slave - */ - int receive(void); - - /** Read from an I2C master. - * - * @param data pointer to the byte array to read data in to - * @param length maximum number of bytes to read - * - * @returns - * 0 on success, - * non-0 otherwise - */ - int read(char *data, int length); - - /** Read a single byte from an I2C master. - * - * @returns - * the byte read - */ - int read(void); - - /** Write to an I2C master. - * - * @param data pointer to the byte array to be transmitted - * @param length the number of bytes to transmite - * - * @returns - * 0 on success, - * non-0 otherwise - */ - int write(const char *data, int length); - - /** Write a single byte to an I2C master. - * - * @data the byte to write - * - * @returns - * '1' if an ACK was received, - * '0' otherwise - */ - int write(int data); - - /** Sets the I2C slave address. - * - * @param address The address to set for the slave (ignoring the least - * signifcant bit). If set to 0, the slave will only respond to the - * general call address. - */ - void address(int address); - - /** Reset the I2C slave back into the known ready receiving state. - */ - void stop(void); - -protected: - int _hz; - int i2c_handle; - int _addr; - gpio_t gpio; -}; - -} diff --git a/api/maa.h b/api/maa.h index f5ef581..f6d90a3 100644 --- a/api/maa.h +++ b/api/maa.h @@ -25,10 +25,9 @@ #pragma once #include "i2c.h" -#include "i2cslave.h" #include "gpio.h" #include "pwm.h" #define MAA_LIBRARY_VERSION 1 -int get_version(); +int maa_get_version(); diff --git a/api/pwm.h b/api/pwm.h index 4b21f32..e3fe6a8 100644 --- a/api/pwm.h +++ b/api/pwm.h @@ -21,112 +21,66 @@ #include #include -namespace maa { - -/** A PWM object, used for interacting with PWM output. - * - * Example: - * @code - * // Set up PWM object then cycle percentage 0-100. - * - * #include "maa.h" - * - * PWM pwm(3); - * - * int main() { - * pwm.period_us(7968750i); //Max Galileo Rev D - * pwm.enable(1); - * - * float value = 0; - * while(1) { - * pwm.write(value); - * sleep(0.5); - * if(value == 1.0) { - * value = 0; - * } else { - * value = value +0.1; - * } - * } - * } - * @endcode - */ -class PWM { - -private: +typedef struct { int chipid, pin; FILE *duty_fp; +} pwm_t; - void write_period(int period); - void write_duty(int duty); - int setup_duty_fp(); - int get_period(); - int get_duty(); - -public: +int maa_pwm_init(pwm_t* pwm, int chipin, int pin); - /** Create an PWM object - * - * @param chipid The chip in which the following pin is on. - * @param pin The PWM channel to operate on - */ - PWM(int chipid, int pin); - - /** Set the ouput duty-cycle percentage, as a float - * - * @param percentage A floating-point value representing percentage of output. - * 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 write(float percentage); - - /** Read the ouput duty-cycle percentage, as a float - * - * @return percentage A floating-point value representing percentage of output. - * 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 read(); +/** Set the ouput duty-cycle percentage, as a float + * + * @param percentage A floating-point value representing percentage of output. + * 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); - /** Set the PWM period as seconds represented in a float - * - * @param seconds Peroid represented as a float in seconds. - */ - void period(float seconds); +/** Read the ouput duty-cycle percentage, as a float + * + * @return percentage A floating-point value representing percentage of output. + * 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); - /** Set period. milli-oseconds. - * @param ms milli-seconds for period. - */ - void period_ms(int ms); +/** 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); - /** Set period. microseconds - * @param ns microseconds as period. - */ - void period_us(int us); +/** Set period. milli-oseconds. + * @param ms milli-seconds for period. + */ +void maa_pwm_period_ms(pwm_t* pwm, int ms); - /** Set pulsewidth, As represnted by seconds in a (float). - * @param seconds The duration of a pulse - */ - void pulsewidth(float seconds); +/** Set period. microseconds + * @param ns microseconds as period. + */ +void maa_pwm_period_us(pwm_t* pwm, int us); - /** Set pulsewidth. Milliseconds - * @param ms milliseconds for pulsewidth. - */ - void pulsewidth_ms(int ms); +/** 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); - /** Set pulsewidth, microseconds. - * @param us microseconds for pulsewidth. - */ - void pulsewidth_us(int us); + /** Set pulsewidth. Milliseconds + * @param ms milliseconds for pulsewidth. + */ +void maa_pwm_pulsewidth_ms(pwm_t* pwm, int ms); - /** 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 enable(int enable); + /** Set pulsewidth, microseconds. + * @param us microseconds for pulsewidth. + */ +void maa_pwm_pulsewidth_us(pwm_t* pwm, int us); - /** Close and unexport the PWM pin. - */ - void close(); +/** 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); -}; -} + /** Close and unexport the PWM pin. + */ +void maa_pwm_close(pwm_t* pwm); diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 56eaa46..f281341 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,9 +1,9 @@ -add_executable (i2c_HMC5883L i2c_HMC5883L.cxx) -add_executable (hellomaa hellomaa.cxx) -add_executable (cycle-pwm3 cycle-pwm3.cxx) +add_executable (i2c_HMC5883L i2c_HMC5883L.c) +add_executable (hellomaa hellomaa.c) +add_executable (cycle-pwm3 cycle-pwm3.c) include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include) target_link_libraries (hellomaa maa) -target_link_libraries (i2c_HMC5883L maa) +target_link_libraries (i2c_HMC5883L maa m) target_link_libraries (cycle-pwm3 maa) diff --git a/examples/blink-io8.cxx b/examples/blink-io8.c similarity index 100% rename from examples/blink-io8.cxx rename to examples/blink-io8.c diff --git a/examples/cycle-pwm3.cxx b/examples/cycle-pwm3.c similarity index 88% rename from examples/cycle-pwm3.cxx rename to examples/cycle-pwm3.c index b59705c..d853947 100644 --- a/examples/cycle-pwm3.cxx +++ b/examples/cycle-pwm3.c @@ -29,20 +29,21 @@ int main () { - maa::PWM pwm(0, 3); - pwm.period_us(200); - pwm.enable(1); + pwm_t pwm; + maa_pwm_init(&pwm, 0, 3); + maa_pwm_period_us(&pwm, 200); + maa_pwm_enable(&pwm, 1); float value = 0.0f; while(1) { value = value + 0.01f; - pwm.write(value); + maa_pwm_write(&pwm, value); usleep(50000); if (value >= 1.0f) { value = 0.0f; } - float output = pwm.read(); + float output = maa_pwm_read(&pwm); } return 0; } diff --git a/examples/hellomaa.cxx b/examples/hellomaa.c similarity index 94% rename from examples/hellomaa.cxx rename to examples/hellomaa.c index 67db5a7..43902ed 100644 --- a/examples/hellomaa.cxx +++ b/examples/hellomaa.c @@ -29,6 +29,6 @@ int main(int argc, char **argv) { - fprintf(stdout, "hello maa\n Version: %d\n", get_version()); + fprintf(stdout, "hello maa\n Version: %d\n", maa_get_version()); return 0; } diff --git a/examples/i2c_HMC5883L.cxx b/examples/i2c_HMC5883L.c similarity index 88% rename from examples/i2c_HMC5883L.cxx rename to examples/i2c_HMC5883L.c index e1d8cd6..e474ff4 100644 --- a/examples/i2c_HMC5883L.cxx +++ b/examples/i2c_HMC5883L.c @@ -82,24 +82,25 @@ main () int16_t x = 0, y = 0, z = 0; char rx_tx_buf[MAX_BUFFER_LENGTH]; - maa::I2CSlave i2c(26, 27); + i2c_t i2c; + maa_i2c_init(&i2c); - i2c.address(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; - i2c.write(rx_tx_buf, 2); + maa_i2c_write(&i2c, rx_tx_buf, 2); - i2c.address(HMC5883L_I2C_ADDR); + maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); rx_tx_buf[0] = HMC5883L_MODE_REG; rx_tx_buf[1] = HMC5883L_CONT_MODE; - i2c.write(rx_tx_buf, 2); + maa_i2c_write(&i2c, rx_tx_buf, 2); for(;;) { - i2c.address(HMC5883L_I2C_ADDR); - i2c.write(HMC5883L_DATA_REG); + maa_i2c_address(&i2c, HMC5883L_I2C_ADDR); + maa_i2c_write_byte(&i2c, HMC5883L_DATA_REG); - i2c.address(HMC5883L_I2C_ADDR); - i2c.read(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/include/smbus.hpp b/include/smbus.hpp deleted file mode 100644 index 51277a7..0000000 --- a/include/smbus.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#pragma once - -// This header is required to remove the issues with g++ name mangling - -extern "C" -{ -#include "smbus.h" -} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b4e8347..1ecb7d6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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}) diff --git a/src/i2c/i2cslave.cxx b/src/i2c/i2c.c similarity index 66% rename from src/i2c/i2cslave.cxx rename to src/i2c/i2c.c index 92f08d2..a14bf40 100644 --- a/src/i2c/i2cslave.cxx +++ b/src/i2c/i2c.c @@ -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); } diff --git a/src/i2c/i2c.cxx b/src/i2c/i2c.cxx deleted file mode 100644 index e2f1912..0000000 --- a/src/i2c/i2c.cxx +++ /dev/null @@ -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() -{ -} diff --git a/src/javascript/CMakeLists.txt b/src/javascript/CMakeLists.txt index e8e8eaf..c8b6943 100644 --- a/src/javascript/CMakeLists.txt +++ b/src/javascript/CMakeLists.txt @@ -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}) diff --git a/src/maa.cxx b/src/maa.c similarity index 89% rename from src/maa.cxx rename to src/maa.c index 5a80c0d..3f93634 100644 --- a/src/maa.cxx +++ b/src/maa.c @@ -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); -} diff --git a/src/maa.i b/src/maa.i index c8f4040..0a7e1b2 100644 --- a/src/maa.i +++ b/src/maa.i @@ -4,6 +4,5 @@ %include "maa.h" %include "i2c.h" -%include "i2cslave.h" %include "gpio.h" %include "pwm.h" diff --git a/src/maa.pc.cmake b/src/maa.pc.cmake index a5e6f75..d23be50 100644 --- a/src/maa.pc.cmake +++ b/src/maa.pc.cmake @@ -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 diff --git a/src/pwm/pwm.c b/src/pwm/pwm.c new file mode 100644 index 0000000..1795e34 --- /dev/null +++ b/src/pwm/pwm.c @@ -0,0 +1,202 @@ +/* + * Author: Thomas Ingleby + * 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 + +#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); +} diff --git a/src/pwm/pwm.cxx b/src/pwm/pwm.cxx deleted file mode 100644 index b29f149..0000000 --- a/src/pwm/pwm.cxx +++ /dev/null @@ -1,204 +0,0 @@ -/* - * Author: Thomas Ingleby - * 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 - -#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); -} diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index a880a09..3f88ccb 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -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}) -- 2.7.4