Private
Public Access
2
0

clang-format: run clang-format on C/C++ code

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2015-03-23 14:39:12 +00:00
parent 2174ee1673
commit ffcf3d7d07
46 changed files with 1630 additions and 1455 deletions

50
.clang-format Normal file
View File

@@ -0,0 +1,50 @@
AccessModifierOffset: -2
AlignEscapedNewlinesLeft: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
AlwaysBreakAfterDefinitionReturnType: true
BinPackParameters: false
BreakBeforeBinaryOperators: false
BreakBeforeBraces: Linux
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 100
CommentPragmas: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
ContinuationIndentWidth: 0
Cpp11BracedListStyle: false
DerivePointerBinding: false
IndentCaseLabels: true
IndentFunctionDeclarationAfterType: false
IndentWidth: 4
Language: Cpp
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 100
PenaltyBreakComment: 100
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 100
PenaltyExcessCharacter: 1
PenaltyReturnTypeOnItsOwnLine: 20
PointerBindsToType: true
PointerAlignment: Left
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpaceAfterCStyleCast: true
SpacesInContainerLiterals: false
SpacesInParentheses: false
Standard: Cpp11
TabWidth: 4
UseTab: Never

View File

@@ -27,7 +27,8 @@
#include <stdexcept> #include <stdexcept>
#include "aio.h" #include "aio.h"
namespace mraa { namespace mraa
{
/** /**
* @brief API to Analog IO * @brief API to Analog IO
@@ -36,7 +37,8 @@ namespace mraa {
* *
* @snippet examples/c++/AioA0.cpp Interesting * @snippet examples/c++/AioA0.cpp Interesting
*/ */
class Aio { class Aio
{
public: public:
/** /**
* Aio Constructor, takes a pin number which will map directly to the * Aio Constructor, takes a pin number which will map directly to the
@@ -44,7 +46,8 @@ class Aio {
* *
* @param pin channel number to read ADC inputs * @param pin channel number to read ADC inputs
*/ */
Aio(unsigned int pin) { Aio(unsigned int pin)
{
m_aio = mraa_aio_init(pin); m_aio = mraa_aio_init(pin);
if (m_aio == NULL) { if (m_aio == NULL) {
throw std::invalid_argument("Invalid AIO pin specified - do you have an ADC?"); throw std::invalid_argument("Invalid AIO pin specified - do you have an ADC?");
@@ -53,7 +56,8 @@ class Aio {
/** /**
* Aio destructor * Aio destructor
*/ */
~Aio() { ~Aio()
{
mraa_aio_close(m_aio); mraa_aio_close(m_aio);
} }
/** /**
@@ -62,7 +66,9 @@ class Aio {
* *
* @returns The current input voltage. By default, a 10bit value * @returns The current input voltage. By default, a 10bit value
*/ */
int read() { int
read()
{
return mraa_aio_read(m_aio); return mraa_aio_read(m_aio);
} }
/** /**
@@ -70,7 +76,9 @@ class Aio {
* *
* @returns The current input voltage as a normalized float (0.0f-1.0f) * @returns The current input voltage as a normalized float (0.0f-1.0f)
*/ */
float readFloat() { float
readFloat()
{
return mraa_aio_read_float(m_aio); return mraa_aio_read_float(m_aio);
} }
/** /**
@@ -79,7 +87,9 @@ class Aio {
* @param bits the bits the return from read should be i.e 10 * @param bits the bits the return from read should be i.e 10
* @return mraa result type * @return mraa result type
*/ */
mraa_result_t setBit(int bits) { mraa_result_t
setBit(int bits)
{
return mraa_aio_set_bit(m_aio, bits); return mraa_aio_set_bit(m_aio, bits);
} }
/** /**
@@ -87,12 +97,13 @@ class Aio {
* *
* @return bit value mraa is set return from the read function * @return bit value mraa is set return from the read function
*/ */
int getBit() { int
getBit()
{
return mraa_aio_get_bit(m_aio); return mraa_aio_get_bit(m_aio);
} }
private: private:
mraa_aio_context m_aio; mraa_aio_context m_aio;
}; };
} }

View File

@@ -30,7 +30,8 @@
/** /**
* @namespace mraa namespace * @namespace mraa namespace
*/ */
namespace mraa { namespace mraa
{
/** /**
* @file * @file
@@ -50,7 +51,8 @@ namespace mraa {
* *
* @return Result of operation * @return Result of operation
*/ */
inline mraa_result_t init() inline mraa_result_t
init()
{ {
return mraa_init(); return mraa_init();
} }
@@ -60,7 +62,8 @@ inline mraa_result_t init()
* *
* @return libmraa version (e.g. v0.4.0-20-gb408207) * @return libmraa version (e.g. v0.4.0-20-gb408207)
*/ */
inline std::string getVersion() inline std::string
getVersion()
{ {
std::string ret = mraa_get_version(); std::string ret = mraa_get_version();
return ret; return ret;
@@ -75,7 +78,8 @@ inline std::string getVersion()
* @param priority Value from typically 0 to 99 * @param priority Value from typically 0 to 99
* @return The priority value set * @return The priority value set
*/ */
inline int setPriority(const unsigned int priority) inline int
setPriority(const unsigned int priority)
{ {
return mraa_set_priority(priority); return mraa_set_priority(priority);
} }
@@ -85,7 +89,8 @@ inline int setPriority(const unsigned int priority)
* *
* @return mraa_platform_t Platform type enum * @return mraa_platform_t Platform type enum
*/ */
inline mraa_platform_t getPlatformType() inline mraa_platform_t
getPlatformType()
{ {
return mraa_get_platform_type(); return mraa_get_platform_type();
} }
@@ -95,7 +100,8 @@ inline mraa_platform_t getPlatformType()
* *
* @param result the result to print * @param result the result to print
*/ */
inline void printError(mraa_result_t result) inline void
printError(mraa_result_t result)
{ {
mraa_result_print(result); mraa_result_print(result);
} }
@@ -107,7 +113,8 @@ inline void printError(mraa_result_t result)
* @param mode the mode to be tested. * @param mode the mode to be tested.
* @return boolean if the mode is supported, 0=false. * @return boolean if the mode is supported, 0=false.
*/ */
inline bool pinModeTest(int pin, mraa_pinmodes_t mode) inline bool
pinModeTest(int pin, mraa_pinmodes_t mode)
{ {
return (bool) mraa_pin_mode_test(pin, mode); return (bool) mraa_pin_mode_test(pin, mode);
} }
@@ -117,7 +124,8 @@ inline bool pinModeTest(int pin, mraa_pinmodes_t mode)
* *
* @return raw bits being read from kernel module. Zero if no ADC * @return raw bits being read from kernel module. Zero if no ADC
*/ */
inline unsigned int adcRawBits() inline unsigned int
adcRawBits()
{ {
return mraa_adc_raw_bits(); return mraa_adc_raw_bits();
} }
@@ -127,7 +135,8 @@ inline unsigned int adcRawBits()
* *
* @return return actual bit size the adc value should be understood as. * @return return actual bit size the adc value should be understood as.
*/ */
inline unsigned int adcSupportedBits() inline unsigned int
adcSupportedBits()
{ {
return mraa_adc_supported_bits(); return mraa_adc_supported_bits();
} }
@@ -137,7 +146,8 @@ inline unsigned int adcSupportedBits()
* *
* @return platform name * @return platform name
*/ */
inline std::string getPlatformName() inline std::string
getPlatformName()
{ {
std::string ret_val(mraa_get_platform_name()); std::string ret_val(mraa_get_platform_name());
return ret_val; return ret_val;
@@ -148,7 +158,8 @@ inline std::string getPlatformName()
* *
* @return uint of physical pins. * @return uint of physical pins.
*/ */
inline unsigned int getPinCount() inline unsigned int
getPinCount()
{ {
return mraa_get_pin_count(); return mraa_get_pin_count();
} }
@@ -160,7 +171,8 @@ inline unsigned int getPinCount()
* *
* @return char* of pin name * @return char* of pin name
*/ */
inline std::string getPinName(int pin) inline std::string
getPinName(int pin)
{ {
std::string ret_val(mraa_get_pin_name(pin)); std::string ret_val(mraa_get_pin_name(pin));
return ret_val; return ret_val;
@@ -173,9 +185,9 @@ inline std::string getPinName(int pin)
* @param level * @param level
* @return Result of operation * @return Result of operation
*/ */
inline mraa_result_t setLogLevel(int level) inline mraa_result_t
setLogLevel(int level)
{ {
return mraa_set_log_level(level); return mraa_set_log_level(level);
} }
} }

View File

@@ -33,7 +33,8 @@
#endif #endif
#endif #endif
namespace mraa { namespace mraa
{
// These enums must match the enums in gpio.h // These enums must match the enums in gpio.h
@@ -74,7 +75,8 @@ typedef enum {
* *
* @snippet Blink-IO.cpp Interesting * @snippet Blink-IO.cpp Interesting
*/ */
class Gpio { class Gpio
{
public: public:
/** /**
* Instanciates a Gpio object * Instanciates a Gpio object
@@ -88,11 +90,11 @@ class Gpio {
* the kernel module. Note that you will not get any muxers set up for * the kernel module. Note that you will not get any muxers set up for
* you so this may not always work as expected. * you so this may not always work as expected.
*/ */
Gpio(int pin, bool owner=true, bool raw=false) { Gpio(int pin, bool owner = true, bool raw = false)
{
if (raw) { if (raw) {
m_gpio = mraa_gpio_init_raw(pin); m_gpio = mraa_gpio_init_raw(pin);
} } else {
else {
m_gpio = mraa_gpio_init(pin); m_gpio = mraa_gpio_init(pin);
} }
@@ -108,7 +110,8 @@ class Gpio {
* Gpio object destructor, this will only unexport the gpio if we where * Gpio object destructor, this will only unexport the gpio if we where
* the owner * the owner
*/ */
~Gpio() { ~Gpio()
{
mraa_gpio_close(m_gpio); mraa_gpio_close(m_gpio);
} }
/** /**
@@ -117,15 +120,21 @@ class Gpio {
* @param mode The edge mode to set * @param mode The edge mode to set
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t edge(Edge mode) { mraa_result_t
edge(Edge mode)
{
return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode); return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
} }
#if defined(SWIGPYTHON) #if defined(SWIGPYTHON)
mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) { mraa_result_t
isr(Edge mode, PyObject* pyfunc, PyObject* args)
{
return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args); return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
} }
#elif defined(SWIGJAVASCRIPT) #elif defined(SWIGJAVASCRIPT)
static void v8isr(uv_work_t* req, int status) { static void
v8isr(uv_work_t* req, int status)
{
mraa::Gpio* This = (mraa::Gpio*) req->data; mraa::Gpio* This = (mraa::Gpio*) req->data;
int argc = 1; int argc = 1;
v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) }; v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
@@ -138,18 +147,23 @@ class Gpio {
delete req; delete req;
} }
static void nop(uv_work_t* req) static void
nop(uv_work_t* req)
{ {
// Do nothing. // Do nothing.
} }
static void uvwork(void *ctx) { static void
uvwork(void* ctx)
{
uv_work_t* req = new uv_work_t; uv_work_t* req = new uv_work_t;
req->data = ctx; req->data = ctx;
uv_queue_work(uv_default_loop(), req, nop, v8isr); uv_queue_work(uv_default_loop(), req, nop, v8isr);
} }
mraa_result_t isr(Edge mode, v8::Handle<v8::Function> func) { mraa_result_t
isr(Edge mode, v8::Handle<v8::Function> func)
{
#if NODE_MODULE_VERSION >= 0x000D #if NODE_MODULE_VERSION >= 0x000D
m_v8isr.Reset(v8::Isolate::GetCurrent(), func); m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
#else #else
@@ -167,7 +181,9 @@ class Gpio {
* @param args Arguments passed to the interrupt handler (fptr) * @param args Arguments passed to the interrupt handler (fptr)
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) { mraa_result_t
isr(Edge mode, void (*fptr)(void*), void* args)
{
return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args); return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
} }
#endif #endif
@@ -177,7 +193,9 @@ class Gpio {
* *
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t isrExit() { mraa_result_t
isrExit()
{
#if defined(SWIGJAVASCRIPT) #if defined(SWIGJAVASCRIPT)
#if NODE_MODULE_VERSION >= 0x000D #if NODE_MODULE_VERSION >= 0x000D
m_v8isr.Reset(); m_v8isr.Reset();
@@ -194,7 +212,9 @@ class Gpio {
* @param mode The mode to change the gpio into * @param mode The mode to change the gpio into
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t mode(Mode mode) { mraa_result_t
mode(Mode mode)
{
return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode); return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
} }
/** /**
@@ -203,7 +223,9 @@ class Gpio {
* @param dir The direction to change the gpio into * @param dir The direction to change the gpio into
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t dir(Dir dir) { mraa_result_t
dir(Dir dir)
{
return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir); return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
} }
/** /**
@@ -211,7 +233,9 @@ class Gpio {
* *
* @return Gpio value * @return Gpio value
*/ */
int read() { int
read()
{
return mraa_gpio_read(m_gpio); return mraa_gpio_read(m_gpio);
} }
/** /**
@@ -220,7 +244,9 @@ class Gpio {
* @param value Value to write to Gpio * @param value Value to write to Gpio
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t write(int value) { mraa_result_t
write(int value)
{
return mraa_gpio_write(m_gpio, value); return mraa_gpio_write(m_gpio, value);
} }
/** /**
@@ -229,7 +255,9 @@ class Gpio {
* @param enable true to use mmap * @param enable true to use mmap
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t useMmap(bool enable) { mraa_result_t
useMmap(bool enable)
{
return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable); return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
} }
/** /**
@@ -239,17 +267,19 @@ class Gpio {
* @param raw (optional) get the raw gpio number. * @param raw (optional) get the raw gpio number.
* @return Pin number * @return Pin number
*/ */
int getPin(bool raw = false) { int
getPin(bool raw = false)
{
if (raw) { if (raw) {
return mraa_gpio_get_pin_raw(m_gpio); return mraa_gpio_get_pin_raw(m_gpio);
} }
return mraa_gpio_get_pin(m_gpio); return mraa_gpio_get_pin(m_gpio);
} }
private: private:
mraa_gpio_context m_gpio; mraa_gpio_context m_gpio;
#if defined(SWIGJAVASCRIPT) #if defined(SWIGJAVASCRIPT)
v8::Persistent<v8::Function> m_v8isr; v8::Persistent<v8::Function> m_v8isr;
#endif #endif
}; };
} }

View File

@@ -27,7 +27,8 @@
#include "i2c.h" #include "i2c.h"
#include <stdexcept> #include <stdexcept>
namespace mraa { namespace mraa
{
/** /**
* @brief API to Inter-Integrated Circuit * @brief API to Inter-Integrated Circuit
@@ -38,7 +39,8 @@ namespace mraa {
* *
* @snippet I2c-compass.cpp Interesting * @snippet I2c-compass.cpp Interesting
*/ */
class I2c { class I2c
{
public: public:
/** /**
* Instantiates an i2c bus. Multiple instances of the same bus can * Instantiates an i2c bus. Multiple instances of the same bus can
@@ -48,11 +50,11 @@ class I2c {
* @param bus The i2c bus to use * @param bus The i2c bus to use
* @param raw Whether to disable pinmapper for your board * @param raw Whether to disable pinmapper for your board
*/ */
I2c(int bus, bool raw=false) { I2c(int bus, bool raw = false)
{
if (raw) { if (raw) {
m_i2c = mraa_i2c_init_raw(bus); m_i2c = mraa_i2c_init_raw(bus);
} } else {
else {
m_i2c = mraa_i2c_init(bus); m_i2c = mraa_i2c_init(bus);
} }
if (m_i2c == NULL) { if (m_i2c == NULL) {
@@ -65,7 +67,8 @@ class I2c {
* be usable by anyone else or communicates this disconnect to any * be usable by anyone else or communicates this disconnect to any
* slaves. * slaves.
*/ */
~I2c() { ~I2c()
{
mraa_i2c_stop(m_i2c); mraa_i2c_stop(m_i2c);
} }
@@ -77,7 +80,9 @@ class I2c {
* @param mode Frequency to set the bus to * @param mode Frequency to set the bus to
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t frequency(mraa_i2c_mode_t mode) { mraa_result_t
frequency(mraa_i2c_mode_t mode)
{
return mraa_i2c_frequency(m_i2c, mode); return mraa_i2c_frequency(m_i2c, mode);
} }
@@ -88,7 +93,9 @@ class I2c {
* @param address Communicate to the i2c slave on this address * @param address Communicate to the i2c slave on this address
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t address(uint8_t address) { mraa_result_t
address(uint8_t address)
{
return mraa_i2c_address(m_i2c, address); return mraa_i2c_address(m_i2c, address);
} }
@@ -97,7 +104,9 @@ class I2c {
* *
* @return char read from the bus * @return char read from the bus
*/ */
uint8_t readByte() { uint8_t
readByte()
{
return (uint8_t) mraa_i2c_read_byte(m_i2c); return (uint8_t) mraa_i2c_read_byte(m_i2c);
} }
@@ -108,7 +117,9 @@ class I2c {
* @param length Size of read in bytes to make * @param length Size of read in bytes to make
* @return length of read, should match length * @return length of read, should match length
*/ */
int read(uint8_t *data, int length) { int
read(uint8_t* data, int length)
{
return mraa_i2c_read(m_i2c, data, length); return mraa_i2c_read(m_i2c, data, length);
} }
@@ -118,7 +129,9 @@ class I2c {
* @param reg Register to read from * @param reg Register to read from
* @return char read from register * @return char read from register
*/ */
uint8_t readReg(uint8_t reg) { uint8_t
readReg(uint8_t reg)
{
return mraa_i2c_read_byte_data(m_i2c, reg); return mraa_i2c_read_byte_data(m_i2c, reg);
} }
@@ -128,7 +141,9 @@ class I2c {
* @param reg Register to read from * @param reg Register to read from
* @return char read from register * @return char read from register
*/ */
uint16_t readWordReg(uint8_t reg) { uint16_t
readWordReg(uint8_t reg)
{
return mraa_i2c_read_word_data(m_i2c, reg); return mraa_i2c_read_word_data(m_i2c, reg);
} }
@@ -141,7 +156,9 @@ class I2c {
* @param length max number of bytes to read * @param length max number of bytes to read
* @return length passed to the function or 0 * @return length passed to the function or 0
*/ */
int readBytesReg(uint8_t reg, uint8_t* data, int length) { int
readBytesReg(uint8_t reg, uint8_t* data, int length)
{
return mraa_i2c_read_bytes_data(m_i2c, reg, data, length); return mraa_i2c_read_bytes_data(m_i2c, reg, data, length);
} }
@@ -151,7 +168,9 @@ class I2c {
* @param data The byte to send on the bus * @param data The byte to send on the bus
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t writeByte(uint8_t data) { mraa_result_t
writeByte(uint8_t data)
{
return mraa_i2c_write_byte(m_i2c, data); return mraa_i2c_write_byte(m_i2c, data);
} }
@@ -163,7 +182,9 @@ class I2c {
* @param length Size of buffer to send * @param length Size of buffer to send
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t write(const uint8_t* data, int length) { mraa_result_t
write(const uint8_t* data, int length)
{
return mraa_i2c_write(m_i2c, data, length); return mraa_i2c_write(m_i2c, data, length);
} }
@@ -174,7 +195,9 @@ class I2c {
* @param data Value to write to register * @param data Value to write to register
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t writeReg(uint8_t reg, uint8_t data) { mraa_result_t
writeReg(uint8_t reg, uint8_t data)
{
return mraa_i2c_write_byte_data(m_i2c, data, reg); return mraa_i2c_write_byte_data(m_i2c, data, reg);
} }
@@ -185,11 +208,13 @@ class I2c {
* @param data Value to write to register * @param data Value to write to register
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t writeWordReg(uint8_t reg, uint16_t data) { mraa_result_t
writeWordReg(uint8_t reg, uint16_t data)
{
return mraa_i2c_write_word_data(m_i2c, data, reg); return mraa_i2c_write_word_data(m_i2c, data, reg);
} }
private: private:
mraa_i2c_context m_i2c; mraa_i2c_context m_i2c;
}; };
} }

View File

@@ -27,7 +27,8 @@
#include "pwm.h" #include "pwm.h"
#include <stdexcept> #include <stdexcept>
namespace mraa { namespace mraa
{
/** /**
* @brief API to Pulse Width Modulation * @brief API to Pulse Width Modulation
@@ -36,7 +37,8 @@ namespace mraa {
* *
* @snippet Pwm3-cycle.cpp Interesting * @snippet Pwm3-cycle.cpp Interesting
*/ */
class Pwm { class Pwm
{
public: public:
/** /**
* instanciates a PWM object on a pin * instanciates a PWM object on a pin
@@ -47,11 +49,11 @@ class Pwm {
* unexport the pin from sysfs, default behaviour is you are the owner * unexport the pin from sysfs, default behaviour is you are the owner
* if the pinmapper exported it * if the pinmapper exported it
*/ */
Pwm(int pin, bool owner=true, int chipid=-1) { Pwm(int pin, bool owner = true, int chipid = -1)
{
if (chipid == -1) { if (chipid == -1) {
m_pwm = mraa_pwm_init(pin); m_pwm = mraa_pwm_init(pin);
} } else {
else {
m_pwm = mraa_pwm_init_raw(chipid, pin); m_pwm = mraa_pwm_init_raw(chipid, pin);
} }
@@ -66,7 +68,8 @@ class Pwm {
/** /**
* Pwm destructor * Pwm destructor
*/ */
~Pwm() { ~Pwm()
{
mraa_pwm_close(m_pwm); mraa_pwm_close(m_pwm);
} }
/** /**
@@ -78,7 +81,9 @@ class Pwm {
* 1.0f * 1.0f
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t write(float percentage) { mraa_result_t
write(float percentage)
{
return mraa_pwm_write(m_pwm, percentage); return mraa_pwm_write(m_pwm, percentage);
} }
/** /**
@@ -89,7 +94,9 @@ class Pwm {
* 1.0f Values above or below this range will be set at either 0.0f or * 1.0f Values above or below this range will be set at either 0.0f or
* 1.0f * 1.0f
*/ */
float read() { float
read()
{
return mraa_pwm_read(m_pwm); return mraa_pwm_read(m_pwm);
} }
/** /**
@@ -98,7 +105,9 @@ class Pwm {
* @param period Period represented as a float in seconds * @param period Period represented as a float in seconds
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t period(float period) { mraa_result_t
period(float period)
{
return mraa_pwm_period(m_pwm, period); return mraa_pwm_period(m_pwm, period);
} }
/** /**
@@ -107,7 +116,9 @@ class Pwm {
* @param ms milliseconds for period * @param ms milliseconds for period
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t period_ms(int ms) { mraa_result_t
period_ms(int ms)
{
return mraa_pwm_period_ms(m_pwm, ms); return mraa_pwm_period_ms(m_pwm, ms);
} }
/** /**
@@ -116,7 +127,9 @@ class Pwm {
* @param us microseconds as period * @param us microseconds as period
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t period_us(int us) { mraa_result_t
period_us(int us)
{
return mraa_pwm_period_us(m_pwm, us); return mraa_pwm_period_us(m_pwm, us);
} }
/** /**
@@ -125,7 +138,9 @@ class Pwm {
* @param seconds The duration of a pulse * @param seconds The duration of a pulse
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t pulsewidth(float seconds) { mraa_result_t
pulsewidth(float seconds)
{
return mraa_pwm_pulsewidth(m_pwm, seconds); return mraa_pwm_pulsewidth(m_pwm, seconds);
} }
/** /**
@@ -134,7 +149,9 @@ class Pwm {
* @param ms milliseconds for pulsewidth * @param ms milliseconds for pulsewidth
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t pulsewidth_ms(int ms) { mraa_result_t
pulsewidth_ms(int ms)
{
return mraa_pwm_pulsewidth_ms(m_pwm, ms); return mraa_pwm_pulsewidth_ms(m_pwm, ms);
} }
/** /**
@@ -143,7 +160,9 @@ class Pwm {
* @param us microseconds for pulsewidth * @param us microseconds for pulsewidth
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t pulsewidth_us(int us) { mraa_result_t
pulsewidth_us(int us)
{
return mraa_pwm_pulsewidth_us(m_pwm, us); return mraa_pwm_pulsewidth_us(m_pwm, us);
} }
/** /**
@@ -153,7 +172,9 @@ class Pwm {
* @param enable enable status of pin * @param enable enable status of pin
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t enable(bool enable) { mraa_result_t
enable(bool enable)
{
if (enable) if (enable)
return mraa_pwm_enable(m_pwm, 1); return mraa_pwm_enable(m_pwm, 1);
else else
@@ -166,7 +187,9 @@ class Pwm {
* @param duty represnted in ms as float. * @param duty represnted in ms as float.
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t config_ms(int period, float duty) { mraa_result_t
config_ms(int period, float duty)
{
return mraa_pwm_config_ms(m_pwm, period, duty); return mraa_pwm_config_ms(m_pwm, period, duty);
} }
/** /**
@@ -176,12 +199,13 @@ class Pwm {
* @param duty percentage i.e. 50% = 0.5f * @param duty percentage i.e. 50% = 0.5f
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t config_percent(int period, float duty) { mraa_result_t
config_percent(int period, float duty)
{
return mraa_pwm_config_percent(m_pwm, period, duty); return mraa_pwm_config_percent(m_pwm, period, duty);
} }
private: private:
mraa_pwm_context m_pwm; mraa_pwm_context m_pwm;
}; };
} }

View File

@@ -51,10 +51,14 @@ extern "C" {
* MRAA SPI Modes * MRAA SPI Modes
*/ */
typedef enum { typedef enum {
MRAA_SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge */ MRAA_SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge,
MRAA_SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */ output data (change) on falling edge */
MRAA_SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */ MRAA_SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge,
MRAA_SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge output data (change) on falling edge */ output data (change) on rising edge */
MRAA_SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge,
output data (change) on rising edge */
MRAA_SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge
output data (change) on falling edge */
} mraa_spi_mode_t; } mraa_spi_mode_t;
/** /**

View File

@@ -27,16 +27,21 @@
#include "spi.h" #include "spi.h"
#include <stdexcept> #include <stdexcept>
namespace mraa { namespace mraa
{
/** /**
* MRAA SPI Modes * MRAA SPI Modes
*/ */
typedef enum { typedef enum {
SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge, output data (change) on falling edge */ SPI_MODE0 = 0, /**< CPOL = 0, CPHA = 0, Clock idle low, data is clocked in on rising edge,
SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */ output data (change) on falling edge */
SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge, output data (change) on rising edge */ SPI_MODE1 = 1, /**< CPOL = 0, CPHA = 1, Clock idle low, data is clocked in on falling edge,
SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge output data (change) on falling edge */ output data (change) on rising edge */
SPI_MODE2 = 2, /**< CPOL = 1, CPHA = 0, Clock idle low, data is clocked in on falling edge,
output data (change) on rising edge */
SPI_MODE3 = 3, /**< CPOL = 1, CPHA = 1, Clock idle low, data is clocked in on rising, edge
output data (change) on falling edge */
} Spi_Mode; } Spi_Mode;
@@ -47,14 +52,16 @@ typedef enum {
* *
* @snippet Spi-pot.cpp Interesting * @snippet Spi-pot.cpp Interesting
*/ */
class Spi { class Spi
{
public: public:
/** /**
* Initialise SPI object using the board mapping to set muxes * Initialise SPI object using the board mapping to set muxes
* *
* @param bus to use, as listed in the platform definition, normally 0 * @param bus to use, as listed in the platform definition, normally 0
*/ */
Spi(int bus) { Spi(int bus)
{
m_spi = mraa_spi_init(bus); m_spi = mraa_spi_init(bus);
if (m_spi == NULL) { if (m_spi == NULL) {
@@ -65,7 +72,8 @@ class Spi {
/** /**
* Closes spi bus * Closes spi bus
*/ */
~Spi() { ~Spi()
{
mraa_spi_stop(m_spi); mraa_spi_stop(m_spi);
} }
@@ -75,7 +83,9 @@ class Spi {
* @param mode the mode. See Linux spidev doc * @param mode the mode. See Linux spidev doc
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t mode(Spi_Mode mode) { mraa_result_t
mode(Spi_Mode mode)
{
return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode); return mraa_spi_mode(m_spi, (mraa_spi_mode_t) mode);
} }
@@ -85,7 +95,9 @@ class Spi {
* @param hz the frequency to set in hz * @param hz the frequency to set in hz
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t frequency(int hz) { mraa_result_t
frequency(int hz)
{
return mraa_spi_frequency(m_spi, hz); return mraa_spi_frequency(m_spi, hz);
} }
@@ -95,7 +107,9 @@ class Spi {
* @param data the byte to send * @param data the byte to send
* @return data received on the miso line or -1 in case of error * @return data received on the miso line or -1 in case of error
*/ */
int writeByte(uint8_t data) { int
writeByte(uint8_t data)
{
return mraa_spi_write(m_spi, (uint8_t) data); return mraa_spi_write(m_spi, (uint8_t) data);
} }
@@ -105,7 +119,9 @@ class Spi {
* @param data the byte to send * @param data the byte to send
* @return data received on the miso line * @return data received on the miso line
*/ */
uint16_t write_word(uint16_t data) { uint16_t
write_word(uint16_t data)
{
return mraa_spi_write_word(m_spi, (uint16_t) data); return mraa_spi_write_word(m_spi, (uint16_t) data);
} }
@@ -118,7 +134,9 @@ class Spi {
* @param length size of buffer to send * @param length size of buffer to send
* @return uint8_t* data received on the miso line. Same length as passed in * @return uint8_t* data received on the miso line. Same length as passed in
*/ */
uint8_t* write(uint8_t* txBuf, int length) { uint8_t*
write(uint8_t* txBuf, int length)
{
return mraa_spi_write_buf(m_spi, txBuf, length); return mraa_spi_write_buf(m_spi, txBuf, length);
} }
@@ -131,7 +149,9 @@ class Spi {
* @param length size of buffer (in bytes) to send * @param length size of buffer (in bytes) to send
* @return uint8_t* data received on the miso line. Same length as passed in * @return uint8_t* data received on the miso line. Same length as passed in
*/ */
uint16_t* write_word(uint16_t* txBuf, int length) { uint16_t*
write_word(uint16_t* txBuf, int length)
{
return mraa_spi_write_buf_word(m_spi, txBuf, length); return mraa_spi_write_buf_word(m_spi, txBuf, length);
} }
@@ -145,7 +165,9 @@ class Spi {
* @param length size of buffer to send * @param length size of buffer to send
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t transfer(uint8_t* txBuf, uint8_t* rxBuf, int length) { mraa_result_t
transfer(uint8_t* txBuf, uint8_t* rxBuf, int length)
{
return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length); return mraa_spi_transfer_buf(m_spi, txBuf, rxBuf, length);
} }
@@ -158,7 +180,9 @@ class Spi {
* @param length size of buffer to send * @param length size of buffer to send
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length) { mraa_result_t
transfer_word(uint16_t* txBuf, uint16_t* rxBuf, int length)
{
return mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length); return mraa_spi_transfer_buf_word(m_spi, txBuf, rxBuf, length);
} }
#endif #endif
@@ -169,7 +193,9 @@ class Spi {
* @param lsb Use least significant bit transmission - 0 for msbi * @param lsb Use least significant bit transmission - 0 for msbi
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t lsbmode(bool lsb) { mraa_result_t
lsbmode(bool lsb)
{
return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb); return mraa_spi_lsbmode(m_spi, (mraa_boolean_t) lsb);
} }
@@ -179,7 +205,9 @@ class Spi {
* @param bits bits per word * @param bits bits per word
* @return Result of operation * @return Result of operation
*/ */
mraa_result_t bitPerWord(unsigned int bits) { mraa_result_t
bitPerWord(unsigned int bits)
{
return mraa_spi_bit_per_word(m_spi, bits); return mraa_spi_bit_per_word(m_spi, bits);
} }

View File

@@ -44,7 +44,8 @@ typedef enum {
MRAA_INTEL_MINNOWBOARD_MAX = 4, /**< The Intel Minnow Board Max */ MRAA_INTEL_MINNOWBOARD_MAX = 4, /**< The Intel Minnow Board Max */
MRAA_RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */ MRAA_RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */
MRAA_UNKNOWN_PLATFORM = 99 /**< An unknown platform type, typically will load INTEL_GALILEO_GEN1 */ MRAA_UNKNOWN_PLATFORM =
99 /**< An unknown platform type, typically will load INTEL_GALILEO_GEN1 */
} mraa_platform_t; } mraa_platform_t;
/** /**

View File

@@ -27,14 +27,16 @@
#include "uart.h" #include "uart.h"
#include <stdexcept> #include <stdexcept>
namespace mraa { namespace mraa
{
/** /**
* @brief API to UART (enabling only) * @brief API to UART (enabling only)
* *
* This file defines the UART interface for libmraa * This file defines the UART interface for libmraa
*/ */
class Uart { class Uart
{
public: public:
/** /**
* Uart Constructor, takes a pin number which will map directly to the * Uart Constructor, takes a pin number which will map directly to the
@@ -42,7 +44,8 @@ class Uart {
* *
* @param uart the index of the uart set to use * @param uart the index of the uart set to use
*/ */
Uart(int uart) { Uart(int uart)
{
m_uart = mraa_uart_init(uart); m_uart = mraa_uart_init(uart);
if (m_uart == NULL) { if (m_uart == NULL) {
@@ -52,7 +55,8 @@ class Uart {
/** /**
* Uart destructor * Uart destructor
*/ */
~Uart() { ~Uart()
{
return; return;
} }
@@ -62,12 +66,14 @@ class Uart {
* *
* @return char pointer of device path * @return char pointer of device path
*/ */
std::string getDevicePath() { std::string
getDevicePath()
{
std::string ret_val(mraa_uart_get_dev_path(m_uart)); std::string ret_val(mraa_uart_get_dev_path(m_uart));
return ret_val; return ret_val;
} }
private: private:
mraa_uart_context m_uart; mraa_uart_context m_uart;
}; };
} }

View File

@@ -23,7 +23,8 @@ Basic rules
- Try not to break master. In any commit. - Try not to break master. In any commit.
- Try to split commits up logically, you will be asked to rebase them if they - Try to split commits up logically, you will be asked to rebase them if they
are not. are not.
- Try to stick to the established coding style regardless of your personal feeling for it! - Try to stick to the established coding style regardless of your personal
feeling for it! Use clang-format (3.6+ required)
Code signing Code signing
------------ ------------

View File

@@ -26,7 +26,8 @@
//! [Interesting] //! [Interesting]
#include "mraa/aio.h" #include "mraa/aio.h"
int main () int
main()
{ {
mraa_aio_context adc_a0; mraa_aio_context adc_a0;
uint16_t adc_value = 0; uint16_t adc_value = 0;

View File

@@ -57,8 +57,7 @@ main(int argc, char **argv)
} }
mraa_init(); mraa_init();
fprintf(stdout, "MRAA Version: %s\nStarting Blinking on IO%d\n", fprintf(stdout, "MRAA Version: %s\nStarting Blinking on IO%d\n", mraa_get_version(), iopin);
mraa_get_version(), iopin);
mraa_gpio_context gpio; mraa_gpio_context gpio;
gpio = mraa_gpio_init(iopin); gpio = mraa_gpio_init(iopin);

View File

@@ -50,8 +50,7 @@ main(int argc, char **argv)
gpio = mraa_gpio_init(13); gpio = mraa_gpio_init(13);
} }
fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s\n", mraa_get_version(), board_name);
mraa_get_version(), board_name);
if (gpio == NULL) { if (gpio == NULL) {

View File

@@ -25,7 +25,8 @@
//! [Interesting] //! [Interesting]
#include "mraa.hpp" #include "mraa.hpp"
int main () int
main()
{ {
uint16_t adc_value; uint16_t adc_value;
float adc_value_float; float adc_value_float;

View File

@@ -43,7 +43,8 @@ sig_handler(int signo)
running = -1; running = -1;
} }
} }
int main (int argc, char **argv) int
main(int argc, char** argv)
{ {
if (argc < 2) { if (argc < 2) {
printf("Provide an int arg if you want to flash on something other than %d\n", DEFAULT_IOPIN); printf("Provide an int arg if you want to flash on something other than %d\n", DEFAULT_IOPIN);

View File

@@ -91,7 +91,8 @@ sig_handler(int signo)
} }
} }
int main () int
main()
{ {
float direction = 0; float direction = 0;
int16_t x = 0, y = 0, z = 0; int16_t x = 0, y = 0, z = 0;
@@ -132,7 +133,8 @@ int main ()
if (direction < 0) if (direction < 0)
direction += 2 * M_PI; direction += 2 * M_PI;
printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG, z * SCALE_0_92_MG) ; printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG,
z * SCALE_0_92_MG);
printf("Heading : %f\n", direction * 180 / M_PI); printf("Heading : %f\n", direction * 180 / M_PI);
sleep(1); sleep(1);
} }

View File

@@ -38,7 +38,8 @@ sig_handler(int signo)
} }
} }
int main () int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
//! [Interesting] //! [Interesting]

View File

@@ -39,7 +39,8 @@ sig_handler(int signo)
} }
} }
int main () int
main()
{ {
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
@@ -71,7 +72,6 @@ int main ()
} }
usleep(100000); usleep(100000);
} }
} }
delete spi; delete spi;
//! [Interesting] //! [Interesting]

View File

@@ -34,12 +34,14 @@ struct gpio_source {
}; };
void void
print_version() { print_version()
{
fprintf(stdout, "Version %s on %s\n", mraa_get_version(), mraa_get_platform_name()); fprintf(stdout, "Version %s on %s\n", mraa_get_version(), mraa_get_platform_name());
} }
void void
print_help() { print_help()
{
fprintf(stdout, "list List pins\n"); fprintf(stdout, "list List pins\n");
fprintf(stdout, "set pin level Set pin to level (0/1)\n"); fprintf(stdout, "set pin level Set pin to level (0/1)\n");
fprintf(stdout, "setraw pin level Set pin to level (0/1) via mmap (if available)\n"); fprintf(stdout, "setraw pin level Set pin to level (0/1) via mmap (if available)\n");
@@ -50,13 +52,15 @@ print_help() {
} }
void void
print_command_error() { print_command_error()
{
fprintf(stdout, "Invalid command, options are:\n"); fprintf(stdout, "Invalid command, options are:\n");
print_help(); print_help();
} }
void void
list_pins() { list_pins()
{
int pin_count = mraa_get_pin_count(); int pin_count = mraa_get_pin_count();
if (pin_count == 0) { if (pin_count == 0) {
fprintf(stdout, "No Pins\n"); fprintf(stdout, "No Pins\n");
@@ -85,13 +89,15 @@ list_pins() {
} }
mraa_result_t mraa_result_t
gpio_set(int pin, int level, mraa_boolean_t raw) { gpio_set(int pin, int level, mraa_boolean_t raw)
{
mraa_gpio_context gpio = mraa_gpio_init(pin); mraa_gpio_context gpio = mraa_gpio_init(pin);
if (gpio != NULL) { if (gpio != NULL) {
mraa_gpio_dir(gpio, MRAA_GPIO_OUT); mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
if (raw != 0) { if (raw != 0) {
if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) { if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) {
fprintf(stdout, "mmapped access to gpio not supported, falling back to normal mode\n", pin); fprintf(stdout,
"mmapped access to gpio not supported, falling back to normal mode\n", pin);
} }
} }
mraa_gpio_write(gpio, level); mraa_gpio_write(gpio, level);
@@ -101,13 +107,15 @@ gpio_set(int pin, int level, mraa_boolean_t raw) {
} }
mraa_result_t mraa_result_t
gpio_get(int pin, int *level, mraa_boolean_t raw) { gpio_get(int pin, int* level, mraa_boolean_t raw)
{
mraa_gpio_context gpio = mraa_gpio_init(pin); mraa_gpio_context gpio = mraa_gpio_init(pin);
if (gpio != NULL) { if (gpio != NULL) {
mraa_gpio_dir(gpio, MRAA_GPIO_IN); mraa_gpio_dir(gpio, MRAA_GPIO_IN);
if (raw != 0) { if (raw != 0) {
if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) { if (mraa_gpio_use_mmaped(gpio, 1) != MRAA_SUCCESS) {
fprintf(stdout, "mmapped access to gpio not supported, falling back to normal mode\n", pin); fprintf(stdout,
"mmapped access to gpio not supported, falling back to normal mode\n", pin);
} }
} }
*level = mraa_gpio_read(gpio); *level = mraa_gpio_read(gpio);
@@ -117,14 +125,17 @@ gpio_get(int pin, int *level, mraa_boolean_t raw) {
} }
void gpio_isr_handler(void * args) { void
gpio_isr_handler(void* args)
{
struct gpio_source* gpio_info = (struct gpio_source*) args; struct gpio_source* gpio_info = (struct gpio_source*) args;
int level = mraa_gpio_read(gpio_info->context); int level = mraa_gpio_read(gpio_info->context);
fprintf(stdout, "Pin %d = %d\n", gpio_info->pin, level); fprintf(stdout, "Pin %d = %d\n", gpio_info->pin, level);
} }
mraa_result_t mraa_result_t
gpio_isr_start(struct gpio_source* gpio_info) { gpio_isr_start(struct gpio_source* gpio_info)
{
gpio_info->context = mraa_gpio_init(gpio_info->pin); gpio_info->context = mraa_gpio_init(gpio_info->pin);
if (gpio_info->context != NULL) { if (gpio_info->context != NULL) {
mraa_result_t status = mraa_gpio_dir(gpio_info->context, MRAA_GPIO_IN); mraa_result_t status = mraa_gpio_dir(gpio_info->context, MRAA_GPIO_IN);
@@ -139,7 +150,8 @@ gpio_isr_start(struct gpio_source* gpio_info) {
mraa_result_t mraa_result_t
gpio_isr_stop(struct gpio_source* gpio_info) { gpio_isr_stop(struct gpio_source* gpio_info)
{
mraa_gpio_isr_exit(gpio_info->context); mraa_gpio_isr_exit(gpio_info->context);
mraa_gpio_close(gpio_info->context); mraa_gpio_close(gpio_info->context);
return MRAA_SUCCESS; return MRAA_SUCCESS;
@@ -176,8 +188,7 @@ main(int argc, char **argv)
mraa_boolean_t rawmode = strcmp(argv[1], "getraw") == 0; mraa_boolean_t rawmode = strcmp(argv[1], "getraw") == 0;
if (gpio_get(pin, &level, rawmode) == MRAA_SUCCESS) { if (gpio_get(pin, &level, rawmode) == MRAA_SUCCESS) {
fprintf(stdout, "Pin %d = %d\n", pin, level); fprintf(stdout, "Pin %d = %d\n", pin, level);
} } else {
else {
fprintf(stdout, "Could not initialize gpio %d\n", pin); fprintf(stdout, "Could not initialize gpio %d\n", pin);
} }
} else { } else {
@@ -191,10 +202,10 @@ main(int argc, char **argv)
if (gpio_isr_start(&gpio_info) == MRAA_SUCCESS) { if (gpio_isr_start(&gpio_info) == MRAA_SUCCESS) {
fprintf(stdout, "Monitoring level changes to pin %d. Press RETURN to exit.\n", pin); fprintf(stdout, "Monitoring level changes to pin %d. Press RETURN to exit.\n", pin);
gpio_isr_handler(&gpio_info); gpio_isr_handler(&gpio_info);
while (getchar() != '\n'); while (getchar() != '\n')
;
gpio_isr_stop(&gpio_info); gpio_isr_stop(&gpio_info);
} } else {
else {
fprintf(stdout, "Failed to register ISR for pin %d\n", pin); fprintf(stdout, "Failed to register ISR for pin %d\n", pin);
} }
} else { } else {

View File

@@ -31,8 +31,7 @@ int
main(int argc, char** argv) main(int argc, char** argv)
{ {
mraa_init(); mraa_init();
fprintf(stdout, "MRAA Version: %s\nStarting Read on IO6\n", fprintf(stdout, "MRAA Version: %s\nStarting Read on IO6\n", mraa_get_version());
mraa_get_version());
//! [Interesting] //! [Interesting]
mraa_gpio_context gpio; mraa_gpio_context gpio;

View File

@@ -129,7 +129,8 @@ main(int argc, char **argv)
if (direction < 0) if (direction < 0)
direction += 2 * M_PI; direction += 2 * M_PI;
printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG, z * SCALE_0_92_MG) ; printf("Compass scaled data x : %f, y : %f, z : %f\n", x * SCALE_0_92_MG, y * SCALE_0_92_MG,
z * SCALE_0_92_MG);
printf("Heading : %f\n", direction * 180 / M_PI); printf("Heading : %f\n", direction * 180 / M_PI);
} }
} }

View File

@@ -29,11 +29,14 @@
static volatile int counter = 0; static volatile int counter = 0;
static volatile int oldcounter = 0; static volatile int oldcounter = 0;
void interrupt (void * args) { void
interrupt(void* args)
{
++counter; ++counter;
} }
int main () int
main()
{ {
mraa_init(); mraa_init();
mraa_gpio_context x; mraa_gpio_context x;

View File

@@ -28,7 +28,8 @@
#include <stdint.h> #include <stdint.h>
int int
main(int argc, char **argv) { main(int argc, char** argv)
{
//! [Interesting] //! [Interesting]
mraa_spi_context spi; mraa_spi_context spi;
spi = mraa_spi_init(1); spi = mraa_spi_init(1);

View File

@@ -42,13 +42,11 @@ aio_get_valid_fp(mraa_aio_context dev)
char file_path[64] = ""; char file_path[64] = "";
// Open file Analog device input channel raw voltage file for reading. // Open file Analog device input channel raw voltage file for reading.
snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw", snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device0/in_voltage%d_raw", dev->channel);
dev->channel );
dev->adc_in_fp = open(file_path, O_RDONLY); dev->adc_in_fp = open(file_path, O_RDONLY);
if (dev->adc_in_fp == -1) { if (dev->adc_in_fp == -1) {
syslog(LOG_ERR, "aio: Failed to open input raw file %s for reading!", syslog(LOG_ERR, "aio: Failed to open input raw file %s for reading!", file_path);
file_path);
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }
@@ -90,7 +88,8 @@ mraa_aio_init(unsigned int aio)
mraa_aio_context dev = malloc(sizeof(struct _aio)); mraa_aio_context dev = malloc(sizeof(struct _aio));
if (dev == NULL) { if (dev == NULL) {
syslog(LOG_ERR, "aio: Insufficient memory for specified input channel " syslog(LOG_ERR, "aio: Insufficient memory for specified input channel "
"%d\n", aio); "%d\n",
aio);
return NULL; return NULL;
} }
dev->channel = plat->pins[pin].aio.pinmap; dev->channel = plat->pins[pin].aio.pinmap;
@@ -140,8 +139,7 @@ mraa_aio_read(mraa_aio_context dev)
unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10); unsigned int analog_value = (unsigned int) strtoul(buffer, &end, 10);
if (end == &buffer[0]) { if (end == &buffer[0]) {
syslog(LOG_ERR, "aio: Value is not a decimal number"); syslog(LOG_ERR, "aio: Value is not a decimal number");
} } else if (errno != 0) {
else if (errno != 0) {
syslog(LOG_ERR, "aio: Errno was set"); syslog(LOG_ERR, "aio: Errno was set");
} }

View File

@@ -65,12 +65,14 @@ static unsigned int mmap_count = 0;
static int platform_detected = 0; static int platform_detected = 0;
mraa_result_t mraa_result_t
mraa_raspberry_pi_spi_init_pre(int index) { mraa_raspberry_pi_spi_init_pre(int index)
{
char devpath[MAX_SIZE]; char devpath[MAX_SIZE];
sprintf(devpath, "/dev/spidev%u.0", plat->spi_bus[index].bus_id); sprintf(devpath, "/dev/spidev%u.0", plat->spi_bus[index].bus_id);
if (!mraa_file_exist(devpath)) { if (!mraa_file_exist(devpath)) {
syslog(LOG_ERR, "spi: Device not initialized"); syslog(LOG_ERR, "spi: Device not initialized");
syslog(LOG_ERR, "spi: If you run a kernel >=3.18 then you will have to add dtparam=spi=on to /boot/config.txt and reboot"); syslog(LOG_ERR, "spi: If you run a kernel >=3.18 then you will have to add dtparam=spi=on "
"to /boot/config.txt and reboot");
syslog(LOG_INFO, "spi: trying modprobe for spi-bcm2708"); syslog(LOG_INFO, "spi: trying modprobe for spi-bcm2708");
system("modprobe spi-bcm2708 >/dev/null 2>&1"); system("modprobe spi-bcm2708 >/dev/null 2>&1");
system("modprobe spi_bcm2708 >/dev/null 2>&1"); system("modprobe spi_bcm2708 >/dev/null 2>&1");
@@ -82,7 +84,8 @@ mraa_raspberry_pi_spi_init_pre(int index) {
} }
mraa_result_t mraa_result_t
mraa_raspberry_pi_i2c_init_pre(unsigned int bus) { mraa_raspberry_pi_i2c_init_pre(unsigned int bus)
{
char devpath[MAX_SIZE]; char devpath[MAX_SIZE];
sprintf(devpath, "/dev/i2c-%u", bus); sprintf(devpath, "/dev/i2c-%u", bus);
if (!mraa_file_exist(devpath)) { if (!mraa_file_exist(devpath)) {
@@ -95,10 +98,11 @@ mraa_raspberry_pi_i2c_init_pre(unsigned int bus) {
if (!mraa_file_exist(devpath)) { if (!mraa_file_exist(devpath)) {
syslog(LOG_ERR, "i2c: Device not initialized"); syslog(LOG_ERR, "i2c: Device not initialized");
if (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_1) { if (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_1) {
syslog(LOG_ERR, "i2c: If you run a kernel >=3.18 then you will have to add dtparam=i2c0=on to /boot/config.txt and reboot"); syslog(LOG_ERR, "i2c: If you run a kernel >=3.18 then you will have to add "
} "dtparam=i2c0=on to /boot/config.txt and reboot");
else { } else {
syslog(LOG_ERR, "i2c: If you run a kernel >=3.18 then you will have to add dtparam=i2c1=on to /boot/config.txt and reboot"); syslog(LOG_ERR, "i2c: If you run a kernel >=3.18 then you will have to add "
"dtparam=i2c1=on to /boot/config.txt and reboot");
} }
return MRAA_ERROR_NO_RESOURCES; return MRAA_ERROR_NO_RESOURCES;
} }
@@ -112,8 +116,7 @@ mraa_raspberry_pi_mmap_write(mraa_gpio_context dev, int value)
if (value) { if (value) {
*(volatile uint32_t*) (mmap_reg + BCM283X_GPSET0 + (dev->pin / 32) * 4) = *(volatile uint32_t*) (mmap_reg + BCM283X_GPSET0 + (dev->pin / 32) * 4) =
(uint32_t)(1 << (dev->pin % 32)); (uint32_t)(1 << (dev->pin % 32));
} } else {
else {
*(volatile uint32_t*) (mmap_reg + BCM283X_GPCLR0 + (dev->pin / 32) * 4) = *(volatile uint32_t*) (mmap_reg + BCM283X_GPCLR0 + (dev->pin / 32) * 4) =
(uint32_t)(1 << (dev->pin % 32)); (uint32_t)(1 << (dev->pin % 32));
} }
@@ -182,16 +185,11 @@ mraa_raspberry_pi_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
} }
if (platform_detected == PLATFORM_RASPBERRY_PI2_B_REV_1) { if (platform_detected == PLATFORM_RASPBERRY_PI2_B_REV_1) {
mmap_reg = (uint8_t*) mmap(NULL, BCM2836_BLOCK_SIZE, mmap_reg = (uint8_t*) mmap(NULL, BCM2836_BLOCK_SIZE, PROT_READ | PROT_WRITE,
PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, mmap_fd, BCM2836_GPIO_BASE);
MAP_FILE | MAP_SHARED, } else {
mmap_fd, BCM2836_GPIO_BASE); mmap_reg = (uint8_t*) mmap(NULL, BCM2835_BLOCK_SIZE, PROT_READ | PROT_WRITE,
} MAP_FILE | MAP_SHARED, mmap_fd, BCM2835_GPIO_BASE);
else {
mmap_reg = (uint8_t*) mmap(NULL, BCM2835_BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_FILE | MAP_SHARED,
mmap_fd, BCM2835_GPIO_BASE);
} }
if (mmap_reg == MAP_FAILED) { if (mmap_reg == MAP_FAILED) {
syslog(LOG_ERR, "raspberry mmap: failed to mmap"); syslog(LOG_ERR, "raspberry mmap: failed to mmap");
@@ -208,7 +206,8 @@ mraa_raspberry_pi_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
} }
mraa_board_t* mraa_board_t*
mraa_raspberry_pi() { mraa_raspberry_pi()
{
mraa_board_t* b = (mraa_board_t*) malloc(sizeof(mraa_board_t)); mraa_board_t* b = (mraa_board_t*) malloc(sizeof(mraa_board_t));
if (b == NULL) { if (b == NULL) {
return NULL; return NULL;
@@ -225,39 +224,32 @@ mraa_raspberry_pi() {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_1; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_1;
platform_detected = PLATFORM_RASPBERRY_PI_B_REV_1; platform_detected = PLATFORM_RASPBERRY_PI_B_REV_1;
b->phy_pin_count = MRAA_RASPBERRY_PI_B_REV_1_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_B_REV_1_PINCOUNT;
} } else if (strstr(line, "0004") || strstr(line, "0005") || strstr(line, "0006") ||
else if (strstr(line, "0004") || strstr(line, "0005") || strstr(line, "0006") \ strstr(line, "000d") || strstr(line, "000e") || strstr(line, "000f")) {
|| strstr(line, "000d") || strstr(line, "000e") || strstr(line, "000f")) {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_2; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_2;
platform_detected = PLATFORM_RASPBERRY_PI_B_REV_2; platform_detected = PLATFORM_RASPBERRY_PI_B_REV_2;
b->phy_pin_count = MRAA_RASPBERRY_PI_AB_REV_2_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_AB_REV_2_PINCOUNT;
} } else if (strstr(line, "0007") || strstr(line, "0008") || strstr(line, "0009")) {
else if (strstr(line, "0007") || strstr(line, "0008") || strstr(line, "0009")) {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_A_REV_2; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_A_REV_2;
platform_detected = PLATFORM_RASPBERRY_PI_A_REV_2; platform_detected = PLATFORM_RASPBERRY_PI_A_REV_2;
b->phy_pin_count = MRAA_RASPBERRY_PI_AB_REV_2_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_AB_REV_2_PINCOUNT;
} } else if (strstr(line, "0010")) {
else if (strstr(line, "0010")) {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_PLUS_REV_1; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_PLUS_REV_1;
platform_detected = PLATFORM_RASPBERRY_PI_B_PLUS_REV_1; platform_detected = PLATFORM_RASPBERRY_PI_B_PLUS_REV_1;
b->phy_pin_count = MRAA_RASPBERRY_PI_AB_PLUS_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_AB_PLUS_PINCOUNT;
} } else if (strstr(line, "0011")) {
else if (strstr(line, "0011")) {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_COMPUTE_MODULE_REV_1; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_COMPUTE_MODULE_REV_1;
platform_detected = PLATFORM_RASPBERRY_PI_COMPUTE_MODULE_REV_1; platform_detected = PLATFORM_RASPBERRY_PI_COMPUTE_MODULE_REV_1;
b->phy_pin_count = MRAA_RASPBERRY_PI_COMPUTE_MODULE_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_COMPUTE_MODULE_PINCOUNT;
} } else if (strstr(line, "0012")) {
else if (strstr(line, "0012")) {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_A_PLUS_REV_1; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_A_PLUS_REV_1;
platform_detected = PLATFORM_RASPBERRY_PI_A_PLUS_REV_1; platform_detected = PLATFORM_RASPBERRY_PI_A_PLUS_REV_1;
b->phy_pin_count = MRAA_RASPBERRY_PI_AB_PLUS_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_AB_PLUS_PINCOUNT;
} } else if (strstr(line, "a01041")) {
else if (strstr(line, "a01041")) {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI2_B_REV_1; b->platform_name = PLATFORM_NAME_RASPBERRY_PI2_B_REV_1;
platform_detected = PLATFORM_RASPBERRY_PI2_B_REV_1; platform_detected = PLATFORM_RASPBERRY_PI2_B_REV_1;
b->phy_pin_count = MRAA_RASPBERRY_PI2_B_REV_1_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI2_B_REV_1_PINCOUNT;
} } else {
else {
b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_1; b->platform_name = PLATFORM_NAME_RASPBERRY_PI_B_REV_1;
platform_detected = PLATFORM_RASPBERRY_PI_B_REV_1; platform_detected = PLATFORM_RASPBERRY_PI_B_REV_1;
b->phy_pin_count = MRAA_RASPBERRY_PI_B_REV_1_PINCOUNT; b->phy_pin_count = MRAA_RASPBERRY_PI_B_REV_1_PINCOUNT;
@@ -419,7 +411,7 @@ mraa_raspberry_pi() {
b->pins[26].spi.pinmap = 0; b->pins[26].spi.pinmap = 0;
b->pins[26].spi.mux_total = 0; b->pins[26].spi.mux_total = 0;
if ((platform_detected == PLATFORM_RASPBERRY_PI_A_REV_2) || \ if ((platform_detected == PLATFORM_RASPBERRY_PI_A_REV_2) ||
(platform_detected == PLATFORM_RASPBERRY_PI_B_REV_2)) { (platform_detected == PLATFORM_RASPBERRY_PI_B_REV_2)) {
strncpy(b->pins[27].name, "5V", 8); strncpy(b->pins[27].name, "5V", 8);
b->pins[27].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 }; b->pins[27].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
@@ -452,7 +444,6 @@ mraa_raspberry_pi() {
strncpy(b->pins[34].name, "GND", 8); strncpy(b->pins[34].name, "GND", 8);
b->pins[34].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 }; b->pins[34].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 0 };
} }
// BUS DEFINITIONS // BUS DEFINITIONS
@@ -479,8 +470,8 @@ mraa_raspberry_pi() {
b->uart_dev[0].rx = 10; b->uart_dev[0].rx = 10;
b->uart_dev[0].tx = 8; b->uart_dev[0].tx = 8;
if ((platform_detected == PLATFORM_RASPBERRY_PI_A_PLUS_REV_1) || \ if ((platform_detected == PLATFORM_RASPBERRY_PI_A_PLUS_REV_1) ||
(platform_detected == PLATFORM_RASPBERRY_PI_B_PLUS_REV_1) || \ (platform_detected == PLATFORM_RASPBERRY_PI_B_PLUS_REV_1) ||
(platform_detected == PLATFORM_RASPBERRY_PI2_B_REV_1)) { (platform_detected == PLATFORM_RASPBERRY_PI2_B_REV_1)) {
strncpy(b->pins[27].name, "ID_SD", 8); strncpy(b->pins[27].name, "ID_SD", 8);

View File

@@ -380,7 +380,6 @@ mraa_gpio_mode(mraa_gpio_context dev, gpio_mode_t mode)
syslog(LOG_ERR, "gpio: Failed to write to drive mode"); syslog(LOG_ERR, "gpio: Failed to write to drive mode");
close(drive); close(drive);
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }
close(drive); close(drive);
@@ -472,8 +471,7 @@ mraa_gpio_read(mraa_gpio_context dev)
syslog(LOG_ERR, "gpio: Failed to get value file pointer"); syslog(LOG_ERR, "gpio: Failed to get value file pointer");
return -1; return -1;
} }
} } else {
else {
// if value_fp is new this is pointless // if value_fp is new this is pointless
lseek(dev->value_fp, 0, SEEK_SET); lseek(dev->value_fp, 0, SEEK_SET);
} }

View File

@@ -37,16 +37,14 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "linux/i2c-dev.h" #include "linux/i2c-dev.h"
typedef union i2c_smbus_data_union typedef union i2c_smbus_data_union {
{
uint8_t byte; ///< data byte uint8_t byte; ///< data byte
unsigned short word; ///< data short word unsigned short word; ///< data short word
uint8_t block[I2C_SMBUS_BLOCK_MAX + 2]; uint8_t block[I2C_SMBUS_BLOCK_MAX + 2];
///< block[0] is used for length and one more for PEC ///< block[0] is used for length and one more for PEC
} i2c_smbus_data_t; } i2c_smbus_data_t;
typedef struct i2c_smbus_ioctl_data_struct typedef struct i2c_smbus_ioctl_data_struct {
{
uint8_t read_write; ///< operation direction uint8_t read_write; ///< operation direction
uint8_t command; ///< ioctl command uint8_t command; ///< ioctl command
int size; ///< data size int size; ///< data size
@@ -54,8 +52,7 @@ typedef struct i2c_smbus_ioctl_data_struct
} i2c_smbus_ioctl_data_t; } i2c_smbus_ioctl_data_t;
int int
mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size, mraa_i2c_smbus_access(int fh, uint8_t read_write, uint8_t command, int size, i2c_smbus_data_t* data)
i2c_smbus_data_t *data)
{ {
i2c_smbus_ioctl_data_t args; i2c_smbus_ioctl_data_t args;
@@ -166,8 +163,7 @@ mraa_i2c_read_byte(mraa_i2c_context dev)
{ {
i2c_smbus_data_t d; i2c_smbus_data_t d;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD, if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, I2C_NOCMD, I2C_SMBUS_BYTE, &d) < 0) {
I2C_SMBUS_BYTE, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write"); syslog(LOG_ERR, "i2c: Failed to write");
return 0; return 0;
} }
@@ -179,8 +175,7 @@ mraa_i2c_read_byte_data(mraa_i2c_context dev, uint8_t command)
{ {
i2c_smbus_data_t d; i2c_smbus_data_t d;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, I2C_SMBUS_BYTE_DATA, &d) < 0) {
I2C_SMBUS_BYTE_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write"); syslog(LOG_ERR, "i2c: Failed to write");
return 0; return 0;
} }
@@ -192,8 +187,7 @@ mraa_i2c_read_word_data(mraa_i2c_context dev, uint8_t command)
{ {
i2c_smbus_data_t d; i2c_smbus_data_t d;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_READ, command, I2C_SMBUS_WORD_DATA, &d) < 0) {
I2C_SMBUS_WORD_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write"); syslog(LOG_ERR, "i2c: Failed to write");
return 0; return 0;
} }
@@ -239,15 +233,13 @@ mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length)
} }
d.block[0] = length; d.block[0] = length;
return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, return mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_I2C_BLOCK_DATA, &d);
I2C_SMBUS_I2C_BLOCK_DATA, &d);
} }
mraa_result_t mraa_result_t
mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data) mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
{ {
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data, if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) < 0) {
I2C_SMBUS_BYTE, NULL) < 0) {
syslog(LOG_ERR, "i2c: Failed to write"); syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE; return MRAA_ERROR_INVALID_HANDLE;
} }
@@ -255,14 +247,12 @@ mraa_i2c_write_byte(mraa_i2c_context dev, const uint8_t data)
} }
mraa_result_t mraa_result_t
mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data, const uint8_t command)
const uint8_t command)
{ {
i2c_smbus_data_t d; i2c_smbus_data_t d;
d.byte = data; d.byte = data;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_BYTE_DATA, &d) < 0) {
I2C_SMBUS_BYTE_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write"); syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE; return MRAA_ERROR_INVALID_HANDLE;
} }
@@ -270,14 +260,12 @@ mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data,
} }
mraa_result_t mraa_result_t
mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command)
const uint8_t command)
{ {
i2c_smbus_data_t d; i2c_smbus_data_t d;
d.word = data; d.word = data;
if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, if (mraa_i2c_smbus_access(dev->fh, I2C_SMBUS_WRITE, command, I2C_SMBUS_WORD_DATA, &d) < 0) {
I2C_SMBUS_WORD_DATA, &d) < 0) {
syslog(LOG_ERR, "i2c: Failed to write"); syslog(LOG_ERR, "i2c: Failed to write");
return MRAA_ERROR_INVALID_HANDLE; return MRAA_ERROR_INVALID_HANDLE;
} }

View File

@@ -76,11 +76,8 @@ mraa_init()
#endif #endif
openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); openlog("libmraa", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
syslog(LOG_NOTICE, syslog(LOG_NOTICE, "libmraa version %s initialised by user '%s' with EUID %d",
"libmraa version %s initialised by user '%s' with EUID %d", mraa_get_version(), (proc_user != NULL) ? proc_user->pw_name : "<unknown>", proc_euid);
mraa_get_version(),
(proc_user != NULL) ? proc_user->pw_name : "<unknown>",
proc_euid);
#ifdef SWIGPYTHON #ifdef SWIGPYTHON
// Initialise python threads, this allows use to grab the GIL when we are // Initialise python threads, this allows use to grab the GIL when we are
@@ -106,10 +103,7 @@ mraa_init()
return MRAA_ERROR_PLATFORM_NOT_INITIALISED; return MRAA_ERROR_PLATFORM_NOT_INITIALISED;
} }
syslog(LOG_INFO, syslog(LOG_INFO, "libmraa initialised for platform '%s' of type %d", mraa_get_platform_name(), platform_type);
"libmraa initialised for platform '%s' of type %d",
mraa_get_platform_name(),
platform_type);
return MRAA_SUCCESS; return MRAA_SUCCESS;
} }
@@ -133,8 +127,7 @@ mraa_set_priority(const unsigned int priority)
memset(&sched_s, 0, sizeof(struct sched_param)); memset(&sched_s, 0, sizeof(struct sched_param));
if (priority > sched_get_priority_max(SCHED_RR)) { if (priority > sched_get_priority_max(SCHED_RR)) {
sched_s.sched_priority = sched_get_priority_max(SCHED_RR); sched_s.sched_priority = sched_get_priority_max(SCHED_RR);
} } else {
else {
sched_s.sched_priority = priority; sched_s.sched_priority = priority;
} }
@@ -270,7 +263,8 @@ mraa_pin_mode_test(int pin, mraa_pinmodes_t mode)
return 0; return 0;
} }
mraa_platform_t mraa_get_platform_type() mraa_platform_t
mraa_get_platform_type()
{ {
return platform_type; return platform_type;
} }
@@ -318,7 +312,8 @@ mraa_get_pin_count()
} }
mraa_boolean_t mraa_boolean_t
mraa_file_exist(char *filename) { mraa_file_exist(char* filename)
{
glob_t results; glob_t results;
results.gl_pathc = 0; results.gl_pathc = 0;
glob(filename, 0, NULL, &results); glob(filename, 0, NULL, &results);
@@ -339,7 +334,8 @@ mraa_get_pin_name(int pin)
} }
char* char*
mraa_file_unglob(char *filename) { mraa_file_unglob(char* filename)
{
glob_t results; glob_t results;
char* res = NULL; char* res = NULL;
results.gl_pathc = 0; results.gl_pathc = 0;
@@ -351,12 +347,12 @@ mraa_file_unglob(char *filename) {
} }
mraa_boolean_t mraa_boolean_t
mraa_link_targets(char *filename,char *targetname) { mraa_link_targets(char* filename, char* targetname)
{
int size = 100; int size = 100;
int nchars = 0; int nchars = 0;
char* buffer = NULL; char* buffer = NULL;
while (nchars == 0) while (nchars == 0) {
{
buffer = (char*) realloc(buffer, size); buffer = (char*) realloc(buffer, size);
if (buffer == NULL) if (buffer == NULL)
return 0; return 0;
@@ -373,8 +369,7 @@ mraa_link_targets(char *filename,char *targetname) {
if (strstr(buffer, targetname)) { if (strstr(buffer, targetname)) {
free(buffer); free(buffer);
return 1; return 1;
} } else {
else {
free(buffer); free(buffer);
return 0; return 0;
} }

View File

@@ -119,8 +119,7 @@ mraa_pwm_read_period(mraa_pwm_context dev)
if ('\0' != *endptr && '\n' != *endptr) { if ('\0' != *endptr && '\n' != *endptr) {
syslog(LOG_ERR, "pwm: Error in string conversion"); syslog(LOG_ERR, "pwm: Error in string conversion");
return -1; return -1;
} } else if (ret > INT_MAX || ret < INT_MIN) {
else if (ret > INT_MAX || ret < INT_MIN) {
syslog(LOG_ERR, "pwm: Number is invalid"); syslog(LOG_ERR, "pwm: Number is invalid");
return -1; return -1;
} }
@@ -152,8 +151,7 @@ mraa_pwm_read_duty(mraa_pwm_context dev)
if ('\0' != *endptr && '\n' != *endptr) { if ('\0' != *endptr && '\n' != *endptr) {
syslog(LOG_ERR, "pwm: Error in string converstion"); syslog(LOG_ERR, "pwm: Error in string converstion");
return -1; return -1;
} } else if (ret > INT_MAX || ret < INT_MIN) {
else if (ret > INT_MAX || ret < INT_MIN) {
syslog(LOG_ERR, "pwm: Number is invalid"); syslog(LOG_ERR, "pwm: Number is invalid");
return -1; return -1;
} }
@@ -302,8 +300,7 @@ mraa_pwm_period_ms(mraa_pwm_context dev, int ms)
mraa_result_t mraa_result_t
mraa_pwm_period_us(mraa_pwm_context dev, int us) mraa_pwm_period_us(mraa_pwm_context dev, int us)
{ {
if (us < plat->pwm_min_period || if (us < plat->pwm_min_period || us > plat->pwm_max_period) {
us > plat->pwm_max_period) {
syslog(LOG_ERR, "pwm: period value outside platform range"); syslog(LOG_ERR, "pwm: period value outside platform range");
return MRAA_ERROR_INVALID_PARAMETER; return MRAA_ERROR_INVALID_PARAMETER;
} }

View File

@@ -139,8 +139,7 @@ mraa_spi_init_raw(unsigned int bus, unsigned int cs)
int speed = 0; int speed = 0;
if ((ioctl(dev->devfd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) != -1) && (speed < 4000000)) { if ((ioctl(dev->devfd, SPI_IOC_RD_MAX_SPEED_HZ, &speed) != -1) && (speed < 4000000)) {
dev->clock = speed; dev->clock = speed;
} } else {
else {
dev->clock = 4000000; dev->clock = 4000000;
} }

View File

@@ -58,10 +58,12 @@ typedef struct {
static mraa_gpio_context tristate; static mraa_gpio_context tristate;
static mraa_intel_edison_pinmodes_t pinmodes[MRAA_INTEL_EDISON_PINCOUNT]; static mraa_intel_edison_pinmodes_t pinmodes[MRAA_INTEL_EDISON_PINCOUNT];
static unsigned int outputen[] = {248,249,250,251,252,253,254,255,256,257,258,259,260,261,232,233,234,235,236,237}; static unsigned int outputen[] = { 248, 249, 250, 251, 252, 253, 254, 255, 256, 257,
258, 259, 260, 261, 232, 233, 234, 235, 236, 237 };
static mraa_gpio_context agpioOutputen[sizeof(outputen) / sizeof(outputen[0])]; static mraa_gpio_context agpioOutputen[sizeof(outputen) / sizeof(outputen[0])];
static unsigned int pullup_map[] = {216,217,218,219,220,221,222,223,224,225,226,227,228,229,208,209,210,211,212,213}; static unsigned int pullup_map[] = { 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
226, 227, 228, 229, 208, 209, 210, 211, 212, 213 };
static int miniboard = 0; static int miniboard = 0;
// MMAP // MMAP
@@ -265,13 +267,13 @@ mraa_intel_edison_aio_get_fp(mraa_aio_context dev)
{ {
char file_path[64] = ""; char file_path[64] = "";
snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device1/in_voltage%d_raw", snprintf(file_path, 64, "/sys/bus/iio/devices/iio:device1/in_voltage%d_raw", dev->channel);
dev->channel );
dev->adc_in_fp = open(file_path, O_RDONLY); dev->adc_in_fp = open(file_path, O_RDONLY);
if (dev->adc_in_fp == -1) { if (dev->adc_in_fp == -1) {
syslog(LOG_ERR, "edison: Failed to open Analog input raw file %s for " syslog(LOG_ERR, "edison: Failed to open Analog input raw file %s for "
"reading!", file_path); "reading!",
file_path);
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }
@@ -490,8 +492,7 @@ mraa_intel_edsion_mb_gpio_mode(mraa_gpio_context dev, gpio_mode_t mode)
} }
char filepath[MAX_SIZE]; char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, snprintf(filepath, MAX_SIZE, SYSFS_PINMODE_PATH "%d/current_pullmode", dev->pin);
SYSFS_PINMODE_PATH "%d/current_pullmode", dev->pin);
int drive = open(filepath, O_WRONLY); int drive = open(filepath, O_WRONLY);
if (drive == -1) { if (drive == -1) {
@@ -592,8 +593,7 @@ mraa_intel_edison_mmap_write(mraa_gpio_context dev, int value)
valoff = 0x4c; valoff = 0x4c;
} }
*(volatile uint32_t*) (mmap_reg + offset + valoff) = *(volatile uint32_t*) (mmap_reg + offset + valoff) = (uint32_t)(1 << (dev->pin % 32));
(uint32_t)(1 << (dev->pin % 32));
return MRAA_SUCCESS; return MRAA_SUCCESS;
} }
@@ -651,10 +651,8 @@ mraa_intel_edison_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
fstat(mmap_fd, &fd_stat); fstat(mmap_fd, &fd_stat);
mmap_size = fd_stat.st_size; mmap_size = fd_stat.st_size;
mmap_reg = (uint8_t*) mmap(NULL, fd_stat.st_size, mmap_reg =
PROT_READ | PROT_WRITE, (uint8_t*) mmap(NULL, fd_stat.st_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, mmap_fd, 0);
MAP_FILE | MAP_SHARED,
mmap_fd, 0);
if (mmap_reg == MAP_FAILED) { if (mmap_reg == MAP_FAILED) {
syslog(LOG_ERR, "edison mmap: failed to mmap"); syslog(LOG_ERR, "edison mmap: failed to mmap");
mmap_reg = NULL; mmap_reg = NULL;

View File

@@ -97,8 +97,7 @@ mraa_intel_galileo_g1_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
syslog(LOG_ERR, "galileo1: Unable to open UIO device"); syslog(LOG_ERR, "galileo1: Unable to open UIO device");
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }
mmap_reg = mmap(NULL, mmap_size, PROT_READ|PROT_WRITE, mmap_reg = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, mmap_fd, 0);
MAP_SHARED, mmap_fd, 0);
if (mmap_reg == MAP_FAILED) { if (mmap_reg == MAP_FAILED) {
syslog(LOG_ERR, "galileo1: Mmap failed to mmap"); syslog(LOG_ERR, "galileo1: Mmap failed to mmap");
@@ -107,8 +106,7 @@ mraa_intel_galileo_g1_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
return MRAA_ERROR_NO_RESOURCES; return MRAA_ERROR_NO_RESOURCES;
} }
} }
if (mraa_setup_mux_mapped(plat->pins[dev->phy_pin].mmap.gpio) if (mraa_setup_mux_mapped(plat->pins[dev->phy_pin].mmap.gpio) != MRAA_SUCCESS) {
!= MRAA_SUCCESS) {
syslog(LOG_ERR, "galileo1: Unable to setup required multiplexers for mmap"); syslog(LOG_ERR, "galileo1: Unable to setup required multiplexers for mmap");
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }

View File

@@ -41,7 +41,8 @@ static int mmap_fd = 0;
static int mmap_size = 0x1000; static int mmap_size = 0x1000;
static unsigned int mmap_count = 0; static unsigned int mmap_count = 0;
static unsigned int pullup_map[] = {33,29,35,17,37,19,21,39,41,23,27,25,43,31,49,51,53,55,57,59}; static unsigned int pullup_map[] = { 33, 29, 35, 17, 37, 19, 21, 39, 41, 23,
27, 25, 43, 31, 49, 51, 53, 55, 57, 59 };
static mraa_gpio_context agpioOutputen[MRAA_INTEL_GALILEO_GEN_2_PINCOUNT]; static mraa_gpio_context agpioOutputen[MRAA_INTEL_GALILEO_GEN_2_PINCOUNT];
@@ -299,8 +300,7 @@ mraa_intel_galileo_g2_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
syslog(LOG_ERR, "mmap: Unable to open UIO device"); syslog(LOG_ERR, "mmap: Unable to open UIO device");
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }
mmap_reg = mmap(NULL, mmap_size, PROT_READ|PROT_WRITE, mmap_reg = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, mmap_fd, 0);
MAP_SHARED, mmap_fd, 0);
if (mmap_reg == MAP_FAILED) { if (mmap_reg == MAP_FAILED) {
syslog(LOG_ERR, "mmap: failed to mmap"); syslog(LOG_ERR, "mmap: failed to mmap");
@@ -309,8 +309,7 @@ mraa_intel_galileo_g2_mmap_setup(mraa_gpio_context dev, mraa_boolean_t en)
return MRAA_ERROR_NO_RESOURCES; return MRAA_ERROR_NO_RESOURCES;
} }
} }
if (mraa_setup_mux_mapped(plat->pins[dev->phy_pin].mmap.gpio) if (mraa_setup_mux_mapped(plat->pins[dev->phy_pin].mmap.gpio) != MRAA_SUCCESS) {
!= MRAA_SUCCESS) {
syslog(LOG_ERR, "mmap: unable to setup required multiplexers"); syslog(LOG_ERR, "mmap: unable to setup required multiplexers");
return MRAA_ERROR_INVALID_RESOURCE; return MRAA_ERROR_INVALID_RESOURCE;
} }

View File

@@ -69,7 +69,8 @@ mraa_set_pininfo(mraa_board_t* board, int mraa_index, char *name, mraa_pincapabi
} }
mraa_result_t mraa_result_t
mraa_get_pin_index(mraa_board_t* board, char *name, int* pin_index) { mraa_get_pin_index(mraa_board_t* board, char* name, int* pin_index)
{
int i; int i;
for (i = 0; i < board->phy_pin_count; ++i) { for (i = 0; i < board->phy_pin_count; ++i) {
if (strcmp(name, board->pins[i].name) == 0) { if (strcmp(name, board->pins[i].name) == 0) {
@@ -141,9 +142,11 @@ mraa_intel_minnow_max()
mraa_set_pininfo(b, 19, "UART2RX", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }, 228); mraa_set_pininfo(b, 19, "UART2RX", (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 0, 1 }, 228);
mraa_set_pininfo(b, 20, "I2S_DI", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 218); mraa_set_pininfo(b, 20, "I2S_DI", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 218);
mraa_set_pininfo(b, 21, "S5_0", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 82); mraa_set_pininfo(b, 21, "S5_0", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 82);
mraa_set_pininfo(b, 22, "PWM0", (mraa_pincapabilities_t){1,0,1,0,0,0,0,0}, 248); // Assume BIOS configured for PWM mraa_set_pininfo(b, 22, "PWM0", (mraa_pincapabilities_t){ 1, 0, 1, 0, 0, 0, 0, 0 },
248); // Assume BIOS configured for PWM
mraa_set_pininfo(b, 23, "S5_1", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 83); mraa_set_pininfo(b, 23, "S5_1", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 83);
mraa_set_pininfo(b, 24, "PWM1", (mraa_pincapabilities_t){1,0,1,0,0,0,0,0}, 249); // Assume BIOS configured for PWM mraa_set_pininfo(b, 24, "PWM1", (mraa_pincapabilities_t){ 1, 0, 1, 0, 0, 0, 0, 0 },
249); // Assume BIOS configured for PWM
mraa_set_pininfo(b, 25, "S5_4", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 84); mraa_set_pininfo(b, 25, "S5_4", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 84);
mraa_set_pininfo(b, 26, "IBL8254", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 208); mraa_set_pininfo(b, 26, "IBL8254", (mraa_pincapabilities_t){ 1, 1, 0, 0, 0, 0, 0, 0 }, 208);