summaryrefslogtreecommitdiff
path: root/peripheral/libmraa/api/mraa
diff options
context:
space:
mode:
Diffstat (limited to 'peripheral/libmraa/api/mraa')
-rw-r--r--peripheral/libmraa/api/mraa/aio.h11
-rw-r--r--peripheral/libmraa/api/mraa/aio.hpp31
-rw-r--r--peripheral/libmraa/api/mraa/common.h38
-rw-r--r--peripheral/libmraa/api/mraa/common.hpp49
-rw-r--r--peripheral/libmraa/api/mraa/firmata.h101
-rw-r--r--peripheral/libmraa/api/mraa/gpio.h30
-rw-r--r--peripheral/libmraa/api/mraa/gpio.hpp27
-rw-r--r--peripheral/libmraa/api/mraa/i2c.h28
-rw-r--r--peripheral/libmraa/api/mraa/i2c.hpp35
-rw-r--r--peripheral/libmraa/api/mraa/iio.h2
-rw-r--r--peripheral/libmraa/api/mraa/pwm.h30
-rw-r--r--peripheral/libmraa/api/mraa/pwm.hpp65
-rw-r--r--peripheral/libmraa/api/mraa/spi.h4
-rw-r--r--peripheral/libmraa/api/mraa/spi.hpp22
-rw-r--r--peripheral/libmraa/api/mraa/types.h10
-rw-r--r--peripheral/libmraa/api/mraa/types.hpp15
-rw-r--r--peripheral/libmraa/api/mraa/uart.h9
-rw-r--r--peripheral/libmraa/api/mraa/uart.hpp32
-rw-r--r--peripheral/libmraa/api/mraa/uart_ow.h197
-rw-r--r--peripheral/libmraa/api/mraa/uart_ow.hpp277
20 files changed, 137 insertions, 876 deletions
diff --git a/peripheral/libmraa/api/mraa/aio.h b/peripheral/libmraa/api/mraa/aio.h
index 23c0857..753c907 100644
--- a/peripheral/libmraa/api/mraa/aio.h
+++ b/peripheral/libmraa/api/mraa/aio.h
@@ -59,20 +59,19 @@ typedef struct _aio* mraa_aio_context;
mraa_aio_context mraa_aio_init(unsigned int pin);
/**
- * Read the input voltage. By default mraa will shift the raw value up or down
- * to a 10 bit value.
+ * Read the input voltage. By default mraa will shift
+ * the raw value up or down to a 10 bit value.
*
* @param dev The AIO context
- * @returns The current input voltage or -1 for error
+ * @returns The current input voltage.
*/
-int mraa_aio_read(mraa_aio_context dev);
+unsigned int mraa_aio_read(mraa_aio_context dev);
/**
* Read the input voltage and return it as a normalized float (0.0f-1.0f).
*
* @param dev The AIO context
- * @returns The current input voltage as a normalized float (0.0f-1.0f), error
- * will be signaled by -1.0f
+ * @returns The current input voltage as a normalized float (0.0f-1.0f)
*/
float mraa_aio_read_float(mraa_aio_context dev);
diff --git a/peripheral/libmraa/api/mraa/aio.hpp b/peripheral/libmraa/api/mraa/aio.hpp
index 38ea8bc..9b07854 100644
--- a/peripheral/libmraa/api/mraa/aio.hpp
+++ b/peripheral/libmraa/api/mraa/aio.hpp
@@ -47,7 +47,7 @@ class Aio
*
* @param pin channel number to read ADC inputs
*/
- Aio(int pin)
+ Aio(unsigned int pin)
{
m_aio = mraa_aio_init(pin);
if (m_aio == NULL) {
@@ -55,19 +55,6 @@ class Aio
}
}
/**
- * Aio Constructor, takes a pointer to the AIO context and initialises
- * the AIO class
- *
- * @param void * to an AIO context
- */
- Aio(void* aio_context)
- {
- m_aio = (mraa_aio_context) aio_context;
- if (m_aio == NULL) {
- throw std::invalid_argument("Invalid AIO context");
- }
- }
- /**
* Aio destructor
*/
~Aio()
@@ -78,32 +65,22 @@ class Aio
* Read a value from the AIO pin. By default mraa will shift
* the raw value up or down to a 10 bit value.
*
- * @throws std::invalid_argument in case of error
* @returns The current input voltage. By default, a 10bit value
*/
- unsigned int
+ int
read()
{
- int x = mraa_aio_read(m_aio);
- if (x == -1) {
- throw std::invalid_argument("Unknown error in Aio::read()");
- }
- return (unsigned int) x;
+ return mraa_aio_read(m_aio);
}
/**
* Read a value from the AIO pin and return it as a normalized float.
*
- * @throws std::invalid_argument in case of error
* @returns The current input voltage as a normalized float (0.0f-1.0f)
*/
float
readFloat()
{
- float x = mraa_aio_read_float(m_aio);
- if (x == -1.0f) {
- throw std::invalid_argument("Unknown error in Aio::readFloat()");
- }
- return x;
+ return mraa_aio_read_float(m_aio);
}
/**
* Set the bit value which mraa will shift the raw reading
diff --git a/peripheral/libmraa/api/mraa/common.h b/peripheral/libmraa/api/mraa/common.h
index c0467ba..0357f0c 100644
--- a/peripheral/libmraa/api/mraa/common.h
+++ b/peripheral/libmraa/api/mraa/common.h
@@ -64,7 +64,9 @@ typedef unsigned int mraa_boolean_t;
*
* Detects running platform and attempts to use included pinmap, this is run on
* module/library init/load but is handy to rerun to check board initialised
- * correctly. MRAA_SUCCESS inidicates correct initialisation.
+ * correctly. MRAA_SUCCESS inidicates correct (first time) initialisation
+ * whilst MRAA_ERROR_PLATFORM_ALREADY_INITIALISED indicates the board is
+ * already initialised correctly
*
* @return Result of operation
*/
@@ -159,7 +161,7 @@ const char* mraa_get_platform_version(int platform_offset);
* @param priority Value from typically 0 to 99
* @return The priority value set
*/
-int mraa_set_priority(const int priority);
+int mraa_set_priority(const unsigned int priority);
/** Get the version string of mraa autogenerated from git tag
*
@@ -214,7 +216,7 @@ int mraa_get_i2c_bus_count();
* @param i2c_bus the logical I2C bus number
* @return I2C adapter number in sysfs. Function will return -1 on failure
*/
-int mraa_get_i2c_bus_id(int i2c_bus);
+int mraa_get_i2c_bus_id(unsigned int i2c_bus);
/**
* Get specified platform pincount, board must be initialised.
@@ -274,37 +276,7 @@ int mraa_get_sub_platform_id(int pin_or_bus_index);
*/
int mraa_get_sub_platform_index(int pin_or_bus_id);
-/**
- * Add mraa subplatform
- *
- * @param subplatform type
- * @param uart device subplatform is on
- *
- * @return mraa_result_t indicating success
- */
-mraa_result_t mraa_add_subplatform(mraa_platform_t subplatformtype, const char* uart_dev);
-/**
- * Remove a mraa subplatform
- *
- * @param subplatform type
- *
- * @return mraa_result indicating success
- */
-mraa_result_t mraa_remove_subplatform(mraa_platform_t subplatformtype);
-
-/**
- * Create IO using a description in the format:
- * [io]-[pin]
- * [io]-[raw]-[pin]
- * [io]-[raw]-[id]-[pin]
- * [io]-[raw]-[path]
- *
- * @param IO description
- *
- * @return void* to IO context or NULL
- */
-void* mraa_init_io(const char* desc);
#ifdef __cplusplus
}
diff --git a/peripheral/libmraa/api/mraa/common.hpp b/peripheral/libmraa/api/mraa/common.hpp
index 29894d3..26f4a60 100644
--- a/peripheral/libmraa/api/mraa/common.hpp
+++ b/peripheral/libmraa/api/mraa/common.hpp
@@ -46,7 +46,9 @@ namespace mraa
*
* Detects running platform and attempts to use included pinmap, this is run on
* module/library init/load but is handy to rerun to check board initialised
- * correctly. mraa::SUCCESS inidicates correct initialisation.
+ * correctly. MRAA_SUCCESS inidicates correct (first time) initialisation
+ * whilst MRAA_ERROR_PLATFORM_ALREADY_INITIALISED indicates the board is
+ * already initialised correctly
*
* @return Result of operation
*/
@@ -78,7 +80,7 @@ getVersion()
* @return The priority value set
*/
inline int
-setPriority(const int priority)
+setPriority(const unsigned int priority)
{
return mraa_set_priority(priority);
}
@@ -195,7 +197,7 @@ getI2cBusCount()
* @return I2C adapter number in sysfs. Function will return -1 on failure
*/
inline int
-getI2cBusId(int i2c_bus)
+getI2cBusId(unsigned int i2c_bus)
{
return mraa_get_i2c_bus_id(i2c_bus);
}
@@ -258,7 +260,7 @@ isSubPlatformId(int pin_or_bus_id)
*
* @return int sub platform pin or bus number
*/
-inline int
+inline int
getSubPlatformId(int pin_or_bus_index)
{
return mraa_get_sub_platform_id(pin_or_bus_index);
@@ -288,43 +290,4 @@ getDefaultI2cBus(int platform_offset=MRAA_MAIN_PLATFORM_OFFSET)
{
return mraa_get_default_i2c_bus(platform_offset);
}
-
-/**
- * Add mraa subplatform
- *
- * @param subplatformtype the type of subplatform to add
- * (e.g. MRAA_GENERIC_FIRMATA)
- * @param uart_dev subplatform device string (e.g. "/dev/ttyACM0")
- * @return Result of operation
- */
-inline Result
-addSubplatform(Platform subplatformtype, std::string uart_dev)
-{
- return (Result) mraa_add_subplatform((mraa_platform_t) subplatformtype, uart_dev.c_str());
-}
-
-inline Result
-removeSubplatform(Platform subplatformtype)
-{
- return (Result) mraa_remove_subplatform((mraa_platform_t) subplatformtype);
-}
-
-/**
- * Create IO using a description in the format:
- * [io]-[pin]
- * [io]-[raw]-[pin]
- * [io]-[raw]-[id]-[pin]
- * [io]-[raw]-[path]
- *
- * @param IO description
- *
- * @return class T initialised using pointer to IO or NULL
- */
-template <class T>
-inline T*
-initIo(std::string desc)
-{
- return new T(mraa_init_io(desc.c_str()));
-}
-
}
diff --git a/peripheral/libmraa/api/mraa/firmata.h b/peripheral/libmraa/api/mraa/firmata.h
deleted file mode 100644
index ba8684e..0000000
--- a/peripheral/libmraa/api/mraa/firmata.h
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * Author: Brendan Le Foll <brendan.le.foll@intel.com>
- * Copyright (c) 2016 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
-
-/**
- * @file
- * @brief Firmata IO
- *
- * Firmata IO lets you SYSEX messages construct and ask for a callback on a
- * SYSEX messages. This is meant to provide a way to call custom firmata APIs
- * especially using the Custom firmata API
- *
- * @snippet firmata_curie_imu.c Interesting
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include "common.h"
-
-/**
- * Opaque pointer definition to the internal struct _firmata. This context
- * refers to one firmata 'extension' letting you write/return SYSEX messages
- * directly
- */
-typedef struct _firmata* mraa_firmata_context;
-
-/**
- * Initialise firmata context on a feature. This feature is what will be
- * listened on if you request a response callback
- *
- * @param firmata feature
- * @return firmata context or NULL
- */
-mraa_firmata_context mraa_firmata_init(int feature);
-
-/**
- * Sends a custom SYSEX message to the firmata board.
- *
- * @param dev The Firmata context
- * @param msg The SYSEX message
- * @param length The length of the sysex message
- */
-mraa_result_t mraa_firmata_write_sysex(mraa_firmata_context dev, char* msg, int length);
-
-/**
- * Set a callback on 'feature'. This function is not thread safe and threads
- * calling it need to make sure they are the only thread calling this.
- *
- * @param dev The Firmata context
- * @param fptr Function pointer to function to be called when interrupt is
- * triggered, the returned buffer and length are the arguments.
- * @return Result of operation
- */
-mraa_result_t mraa_firmata_response(mraa_firmata_context dev, void (*fptr)(uint8_t*, int));
-
-/**
- * Stop getting events on feature. This is more efficient than mraa_firmata_close
- * as it can be re-enabled without adding a feature
- *
- * @param dev The Firmata context
- * @return Result of operation
- */
-mraa_result_t mraa_firmata_response_stop(mraa_firmata_context dev);
-
-/**
- * Free all firmata handle resources, this will leave an element in an array
- * internally that will be skipped, avoid closing many firmata contexts often
- * as there is a cost to doing this
- *
- * @param dev The Firmata context
- * @return Result of operation
- */
-mraa_result_t mraa_firmata_close(mraa_firmata_context dev);
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/peripheral/libmraa/api/mraa/gpio.h b/peripheral/libmraa/api/mraa/gpio.h
index 32e1c6e..2287c0c 100644
--- a/peripheral/libmraa/api/mraa/gpio.h
+++ b/peripheral/libmraa/api/mraa/gpio.h
@@ -40,17 +40,21 @@
extern "C" {
#endif
-#include <stdio.h>
-#include <pthread.h>
-#include "common.h"
+#ifdef SWIGPYTHON
+#include <Python.h>
+#endif
#if defined(SWIGJAVA) || defined(JAVACALLBACK)
#include <jni.h>
-extern "C" {
- void mraa_java_isr_callback(void *args);
-}
+extern JavaVM *globVM;
+extern void mraa_java_isr_callback(void *);
#endif
+#include <stdio.h>
+#include <pthread.h>
+
+#include "common.h"
+
/**
* Opaque pointer definition to the internal struct _gpio
*/
@@ -77,13 +81,13 @@ typedef enum {
} mraa_gpio_dir_t;
/**
- * Gpio Edge types for interrupts
+ * Gpio Edge types for interupts
*/
typedef enum {
MRAA_GPIO_EDGE_NONE = 0, /**< No interrupt on Gpio */
- MRAA_GPIO_EDGE_BOTH = 1, /**< Interrupt on rising & falling */
- MRAA_GPIO_EDGE_RISING = 2, /**< Interrupt on rising only */
- MRAA_GPIO_EDGE_FALLING = 3 /**< Interrupt on falling only */
+ MRAA_GPIO_EDGE_BOTH = 1, /**< Interupt on rising & falling */
+ MRAA_GPIO_EDGE_RISING = 2, /**< Interupt on rising only */
+ MRAA_GPIO_EDGE_FALLING = 3 /**< Interupt on falling only */
} mraa_gpio_edge_t;
/**
@@ -112,11 +116,11 @@ mraa_gpio_context mraa_gpio_init_raw(int gpiopin);
mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode);
/**
- * Set an interrupt on pin
+ * Set an interupt on pin
*
* @param dev The Gpio context
* @param edge The edge mode to set the gpio into
- * @param fptr Function pointer to function to be called when interrupt is
+ * @param fptr Function pointer to function to be called when interupt is
* triggered
* @param args Arguments passed to the interrupt handler (fptr)
* @return Result of operation
@@ -124,7 +128,7 @@ mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode);
mraa_result_t mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t edge, void (*fptr)(void*), void* args);
/**
- * Stop the current interrupt watcher on this Gpio, and set the Gpio edge mode
+ * Stop the current interupt watcher on this Gpio, and set the Gpio edge mode
* to MRAA_GPIO_EDGE_NONE
*
* @param dev The Gpio context
diff --git a/peripheral/libmraa/api/mraa/gpio.hpp b/peripheral/libmraa/api/mraa/gpio.hpp
index e2d7ad5..98693ac 100644
--- a/peripheral/libmraa/api/mraa/gpio.hpp
+++ b/peripheral/libmraa/api/mraa/gpio.hpp
@@ -60,13 +60,13 @@ typedef enum {
} Dir;
/**
- * Gpio Edge types for interrupts
+ * Gpio Edge types for interupts
*/
typedef enum {
EDGE_NONE = 0, /**< No interrupt on Gpio */
- EDGE_BOTH = 1, /**< Interrupt on rising & falling */
- EDGE_RISING = 2, /**< Interrupt on rising only */
- EDGE_FALLING = 3 /**< Interrupt on falling only */
+ EDGE_BOTH = 1, /**< Interupt on rising & falling */
+ EDGE_RISING = 2, /**< Interupt on rising only */
+ EDGE_FALLING = 3 /**< Interupt on falling only */
} Edge;
/**
@@ -80,7 +80,7 @@ class Gpio
{
public:
/**
- * Instantiates a Gpio object
+ * Instanciates a Gpio object
*
* @param pin pin number to use
* @param owner (optional) Set pin owner, default behaviour is to 'own'
@@ -108,19 +108,6 @@ class Gpio
}
}
/**
- * Gpio Constructor, takes a pointer to the GPIO context and initialises
- * the GPIO class
- *
- * @param void * to GPIO context
- */
- Gpio(void* gpio_context)
- {
- m_gpio = (mraa_gpio_context) gpio_context;
- if (m_gpio == NULL) {
- throw std::invalid_argument("Invalid GPIO context");
- }
- }
- /**
* Gpio object destructor, this will only unexport the gpio if we where
* the owner
*/
@@ -199,7 +186,7 @@ class Gpio
* Sets a callback to be called when pin value changes
*
* @param mode The edge mode to set
- * @param fptr Function pointer to function to be called when interrupt is
+ * @param fptr Function pointer to function to be called when interupt is
* triggered
* @param args Arguments passed to the interrupt handler (fptr)
* @return Result of operation
@@ -211,7 +198,7 @@ class Gpio
}
/**
- * Exits callback - this call will not kill the isr thread immediately
+ * Exits callback - this call will not kill the isr thread immediatly
* but only when it is out of it's critical section
*
* @return Result of operation
diff --git a/peripheral/libmraa/api/mraa/i2c.h b/peripheral/libmraa/api/mraa/i2c.h
index 657a53d..d0dbde2 100644
--- a/peripheral/libmraa/api/mraa/i2c.h
+++ b/peripheral/libmraa/api/mraa/i2c.h
@@ -78,40 +78,42 @@ mraa_i2c_context mraa_i2c_init_raw(unsigned int bus);
mraa_result_t mraa_i2c_frequency(mraa_i2c_context dev, mraa_i2c_mode_t mode);
/**
- * Simple bulk read from an i2c context
+ * Simple bulk read from an i2c context, this will always begin with the i2c
+ * offset 0x0
*
* @param dev The i2c context
* @param data pointer to the byte array to read data in to
* @param length max number of bytes to read
- * @return length of the read in bytes or -1
+ * @return length of the read in bytes or 0
*/
int mraa_i2c_read(mraa_i2c_context dev, uint8_t* data, int length);
/**
- * Simple read for a single byte from the i2c context
+ * Simple read for a single byte from the i2c context, this will always begin
+ * with the i2c offset 0x0
*
* @param dev The i2c context
- * @return The result of the read or -1 if failed
+ * @return The result of the read and 0 if failed
*/
-int mraa_i2c_read_byte(mraa_i2c_context dev);
+uint8_t mraa_i2c_read_byte(mraa_i2c_context dev);
/**
* Read a single byte from i2c context, from designated register
*
* @param dev The i2c context
* @param command The register
- * @return The result of the read or -1 if failed
+ * @return The result of the read and 0 if failed
*/
-int mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
+uint8_t mraa_i2c_read_byte_data(mraa_i2c_context dev, const uint8_t command);
/**
* Read a single word from i2c context, from designated register
*
* @param dev The i2c context
* @param command The register
- * @return The result of the read or -1 if failed
+ * @return The result of the read and 0 if failed
*/
-int mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command);
+uint16_t mraa_i2c_read_word_data(mraa_i2c_context dev, const uint8_t command);
/**
* Bulk read from i2c context, starting from designated register
@@ -136,7 +138,7 @@ int mraa_i2c_read_bytes_data(mraa_i2c_context dev, uint8_t command, uint8_t* dat
mraa_result_t mraa_i2c_write(mraa_i2c_context dev, const uint8_t* data, int length);
/**
- * Write a single byte to an i2c context
+ * Write a single byte to an i2c context, always at offset 0x0
*
* @param dev The i2c context
* @param data The byte to write
@@ -165,10 +167,12 @@ mraa_result_t mraa_i2c_write_byte_data(mraa_i2c_context dev, const uint8_t data,
mraa_result_t mraa_i2c_write_word_data(mraa_i2c_context dev, const uint16_t data, const uint8_t command);
/**
- * Sets the i2c slave address.
+ * Sets the i2c context address.
*
* @param dev The i2c context
- * @param address The address to set for the slave (7-bit 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.
* @return Result of operation
*/
mraa_result_t mraa_i2c_address(mraa_i2c_context dev, uint8_t address);
diff --git a/peripheral/libmraa/api/mraa/i2c.hpp b/peripheral/libmraa/api/mraa/i2c.hpp
index 52e2153..cd3c31c 100644
--- a/peripheral/libmraa/api/mraa/i2c.hpp
+++ b/peripheral/libmraa/api/mraa/i2c.hpp
@@ -62,18 +62,6 @@ class I2c
throw std::invalid_argument("Invalid i2c bus");
}
}
- /**
- * I2C constructor, takes a pointer to a I2C context and initialises the I2C class
- *
- * @param void * to an I2C context
- */
- I2c(void* i2c_context)
- {
- m_i2c = (mraa_i2c_context) i2c_context;
- if (m_i2c == NULL) {
- throw std::invalid_argument("Invalid I2C context");
- }
- }
/**
* Closes the I2c Bus used. This does not guarrantee the bus will not
@@ -115,17 +103,12 @@ class I2c
/**
* Read exactly one byte from the bus
*
- * @throws std::invalid_argument in case of error
* @return char read from the bus
*/
uint8_t
readByte()
{
- int x = mraa_i2c_read_byte(m_i2c);
- if (x == -1) {
- throw std::invalid_argument("Unknown error in I2c::readByte()");
- }
- return (uint8_t) x;
+ return (uint8_t) mraa_i2c_read_byte(m_i2c);
}
/**
@@ -145,36 +128,24 @@ class I2c
* Read byte from an i2c register
*
* @param reg Register to read from
- *
- * @throws std::invalid_argument in case of error
* @return char read from register
*/
uint8_t
readReg(uint8_t reg)
{
- int x = mraa_i2c_read_byte_data(m_i2c, reg);
- if (x == -1) {
- throw std::invalid_argument("Unknown error in I2c::readReg()");
- }
- return (uint8_t) x;
+ return mraa_i2c_read_byte_data(m_i2c, reg);
}
/**
* Read word from an i2c register
*
* @param reg Register to read from
- *
- * @throws std::invalid_argument in case of error
* @return char read from register
*/
uint16_t
readWordReg(uint8_t reg)
{
- int x = mraa_i2c_read_word_data(m_i2c, reg);
- if (x == -1) {
- throw std::invalid_argument("Unknown error in I2c::readReg()");
- }
- return (uint16_t) x;
+ return mraa_i2c_read_word_data(m_i2c, reg);
}
/**
diff --git a/peripheral/libmraa/api/mraa/iio.h b/peripheral/libmraa/api/mraa/iio.h
index a205a76..eadbab9 100644
--- a/peripheral/libmraa/api/mraa/iio.h
+++ b/peripheral/libmraa/api/mraa/iio.h
@@ -121,7 +121,7 @@ mraa_result_t mraa_iio_event_extract_event(struct iio_event_data* event,
int* channel2,
int* different);
-mraa_result_t mraa_iio_get_mount_matrix(mraa_iio_context dev, const char *sysfs_name, float mm[9]);
+mraa_result_t mraa_iio_get_mounting_matrix(mraa_iio_context dev, float mm[9]);
mraa_result_t mraa_iio_create_trigger(mraa_iio_context dev, const char* trigger);
diff --git a/peripheral/libmraa/api/mraa/pwm.h b/peripheral/libmraa/api/mraa/pwm.h
index d6f17df..ef78110 100644
--- a/peripheral/libmraa/api/mraa/pwm.h
+++ b/peripheral/libmraa/api/mraa/pwm.h
@@ -65,7 +65,7 @@ mraa_pwm_context mraa_pwm_init(int pin);
mraa_pwm_context mraa_pwm_init_raw(int chipid, int pin);
/**
- * Set the output duty-cycle percentage, as a float
+ * Set the ouput duty-cycle percentage, as a float
*
* @param dev The Pwm context to use
* @param percentage A floating-point value representing percentage of output.
@@ -76,7 +76,7 @@ mraa_pwm_context mraa_pwm_init_raw(int chipid, int pin);
mraa_result_t mraa_pwm_write(mraa_pwm_context dev, float percentage);
/**
- * Read the output duty-cycle percentage, as a float
+ * Read the ouput duty-cycle percentage, as a float
*
* @param dev The Pwm context to use
* @return percentage A floating-point value representing percentage of output.
@@ -167,20 +167,38 @@ mraa_result_t mraa_pwm_owner(mraa_pwm_context dev, mraa_boolean_t owner);
mraa_result_t mraa_pwm_close(mraa_pwm_context dev);
/**
- * Get the maximum pwm period in us
+ * Set Both Period and DutyCycle on a PWM context
+ *
+ * @param dev The pwm context to use
+ * @param period represented in ms.
+ * @param duty dutycycle of the pwm signal.
+ * @return Result of operation
+ */
+mraa_result_t mraa_pwm_config_ms(mraa_pwm_context dev, int period, float duty);
+
+/**
+ * Set Both Period and DutyCycle on a PWM context. Duty represented as percentage.
*
* @param dev The pwm context to use
+ * @param period represented in ms.
+ * @param duty duty percantage. i.e. 50% = 0.5f
+ * @return Result of operation
+ */
+mraa_result_t mraa_pwm_config_percent(mraa_pwm_context dev, int period, float duty);
+
+/**
+ * Get the maximum pwm period in us
+ *
* @return max pwm in us
*/
-int mraa_pwm_get_max_period(mraa_pwm_context dev);
+int mraa_pwm_get_max_period();
/**
* Get the minimum pwm period in us
*
- * @param dev The pwm context to use
* @return min pwm in us
*/
-int mraa_pwm_get_min_period(mraa_pwm_context dev);
+int mraa_pwm_get_min_period();
#ifdef __cplusplus
}
diff --git a/peripheral/libmraa/api/mraa/pwm.hpp b/peripheral/libmraa/api/mraa/pwm.hpp
index c75255a..a449a43 100644
--- a/peripheral/libmraa/api/mraa/pwm.hpp
+++ b/peripheral/libmraa/api/mraa/pwm.hpp
@@ -46,9 +46,9 @@ class Pwm
*
* @param pin the pin number used on your board
* @param owner if you are the owner of the pin the destructor will
+ * @param chipid the pwmchip to use, use only in raw mode
* unexport the pin from sysfs, default behaviour is you are the owner
* if the pinmapper exported it
- * @param chipid the pwmchip to use, use only in raw mode
*/
Pwm(int pin, bool owner = true, int chipid = -1)
{
@@ -66,20 +66,6 @@ class Pwm
mraa_pwm_owner(m_pwm, 0);
}
}
-
- /**
- * Pwm constructor, takes a pointer to the PWM context and
- * initialises the class
- *
- * @param void * to a PWM context
- */
- Pwm(void* pwm_context)
- {
- m_pwm = (mraa_pwm_context) pwm_context;
- if (m_pwm == NULL) {
- throw std::invalid_argument("Invalid PWM context");
- }
- }
/**
* Pwm destructor
*/
@@ -91,7 +77,7 @@ class Pwm
* Set the output duty-cycle percentage, as a float
*
* @param percentage A floating-point value representing percentage of
- * output. The value should lie between 0.0f (representing 0%) and
+ * 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
* @return Result of operation
@@ -102,10 +88,10 @@ class Pwm
return (Result) mraa_pwm_write(m_pwm, percentage);
}
/**
- * Read the output duty-cycle percentage, as a float
+ * Read the ouput duty-cycle percentage, as a float
*
* @return A floating-point value representing percentage of
- * output. The value should lie between 0.0f (representing 0%) and
+ * 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
*/
@@ -148,7 +134,7 @@ class Pwm
return (Result) mraa_pwm_period_us(m_pwm, us);
}
/**
- * Set pulsewidth, as represented by seconds in a float
+ * Set pulsewidth, As represnted by seconds in a (float)
*
* @param seconds The duration of a pulse
* @return Result of operation
@@ -190,27 +176,54 @@ class Pwm
Result
enable(bool enable)
{
- return (Result) mraa_pwm_enable(m_pwm, enable);
+ if (enable)
+ return (Result) mraa_pwm_enable(m_pwm, 1);
+ else
+ return (Result) mraa_pwm_enable(m_pwm, 0);
+ }
+ /**
+ * Set the period and duty of a PWM object.
+ *
+ * @param period represented in ms.
+ * @param duty represnted in ms as float.
+ * @return Result of operation
+ */
+ Result
+ config_ms(int period, float duty)
+ {
+ return (Result) mraa_pwm_config_ms(m_pwm, period, duty);
+ }
+ /**
+ * Set the period and duty (percent) of a PWM object.
+ *
+ * @param period as represented in ms.
+ * @param duty percentage i.e. 50% = 0.5f
+ * @return Result of operation
+ */
+ Result
+ config_percent(int period, float duty)
+ {
+ return (Result) mraa_pwm_config_percent(m_pwm, period, duty);
}
/**
- * Get the maximum PWM period in us
+ * Get the maximum pwm period in us
*
- * @return max PWM period in us
+ * @return max pwm in us
*/
int
max_period()
{
- return mraa_pwm_get_max_period(m_pwm);
+ return mraa_pwm_get_max_period();
}
/**
- * Get the minimum PWM period in us
+ * Get the minimum pwm period in us
*
- * @return min PWM period in us
+ * @return min pwm in us
*/
int
min_period()
{
- return mraa_pwm_get_min_period(m_pwm);
+ return mraa_pwm_get_min_period();
}
private:
diff --git a/peripheral/libmraa/api/mraa/spi.h b/peripheral/libmraa/api/mraa/spi.h
index d53ec3a..6902a6e 100644
--- a/peripheral/libmraa/api/mraa/spi.h
+++ b/peripheral/libmraa/api/mraa/spi.h
@@ -111,13 +111,13 @@ mraa_result_t mraa_spi_frequency(mraa_spi_context dev, int hz);
int mraa_spi_write(mraa_spi_context dev, uint8_t data);
/**
- * Write Two Bytes to the SPI device.
+ *Write Two Bytes to the SPI device.
*
* @param dev The Spi context
* @param data Data to send
* @return Data received on the miso line
*/
-int mraa_spi_write_word(mraa_spi_context dev, uint16_t data);
+uint16_t mraa_spi_write_word(mraa_spi_context dev, uint16_t data);
/**
* Write Buffer of bytes to the SPI device. The pointer return has to be
diff --git a/peripheral/libmraa/api/mraa/spi.hpp b/peripheral/libmraa/api/mraa/spi.hpp
index 94a3f64..e5ffb09 100644
--- a/peripheral/libmraa/api/mraa/spi.hpp
+++ b/peripheral/libmraa/api/mraa/spi.hpp
@@ -80,20 +80,6 @@ class Spi
}
/**
- * Spi Constructor, takes a pointer to a SPI context and initialises
- * the SPI class
- *
- * @param void * to SPI context
- */
- Spi(void* spi_context)
- {
- m_spi = (mraa_spi_context) spi_context;
- if (m_spi == NULL) {
- throw std::invalid_argument("Invalid SPI context");
- }
- }
-
- /**
* Closes spi bus
*/
~Spi()
@@ -141,10 +127,10 @@ class Spi
* Write single byte to the SPI device
*
* @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
*/
- int
- writeWord(uint16_t data)
+ uint16_t
+ write_word(uint16_t data)
{
return mraa_spi_write_word(m_spi, (uint16_t) data);
}
@@ -175,7 +161,7 @@ class Spi
* @return uint8_t* data received on the miso line. Same length as passed in
*/
uint16_t*
- writeWord(uint16_t* txBuf, int length)
+ write_word(uint16_t* txBuf, int length)
{
return mraa_spi_write_buf_word(m_spi, txBuf, length);
}
diff --git a/peripheral/libmraa/api/mraa/types.h b/peripheral/libmraa/api/mraa/types.h
index 5c43dd4..fe0daad 100644
--- a/peripheral/libmraa/api/mraa/types.h
+++ b/peripheral/libmraa/api/mraa/types.h
@@ -50,14 +50,10 @@ typedef enum {
MRAA_INTEL_SOFIA_3GR = 10, /**< The Intel SoFIA 3GR */
MRAA_INTEL_CHERRYHILLS = 11, /**< The Intel Braswell Cherryhills */
MRAA_UP = 12, /**< The UP Board */
- MRAA_INTEL_GT_TUCHUCK = 13, /**< The Intel GT Tuchuck Board */
// USB platform extenders start at 256
MRAA_FTDI_FT4222 = 256, /**< FTDI FT4222 USB to i2c bridge */
- // contains bit 9 so is subplatform
- MRAA_GENERIC_FIRMATA = 1280, /**< Firmata uart platform/bridge */
-
MRAA_NULL_PLATFORM = 98, /**< Platform with no capabilities that hosts a sub platform */
MRAA_UNKNOWN_PLATFORM =
99 /**< An unknown platform type, typically will load INTEL_GALILEO_GEN1 */
@@ -207,9 +203,7 @@ typedef enum {
MRAA_ERROR_NO_DATA_AVAILABLE = 9, /**< No data available */
MRAA_ERROR_INVALID_PLATFORM = 10, /**< Platform not recognised */
MRAA_ERROR_PLATFORM_NOT_INITIALISED = 11, /**< Board information not initialised */
- MRAA_ERROR_UART_OW_SHORTED = 12, /**< UART OW Short Circuit Detected*/
- MRAA_ERROR_UART_OW_NO_DEVICES = 13, /**< UART OW No devices detected */
- MRAA_ERROR_UART_OW_DATA_ERROR = 14, /**< UART OW Data/Bus error detected */
+ MRAA_ERROR_PLATFORM_ALREADY_INITIALISED = 12, /**< Board is already initialised */
MRAA_ERROR_UNSPECIFIED = 99 /**< Unknown Error */
} mraa_result_t;
@@ -245,6 +239,8 @@ typedef enum {
MRAA_UART_PARITY_SPACE = 4
} mraa_uart_parity_t;
+
+
#ifdef __cplusplus
}
#endif
diff --git a/peripheral/libmraa/api/mraa/types.hpp b/peripheral/libmraa/api/mraa/types.hpp
index 4aa50a8..cc6f53a 100644
--- a/peripheral/libmraa/api/mraa/types.hpp
+++ b/peripheral/libmraa/api/mraa/types.hpp
@@ -46,18 +46,7 @@ typedef enum {
RASPBERRY_PI = 5, /**< The different Raspberry PI Models -like A,B,A+,B+ */
BEAGLEBONE = 6, /**< The different BeagleBone Black Modes B/C */
BANANA = 7, /**< Allwinner A20 based Banana Pi and Banana Pro */
- INTEL_NUC5 = 8, /**< The Intel 5th generations Broadwell NUCs */
- A96BOARDS = 9, /**< Linaro 96boards, A prefix for 'ARM' since not allowed numerical */
- INTEL_SOFIA_3GR = 10, /**< The Intel SoFIA 3GR */
- INTEL_CHERRYHILLS = 11, /**< The Intel Braswell Cherryhills */
- INTEL_UP = 12, /**< The UP Board */
- INTEL_GT_TUCHUCK = 13, /**< The Intel GT Board */
- FTDI_FT4222 = 256, /**< FTDI FT4222 USB to i2c bridge */
-
- GENERIC_FIRMATA = 1280, /**< Firmata uart platform/bridge */
-
- NULL_PLATFORM = 98,
UNKNOWN_PLATFORM =
99 /**< An unknown platform type, typically will load INTEL_GALILEO_GEN1 */
} Platform;
@@ -206,9 +195,7 @@ typedef enum {
ERROR_NO_DATA_AVAILABLE = 9, /**< No data available */
ERROR_INVALID_PLATFORM = 10, /**< Platform not recognised */
ERROR_PLATFORM_NOT_INITIALISED = 11, /**< Board information not initialised */
- ERROR_UART_OW_SHORTED = 12, /**< UART OW Short Circuit Detected*/
- ERROR_UART_OW_NO_DEVICES = 13, /**< UART OW No devices detected */
- ERROR_UART_OW_DATA_ERROR = 14, /**< UART OW Data/Bus error detected */
+ ERROR_PLATFORM_ALREADY_INITIALISED = 12, /**< Board is already initialised */
ERROR_UNSPECIFIED = 99 /**< Unknown Error */
} Result;
diff --git a/peripheral/libmraa/api/mraa/uart.h b/peripheral/libmraa/api/mraa/uart.h
index c6f5a7a..3ca196f 100644
--- a/peripheral/libmraa/api/mraa/uart.h
+++ b/peripheral/libmraa/api/mraa/uart.h
@@ -119,15 +119,6 @@ mraa_result_t mraa_uart_set_flowcontrol(mraa_uart_context dev, mraa_boolean_t xo
mraa_result_t mraa_uart_set_timeout(mraa_uart_context dev, int read, int write, int interchar);
/**
- * Set the blocking state for write operations
- *
- * @param dev The UART context
- * @param nonblock new nonblocking state
- * @return Result of operation
- */
-mraa_result_t mraa_uart_set_non_blocking(mraa_uart_context dev, mraa_boolean_t nonblock);
-
-/**
* Get Char pointer with tty device path within Linux
* For example. Could point to "/dev/ttyS0"
*
diff --git a/peripheral/libmraa/api/mraa/uart.hpp b/peripheral/libmraa/api/mraa/uart.hpp
index 0771fdc..cc485a3 100644
--- a/peripheral/libmraa/api/mraa/uart.hpp
+++ b/peripheral/libmraa/api/mraa/uart.hpp
@@ -76,20 +76,6 @@ class Uart
}
/**
- * Uart Constructor, takes a pointer to the UART context and initialises
- * the UART class
- *
- * @param void * to a UART context
- */
- Uart(void* uart_context)
- {
- m_uart = (mraa_uart_context) uart_context;
-
- if (m_uart == NULL) {
- throw std::invalid_argument("Invalid UART context");
- }
- }
- /**
* Uart destructor
*/
~Uart()
@@ -140,17 +126,12 @@ class Uart
* Read bytes from the device into a String object
*
* @param length to read
- * @throws std::bad_alloc If there is no space left for read.
* @return string of data
*/
std::string
readStr(int length)
{
char* data = (char*) malloc(sizeof(char) * length);
- if (data == NULL) {
- throw std::bad_alloc();
- }
-
int v = mraa_uart_read(m_uart, data, (size_t) length);
std::string ret(data, v);
free(data);
@@ -255,19 +236,6 @@ class Uart
return (Result) mraa_uart_set_timeout(m_uart, read, write, interchar);
}
- /**
- * Set the blocking state for write operations
- *
- * @param dev The UART context
- * @param nonblock new nonblocking state
- * @return Result of operation
- */
- Result
- SetNonBlocking(bool nonblock)
- {
- return (Result) mraa_uart_set_non_blocking(m_uart, nonblock);
- }
-
private:
mraa_uart_context m_uart;
};
diff --git a/peripheral/libmraa/api/mraa/uart_ow.h b/peripheral/libmraa/api/mraa/uart_ow.h
deleted file mode 100644
index 7cd098f..0000000
--- a/peripheral/libmraa/api/mraa/uart_ow.h
+++ /dev/null
@@ -1,197 +0,0 @@
-/*
- * Author: Jon Trulson <jtrulson@ics.com>
- * Copyright (c) 2016 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
-
-/**
- * @file
- * @brief UART OW module
- *
- * This module allows one to use MRAA's UART support in order to
- * interact with Dallas 1-wire compliant devices on a 1-wire bus. It
- * makes use of the UART for timing purposes. The principle of
- * operation is described here:
- * https://www.maximintegrated.com/en/app-notes/index.mvp/id/214
- *
- * It is important the you use a UART with CMOS/TTL level voltages
- * (3.3v/5v) RX and TX lines. DO NOT use standard RS232 level
- * voltages, or you are going to have a bad day.
- *
- * In addition, a diode should be placed across the RX and
- * TX lines like so:
- *
- * -|
- * U|
- * A| TX---|<--+
- * R| |
- * T| RX-------o--------o 1-wire data bus
- * -|
- *
- * The diode on TX is a 1N4148 (cheap and common), with the cathode
- * connected to TX, and the anode connected to RX and the rest of the
- * 1-wire data line.
- *
- * @snippet uart_ow.c Interesting
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stdio.h>
-
-#include "common.h"
-#include "uart.h"
-
-/* for now, we simply use the normal MRAA UART context */
-typedef struct _mraa_uart_ow {
- mraa_uart_context uart;
- /* search state */
- unsigned char ROM_NO[8]; /* 8 byte (64b) rom code */
- int LastDiscrepancy;
- int LastFamilyDiscrepancy;
- mraa_boolean_t LastDeviceFlag;
-} *mraa_uart_ow_context;
-
-/* 8 bytes (64 bits) for a device rom code */
-static const int MRAA_UART_OW_ROMCODE_SIZE = 8;
-
-/**
- * UART One Wire ROM related Command bytes
- */
-typedef enum {
- MRAA_UART_OW_CMD_READ_ROM = 0x33, /**< read rom, when only one device on bus */
- MRAA_UART_OW_CMD_MATCH_ROM = 0x55, /**< match a specific rom code */
- MRAA_UART_OW_CMD_SKIP_ROM = 0xcc, /**< skip match/search rom */
- MRAA_UART_OW_CMD_SEARCH_ROM_ALARM = 0xec, /**< search all roms in alarm state */
- MRAA_UART_OW_CMD_SEARCH_ROM = 0xf0 /**< search all rom codes */
-} mraa_uart_ow_rom_cmd_t;
-
-/**
- * Initialise uart_ow_context, uses UART board mapping
- *
- * @param uart the index of the uart set to use
- * @return uart_ow context or NULL
- */
-mraa_uart_ow_context mraa_uart_ow_init(int uart);
-
-/**
- * Initialise a raw uart_ow_context. No board setup.
- *
- * @param path for example "/dev/ttyS0"
- * @return uart_ow context or NULL
- */
-mraa_uart_ow_context mraa_uart_ow_init_raw(const char* path);
-
-/**
- * Get char pointer with tty device path within Linux
- * For example. Could point to "/dev/ttyS0"
- *
- * @param dev uart_ow context
- * @return char pointer of device path
- */
-const char* mraa_uart_ow_get_dev_path(mraa_uart_ow_context dev);
-
-/**
- * Destroy a mraa_uart_ow_context
- *
- * @param dev uart_ow context
- * @return mraa_result_t
- */
-mraa_result_t mraa_uart_ow_stop(mraa_uart_ow_context dev);
-
-/**
- * Read a byte from the 1-wire bus
- *
- * @param dev uart_ow context
- * @return the byte read or -1 for error
- */
-int mraa_uart_ow_read_byte(mraa_uart_ow_context dev);
-
-/**
- * Write a byte to a 1-wire bus
- *
- * @param dev uart_ow context
- * @param byte the byte to write to the bus
- * @return the byte read back during the time slot or -1 for error
- */
-int mraa_uart_ow_write_byte(mraa_uart_ow_context dev, uint8_t byte);
-
-/**
- * Write a bit to a 1-wire bus and read a bit corresponding to the
- * time slot back. This is possible due to the way we wired the TX
- * and RX together with a diode, forming a loopback.
- *
- * @param dev uart_ow context
- * @param bit the bit to write to the bus
- * @return the bit read back during the time slot or -1 for error
- */
-int mraa_uart_ow_bit(mraa_uart_ow_context dev, uint8_t bit);
-
-/**
- * Send a reset pulse to the 1-wire bus and test for device presence
- *
- * @param dev uart_ow context
- * @return one of the mraa_result_t values
- */
-mraa_result_t mraa_uart_ow_reset(mraa_uart_ow_context dev);
-
-/**
- * Begin a rom code search of the 1-wire bus. This function
- * implements the 1-wire search algorithm. See the uart_ow.c example
- * for an idea on how to use this function to identify all devices
- * present on the bus.
- *
- * @param dev uart_ow context
- * @param start true to start a new search from scratch, false to
- * continue an existing search
- * @param id the 8-byte rom code id of the current matched device when
- * a device is found
- * @return one of the mraa_result_t values
- */
-mraa_result_t mraa_uart_ow_rom_search(mraa_uart_ow_context dev, mraa_boolean_t start, uint8_t* id);
-
-/**
- * Send a command byte to a device on the 1-wire bus
- *
- * @param dev uart_ow context
- * @param command the command byte to send
- * @param id the rom code id of the device to receive the command,
- * NULL for all devices on the bus
- * @return one of the mraa_result_t values
- */
-mraa_result_t mraa_uart_ow_command(mraa_uart_ow_context dev, uint8_t command, uint8_t* id);
-
-/**
- * Perform a Dallas 1-wire compliant CRC8 computation on a buffer
- *
- * @param buffer the buffer containing the data
- * @param length the length of the buffer
- * @return the computed CRC
- */
-uint8_t mraa_uart_ow_crc8(uint8_t* buffer, uint16_t length);
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/peripheral/libmraa/api/mraa/uart_ow.hpp b/peripheral/libmraa/api/mraa/uart_ow.hpp
deleted file mode 100644
index d20f87e..0000000
--- a/peripheral/libmraa/api/mraa/uart_ow.hpp
+++ /dev/null
@@ -1,277 +0,0 @@
-/*
- * Author: Jon Trulson <jtrulson@ics.com>
- * Copyright (c) 2016 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
-
-#include "uart_ow.h"
-#include "types.hpp"
-#include <stdexcept>
-#include <cstring>
-
-namespace mraa
-{
-
-/**
- * @brief API for UART One Wire
- *
- * This file defines the UartOW (UART to Dallas 1-wire) interface for libmraa
- *
- * @snippet UartOW.cpp Interesting
- */
-class UartOW
-{
- public:
- /**
- * UartOW Constructor, takes a pin number which will map directly to the
- * linux uart number, this 'enables' the uart, nothing more
- *
- * @throws std::invalid_argument in case of error
- * @param uart the index of the uart to use
- */
- UartOW(int uart)
- {
- m_uart = mraa_uart_ow_init(uart);
-
- if (m_uart == NULL) {
- throw std::invalid_argument("Error initialising UART_OW");
- }
- }
-
- /**
- * UartOW Constructor, takes a string to the path of the serial
- * interface that is needed.
- *
- * @throws std::invalid_argument in case of error
- * @param path the file path for the UART to use
- */
- UartOW(std::string path)
- {
- m_uart = mraa_uart_ow_init_raw(path.c_str());
-
- if (m_uart == NULL) {
- throw std::invalid_argument("Error initialising UART");
- }
- }
-
- /**
- * Uart destructor
- */
- ~UartOW()
- {
- mraa_uart_ow_stop(m_uart);
- }
-
- /**
- * Get string with tty device path within Linux
- * For example. Could point to "/dev/ttyS0"
- *
- * @return char pointer of device path
- */
- std::string
- getDevicePath()
- {
- std::string ret_val(mraa_uart_ow_get_dev_path(m_uart));
- return ret_val;
- }
-
- /**
- * Read a byte from the 1-wire bus
- *
- * @throws std::invalid_argument in case of error
- * @return the byte read
- */
- uint8_t
- readByte()
- {
- int res = mraa_uart_ow_read_byte(m_uart);
- if (res == -1) {
- throw std::invalid_argument("Unknown UART_OW error");
- }
- return (uint8_t) res;
- }
-
- /**
- * Write a byte to a 1-wire bus
- *
- * @param byte the byte to write to the bus
- *
- * @throws std::invalid_argument in case of error
- * @return the byte read back during the time slot
- */
- uint8_t
- writeByte(uint8_t byte)
- {
- int res = mraa_uart_ow_write_byte(m_uart, byte);
- if (res == -1) {
- throw std::invalid_argument("Unknown UART_OW error");
- }
- return (uint8_t) res;
- }
-
- /**
- * Write a bit to a 1-wire bus and read a bit corresponding to the
- * time slot back. This is possible due to the way we wired the TX
- * and RX together with a diode, forming a loopback.
- *
- * @param bit the bit to write to the bus
- * @throws std::invalid_argument in case of error
- * @return the bit read back during the time slot
- */
- bool
- writeBit(bool bit)
- {
- int res = mraa_uart_ow_bit(m_uart, (bit) ? 1 : 0);
- if (res == -1) {
- throw std::invalid_argument("Unknown UART_OW error");
- }
- return ((res) ? true : false);
- }
-
- /**
- * Send a reset pulse to the 1-wire bus and test for device presence
- *
- * @return one of the mraa::Result values
- */
- mraa::Result
- reset()
- {
- return (mraa::Result) mraa_uart_ow_reset(m_uart);
- }
-
- /**
- * Begin a rom code search of the 1-wire bus. This function
- * implements the 1-wire search algorithm. See the uart_ow.c example
- * for an idea on how to use this function to identify all devices
- * present on the bus.
- *
- * @param start true to start a search from scratch, false to
- * continue a previously started search
- * @param id the 8-byte rom code id of the current matched device when a
- * device is found
- * @return one of the mraa::Result values
- */
- mraa::Result
- search(bool start, uint8_t* id)
- {
- return (mraa::Result) mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
- }
-
- /**
- * Begin a rom code search of the 1-wire bus. This function
- * implements the 1-wire search algorithm. See the UartOW.cpp
- * example for an idea on how to use this function to identify all
- * devices present on the bus.
- *
- * @param start true to start a search from scratch, false to
- * continue a previously started search
- * @return an empty string if no [more] devices are found, or a
- * string containing the 8-byte romcode of a detected device.
- */
- std::string
- search(bool start)
- {
- uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
- mraa_result_t rv;
-
- rv = mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
-
- if (rv == MRAA_SUCCESS) {
- // we found one
- std::string idStr((char*) id, MRAA_UART_OW_ROMCODE_SIZE);
- return idStr;
- } else {
- // failure, or end of search
- return "";
- }
- }
-
- /**
- * Send a command byte to a device on the 1-wire bus
- *
- * @param command the command byte to send
- * @param id the rom code id of the device to receive the command,
- * NULL for all devices on the bus
- * @return one of the mraa::Result values
- */
- mraa::Result
- command(uint8_t command, uint8_t* id)
- {
- return (mraa::Result) mraa_uart_ow_command(m_uart, command, id);
- }
-
- /**
- * Send a command byte to a device on the 1-wire bus, supplying
- * the id as a std::string
- *
- * @param command the command byte to send
- * @param id std::string representing the code id of the device to
- * receive the command, or an empty string for all devices on the
- * bus. This string should be 8 bytes in size.
- * @return one of the mraa::Result values
- */
- mraa::Result
- command(uint8_t command, std::string id)
- {
- if (id.empty() == 0)
- return (mraa::Result) mraa_uart_ow_command(m_uart, command, NULL);
- else {
- if (id.size() != 8) {
- // Only 8 byte romcodes are legal.
- throw std::invalid_argument(std::string(__FUNCTION__) +
- ": id must be 8 bytes only");
- }
- return (mraa::Result) mraa_uart_ow_command(m_uart, command, (uint8_t*) id.c_str());
- }
- }
-
- /**
- * Perform a Dallas 1-wire compliant CRC8 computation on a buffer
- *
- * @param buffer the buffer containing the data
- * @param length the length of the buffer
- * @return the computed CRC
- */
- uint8_t
- crc8(uint8_t* buffer, uint16_t length)
- {
- return mraa_uart_ow_crc8(buffer, length);
- }
-
- /**
- * Perform a Dallas 1-wire compliant CRC8 computation on a
- * std::string based buffer
- *
- * @param buffer std::string buffer containing the data
- * @return the computed CRC
- */
- uint8_t
- crc8(std::string buffer)
- {
- return mraa_uart_ow_crc8((uint8_t*) buffer.c_str(), buffer.size());
- }
-
- private:
- mraa_uart_ow_context m_uart;
-};
-}