Private
Public Access
2
0

examples: Cleanup C++ examples

Modify the C++ examples to be of same coding standard like C. As a
part of this cleanup, a new LED example is also added.

Signed-off-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Manivannan Sadhasivam
2018-01-24 15:09:29 +05:30
committed by Brendan Le Foll
parent eaaebae69d
commit 83a67b96fd
13 changed files with 463 additions and 225 deletions

View File

@@ -1,60 +0,0 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Contributors: Alex Tereschenko <alex.mkrs@gmail.com>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <signal.h>
#include "mraa.hpp"
int running = 0;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
printf("closing down nicely\n");
running = -1;
}
}
//! [Interesting]
int
main()
{
uint16_t adc_value;
float adc_value_float;
mraa::Aio a0(0);
signal(SIGINT, sig_handler);
while (running == 0) {
adc_value = a0.read();
adc_value_float = a0.readFloat();
fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
fprintf(stdout, "ADC A0 read float - %.5f (Ctrl+C to exit)\n", adc_value_float);
}
return MRAA_SUCCESS;
}
//! [Interesting]

View File

@@ -1,27 +1,29 @@
enable_language(CXX)
add_executable (AioA0 AioA0.cpp)
add_executable (blink-io-cpp Blink-IO.cpp)
add_executable (Pwm3-cycle Pwm3-cycle.cpp)
add_executable (I2c-compass I2c-compass.cpp)
add_executable (Spi-pot Spi-pot.cpp)
add_executable (Uart Uart-example.cpp)
add_executable (Isr-pin6 Isr-pin6.cpp)
add_executable (Iio-dummy Iio-dummy.cpp)
add_executable (aio_cpp aio.cpp)
add_executable (gpio_advanced_cpp gpio_advanced.cpp)
add_executable (gpio_cpp gpio.cpp)
add_executable (pwm_cpp pwm.cpp)
add_executable (i2c_cpp i2c.cpp)
add_executable (spi_cpp spi.cpp)
add_executable (uart_cpp uart.cpp)
add_executable (iio_cpp iio.cpp)
add_executable (led_cpp led.cpp)
include_directories(${PROJECT_SOURCE_DIR}/api)
include_directories(${PROJECT_SOURCE_DIR}/api/mraa)
target_link_libraries (AioA0 mraa stdc++)
target_link_libraries (blink-io-cpp mraa stdc++)
target_link_libraries (Pwm3-cycle mraa stdc++)
target_link_libraries (I2c-compass mraa stdc++ m)
target_link_libraries (Spi-pot mraa stdc++)
target_link_libraries (Uart mraa stdc++)
target_link_libraries (Isr-pin6 mraa stdc++)
target_link_libraries (Iio-dummy mraa stdc++)
target_link_libraries (aio_cpp mraa stdc++)
target_link_libraries (gpio_advanced_cpp mraa stdc++)
target_link_libraries (gpio_cpp mraa stdc++)
target_link_libraries (pwm_cpp mraa stdc++)
target_link_libraries (i2c_cpp mraa stdc++ m)
target_link_libraries (spi_cpp mraa stdc++)
target_link_libraries (uart_cpp mraa stdc++)
target_link_libraries (iio_cpp mraa stdc++)
target_link_libraries (led_cpp mraa stdc++)
if (ONEWIRE)
add_executable (UartOW UartOW.cpp)
target_link_libraries (UartOW mraa stdc++)
add_executable (uart_ow_cpp uart_ow.cpp)
target_link_libraries (uart_ow_cpp mraa stdc++)
endif ()

View File

