Private
Public Access
2
0

examples: cleanup examples directory

Following changes are done as a part of cleanup:

1. Moved the platform specific `C` examples from top level examples/
directory to platform/ subdirectory and renamed helloedison.c to
gpio_edison.c

2. C specific examples are moved to a new c/ subdirectory. As a part
of this process, examples are modified to follow same standards
and few new examples are also added.

3. Include the newly added C examples to relevant API documentation

4. Ran clang-format for all source files in c/, c++/, platform/
subdirectories

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
2017-11-17 17:22:34 +05:30
committed by Brendan Le Foll
parent 3fb65de4b8
commit e562c774cf
46 changed files with 1542 additions and 1025 deletions

View File

@@ -0,0 +1,12 @@
add_executable (gpio_edison gpio_edison.c)
target_link_libraries (gpio_edison mraa)
include_directories(${PROJECT_SOURCE_DIR}/api)
include_directories(${PROJECT_SOURCE_DIR}/api/mraa)
if (FIRMATA)
add_executable (firmata_curie_imu firmata_curie_imu.c)
add_executable (i2c_firmata i2c_firmata.c)
target_link_libraries (firmata_curie_imu mraa)
target_link_libraries (i2c_firmata mraa)
endif()

View File

@@ -0,0 +1,92 @@
/*
* Author: Brendan Le Foll
* 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.
*/
#include <unistd.h>
#include "mraa.h"
#include "mraa/firmata.h"
#define FIRMATA_START_SYSEX 0xF0
#define FIRMATA_END_SYSEX 0xF7
#define FIRMATA_CURIE_IMU 0x11
#define FIRMATA_CURIE_IMU_READ_ACCEL 0x00
void
interrupt(uint8_t* buf, int length)
{
printf("reg read returned: %d, with buffer size %d\n", ((buf[6] & 0x7f) | ((buf[7] & 0x7f) << 7)), length);
}
int
main()
{
mraa_init();
//! [Interesting]
/**
* This example reads from the FirmataCurieIMU plugin
*/
mraa_add_subplatform(MRAA_GENERIC_FIRMATA, "/dev/ttyACM0");
mraa_firmata_context firm = mraa_firmata_init(FIRMATA_CURIE_IMU);
if (firm == NULL) {
return EXIT_FAILURE;
}
mraa_firmata_response(firm, interrupt);
char* buffer = calloc(4, 0);
if (buffer == NULL) {
free(firm);
return EXIT_FAILURE;
}
buffer[0] = FIRMATA_START_SYSEX;
buffer[1] = FIRMATA_CURIE_IMU;
buffer[2] = FIRMATA_CURIE_IMU_READ_ACCEL;
buffer[3] = FIRMATA_END_SYSEX;
mraa_firmata_write_sysex(firm, buffer, 4);
sleep(1);
// stop the isr and set it again
mraa_firmata_response_stop(firm);
mraa_firmata_response(firm, interrupt);
mraa_firmata_write_sysex(firm, buffer, 4);
sleep(1);
// close everything and try again
mraa_firmata_close(firm);
firm = mraa_firmata_init(FIRMATA_CURIE_IMU);
mraa_firmata_response(firm, interrupt);
mraa_firmata_write_sysex(firm, buffer, 4);
sleep(10);
mraa_firmata_close(firm);
//! [Interesting]
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,69 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2015 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 <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
//! [Interesting]
#include "mraa.h"
int
main(int argc, char** argv)
{
mraa_result_t ret = MRAA_SUCCESS;
mraa_platform_t platform_type = mraa_get_platform_type();
if (platform_type != MRAA_INTEL_EDISON_FAB_C) {
fprintf(stderr, "Error: This program can only run on an edison\n");
ret = MRAA_ERROR_INVALID_PLATFORM;
goto end;
}
// MRAA_INTEL_EDISON_GP182 == 0 so this will initialise pin0 on arduino
// which is hardware gpio 130 and not 182
mraa_gpio_context gpio182 = mraa_gpio_init(MRAA_INTEL_EDISON_GP182);
if (gpio182 == NULL) {
fprintf(stderr, "Error: Failed to open gpio182\n");
ret = MRAA_ERROR_INVALID_PLATFORM;
goto end;
}
mraa_gpio_dir(gpio182, MRAA_GPIO_OUT);
// we set the owner to false here, this makes sure that we do not close the
// gpio from sysfs in mraa_gpio_close meaning it will stay as an output and
// we will not always transition from 0->1 as gpio182 as output has the
// default position of '0'. Note that the value could change as a result of
// a mraa_gpio_dir however meaning we always go from 0->1 or 1->0
mraa_gpio_owner(gpio182, false);
int val = mraa_gpio_read(gpio182);
printf("GPIO%d (mraa pin %d) was: %d, will set to %d\n", 182, mraa_gpio_get_pin(gpio182), val, !val);
mraa_gpio_write(gpio182, !val);
mraa_gpio_close(gpio182);
end:
mraa_deinit();
return ret;
}
//! [Interesting]

View File

@@ -0,0 +1,63 @@
/*
* 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.
*/
#include "mraa.h"
int
main(int argc, char** argv)
{
mraa_init();
mraa_add_subplatform(MRAA_GENERIC_FIRMATA, "/dev/ttyACM0");
mraa_i2c_context i2c;
i2c = mraa_i2c_init(0 + 512);
#if 0
mraa_i2c_address(i2c, 0x62);
#if 1
uint8_t rx_tx_buf[2];
rx_tx_buf[0] = 0x0;
rx_tx_buf[1] = 0x0;
mraa_i2c_write(i2c, rx_tx_buf, 2);
#endif
//mraa_i2c_write_byte_data(i2c, 0x0, 0x0);
mraa_i2c_write_byte_data(i2c, 0x0, 0x1);
mraa_i2c_write_byte_data(i2c, 0xFF, 0x08);
mraa_i2c_write_byte_data(i2c, 0x00, 0x04);
mraa_i2c_write_byte_data(i2c, 0xA0, 0x02);
#else
mraa_i2c_address(i2c, 0x77);
int res = mraa_i2c_read_byte_data(i2c, 0xd0);
printf("res is 0x%x\n", res);
uint8_t data[2];
mraa_i2c_write_byte(i2c, 0x77);
mraa_i2c_read(i2c, data, 1);
res = mraa_i2c_read_word_data(i2c, 0xAA); // BMP085_CAL_AC1
printf("res is %d\n", res);
#endif
sleep(10);
}

View File

@@ -0,0 +1,75 @@
/*
* Author: Houman Brinjcargorabi <houman.brinjcargorabi@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.
*
* Example usage: Demonstrates how to initialize and close different
* peripherals
*/
/* standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* mraa header */
#include "mraa.h"
int
main()
{
// GPIO-PIN
mraa_gpio_context gpio = (mraa_gpio_context) init_io("gpio-1");
// GPIO-RAW-RAWPIN
mraa_gpio_context gpioraw = (mraa_gpio_context) init_io("gpio-raw-131");
// AIO-PIN
mraa_aio_context aio = (mraa_aio_context) init_io("aio-0");
// PWM-PIN
mraa_pwm_context pwm = (mraa_pwm_context) init_io("pwm-6");
// PWM-RAW-CHIPID-PIN
mraa_pwm_context pwmraw = (mraa_pwm_context) init_io("pwm-raw-0,1");
// UART-INDEX: the index is the one represented internally in the uart_dev array
mraa_uart_context uart = (mraa_uart_context) init_io("uart-1");
// UART-RAW-PATH
mraa_uart_context uartraw = (mraa_uart_context) init_io("uart-raw-/dev/ttyS0");
// SPI-INDEX: same as UART
mraa_spi_context spi = (mraa_spi_context) init_io("spi-0");
// SPI-RAW-BUS-CS: USED to open and use /dev/spidev<BUS>.<CS>
mraa_spi_context spiraw = (mraa_spi_context) init_io("spi-raw-0-1");
// I2C-INDEX: same as UART
mraa_i2c_context i2c = (mraa_i2c_context) init_io("i2c-0");
// I2C-RAW-BUS
mraa_i2c_context i2craw = (mraa_i2c_context) init_io("i2c-raw-0");
// FREE STUFF
mraa_gpio_close(gpio);
mraa_gpio_close(gpioraw);
mraa_aio_close(aio);
mraa_pwm_close(pwm);
mraa_pwm_close(pwmraw);
mraa_uart_close(uart);
mraa_uart_close(uartraw);
mraa_spi_close(spi);
mraa_spi_close(spiraw);
mraa_i2c_close(i2c);
mraa_i2c_close(i2craw);
}