Private
Public Access
2
0

Merge pull request #4 from emea-ssg-drd/dumpc++

Remove all C++ code and move to a standard C api
This commit is contained in:
Brendan Le Foll
2014-04-28 14:12:40 +01:00
30 changed files with 3094 additions and 816 deletions

View File

@@ -1,26 +1,61 @@
cmake_minimum_required (VERSION 2.8)
project (maa)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
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_STRING ${maa_VERSION_MAJOR}.${maa_VERSION_MINOR}.${maa_VERSION_PATCH})
# Appends the cmake/modules path to MAKE_MODULE_PATH variable.
set (CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH})
# Make a version file containing the current version from git.
include (GetGitRevisionDescription)
git_describe (VERSION "--tags")
if ("x_${VERSION}" STREQUAL "x_GIT-NOTFOUND")
message (WARNING " - Install git to compile a production libmaa!")
set (VERSION "v0.2.0-dirty")
endif ()
message (INFO " - MAA Version ${VERSION}")
#parse the version information into pieces.
string (REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${VERSION}")
string (REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${VERSION}")
string (REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${VERSION}")
string (REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+(.*)" "\\1" VERSION_SHA1 "${VERSION}")
set (VERSION_SHORT "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
configure_file (${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules/version.c.in
${CMAKE_CURRENT_BINARY_DIR}/src/version.c)
# this is the library version, independant of git revision
set (maa_VERSION_MAJOR ${VERSION_MAJOR})
set (maa_VERSION_MINOR ${VERSION_MINOR})
set (maa_VERSION_PATCH ${VERSION_PATCH})
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)
add_subdirectory (src)
add_subdirectory (examples)
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)
add_subdirectory (src)
add_subdirectory (examples)

2354
Doxyfile.in Normal file

File diff suppressed because it is too large Load Diff

8
README
View File

@@ -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
@@ -24,6 +25,9 @@ http://github.com:oliver----/swig-v8
=== COMPILING ===
NOTE: The only supported cmake build configuration is to have the build/ dir
inside of the repo/tarball.
if swig-v8 is not in your default path you can try run cmake with
"-DCMAKE_PREFIX_PATH="/path/to/swig-v8"

View File

@@ -20,30 +20,22 @@
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
#include "maa.h"
typedef struct gpio_struct
{
typedef struct {
int pin;
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];
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_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 gpio_close(gpio_t *gpio);
int gpio_read(gpio_t *gpio);
void gpio_write(gpio_t *gpio, int value);
#ifdef __cplusplus
}
#endif
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);

171
api/i2c.h
View File

@@ -18,119 +18,86 @@
#pragma once
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include "smbus.hpp"
#include "maa.h"
#include "gpio.h"
namespace maa {
typedef struct {
int hz;
int fh;
int addr;
maa_gpio_context gpio;
} maa_i2c_context;
/** An I2C Master, used for communicating with I2C slave devices
maa_i2c_context* maa_i2c_init();
/** Set the frequency of the I2C interface
*
* Example:
* @code
* // Read from I2C slave at address 0x62
*
* #include "maa.h"
*
* I2C i2c(p28, p27);
*
* int main() {
* int address = 0x62;
* char data[2];
* i2c.read(address, data, 2);
* }
* @endcode
* @param hz The bus frequency in hertz
*/
class I2C {
void maa_i2c_frequency(maa_i2c_context* dev, int hz);
public:
enum RxStatus {
NoData,
MasterGeneralCall,
MasterWrite,
MasterRead
};
/** 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 maa_i2c_receive(maa_i2c_context* dev);
enum Acknowledge {
NoACK = 0,
ACK = 1
};
/** 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 maa_i2c_read(maa_i2c_context* 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);
/** Read a single byte from an I2C master.
*
* @returns
* the byte read
*/
int maa_i2c_read_byte(maa_i2c_context* dev);
/** Set the frequency of the I2C interface
*
* @param hz The bus frequency in hertz
*/
void frequency(int hz);
/** 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(maa_i2c_context* dev, const char *data, int length);
/** 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);
/** 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(maa_i2c_context* dev, int data);
/** 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);
/** 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(maa_i2c_context* dev, int address);
/** 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);
/** 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);
/** Creates a start condition on the I2C bus
*/
void start(void);
/** Creates a stop condition on the I2C bus
*/
void stop(void);
protected:
void aquire();
int _hz;
int i2c_handle;
};
}
/** De-inits an maa_i2c_context device
*/
void maa_i2c_stop(maa_i2c_context* dev);

View File

@@ -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 <stdio.h>
#include <fcntl.h>
#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 <mbed.h>
*
* 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;
};
}

View File

@@ -24,11 +24,19 @@
#pragma once
#include "i2c.h"
#include "i2cslave.h"
#include "gpio.h"
#include "pwm.h"
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,
#define MAA_LIBRARY_VERSION 1
MAA_ERROR_UNSPECIFIED = 99
} maa_result_t;
int get_version();
const char* maa_get_version();