@@ -1,6 +1,7 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Contributors: Alex Tereschenko <alex.mkrs@gmail.com>
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@@ -21,53 +22,52 @@
* 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.
*
* Example usage: Reads ADC A0 value continuously. Press Ctrl+C to exit.
*/
/* standard headers */
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa.hpp"
#define DEFAULT_IOPIN 8
/* mraa headers */
#include "mraa/aio.hpp"
#include "mraa/common.hpp"
static int iopin;
int running = 0;
/* AIO port */
#define AIO_PORT 0
volatile sig_atomic_t flag = 1;
void
sig_handler(int signo)
sig_handler(int signum)
{
if (signo == SIGINT) {
printf("closing IO%d nicely\n", iopin);
running = -1;
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main(int argc, char** argv)
main(void)
{
if (argc < 2) {
printf("Provide an int arg if you want to flash on something other than %d\n", DEFAULT_IOPIN);
iopin = DEFAULT_IOPIN;
} else {
iopin = strtol(argv[1], NULL, 10);
}
uint16_t value;
float float_value;
signal(SIGINT, sig_handler);
//! [Interesting]
mraa::Gpio gpio(iopin);
mraa::Result response = gpio.dir(mraa::DIR_OUT);
if (response != mraa::SUCCESS) {
mraa::printError(response);
return 1;
}
/* initialize AIO */
mraa::Aio aio(AIO_PORT);
while (running == 0) {
response = gpio.write(1);
sleep(1);
response = gpio.write(0);
sleep(1);
while (flag) {
value = aio.read();
float_value = aio.readFloat();
std::cout << "ADC A0 read %X - %d" << value << value << std::endl;
std::cout << "ADC A0 read float - %.5f" << float_value << std::endl;
}
return response;
//! [Interesting]
return EXIT_SUCCESS;
}

114
examples/c++/gpio.cpp Normal file
View File

@@ -0,0 +1,114 @@
/*
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2018 Linaro Ltd.
*
* 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.
*
* Example usage: Toggles GPIO's 23 and 24 recursively. Press Ctrl+C to exit
*
*/
/* standard headers */
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/gpio.hpp"
/* gpio declaration */
#define GPIO_PIN_1 23
#define GPIO_PIN_2 24
volatile sig_atomic_t flag = 1;
void
sig_handler(int signum)
{
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main(void)
{
mraa::Result status;
/* install signal handler */
signal(SIGINT, sig_handler);
//! [Interesting]
/* initialize GPIO pin */
mraa::Gpio gpio_1(GPIO_PIN_1);
/* initialize GPIO pin */
mraa::Gpio gpio_2(GPIO_PIN_2);
/* set GPIO to output */
status = gpio_1.dir(mraa::DIR_OUT);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
/* set gpio 24 to output */
status = gpio_2.dir(mraa::DIR_OUT);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
/* toggle both GPIO's */
while (flag) {
status = gpio_1.write(1);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
status = gpio_2.write(0);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
sleep(1);
status = gpio_1.write(0);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
status = gpio_2.write(1);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
sleep(1);
}
//! [Interesting]
return EXIT_SUCCESS;
}

View File

@@ -1,7 +1,6 @@
/*
* Author: Brendan Le Foll
* Contributors: Alex Tereschenko <alext.mkrs@gmail.com>
* Copyright (c) 2015 Intel Corporation.
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2018 Linaro Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
@@ -21,39 +20,54 @@
* 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.
*
* Example usage: Configures GPIO pin for interrupt and waits 30 seconds for the isr to trigger
*
*/
/* standard headers */
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include "mraa.hpp"
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/gpio.hpp"
static volatile int counter = 0;
static volatile int oldcounter = 0;
#define GPIO_PIN 6
void
interrupt(void* args)
int_handler(void* args)
{
++counter;
std::cout << "ISR triggered" << std::endl;
}
int
main()
main(void)
{
mraa::Gpio x(6);
mraa::Result status;
x.dir(mraa::DIR_IN);
//! [Interesting]
/* initialize GPIO */
mraa::Gpio gpio(GPIO_PIN);
x.isr(mraa::EDGE_BOTH, &interrupt, NULL);
int i = 100;
for (; i > 0; --i) {
if (counter != oldcounter) {
fprintf(stdout, "timeout counter == %d\n", counter);
oldcounter = counter;
}
// got to relieve our poor CPU!
sleep(1);
/* set GPIO to input */
status = gpio.dir(mraa::DIR_IN);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
return MRAA_SUCCESS;
/* configure ISR for GPIO */
status = gpio.isr(mraa::EDGE_BOTH, &int_handler, NULL);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
/* wait 30 seconds isr trigger */
sleep(30);
//! [Interesting]
return EXIT_SUCCESS;
}

View File

@@ -21,13 +21,22 @@
* 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.
*
* Example usage: Outputs X,Y,Z co-ordinates and direction recursively using
* HMC5883L. Press Ctrl+C to exit.
*
*/
/* standard headers */
#include <iostream>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "math.h"
#include "mraa.hpp"
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/i2c.hpp"
#define MAX_BUFFER_LENGTH 6
#define HMC5883L_I2C_ADDR 0x1E
@@ -79,42 +88,41 @@
#define SCALE_3_03_MG 3.03
#define SCALE_4_35_MG 4.35
#define I2C_BUS 0
int running = 0;
volatile sig_atomic_t flag = 1;
void
sig_handler(int signo)
sig_handler(int signum)
{
if (signo == SIGINT) {
printf("closing nicely\n");
running = -1;
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main()
main(void)
{
float direction = 0;
int16_t x = 0, y = 0, z = 0;
float direction;
int16_t x, y, z;
uint8_t rx_tx_buf[MAX_BUFFER_LENGTH];
signal(SIGINT, sig_handler);
//! [Interesting]
mraa::I2c i2c(0);
mraa::I2c i2c(I2C_BUS);
i2c.address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_CONF_REG_B;
rx_tx_buf[1] = GA_1_3_REG;
i2c.write(rx_tx_buf, 2);
//! [Interesting]
i2c.address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_MODE_REG;
rx_tx_buf[1] = HMC5883L_CONT_MODE;
i2c.write(rx_tx_buf, 2);
signal(SIGINT, sig_handler);
while (running == 0) {
while (flag) {
i2c.address(HMC5883L_I2C_ADDR);
i2c.writeByte(HMC5883L_DATA_REG);
@@ -132,11 +140,13 @@ main()
if (direction < 0)
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("Heading : %f\n", direction * 180 / M_PI);
std::cout << "Compass scaled data x : %f, y : %f, z : %f" << x * SCALE_0_92_MG
<< y * SCALE_0_92_MG << z * SCALE_0_92_MG << std::endl;
std::cout << "Heading : %f" << direction * 180 / M_PI << std::endl;
sleep(1);
}
//! [Interesting]
return MRAA_SUCCESS;
return EXIT_SUCCESS;
}

View File

@@ -20,14 +20,23 @@
* 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.
*
* Example usage: Sets accelerometer scale and registers for the threshold event
*
*/
#include "mraa/iio.hpp"
/* standard headers */
#include <float.h>
#include <iostream>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/iio.hpp"
#define EXPECT_FAILURE 0
#define EXPECT_SUCCESS 1
@@ -82,9 +91,9 @@ log_result(std::string test_name, std::string attr_name, bool expect_success, bo
else
result = success ? "FAIL" : "PASS";
if (attr_name.empty())
fprintf(stdout, "%s: %s\n", test_name.c_str(), result.c_str());
std::cout << "%s: %s" << test_name.c_str() << result.c_str() << std::endl;
else
fprintf(stdout, "%s(%s): %s\n", test_name.c_str(), attr_name.c_str(), result.c_str());
std::cout << "%s(%s): %s" << test_name.c_str() << attr_name.c_str() << result.c_str() << std::endl;
}
// Generate iio_dummy driver event by writing a string to a specific sysfs node
@@ -113,10 +122,10 @@ class IioTestHandler : public mraa::IioHandler
}
};
//! [Interesting]
int
main()
main(void)
{
//! [Interesting]
IioTestHandler testHandler;
std::string deviceName;
try {
@@ -144,7 +153,6 @@ main()
return EXIT_FAILURE;
}
std::cout << "Using IIO device0. Name is " << iio_device->getDeviceName() << std::endl;
IIO_RUN(writeFloat, "in_accel_x_raw", 100, EXPECT_FAILURE);
IIO_RUN(writeFloat, "in_voltage0_scale", 100, EXPECT_FAILURE);
@@ -157,8 +165,9 @@ main()
generate_event();
usleep(500000);
log_result("eventReceived", "", (eventCount == 1), true);
//! [Interesting]
delete iio_device;
return EXIT_SUCCESS;
}
//! [Interesting]

86
examples/c++/led.cpp Normal file
View File

@@ -0,0 +1,86 @@
/*
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2018, Linaro Ltd.
*
* 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.
*
* Example usage: Reads maximum brightness value for user led and turns it
* on/off depending on current state. Then sets led trigger
* to heartbeat.
*
*/
/* standard headers */
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/led.hpp"
/* LED name */
#define USER_LED "user1"
/* trigger type */
#define LED_TRIGGER "heartbeat"
int
main(void)
{
mraa::Result status;
int val;
//! [Interesting]
/* initialize user1 led */
mraa::Led led(USER_LED);
/* read maximum brightness */
val = led.readMaxBrightness();
std::cout << "maximum brightness value for user1 led is: %d" << val << std::endl;
if (val >= 1) {
val = 0;
} else {
/* never reached mostly */
val = 1;
}
/* turn led on/off depending on max_brightness value */
status = led.setBrightness(val);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
/* sleep for 5 seconds */
usleep(5000000);
/* set led trigger to heartbeat */
status = led.trigger(LED_TRIGGER);
if (status != mraa::SUCCESS) {
printError(status);
return EXIT_FAILURE;
}
std::cout << "led trigger set to: heartbeat" << std::endl;
//! [Interesting]
return EXIT_SUCCESS;
}

View File

@@ -20,35 +20,47 @@
* 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.
*
* Example usage: Generates PWM at a step rate of 0.01 continuously.
* Press Ctrl+C to exit
*/
/* standard headers */
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa.hpp"
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/pwm.hpp"
int running = 0;
#define PWM_PORT 3
volatile sig_atomic_t flag = 1;
void
sig_handler(int signo)
sig_handler(int signum)
{
if (signo == SIGINT) {
printf("closing PWM nicely\n");
running = -1;
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main()
main(void)
{
float value = 0.0f;
signal(SIGINT, sig_handler);
//! [Interesting]
mraa::Pwm pwm(3);
fprintf(stdout, "Cycling PWM on IO3 (pwm3) \n");
mraa::Pwm pwm(PWM_PORT);
std::cout << "Cycling PWM on IO3 (pwm3)" << std::endl;
pwm.enable(true);
float value = 0.0f;
while (running == 0) {
while (flag) {
value = value + 0.01f;
pwm.write(value);
usleep(50000);
@@ -58,5 +70,5 @@ main()
}
//! [Interesting]
return MRAA_SUCCESS;
return EXIT_SUCCESS;
}

View File

@@ -1,5 +1,6 @@
/*
* Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2014 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@@ -20,58 +21,69 @@
* 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.
*
* Example usage: Sends data continuously to a Spi device. Press Ctrl+C to exit
*
*/
/* standard headers */
#include <iostream>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa.hpp"
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/spi.hpp"
int running = 0;
#define SPI_PORT 0
volatile sig_atomic_t flag = 1;
void
sig_handler(int signo)
sig_handler(int signum)
{
if (signo == SIGINT) {
printf("closing spi nicely\n");
running = -1;
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main()
main(void)
{
signal(SIGINT, sig_handler);
//! [Interesting]
mraa::Spi spi(0);
uint8_t data[] = { 0x00, 100 };
uint8_t rxBuf[2];
uint8_t* recv;
while (running == 0) {
int i;
int i;
signal(SIGINT, sig_handler);
//! [Interesting]
mraa::Spi spi(SPI_PORT);
while (flag) {
for (i = 90; i < 130; i++) {
data[1] = i;
recv = spi.write(data, 2);
printf("Writing -%i", i);
std::cout << "Writing -%i" << i << std::endl;
if (recv) {
printf("RECIVED-%i-%i\n", recv[0], recv[1]);
std::cout << "RECIVED-%i-%i" << recv[0] << recv[1] << std::endl;
free(recv);
}
usleep(100000);
}
for (i = 130; i > 90; i--) {
data[1] = i;
if (spi.transfer(data, rxBuf, 2) == mraa::SUCCESS) {
printf("Writing -%i", i);
printf("RECIVED-%i-%i\n", rxBuf[0], rxBuf[1]);
std::cout << "Writing -%i" << i << std::endl;
std::cout << "RECIVED-%i-%i" << rxBuf[0] << rxBuf[1] << std::endl;
}
usleep(100000);
}
}
//! [Interesting]
return mraa::SUCCESS;
return EXIT_SUCCESS;
}

View File

@@ -1,5 +1,6 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Contributors: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
@@ -20,51 +21,81 @@
* 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.
*
* Example usage: Prints "Hello Mraa!" recursively. Press Ctrl+C to exit
*
*/
#include <exception>
/* standard headers */
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include "mraa.hpp"
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/uart.hpp"
/* UART port */
#define UART_PORT 0
const char* dev_path = "/dev/ttyS0";
volatile sig_atomic_t flag = 1;
void
sig_handler(int signum)
{
if (signum == SIGINT) {
std::cout << "Exiting..." << std::endl;
flag = 0;
}
}
int
main()
main(void)
{
signal(SIGINT, sig_handler);
//! [Interesting]
// If you have a valid platform configuration use numbers to represent uart
// device. If not use raw mode where std::string is taken as a constructor
// parameter
mraa::Uart* dev;
mraa::Uart* uart;
try {
dev = new mraa::Uart(0);
uart = new mraa::Uart(UART_PORT);
} catch (std::exception& e) {
std::cout << e.what() << ", likely invalid platform config" << std::endl;
std::cerr << e.what() << ", likely invalid platform config" << std::endl;
}
try {
dev = new mraa::Uart("/dev/ttyACM0");
uart = new mraa::Uart(dev_path);
} catch (std::exception& e) {
std::cout << "Error while setting up raw UART, do you have a uart?" << std::endl;
std::cerr << "Error while setting up raw UART, do you have a uart?" << std::endl;
std::terminate();
}
if (dev->setBaudRate(115200) != mraa::SUCCESS) {
std::cout << "Error setting parity on UART" << std::endl;
if (uart->setBaudRate(115200) != mraa::SUCCESS) {
std::cerr << "Error setting parity on UART" << std::endl;
}
if (dev->setMode(8, mraa::UART_PARITY_NONE, 1) != mraa::SUCCESS) {
std::cout << "Error setting parity on UART" << std::endl;
if (uart->setMode(8, mraa::UART_PARITY_NONE, 1) != mraa::SUCCESS) {
std::cerr << "Error setting parity on UART" << std::endl;
}
if (dev->setFlowcontrol(false, false) != mraa::SUCCESS) {
std::cout << "Error setting flow control UART" << std::endl;
if (uart->setFlowcontrol(false, false) != mraa::SUCCESS) {
std::cerr << "Error setting flow control UART" << std::endl;
}
dev->writeStr("Hello monkeys");
while (flag) {
/* send data through uart */
uart->writeStr("Hello Mraa!");
sleep(1);
}
//! [Interesting]
delete dev;
delete uart;
return mraa::SUCCESS;
return EXIT_SUCCESS;
}

View File

@@ -20,51 +20,59 @@
* 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.
*
* Example usage: Search and print the found OW device ID recursively.
*
*/
#include "iostream"
#include "stdio.h"
//! [Interesting]
#include "uart_ow.hpp"
/* standard headers */
#include <iostream>
#include <stdlib.h>
/* mraa headers */
#include "mraa/common.hpp"
#include "mraa/uart_ow.hpp"
int
main(int argc, char** argv)
main(void)
{
mraa::Result status;
std::string id;
uint8_t count = 0;
uint8_t* ptr;
//! [Interesting]
mraa::UartOW uart(0);
// Reset the ow bus and see if anything is present
mraa::Result rv;
if ((rv = uart.reset()) == mraa::SUCCESS) {
if ((status = uart.reset()) == mraa::SUCCESS) {
std::cout << "Reset succeeded, device(s) detected!" << std::endl;
} else {
std::cout << "Reset failed, returned " << int(rv) << ". No devices on bus?" << std::endl;
return 1;
std::cout << "Reset failed, returned " << int(status) << ". No devices on bus?" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Looking for devices..." << std::endl;
;
uint8_t count = 0;
// start the search from scratch
std::string id = uart.search(true);
id = uart.search(true);
if (id.empty()) {
std::cout << "No devices detected." << std::endl;
return 1;
return EXIT_FAILURE;
}
while (!id.empty()) {
// hack so we don't need to cast each element of the romcode
// for printf purposes
uint8_t* ptr = (uint8_t*) id.data();
ptr = (uint8_t*) id.data();
// The first byte (0) is the device type (family) code.
// The last byte (7) is the rom code CRC value. The
// intervening bytes are the unique 48 bit device ID.
printf("Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count, ptr[0],
ptr[6], ptr[5], ptr[4], ptr[3], ptr[2], ptr[1], ptr[7]);
std::cout << "Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x" << count << ptr[0]
<< ptr[6] << ptr[5] << ptr[4] << ptr[3] << ptr[2] << ptr[1] << ptr[7] << std::endl;
count++;
// continue the search with start argument set to false
@@ -72,7 +80,7 @@ main(int argc, char** argv)
}
std::cout << "Exiting..." << std::endl;
//! [Interesting]
return 0;
return EXIT_SUCCESS;
}
//! [Interesting]