146
api/pwm.h
View File

@@ -21,112 +21,68 @@
#include <stdio.h>
#include <fcntl.h>
namespace maa {
#include "maa.h"
/** 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;
} maa_pwm_context;
void write_period(int period);
void write_duty(int duty);
int setup_duty_fp();
int get_period();
int get_duty();
maa_pwm_context* maa_pwm_init(int chipin, int pin);
public:
/** 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(maa_pwm_context* pwm, float percentage);
/** 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);
/** 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(maa_pwm_context* pwm);
/** 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);
/** Set the PWM period as seconds represented in a float
*
* @param seconds Peroid represented as a float in seconds.
*/
void maa_pwm_period(maa_pwm_context* pwm, 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 read();
/** Set period. milli-oseconds.
* @param ms milli-seconds for period.
*/
void maa_pwm_period_ms(maa_pwm_context* pwm, int ms);
/** Set the PWM period as seconds represented in a float
*
* @param seconds Peroid represented as a float in seconds.
*/
void period(float seconds);
/** Set period. microseconds
* @param ns microseconds as period.
*/
void maa_pwm_period_us(maa_pwm_context* pwm, int us);
/** Set period. milli-oseconds.
* @param ms milli-seconds for period.
*/
void period_ms(int ms);
/** Set pulsewidth, As represnted by seconds in a (float).
* @param seconds The duration of a pulse
*/
void maa_pwm_pulsewidth(maa_pwm_context* pwm, float seconds);
/** Set period. microseconds
* @param ns microseconds as period.
*/
void period_us(int us);
/** Set pulsewidth. Milliseconds
* @param ms milliseconds for pulsewidth.
*/
void maa_pwm_pulsewidth_ms(maa_pwm_context* pwm, int ms);
/** Set pulsewidth, As represnted by seconds in a (float).
* @param seconds The duration of a pulse
*/
void pulsewidth(float seconds);
/** Set pulsewidth, microseconds.
* @param us microseconds for pulsewidth.
*/
void maa_pwm_pulsewidth_us(maa_pwm_context* pwm, int us);
/** Set pulsewidth. Milliseconds
* @param ms milliseconds for pulsewidth.
*/
void pulsewidth_ms(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 maa_pwm_enable(maa_pwm_context* pwm, int enable);
/** Set pulsewidth, microseconds.
* @param us microseconds for pulsewidth.
*/
void pulsewidth_us(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 enable(int enable);
/** Close and unexport the PWM pin.
*/
void close();
};
}
/** Close and unexport the PWM pin.
*/
void maa_pwm_close(maa_pwm_context* pwm);

View File

@@ -0,0 +1,130 @@
# - Returns a version string from Git
#
# These functions force a re-configure on each git commit so that you can
# trust the values of the variables in your build system.
#
# get_git_head_revision(<refspecvar> <hashvar> [<additional arguments to git describe> ...])
#
# Returns the refspec and sha hash of the current head revision
#
# git_describe(<var> [<additional arguments to git describe> ...])
#
# Returns the results of git describe on the source tree, and adjusting
# the output so that it tests false if an error occurs.
#
# git_get_exact_tag(<var> [<additional arguments to git describe> ...])
#
# Returns the results of git describe --exact-match on the source tree,
# and adjusting the output so that it tests false if there was no exact
# matching tag.
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
if(__get_git_revision_description)
return()
endif()
set(__get_git_revision_description YES)
# We must run the following at "include" time, not at function call time,
# to find the path to this module rather than the path to a calling list file
get_filename_component(_gitdescmoddir ${CMAKE_CURRENT_LIST_FILE} PATH)
function(get_git_head_revision _refspecvar _hashvar)
set(GIT_PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
while(NOT EXISTS "${GIT_DIR}") # .git dir not found, search parent directories
set(GIT_PREVIOUS_PARENT "${GIT_PARENT_DIR}")
get_filename_component(GIT_PARENT_DIR ${GIT_PARENT_DIR} PATH)
if(GIT_PARENT_DIR STREQUAL GIT_PREVIOUS_PARENT)
# We have reached the root directory, we are not in git
set(${_refspecvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
set(${_hashvar} "GITDIR-NOTFOUND" PARENT_SCOPE)
return()
endif()
set(GIT_DIR "${GIT_PARENT_DIR}/.git")
endwhile()
# check if this is a submodule
if(NOT IS_DIRECTORY ${GIT_DIR})
file(READ ${GIT_DIR} submodule)
string(REGEX REPLACE "gitdir: (.*)\n$" "\\1" GIT_DIR_RELATIVE ${submodule})
get_filename_component(SUBMODULE_DIR ${GIT_DIR} PATH)
get_filename_component(GIT_DIR ${SUBMODULE_DIR}/${GIT_DIR_RELATIVE} ABSOLUTE)
endif()
set(GIT_DATA "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/git-data")
if(NOT EXISTS "${GIT_DATA}")
file(MAKE_DIRECTORY "${GIT_DATA}")
endif()
if(NOT EXISTS "${GIT_DIR}/HEAD")
return()
endif()
set(HEAD_FILE "${GIT_DATA}/HEAD")
configure_file("${GIT_DIR}/HEAD" "${HEAD_FILE}" COPYONLY)
configure_file("${_gitdescmoddir}/GetGitRevisionDescription.cmake.in"
"${GIT_DATA}/grabRef.cmake"
@ONLY)
include("${GIT_DATA}/grabRef.cmake")
set(${_refspecvar} "${HEAD_REF}" PARENT_SCOPE)
set(${_hashvar} "${HEAD_HASH}" PARENT_SCOPE)
endfunction()
function(git_describe _var)
if(NOT GIT_FOUND)
find_package(Git QUIET)
endif()
get_git_head_revision(refspec hash)
if(NOT GIT_FOUND)
set(${_var} "GIT-NOTFOUND" PARENT_SCOPE)
return()
endif()
if(NOT hash)
set(${_var} "HEAD-HASH-NOTFOUND" PARENT_SCOPE)
return()
endif()
# TODO sanitize
#if((${ARGN}" MATCHES "&&") OR
# (ARGN MATCHES "||") OR
# (ARGN MATCHES "\\;"))
# message("Please report the following error to the project!")
# message(FATAL_ERROR "Looks like someone's doing something nefarious with git_describe! Passed arguments ${ARGN}")
#endif()
#message(STATUS "Arguments to execute_process: ${ARGN}")
execute_process(COMMAND
"${GIT_EXECUTABLE}"
describe
${hash}
${ARGN}
WORKING_DIRECTORY
"${CMAKE_SOURCE_DIR}"
RESULT_VARIABLE
res
OUTPUT_VARIABLE
out
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(NOT res EQUAL 0)
set(out "${out}-${res}-NOTFOUND")
endif()
set(${_var} "${out}" PARENT_SCOPE)
endfunction()
function(git_get_exact_tag _var)
git_describe(out --exact-match ${ARGN})
set(${_var} "${out}" PARENT_SCOPE)
endfunction()

View File

@@ -0,0 +1,38 @@
#
# Internal file for GetGitRevisionDescription.cmake
#
# Requires CMake 2.6 or newer (uses the 'function' command)
#
# Original Author:
# 2009-2010 Ryan Pavlik <rpavlik@iastate.edu> <abiryan@ryand.net>
# http://academic.cleardefinition.com
# Iowa State University HCI Graduate Program/VRAC
#
# Copyright Iowa State University 2009-2010.
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt)
set(HEAD_HASH)
file(READ "@HEAD_FILE@" HEAD_CONTENTS LIMIT 1024)
string(STRIP "${HEAD_CONTENTS}" HEAD_CONTENTS)
if(HEAD_CONTENTS MATCHES "ref")
# named branch
string(REPLACE "ref: " "" HEAD_REF "${HEAD_CONTENTS}")
if(EXISTS "@GIT_DIR@/${HEAD_REF}")
configure_file("@GIT_DIR@/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
elseif(EXISTS "@GIT_DIR@/logs/${HEAD_REF}")
configure_file("@GIT_DIR@/logs/${HEAD_REF}" "@GIT_DATA@/head-ref" COPYONLY)
set(HEAD_HASH "${HEAD_REF}")
endif()
else()
# detached HEAD
configure_file("@GIT_DIR@/HEAD" "@GIT_DATA@/head-ref" COPYONLY)
endif()
if(NOT HEAD_HASH)
file(READ "@GIT_DATA@/head-ref" HEAD_HASH LIMIT 1024)
string(STRIP "${HEAD_HASH}" HEAD_HASH)
endif()

View File

@@ -0,0 +1,23 @@
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,4 @@
#include "version.h"
const char* gVERSION = "@VERSION@";
const char* gVERSION_SHORT = "@VERSION_SHORT@";

View File

@@ -1,9 +1,11 @@
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)
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)
target_link_libraries (i2c_HMC5883L maa m)
target_link_libraries (cycle-pwm3 maa)
target_link_libraries (blink-io8 maa)

View File

@@ -24,20 +24,21 @@
#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());
gpio_t gpio;
gpio_init(&gpio, 26);
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){
gpio_write(&gpio, 0);
while (1) {
maa_gpio_write(gpio, 0);
sleep(1);
gpio_write(&gpio, 1);
maa_gpio_write(gpio, 1);
sleep(1);
}
return 0;

View File

@@ -24,25 +24,29 @@
#include <unistd.h>
#include "maa.h"
#include "pwm.h"
int
main ()
{
maa::PWM pwm(0, 3);
pwm.period_us(200);
pwm.enable(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;
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;
}

View File

@@ -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: %s\n", maa_get_version());
return 0;
}

View File

@@ -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
@@ -76,30 +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];
maa::I2CSlave i2c(26, 27);
maa_i2c_context *i2c;
i2c = maa_i2c_init();
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] ;

View File

@@ -1,8 +0,0 @@
#pragma once
// This header is required to remove the issues with g++ name mangling
extern "C"
{
#include "smbus.h"
}

28
include/version.h Normal file
View File

@@ -0,0 +1,28 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@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.
*/
#pragma once
const char* gVERSION;
const char* gVERSION_SHORT;

View File

@@ -5,10 +5,9 @@ include_directories(
set (maa_LIB_HEADERS
${PROJECT_SOURCE_DIR}/api/maa.h
${PROJECT_SOURCE_DIR}/api/i2cslave.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,12 +20,13 @@ 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/i2c/smbus.c
${PROJECT_SOURCE_DIR}/src/maa.c
${PROJECT_SOURCE_DIR}/src/gpio/gpio.c
${PROJECT_SOURCE_DIR}/src/pwm/pwm.cxx
${PROJECT_SOURCE_DIR}/src/i2c/i2c.c
${PROJECT_SOURCE_DIR}/src/i2c/smbus.c
${PROJECT_SOURCE_DIR}/src/pwm/pwm.c
# autogenerated version file
${CMAKE_CURRENT_BINARY_DIR}/version.c
)
add_library (maa SHARED ${maa_LIB_SRCS})

View File

@@ -29,106 +29,93 @@
#include "gpio.h"
#ifdef __cplusplus
extern "C" {
#endif
static int
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;
}
void
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) {
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;
}
int
gpio_set(int pin)
{
//Stuff
return 0;
dev->pin = pin;
return dev;
}
void
gpio_mode(gpio_t *gpio, gpio_mode_t mode)
maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
{
//gpio->pin
}
void
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) {
if ((direction = fopen(filepath, "w")) == NULL) {
fprintf(stderr, "Failed to open direction for writing!\n");
} else {
fprintf(direction, dir);
fclose(direction);
gpio->value_fp = NULL;
dev->value_fp = NULL;
}
}
int
gpio_read(gpio_t *gpio)
maa_gpio_read(maa_gpio_context *dev)
{
if(gpio->value_fp == NULL) {
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
gpio_write(gpio_t *gpio, int value)
maa_gpio_write(maa_gpio_context *dev, int value)
{
if(gpio->value_fp == NULL) {
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
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) {
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);
}
}
#ifdef __cplusplus
}
#endif

View File

@@ -22,55 +22,61 @@
* 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)
maa_i2c_context*
maa_i2c_init()
{
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!
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");
}
return MAA_SUCCESS;
}
void
I2CSlave::frequency(int hz)
maa_i2c_frequency(maa_i2c_context* dev, int hz)
{
_hz = hz;
dev->hz = hz;
}
int
I2CSlave::receive(void)
maa_i2c_receive(maa_i2c_context* dev)
{
return -1;
}
int
I2CSlave::read(char *data, int length)
maa_i2c_read(maa_i2c_context* 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(maa_i2c_context* dev)
{
int byte;
if (byte = i2c_smbus_read_byte(i2c_handle) < 0) {
byte = i2c_smbus_read_byte(dev->fh);
if (byte < 0) {
return -1;
}
return byte;
}
int
I2CSlave::write(const char *data, int length)
maa_i2c_write(maa_i2c_context* 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 +84,9 @@ I2CSlave::write(const char *data, int length)
}
int
I2CSlave::write(int data)
maa_i2c_write_byte(maa_i2c_context* 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 +94,16 @@ I2CSlave::write(int data)
}
void
I2CSlave::address(int addr)
maa_i2c_address(maa_i2c_context* 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(maa_i2c_context* 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

@@ -23,20 +23,10 @@
*/
#include "maa.h"
#include "version.h"
using namespace maa;
int
get_version()
const char *
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);
return gVERSION;
}

View File

@@ -1,9 +1,11 @@
%{
#include "maa.h"
#include "gpio.h"
#include "pwm.h"
#include "i2c.h"
%}
%include "maa.h"
%include "i2c.h"
%include "i2cslave.h"
%include "gpio.h"
%include "pwm.h"
%include "i2c.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

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

@@ -0,0 +1,207 @@
/*
* 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(maa_pwm_context* 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(maa_pwm_context* 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(maa_pwm_context* 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(maa_pwm_context* 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(maa_pwm_context* 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);
}
maa_pwm_context*
maa_pwm_init(int chipin, int pin)
{
maa_pwm_context* dev = (maa_pwm_context*) malloc(sizeof(maa_pwm_context));
if (dev == NULL)
return NULL;
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");
free(dev);
return NULL;
} else {
fprintf(export_f, "%d", dev->pin);
fclose(export_f);
maa_pwm_setup_duty_fp(dev);
}
return dev;
}
void
maa_pwm_write(maa_pwm_context* dev, float percentage)
{
maa_pwm_write_duty(dev, percentage * maa_pwm_get_period(dev));
}
float
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(maa_pwm_context* dev, float seconds)
{
maa_pwm_period_ms(dev, seconds*1000);
}
void
maa_pwm_period_ms(maa_pwm_context* dev, int ms)
{
maa_pwm_period_us(dev, ms*1000);
}
void
maa_pwm_period_us(maa_pwm_context* dev, int us)
{
maa_pwm_write_period(dev, us*1000);
}
void
maa_pwm_pulsewidth(maa_pwm_context* dev, float seconds)
{
maa_pwm_pulsewidth_ms(dev, seconds*1000);
}
void
maa_pwm_pulsewidth_ms(maa_pwm_context* dev, int ms)
{
maa_pwm_pulsewidth_us(dev, ms*1000);
}
void
maa_pwm_pulsewidth_us(maa_pwm_context* dev, int us)
{
maa_pwm_write_duty(dev, us*1000);
}
void
maa_pwm_enable(maa_pwm_context* 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(maa_pwm_context* 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})