diff --git a/Doxyfile.in b/Doxyfile.in index 816f7ac..d953383 100644 --- a/Doxyfile.in +++ b/Doxyfile.in @@ -866,7 +866,9 @@ EXCLUDE_SYMBOLS = # command). EXAMPLE_PATH = @CMAKE_CURRENT_SOURCE_DIR@/examples/ \ + @CMAKE_CURRENT_SOURCE_DIR@/examples/c \ @CMAKE_CURRENT_SOURCE_DIR@/examples/c++/ \ + @CMAKE_CURRENT_SOURCE_DIR@/examples/platform/ \ @CMAKE_CURRENT_SOURCE_DIR@/docs/ # If the value of the EXAMPLE_PATH tag contains directories, you can use the diff --git a/api/mraa/aio.h b/api/mraa/aio.h index 640eadb..e409c5c 100644 --- a/api/mraa/aio.h +++ b/api/mraa/aio.h @@ -30,7 +30,7 @@ * AIO is the anlog input & output interface to libmraa. It is used to read or * set the voltage applied to an AIO pin. * - * @snippet analogin_a0.c Interesting + * @snippet aio.c Interesting */ #ifdef __cplusplus diff --git a/api/mraa/gpio.h b/api/mraa/gpio.h index 90ad07c..561d967 100644 --- a/api/mraa/gpio.h +++ b/api/mraa/gpio.h @@ -33,7 +33,7 @@ * through sysfs), or memory mapped IO via a /dev/uio device or /dev/mem * depending again on the board configuration. * - * @snippet gpio_read6.c Interesting + * @snippet gpio.c Interesting */ #ifdef __cplusplus diff --git a/api/mraa/i2c.h b/api/mraa/i2c.h index 657a53d..7d17adf 100644 --- a/api/mraa/i2c.h +++ b/api/mraa/i2c.h @@ -32,7 +32,7 @@ * communicate to multiple i2c slaves by configuring the address. * @htmlinclude i2c.txt * - * @snippet i2c_HMC5883L.c Interesting + * @snippet i2c_hmc5883l.c Interesting */ #ifdef __cplusplus diff --git a/api/mraa/iio.h b/api/mraa/iio.h index 717944a..0f82ba4 100644 --- a/api/mraa/iio.h +++ b/api/mraa/iio.h @@ -67,7 +67,7 @@ typedef struct { * * An iio context represents an IIO device * - * @snippet iio_driver.c Interesting + * @snippet iio.c Interesting */ #ifdef __cplusplus diff --git a/api/mraa/led.h b/api/mraa/led.h index 1d3c4a5..eec88e1 100644 --- a/api/mraa/led.h +++ b/api/mraa/led.h @@ -31,6 +31,7 @@ * LED is the Light Emitting Diode interface to libmraa. It is used to * access the on board LED's via sysfs. * + * @snippet led.c Interesting */ #ifdef __cplusplus diff --git a/api/mraa/pwm.h b/api/mraa/pwm.h index a121e3b..599da2f 100644 --- a/api/mraa/pwm.h +++ b/api/mraa/pwm.h @@ -33,7 +33,7 @@ * of resolution so make sure you check the board & pin you are using before * hand. * - * @snippet cycle-pwm3.c Interesting + * @snippet pwm.c Interesting */ #ifdef __cplusplus diff --git a/api/mraa/spi.h b/api/mraa/spi.h index d53ec3a..6c0672f 100644 --- a/api/mraa/spi.h +++ b/api/mraa/spi.h @@ -34,7 +34,7 @@ * more information on spidev devices is available * [here](https://www.kernel.org/doc/Documentation/spi/spidev). * - * @snippet spi_mcp4261.c Interesting + * @snippet spi.c Interesting */ #ifdef __cplusplus diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index a7556b5..0008345 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,48 +1,5 @@ -add_executable (i2c_HMC5883L i2c_HMC5883L.c) -add_executable (hellomraa hellomraa.c) -add_executable (cycle-pwm3 cycle-pwm3.c) -add_executable (blink-io blink-io.c) -add_executable (analogin_a0 analogin_a0.c) -add_executable (isr_pin6 isr_pin6.c) -add_executable (gpio_read6 gpio_read6.c) -add_executable (spi_mcp4261 spi_mcp4261.c) -add_executable (mmap-io2 mmap-io2.c) -add_executable (blink_onboard blink_onboard.c) -add_executable (uart uart.c) -add_executable (spi_max7219 spi_max7219.c) -if (NOT ANDROID_TOOLCHAIN) - add_executable (iio_driver iio_driver.c) -endif () - -include_directories(${PROJECT_SOURCE_DIR}/api) -include_directories(${PROJECT_SOURCE_DIR}/api/mraa) - -target_link_libraries (hellomraa mraa) -target_link_libraries (i2c_HMC5883L mraa m) -target_link_libraries (cycle-pwm3 mraa) -target_link_libraries (blink-io mraa) -target_link_libraries (analogin_a0 mraa) -target_link_libraries (isr_pin6 mraa) -target_link_libraries (gpio_read6 mraa) -target_link_libraries (spi_mcp4261 mraa) -target_link_libraries (mmap-io2 mraa) -target_link_libraries (blink_onboard mraa) -target_link_libraries (uart mraa) -target_link_libraries (spi_max7219 mraa) -if (NOT ANDROID_TOOLCHAIN) - target_link_libraries (iio_driver mraa) -endif () -if (ONEWIRE) - add_executable (uart_ow uart_ow.c) - target_link_libraries (uart_ow mraa) -endif () - -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 () +add_subdirectory (c) +add_subdirectory (platform) if (NOT ANDROID_TOOLCHAIN) add_subdirectory (c++) diff --git a/examples/analogin_a0.c b/examples/analogin_a0.c deleted file mode 100644 index 48595c2..0000000 --- a/examples/analogin_a0.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Author: Nandkishor Sonar - * Contributors: Alex Tereschenko - * 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 -#include - -#include "mraa/aio.h" - -int running = 0; - -void -sig_handler(int signo) -{ - if (signo == SIGINT) { - printf("closing down nicely\n"); - running = -1; - } -} - -//! [Interesting] -int -main() -{ - mraa_aio_context adc_a0; - uint16_t adc_value = 0; - float adc_value_float = 0.0; - mraa_result_t r = MRAA_SUCCESS; - - adc_a0 = mraa_aio_init(0); - if (adc_a0 == NULL) { - return 1; - } - - signal(SIGINT, sig_handler); - - while (running == 0) { - adc_value = mraa_aio_read(adc_a0); - adc_value_float = mraa_aio_read_float(adc_a0); - 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); - sleep(1); - } - - r = mraa_aio_close(adc_a0); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - - return r; -} -//! [Interesting] diff --git a/examples/blink-io.c b/examples/blink-io.c deleted file mode 100644 index 212c592..0000000 --- a/examples/blink-io.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Author: Brendan Le Foll - * Copyright (c) 2014 Intel Corporation. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#include -#include -#include -#include -#include - -#include "mraa.h" - -#define DEFAULT_IOPIN 8 - -int running = 0; -static int iopin; - -void -sig_handler(int signo) -{ - if (signo == SIGINT) { - printf("closing IO%d nicely\n", iopin); - running = -1; - } -} - -int -main(int argc, char** argv) -{ - mraa_result_t r = MRAA_SUCCESS; - iopin = DEFAULT_IOPIN; - - if (argc < 2) { - printf("Provide an int arg if you want to flash on something other than %d\n", DEFAULT_IOPIN); - } else { - iopin = strtol(argv[1], NULL, 10); - } - - mraa_init(); - fprintf(stdout, "MRAA Version: %s\nStarting Blinking on IO%d\n", mraa_get_version(), iopin); - - mraa_gpio_context gpio; - gpio = mraa_gpio_init(iopin); - if (gpio == NULL) { - fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", iopin); - exit(1); - } - printf("Initialised pin%d\n", iopin); - - // set direction to OUT - r = mraa_gpio_dir(gpio, MRAA_GPIO_OUT); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - - signal(SIGINT, sig_handler); - - while (running == 0) { - r = mraa_gpio_write(gpio, 0); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } else { - printf("off\n"); - } - - sleep(1); - - r = mraa_gpio_write(gpio, 1); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } else { - printf("on\n"); - } - - sleep(1); - } - - r = mraa_gpio_close(gpio); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - - return r; -} diff --git a/examples/blink_onboard.c b/examples/blink_onboard.c deleted file mode 100644 index e4c7efa..0000000 --- a/examples/blink_onboard.c +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Author: Brendan Le Foll - * Contributors: Alex Tereschenko - * 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 -#include -#include -#include - -#include "mraa/gpio.h" - -int running = 0; - -void -sig_handler(int signo) -{ - if (signo == SIGINT) { - printf("closing down nicely\n"); - running = -1; - } -} - -int -main(int argc, char** argv) -{ - mraa_platform_t platform = mraa_get_platform_type(); - mraa_gpio_context gpio, gpio_in = NULL; - const char* board_name = mraa_get_platform_name(); - int ledstate = 0; - mraa_result_t r = MRAA_SUCCESS; - - switch (platform) { - case MRAA_INTEL_GALILEO_GEN1: - gpio = mraa_gpio_init_raw(3); - break; - case MRAA_INTEL_MINNOWBOARD_MAX: - // there is no onboard LED that we can flash on the minnowboard max - // but on the calamari lure pin 21 is an LED. If you don't have the - // lure put an LED on pin 21 - gpio = mraa_gpio_init(21); - break; - case MRAA_INTEL_JOULE_EXPANSION: - gpio = mraa_gpio_init(101); - break; - default: - gpio = mraa_gpio_init(13); - } - - fprintf(stdout, "Welcome to libmraa\n Version: %s\n Running on %s (Ctrl+C to exit)\n", mraa_get_version(), board_name); - - - if (gpio == NULL) { - fprintf(stdout, "Could not initilaize gpio\n"); - return 1; - } - - // on platforms with physical button use gpio_in - if (platform == MRAA_INTEL_MINNOWBOARD_MAX) { - gpio_in = mraa_gpio_init(14); - if (gpio_in != NULL) { - mraa_gpio_dir(gpio_in, MRAA_GPIO_IN); - // S1 on minnowboardmax's calamari lure maps to pin 14, SW1 != S1 - fprintf(stdout, "Press and hold S1 to stop, Press SW1 to shutdown!\n"); - } - } - - mraa_gpio_dir(gpio, MRAA_GPIO_OUT); - - signal(SIGINT, sig_handler); - - while (running == 0) { - if (gpio_in != NULL && mraa_gpio_read(gpio_in) == 0) { - mraa_gpio_close(gpio_in); - return 0; - } - ledstate = !ledstate; - mraa_gpio_write(gpio, !ledstate); - sleep(1); - } - - r = mraa_gpio_close(gpio); - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - - return r; -} diff --git a/examples/c++/I2c-compass.cpp b/examples/c++/I2c-compass.cpp index 068f7b0..0acfede 100644 --- a/examples/c++/I2c-compass.cpp +++ b/examples/c++/I2c-compass.cpp @@ -23,11 +23,11 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include -#include "mraa.hpp" #include "math.h" +#include "mraa.hpp" #define MAX_BUFFER_LENGTH 6 #define HMC5883L_I2C_ADDR 0x1E diff --git a/examples/c++/Iio-dummy.cpp b/examples/c++/Iio-dummy.cpp index 118531b..b670e04 100644 --- a/examples/c++/Iio-dummy.cpp +++ b/examples/c++/Iio-dummy.cpp @@ -22,63 +22,65 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include +#include "mraa/iio.hpp" +#include #include #include -#include -#include "mraa/iio.hpp" +#include #define EXPECT_FAILURE 0 #define EXPECT_SUCCESS 1 -#define IIO_TRY(func) \ -{ \ - bool success = true; \ - try { \ - iio_device->func; \ - } catch (std::exception& e) { \ - success = false; \ - } \ - log_result(#func, "", true, success); \ -} +#define IIO_TRY(func) \ + { \ + bool success = true; \ + try { \ + iio_device->func; \ + } catch (std::exception & e) { \ + success = false; \ + } \ + log_result(#func, "", true, success); \ + } // Macro to run IIO method on attribute and log output -#define IIO_RUN(func, attr, value, expect) \ -{ \ - std::string attr_name = attr; \ - bool success = true; \ - try { \ - iio_device->func(attr_name, value); \ - } catch (std::exception& e) { \ - success = false; \ - } \ - log_result(#func, attr_name, expect, success); \ -} +#define IIO_RUN(func, attr, value, expect) \ + { \ + std::string attr_name = attr; \ + bool success = true; \ + try { \ + iio_device->func(attr_name, value); \ + } catch (std::exception & e) { \ + success = false; \ + } \ + log_result(#func, attr_name, expect, success); \ + } // Macro to run IIO method on attribute and check for expected result and log output -#define IIO_TEST(func, attr, value, expect) \ -{ \ - std::string attr_name = attr; \ - bool success = false; \ - try { \ - success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \ - } catch (std::exception& e) { \ - success = false; \ - } \ - log_result(#func, attr_name, expect, success); \ -} +#define IIO_TEST(func, attr, value, expect) \ + { \ + std::string attr_name = attr; \ + bool success = false; \ + try { \ + success = fabs(iio_device->func(attr_name) - value) < FLT_EPSILON; \ + } catch (std::exception & e) { \ + success = false; \ + } \ + log_result(#func, attr_name, expect, success); \ + } mraa::Iio* iio_device; int eventCount = 0; -// Log result of test. Note a "fail" (i.e. success is false) will be displayed as a pass if a fail was expected -void log_result(std::string test_name, std::string attr_name, bool expect_success, bool success) +// Log result of test. Note a "fail" (i.e. success is false) will be displayed as a pass if a fail +// was expected +void +log_result(std::string test_name, std::string attr_name, bool expect_success, bool success) { std::string result; if (expect_success) - result = success ? "PASS" : "FAIL"; + result = success ? "PASS" : "FAIL"; else - result = success ? "FAIL" : "PASS"; + result = success ? "FAIL" : "PASS"; if (attr_name.empty()) fprintf(stdout, "%s: %s\n", test_name.c_str(), result.c_str()); else @@ -86,9 +88,10 @@ void log_result(std::string test_name, std::string attr_name, bool expect_succes } // Generate iio_dummy driver event by writing a string to a specific sysfs node -bool generate_event() +bool +generate_event() { - FILE *fp = fopen("/sys/bus/iio/devices/iio_evgen/poke_ev0", "w"); + FILE* fp = fopen("/sys/bus/iio/devices/iio_evgen/poke_ev0", "w"); if (fp == NULL) return false; fprintf(fp, "1\n"); @@ -100,9 +103,12 @@ bool generate_event() // IIO event handler that checks for event from dummy_iio_evgen driver class IioTestHandler : public mraa::IioHandler { -protected: - void onIioEvent(const mraa::IioEventData& eventData) { - if (eventData.channelType == IIO_VOLTAGE && eventData.direction == IIO_EV_DIR_RISING && eventData.type == IIO_EV_TYPE_THRESH) + protected: + void + onIioEvent(const mraa::IioEventData& eventData) + { + if (eventData.channelType == IIO_VOLTAGE && eventData.direction == IIO_EV_DIR_RISING && + eventData.type == IIO_EV_TYPE_THRESH) eventCount++; } }; diff --git a/examples/c++/Pwm3-cycle.cpp b/examples/c++/Pwm3-cycle.cpp index 1b26562..df86c4f 100644 --- a/examples/c++/Pwm3-cycle.cpp +++ b/examples/c++/Pwm3-cycle.cpp @@ -22,8 +22,8 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include #include "mraa.hpp" diff --git a/examples/c++/Spi-pot.cpp b/examples/c++/Spi-pot.cpp index a20ce4c..8e25473 100644 --- a/examples/c++/Spi-pot.cpp +++ b/examples/c++/Spi-pot.cpp @@ -22,9 +22,9 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include #include +#include #include "mraa.hpp" diff --git a/examples/c++/Uart-example.cpp b/examples/c++/Uart-example.cpp index 5cdc688..60e5258 100644 --- a/examples/c++/Uart-example.cpp +++ b/examples/c++/Uart-example.cpp @@ -22,9 +22,9 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include #include +#include +#include #include "mraa.hpp" diff --git a/examples/c++/UartOW.cpp b/examples/c++/UartOW.cpp index 623081f..45acd51 100644 --- a/examples/c++/UartOW.cpp +++ b/examples/c++/UartOW.cpp @@ -22,8 +22,8 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include "stdio.h" #include "iostream" +#include "stdio.h" //! [Interesting] #include "uart_ow.hpp" diff --git a/examples/c++/initio.cpp b/examples/c++/initio.cpp index 17526f1..45e542d 100644 --- a/examples/c++/initio.cpp +++ b/examples/c++/initio.cpp @@ -28,12 +28,13 @@ #include "mraa.hpp" -int main() +int +main() { // GPIO-PIN mraa::Gpio* gpio = mraa::initIo("gpio-1"); // GPIO-RAW-RAWPIN - mmraa::Gpio* gpioraw = mraa::initIoinitIo("gpio-raw-131"); + mmraa::Gpio* gpioraw = mraa::initIo initIo("gpio-raw-131"); // AIO-PIN mraa::Aio aio = mraa::initIo("aio-0"); // PWM-PIN diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt new file mode 100644 index 0000000..0821810 --- /dev/null +++ b/examples/c/CMakeLists.txt @@ -0,0 +1,36 @@ +add_executable(aio aio.c) +add_executable(gpio gpio.c) +add_executable(gpio_advanced gpio_advanced.c) +add_executable(hellomraa hellomraa.c) +add_executable(i2c_hmc5883l i2c_hmc5883l.c) +add_executable(i2c_mpu6050 i2c_mpu6050.c) +add_executable(led led.c) +add_executable(pwm pwm.c) +add_executable(spi spi.c) +add_executable(uart uart.c) +add_executable(uart_advanced uart_advanced.c) +if (NOT ANDROID_TOOLCHAIN) + add_executable(iio iio.c) +endif() + +include_directories(${PROJECT_SOURCE_DIR}/api) +include_directories(${PROJECT_SOURCE_DIR}/api/mraa) + +target_link_libraries(aio mraa) +target_link_libraries(gpio mraa) +target_link_libraries(gpio_advanced mraa) +target_link_libraries(hellomraa mraa) +target_link_libraries(i2c_hmc5883l mraa m) +target_link_libraries(i2c_mpu6050 mraa) +target_link_libraries(led mraa) +target_link_libraries(pwm mraa) +target_link_libraries(spi mraa) +target_link_libraries(uart mraa) +target_link_libraries(uart_advanced mraa) +if (NOT ANDROID_TOOLCHAIN) + target_link_libraries(iio mraa) +endif() +if (ONEWIRE) + add_executable (uart_ow uart_ow.c) + target_link_libraries (uart_ow mraa) +endif () diff --git a/examples/c/aio.c b/examples/c/aio.c new file mode 100644 index 0000000..ea9e023 --- /dev/null +++ b/examples/c/aio.c @@ -0,0 +1,99 @@ +/* + * Author: Nandkishor Sonar + * Contributors: Alex Tereschenko + * Contributors: Manivannan Sadhasivam + * 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. + * + * Example usage: Reads ADC A0 value continuously. Press Ctrl+C to exit. + */ + +/* standard headers */ +#include +#include +#include + +/* mraa header */ +#include "mraa/aio.h" + +/* AIO port */ +#define AIO_PORT 0 + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int +main() +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_aio_context aio; + uint16_t value = 0; + float float_value = 0.0; + + signal(SIGINT, sig_handler); + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize AIO */ + aio = mraa_aio_init(AIO_PORT); + if (aio == NULL) { + fprintf(stderr, "Failed to initialize AIO\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + while (flag) { + value = mraa_aio_read(aio); + float_value = mraa_aio_read_float(aio); + fprintf(stdout, "ADC A0 read %X - %d\n", value, value); + fprintf(stdout, "ADC A0 read float - %.5f\n", float_value); + } + + /* close AIO */ + status = mraa_aio_close(aio); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/c/gpio.c b/examples/c/gpio.c new file mode 100644 index 0000000..793a4aa --- /dev/null +++ b/examples/c/gpio.c @@ -0,0 +1,147 @@ +/* + * Author: Thomas Ingleby + * Contributors: Alex Tereschenko + * Contributors: Manivannan Sadhasivam + * 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. + * + * Example usage: Toggles GPIO's 23 and 24 recursively. Press Ctrl+C to exit + * + */ + +/* standard headers */ +#include +#include +#include +#include + +/* mraa header */ +#include "mraa/gpio.h" + +/* 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) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int +main(void) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_gpio_context gpio_1, gpio_2; + + /* install signal handler */ + signal(SIGINT, sig_handler); + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize GPIO pin */ + gpio_1 = mraa_gpio_init(GPIO_PIN_1); + if (gpio_1 == NULL) { + fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_1); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* initialize GPIO pin */ + gpio_2 = mraa_gpio_init(GPIO_PIN_2); + if (gpio_2 == NULL) { + fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN_2); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* set GPIO to output */ + status = mraa_gpio_dir(gpio_1, MRAA_GPIO_OUT); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* set GPIO to output */ + status = mraa_gpio_dir(gpio_2, MRAA_GPIO_OUT); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* toggle both GPIO's */ + while (flag) { + status = mraa_gpio_write(gpio_1, 1); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + status = mraa_gpio_write(gpio_2, 0); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + sleep(1); + + status = mraa_gpio_write(gpio_1, 0); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + status = mraa_gpio_write(gpio_2, 1); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + sleep(1); + } + + /* release gpio's */ + status = mraa_gpio_close(gpio_1); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* close GPIO */ + status = mraa_gpio_close(gpio_2); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/c/gpio_advanced.c b/examples/c/gpio_advanced.c new file mode 100644 index 0000000..3e07a03 --- /dev/null +++ b/examples/c/gpio_advanced.c @@ -0,0 +1,94 @@ +/* + * Author: Brendan Le Foll + * Contributors: Alex Tereschenko + * Contributors: Manivannan Sadhasivam + * 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. + * + * Example usage: Configures GPIO pin for interrupt and waits 30 seconds for the isr to trigger + * + */ + +/* standard headers */ +#include +#include + +/* mraa header */ +#include "mraa/gpio.h" + +#define GPIO_PIN 6 + +void +int_handler(void* args) +{ + fprintf(stdout, "ISR triggered\n"); +} + +int +main() +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_gpio_context gpio; + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize GPIO pin */ + gpio = mraa_gpio_init(GPIO_PIN); + if (gpio == NULL) { + fprintf(stderr, "Failed to initialize GPIO %d\n", GPIO_PIN); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* set GPIO to input */ + status = mraa_gpio_dir(gpio, MRAA_GPIO_IN); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* configure ISR for GPIO */ + status = mraa_gpio_isr(gpio, MRAA_GPIO_EDGE_BOTH, &int_handler, NULL); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* wait 30 seconds isr trigger */ + sleep(30); + + /* close GPIO */ + mraa_gpio_close(gpio); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/hellomraa.c b/examples/c/hellomraa.c similarity index 100% rename from examples/hellomraa.c rename to examples/c/hellomraa.c index f8fd440..cb5a551 100644 --- a/examples/hellomraa.c +++ b/examples/c/hellomraa.c @@ -23,8 +23,8 @@ */ #include -#include #include +#include //! [Interesting] #include "mraa.h" diff --git a/examples/i2c_HMC5883L.c b/examples/c/i2c_hmc5883l.c similarity index 51% rename from examples/i2c_HMC5883L.c rename to examples/c/i2c_hmc5883l.c index cfaac17..bd72964 100644 --- a/examples/i2c_HMC5883L.c +++ b/examples/c/i2c_hmc5883l.c @@ -20,22 +20,33 @@ * 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. + * */ -#include "mraa.h" -#include "math.h" +/* standard headers */ +#include +#include +#include +#include +#include + +/* mraa header */ +#include "mraa/i2c.h" #define MAX_BUFFER_LENGTH 6 #define HMC5883L_I2C_ADDR 0x1E -// configuration registers +/* configuration registers */ #define HMC5883L_CONF_REG_A 0x00 #define HMC5883L_CONF_REG_B 0x01 -// mode register +/* mode register */ #define HMC5883L_MODE_REG 0x02 -// data register +/* data register */ #define HMC5883L_X_MSB_REG 0 #define HMC5883L_X_LSB_REG 1 #define HMC5883L_Z_MSB_REG 2 @@ -44,10 +55,10 @@ #define HMC5883L_Y_LSB_REG 5 #define DATA_REG_SIZE 6 -// status register +/* status register */ #define HMC5883L_STATUS_REG 0x09 -// ID registers +/* ID registers */ #define HMC5883L_ID_A_REG 0x0A #define HMC5883L_ID_B_REG 0x0B #define HMC5883L_ID_C_REG 0x0C @@ -55,7 +66,7 @@ #define HMC5883L_CONT_MODE 0x00 #define HMC5883L_DATA_REG 0x03 -// scales +/* scales */ #define GA_0_88_REG 0x00 << 5 #define GA_1_3_REG 0x01 << 5 #define GA_1_9_REG 0x02 << 5 @@ -65,7 +76,7 @@ #define GA_5_6_REG 0x06 << 5 #define GA_8_1_REG 0x07 << 5 -// digital resolutions +/* digital resolutions */ #define SCALE_0_73_MG 0.73 #define SCALE_0_92_MG 0.92 #define SCALE_1_22_MG 1.22 @@ -75,62 +86,139 @@ #define SCALE_3_03_MG 3.03 #define SCALE_4_35_MG 4.35 -int -main(int argc, char** argv) +#define I2C_BUS 0 + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) { - mraa_init(); + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int +main(void) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_i2c_context i2c; float direction = 0; int16_t x = 0, y = 0, z = 0; uint8_t rx_tx_buf[MAX_BUFFER_LENGTH]; - //! [Interesting] - mraa_i2c_context i2c; - i2c = mraa_i2c_init(0); + /* install signal handler */ + signal(SIGINT, sig_handler); + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + i2c = mraa_i2c_init(I2C_BUS); + if (i2c == NULL) { + fprintf(stderr, "Failed to initialize I2C\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* set slave address */ mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* set device gain */ rx_tx_buf[0] = HMC5883L_CONF_REG_B; rx_tx_buf[1] = GA_1_3_REG; - mraa_i2c_write(i2c, rx_tx_buf, 2); - //! [Interesting] - mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); + status = mraa_i2c_write(i2c, rx_tx_buf, 2); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* set continuous mode */ rx_tx_buf[0] = HMC5883L_MODE_REG; rx_tx_buf[1] = HMC5883L_CONT_MODE; - mraa_i2c_write(i2c, rx_tx_buf, 2); - for (;;) { + status = mraa_i2c_write(i2c, rx_tx_buf, 2); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + while (flag) { #if 0 int i = 0; - //alternative, equivalent method which helps to understand exactly what - //the below does + /* + * alternative, equivalent method which helps to understand exactly what + * the below does + */ mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); for (i = 0; i < DATA_REG_SIZE; i++) { mraa_i2c_read_byte_data(i2c, HMC5883L_DATA_REG+i); } #endif - // first 'select' the register we want to read from - mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); - mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG); + /* select data register to read */ + status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); + if (status != MRAA_SUCCESS) { + goto err_exit; + } - // then we read from that register incrementing with every read the - // chosen register - mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); - // this call behaves very similarly to the Wire receive() call + status = mraa_i2c_write_byte(i2c, HMC5883L_DATA_REG); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* do a incremental read from the chosen address */ + status = mraa_i2c_address(i2c, HMC5883L_I2C_ADDR); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* this call behaves very similarly to the Wire receive() call */ mraa_i2c_read(i2c, rx_tx_buf, DATA_REG_SIZE); x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG]; z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG]; y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Y_LSB_REG]; - // scale and calculate direction + /* scale and calculate direction */ direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG); - // check if the signs are reversed - if (direction < 0) + /* check if the signs are reversed */ + 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); + fprintf(stdout, "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); + fprintf(stdout, "Heading : %f\n", direction * 180 / M_PI); + + sleep(2); } + + /* stop i2c */ + mraa_i2c_stop(i2c); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* stop i2c */ + mraa_i2c_stop(i2c); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; } diff --git a/examples/c/i2c_mpu6050.c b/examples/c/i2c_mpu6050.c new file mode 100644 index 0000000..1905376 --- /dev/null +++ b/examples/c/i2c_mpu6050.c @@ -0,0 +1,170 @@ +/* + * Author: Manivannan Sadhasivam + * 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: Outputs Accelerometer and Gyroscope value from MPU6050 recursively. + * Press Ctrl+C to exit. + * + */ + +/* standard headers */ +#include +#include +#include +#include + +/* mraa header */ +#include "mraa/i2c.h" + +#define I2C_BUS 0 + +/* register definitions */ +#define MPU6050_ADDR 0x68 +#define MPU6050_REG_PWR_MGMT_1 0x6b +#define MPU6050_REG_RAW_ACCEL_X 0x3b +#define MPU6050_REG_RAW_ACCEL_Y 0x3d +#define MPU6050_REG_RAW_ACCEL_Z 0x3f +#define MPU6050_REG_RAW_GYRO_X 0x43 +#define MPU6050_REG_RAW_GYRO_Y 0x45 +#define MPU6050_REG_RAW_GYRO_Z 0x47 + +/* bit definitions */ +#define MPU6050_RESET 0x80 +#define MPU6050_SLEEP (1 << 6) +#define MPU6050_PLL_GYRO_X (1 << 1) + +/* accelerometer scale factor for (+/-)2g */ +#define MPU6050_ACCEL_SCALE 16384.0 + +/* gyroscope scale factor for (+/-)250/s */ +#define MPU6050_GYRO_SCALE 131.0 + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int16_t +i2c_read_word(mraa_i2c_context dev, uint8_t command) +{ + return be16toh(mraa_i2c_read_word_data(dev, command)); +} + +int +main(void) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_i2c_context i2c; + uint8_t data; + int16_t accel_data[3]; + int16_t gyro_data[3]; + int ret; + + /* install signal handler */ + signal(SIGINT, sig_handler); + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize I2C bus */ + i2c = mraa_i2c_init(I2C_BUS); + if (i2c == NULL) { + fprintf(stderr, "Failed to initialize I2C\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* set slave address */ + status = mraa_i2c_address(i2c, MPU6050_ADDR); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* reset the sensor */ + status = mraa_i2c_write_byte_data(i2c, MPU6050_RESET, MPU6050_REG_PWR_MGMT_1); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + sleep(5); + + /* configure power management register */ + ret = mraa_i2c_read_byte_data(i2c, MPU6050_REG_PWR_MGMT_1); + if (ret == -1) { + return EXIT_FAILURE; + } + + data = ret; + data |= MPU6050_PLL_GYRO_X; + data &= ~(MPU6050_SLEEP); + + status = mraa_i2c_write_byte_data(i2c, data, MPU6050_REG_PWR_MGMT_1); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + sleep(5); + + while (flag) { + /* read raw accel data */ + accel_data[0] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_X) / MPU6050_ACCEL_SCALE; + accel_data[1] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_Y) / MPU6050_ACCEL_SCALE; + accel_data[2] = i2c_read_word(i2c, MPU6050_REG_RAW_ACCEL_Z) / MPU6050_ACCEL_SCALE; + + /* read raw gyro data */ + gyro_data[0] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_X) / MPU6050_GYRO_SCALE; + gyro_data[1] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_Y) / MPU6050_GYRO_SCALE; + gyro_data[2] = i2c_read_word(i2c, MPU6050_REG_RAW_GYRO_Z) / MPU6050_GYRO_SCALE; + + fprintf(stdout, "accel: x:%d y:%d z:%d\n", accel_data[0], accel_data[1], accel_data[2]); + fprintf(stdout, "gyro: x:%d y:%d z:%d\n\n", gyro_data[0], gyro_data[1], gyro_data[2]); + + sleep(2); + } + + /* stop i2c */ + mraa_i2c_stop(i2c); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* stop i2c */ + mraa_i2c_stop(i2c); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/c/iio.c b/examples/c/iio.c new file mode 100644 index 0000000..6b7be35 --- /dev/null +++ b/examples/c/iio.c @@ -0,0 +1,172 @@ +/* + * Author: Brendan Le Foll + * 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. + * + * Example usage: Sets accelerometer scale, enables the x axis measurement and sets the trigger. + * Waits 100 seconds for the buffer trigger to happen. + */ + +/* standard headers */ +#include +#include + +/* mraa header */ +#include "mraa/iio.h" + +/* IIO device */ +#define IIO_DEV 0 + +static void +printword(uint16_t input, mraa_iio_channel* chan) +{ + int16_t res; + + if (!chan->lendian) { + input = be16toh(input); + } else { + input = le16toh(input); + } + + input >>= chan->shift; + input &= chan->mask; + if (chan->signedd) { + res = (int16_t)(input << (16 - chan->bits_used)) >> (16 - chan->bits_used); + } else { + res = input; + } + fprintf(stdout, " value = %05f\n", (float) res); +} + +void +interrupt(char* data, void* args) +{ + mraa_iio_context thisdevice = (mraa_iio_context) args; + mraa_iio_channel* channels = mraa_iio_get_channels(thisdevice); + int i = 0; + + for (; i < mraa_iio_get_channel_count(thisdevice); i++) { + if (channels[i].enabled) { + fprintf(stdout, "channel %d - bytes %d\n", channels[i].index, channels[i].bytes); + switch (channels[i].bytes) { + case 2: + printword(*(uint16_t*) (data + channels[i].location), &channels[i]); + } + } + } +} + +void +event_interrupt(struct iio_event_data* data) +{ + int chan_type; + int modifier; + int type; + int direction; + int channel; + int channel2; + int different; + mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different); + + fprintf(stdout, + "event time %lld id %lld extracted chan_type %d modifier %d type %d direction %d " + "channel %d channel2 %d different %d\n", + data->timestamp, data->id, chan_type, modifier, type, direction, channel, channel2, different); +} + +int +main(void) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_iio_context iio; + float iio_float; + int iio_int; + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize iio */ + iio = mraa_iio_init(IIO_DEV); + if (iio == NULL) { + fprintf(stderr, "Failed to initialize IIO device\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + status = mraa_iio_write_float(iio, "in_accel_scale", 0.019163); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "Failed to write scale to IIO device\n"); + goto err_exit; + } + + status = mraa_iio_read_float(iio, "in_accel_scale", &iio_float); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "Failed to read scale from IIO device\n"); + goto err_exit; + } + + fprintf(stdout, "IIO scale: %f\n", iio_float); + + status = mraa_iio_write_int(iio, "scan_elements/in_accel_x_en", 1); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "Failed to enable axis of the IIO device\n"); + goto err_exit; + } + + status = mraa_iio_read_int(iio, "scan_elements/in_accel_x_en", &iio_int); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "Failed to read enable status from IIO device\n"); + goto err_exit; + } + + fprintf(stdout, "IIO enable status: %d\n", iio_int); + + status = mraa_iio_trigger_buffer(iio, interrupt, (void*) iio); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "Failed to set IIO buffer trigger\n"); + goto err_exit; + } + + /* wait for the trigger */ + fprintf(stdout, "Waiting for the trigger...\n"); + sleep(100); + + /* close IIO device */ + mraa_iio_close(iio); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* close IIO device */ + mraa_iio_close(iio); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/c/led.c b/examples/c/led.c new file mode 100644 index 0000000..ee2c993 --- /dev/null +++ b/examples/c/led.c @@ -0,0 +1,108 @@ +/* + * Author: Manivannan Sadhasivam + * 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 +#include +#include + +/* mraa header */ +#include "mraa/led.h" + +/* LED name */ +#define USER_LED "user1" + +/* trigger type */ +#define LED_TRIGGER "heartbeat" + +int +main(void) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_led_context led; + int val; + + /* initialize mraa for the platform (not needed most of the time) */ + mraa_init(); + + //! [Interesting] + /* initialize LED */ + led = mraa_led_init(USER_LED); + if (led == NULL) { + fprintf(stderr, "Failed to initialize LED\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* read maximum brightness */ + val = mraa_led_read_max_brightness(led); + fprintf(stdout, "maximum brightness value for LED is: %d\n", val); + if (val >= 1) { + val = 0; + } else { + /* never reached mostly */ + val = 1; + } + + /* turn LED on/off depending on max_brightness value */ + status = mraa_led_set_brightness(led, val); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "unable to set LED brightness\n"); + goto err_exit; + } + + /* sleep for 5 seconds */ + sleep(5); + + /* set LED trigger to heartbeat */ + status = mraa_led_set_trigger(led, LED_TRIGGER); + if (status != MRAA_SUCCESS) { + fprintf(stderr, "unable to set LED trigger to: heartbeat\n"); + goto err_exit; + } + + fprintf(stdout, "LED trigger set to: heartbeat\n"); + + /* close LED */ + mraa_led_close(led); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/c/pwm.c b/examples/c/pwm.c new file mode 100644 index 0000000..7dd305c --- /dev/null +++ b/examples/c/pwm.c @@ -0,0 +1,124 @@ +/* + * Author: Thomas Ingleby + * Contributors: Manivannan Sadhasivam + * 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. + * + * Example usage: Generates PWM signal of period 200us with variable dutycyle + * repeately. Press Ctrl+C to exit + */ + +/* standard headers */ +#include +#include +#include + +/* mraa header */ +#include "mraa/pwm.h" + +/* PWM declaration */ +#define PWM 3 + +/* PWM period in us */ +#define PWM_FREQ 200 + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int +main(void) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_pwm_context pwm; + float value = 0.0f; + float output; + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + pwm = mraa_pwm_init(PWM); + if (pwm == NULL) { + fprintf(stderr, "Failed to initialize PWM\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* set PWM period */ + status = mraa_pwm_period_us(pwm, PWM_FREQ); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* enable PWM */ + status = mraa_pwm_enable(pwm, 1); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + while (flag) { + value = value + 0.01f; + + /* write PWM duty cyle */ + status = mraa_pwm_write(pwm, value); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + usleep(50000); + + if (value >= 1.0f) { + value = 0.0f; + } + + /* read PWM duty cyle */ + output = mraa_pwm_read(pwm); + fprintf(stdout, "PWM value is %f\n", output); + } + + /* close PWM */ + mraa_pwm_close(pwm); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* close PWM */ + mraa_pwm_close(pwm); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/c/spi.c b/examples/c/spi.c new file mode 100644 index 0000000..47e1a82 --- /dev/null +++ b/examples/c/spi.c @@ -0,0 +1,154 @@ +/* + * Author: Michael Ring + * Author: Thomas Ingleby + * Contributors: Manivannan Sadhasivam + * 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. + * + * Example usage: Display set of patterns on MAX7219 repeately. + * Press Ctrl+C to exit + */ + +/* standard headers */ +#include +#include +#include + +/* mraa header */ +#include "mraa/spi.h" + +/* SPI declaration */ +#define SPI_BUS 0 + +/* SPI frequency in Hz */ +#define SPI_FREQ 400000 + +uint16_t pat[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 }; +uint16_t pat_inv[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa }; +uint16_t pat_clear[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 }; + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int +main(int argc, char** argv) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_spi_context spi; + int i, j; + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize SPI bus */ + spi = mraa_spi_init(SPI_BUS); + if (spi == NULL) { + fprintf(stderr, "Failed to initialize SPI\n"); + mraa_deinit(); + return EXIT_FAILURE; + } + + /* set SPI frequency */ + status = mraa_spi_frequency(spi, SPI_FREQ); + if (status != MRAA_SUCCESS) + goto err_exit; + + /* set big endian mode */ + status = mraa_spi_lsbmode(spi, 0); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + /* MAX7219/21 chip needs the data in word size */ + status = mraa_spi_bit_per_word(spi, 16); + if (status != MRAA_SUCCESS) { + fprintf(stdout, "Failed to set SPI Device to 16Bit mode\n"); + goto err_exit; + } + + /* do not decode bits */ + mraa_spi_write_word(spi, 0x0900); + + /* brightness of LEDs */ + mraa_spi_write_word(spi, 0x0a05); + + /* show all scan lines */ + mraa_spi_write_word(spi, 0x0b07); + + /* set display on */ + mraa_spi_write_word(spi, 0x0c01); + + /* testmode off */ + mraa_spi_write_word(spi, 0x0f00); + + while (flag) { + /* set display pattern */ + mraa_spi_write_buf_word(spi, pat, 16); + + sleep(2); + + /* set inverted display pattern */ + mraa_spi_write_buf_word(spi, pat_inv, 16); + + sleep(2); + + /* clear the LED's */ + mraa_spi_write_buf_word(spi, pat_clear, 16); + + /* cycle through all LED's */ + for (i = 1; i <= 8; i++) { + for (j = 0; j < 8; j++) { + mraa_spi_write_word(spi, (i << 8) + (1 << j)); + sleep(1); + } + mraa_spi_write_word(spi, i << 8); + } + } + + /* stop spi */ + mraa_spi_stop(spi); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* stop spi */ + mraa_spi_stop(spi); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/uart.c b/examples/c/uart.c similarity index 55% rename from examples/uart.c rename to examples/c/uart.c index 51268aa..3b9ae95 100644 --- a/examples/uart.c +++ b/examples/c/uart.c @@ -1,6 +1,7 @@ /* * Author: Thomas Ingleby * Author: Brendan Le Foll + * Contributors: Manivannan Sadhasivam * Copyright (c) 2014, 2015 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining @@ -21,30 +22,72 @@ * 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 "stdio.h" -//! [Interesting] -#include "mraa.h" +/* standard headers */ +#include +#include +#include +#include + +/* mraa header */ +#include "mraa/uart.h" + +#define UART 0 + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} int main(int argc, char** argv) { mraa_uart_context uart; - uart = mraa_uart_init(0); + char buffer[] = "Hello Mraa!"; + /* install signal handler */ + signal(SIGINT, sig_handler); + + /* initialize mraa for the platform (not needed most of the times) */ + mraa_init(); + + //! [Interesting] + /* initialize UART */ + uart = mraa_uart_init(UART); if (uart == NULL) { - fprintf(stderr, "UART failed to setup\n"); - return EXIT_FAILURE; + fprintf(stderr, "Failed to initialize UART\n"); + goto err_exit; } - char buffer[] = "Hello Mraa!"; - mraa_uart_write(uart, buffer, sizeof(buffer)); + while (flag) { + /* send data through UART */ + mraa_uart_write(uart, buffer, sizeof(buffer)); + sleep(1); + } + + /* stop UART */ mraa_uart_stop(uart); + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the times) */ mraa_deinit(); return EXIT_SUCCESS; + +err_exit: + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; } -//! [Interesting] diff --git a/examples/c/uart_advanced.c b/examples/c/uart_advanced.c new file mode 100644 index 0000000..da88a0e --- /dev/null +++ b/examples/c/uart_advanced.c @@ -0,0 +1,115 @@ +/* + * Author: Manivannan Sadhasivam + * 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: Prints "Hello Mraa!" recursively. Press Ctrl+C to exit + * + */ + +/* standard headers */ +#include +#include +#include +#include + +/* mraa header */ +#include "mraa/uart.h" + +#ifndef FALSE +#define FALSE 0 +#define TRUE (!FALSE) +#endif + +/* UART port name */ +const char* dev_path = "/dev/ttyS0"; + +volatile sig_atomic_t flag = 1; + +void +sig_handler(int signum) +{ + if (signum == SIGINT) { + fprintf(stdout, "Exiting...\n"); + flag = 0; + } +} + +int +main(int argc, char** argv) +{ + mraa_result_t status = MRAA_SUCCESS; + mraa_uart_context uart; + char buffer[] = "Hello Mraa!"; + + int baudrate = 9600, stopbits = 1, databits = 8; + mraa_uart_parity_t parity = MRAA_UART_PARITY_NONE; + unsigned int ctsrts = FALSE, xonxoff = FALSE; + const char* name = NULL; + + /* install signal handler */ + signal(SIGINT, sig_handler); + + /* initialize mraa for the platform (not needed most of the time) */ + mraa_init(); + + //! [Interesting] + /* initialize uart */ + uart = mraa_uart_init_raw(dev_path); + if (uart == NULL) { + fprintf(stderr, "Failed to initialize UART\n"); + return EXIT_FAILURE; + } + + /* set serial port parameters */ + status = + mraa_uart_settings(-1, &dev_path, &name, &baudrate, &databits, &stopbits, &parity, &ctsrts, &xonxoff); + if (status != MRAA_SUCCESS) { + goto err_exit; + } + + while (flag) { + /* send data through uart */ + mraa_uart_write(uart, buffer, sizeof(buffer)); + + sleep(1); + } + + /* stop uart */ + mraa_uart_stop(uart); + + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the time) */ + mraa_deinit(); + + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* stop uart */ + mraa_uart_stop(uart); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; +} diff --git a/examples/uart_ow.c b/examples/c/uart_ow.c similarity index 54% rename from examples/uart_ow.c rename to examples/c/uart_ow.c index 63ed410..3a0e7d5 100644 --- a/examples/uart_ow.c +++ b/examples/c/uart_ow.c @@ -20,73 +20,86 @@ * 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. + * */ +/* standard headers */ #include "stdio.h" -//! [Interesting] + +/* mraa header */ #include "mraa.h" int -main(int argc, char** argv) +main(void) { - mraa_uart_ow_context uart; - uart = mraa_uart_ow_init(0); + mraa_uart_ow_context uart_ow; + mraa_result_t status; + uint8_t id[MRAA_UART_OW_ROMCODE_SIZE]; + uint8_t count = 0; - if (!uart) { - printf("mraa_uart_ow_init() failed\n"); - return 1; + //! [Interesting] + uart_ow = mraa_uart_ow_init(0); + if (uart_ow == NULL) { + fprintf(stderr, "Failed to initialize UART OW\n"); + mraa_deinit(); + return EXIT_FAILURE; } // Reset the ow bus and see if anything is present - mraa_result_t rv; - - if ((rv = mraa_uart_ow_reset(uart)) == MRAA_SUCCESS) { - printf("Reset succeeded, device(s) detected!\n"); + status = mraa_uart_ow_reset(uart_ow); + if (status == MRAA_SUCCESS) { + fprintf(stdout, "Reset succeeded, device(s) detected!\n"); } else { - printf("Reset failed, returned %d. No devices on bus?\n", rv); - return 1; + goto err_exit; } - printf("Looking for devices...\n"); - uint8_t id[MRAA_UART_OW_ROMCODE_SIZE]; + fprintf(stdout, "Looking for devices...\n"); - /* we are essentially doing a binary tree search through the 64 + /* + * we are essentially doing a binary tree search through the 64 * bit address space. id is modified during this search, and will * be set to the valid rom code for each device found. + * + * start the search from scratch */ - - uint8_t count = 0; - - // start the search from scratch - mraa_result_t result = mraa_uart_ow_rom_search(uart, 1, id); - if (result == MRAA_ERROR_UART_OW_NO_DEVICES) { - printf("No devices detected.\n"); - return 1; + status = mraa_uart_ow_rom_search(uart_ow, 1, id); + if (status != MRAA_SUCCESS) { + goto err_exit; } - if (result == MRAA_ERROR_UART_OW_DATA_ERROR) { - printf("Bus/Data error.\n"); - return 1; - } - - while (result == MRAA_SUCCESS) { + while (status == MRAA_SUCCESS) { /* The first byte (id[0]]) is the device type (family) code. * The last byte (id[7]) is the rom code CRC value. The * intervening bytes (id[1]-id[6]) 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, id[0], - id[6], id[5], id[4], id[3], id[2], id[1], id[7]); + fprintf(stdout, "Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count, + id[0], id[6], id[5], id[4], id[3], id[2], id[1], id[7]); count++; // continue the search with start argument set to 0 - result = mraa_uart_ow_rom_search(uart, 0, id); + status = mraa_uart_ow_rom_search(uart_ow, 0, id); } - printf("Exiting...\n"); + /* stop uart_ow */ + mraa_uart_ow_stop(uart_ow); - mraa_uart_ow_stop(uart); + //! [Interesting] + /* deinitialize mraa for the platform (not needed most of the time) */ + mraa_deinit(); - return 0; + return EXIT_SUCCESS; + +err_exit: + mraa_result_print(status); + + /* stop uart_ow */ + mraa_uart_ow_stop(uart_ow); + + /* deinitialize mraa for the platform (not needed most of the times) */ + mraa_deinit(); + + return EXIT_FAILURE; } -//! [Interesting] diff --git a/examples/cycle-pwm3.c b/examples/cycle-pwm3.c deleted file mode 100644 index 5924e23..0000000 --- a/examples/cycle-pwm3.c +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Author: Thomas Ingleby - * 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 - -#include "mraa.h" - -int -main() -{ - mraa_init(); - //! [Interesting] - mraa_pwm_context pwm; - pwm = mraa_pwm_init(3); - if (pwm == NULL) { - return 1; - } - mraa_pwm_period_us(pwm, 200); - mraa_pwm_enable(pwm, 1); - - float value = 0.0f; - - while (1) { - value = value + 0.01f; - mraa_pwm_write(pwm, value); - usleep(50000); - if (value >= 1.0f) { - value = 0.0f; - } - float output = mraa_pwm_read(pwm); - printf("PWM value is %f\n", output); - } - //! [Interesting] - return 0; -} diff --git a/examples/gpio_read6.c b/examples/gpio_read6.c deleted file mode 100644 index f750ad7..0000000 --- a/examples/gpio_read6.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Author: Thomas Ingleby - * Contributors: Alex Tereschenko - * 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 "stdio.h" -#include "unistd.h" -#include - -#include "mraa.h" - -int running = 0; - -void -sig_handler(int signo) -{ - if (signo == SIGINT) { - printf("closing down nicely\n"); - running = -1; - } -} - -int -main(int argc, char** argv) -{ - mraa_result_t r = MRAA_SUCCESS; - - mraa_init(); - fprintf(stdout, "MRAA Version: %s\nStarting Read on IO6 (Ctrl+C to exit)\n", mraa_get_version()); - - //! [Interesting] - mraa_gpio_context gpio; - - gpio = mraa_gpio_init(6); - - mraa_gpio_dir(gpio, MRAA_GPIO_IN); - - signal(SIGINT, sig_handler); - - while (running == 0) { - fprintf(stdout, "Gpio is %d\n", mraa_gpio_read(gpio)); - sleep(1); - } - - r = mraa_gpio_close(gpio); - //! [Interesting] - if (r != MRAA_SUCCESS) { - mraa_result_print(r); - } - - return r; -} diff --git a/examples/iio_driver.c b/examples/iio_driver.c deleted file mode 100644 index e48c32e..0000000 --- a/examples/iio_driver.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Author: Brendan Le Foll - * 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 -#include "mraa/iio.h" - -static void -printword(uint16_t input, mraa_iio_channel* chan) -{ - int16_t res; - - if (!chan->lendian) { - input = be16toh(input); - } else { - input = le16toh(input); - } - - input >>= chan->shift; - input &= chan->mask; - if (chan->signedd) { - res = (int16_t)(input << (16 - chan->bits_used)) >> (16 - chan->bits_used); - } else { - res = input; - } - printf(" value = %05f\n", (float) res); -} - -void -interrupt(char* data, void* args) -{ - mraa_iio_context thisdevice = (mraa_iio_context)args; - - mraa_iio_channel* channels = mraa_iio_get_channels(thisdevice); - int i = 0; - - for (; i < mraa_iio_get_channel_count(thisdevice); i++) { - if (channels[i].enabled) { - printf("channel %d - bytes %d\n", channels[i].index, channels[i].bytes); - switch (channels[i].bytes) { - case 2: - printword(*(uint16_t*) (data + channels[i].location), &channels[i]); - } - } - } -} - -void -event_interrupt(struct iio_event_data* data) -{ - int chan_type; - int modifier; - int type; - int direction; - int channel; - int channel2; - int different; - mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different); - - printf("event time %lld id %lld extracted chan_type %d modifier %d type %d direction %d " - "channel %d channel2 %d different %d\n", - data->timestamp, data->id, chan_type, modifier, type, direction, channel, channel2, different); -} - -int -main() -{ - //! [Interesting] - mraa_iio_context iio_device0 = mraa_iio_init(0); - - if (iio_device0 == NULL) { - return EXIT_FAILURE; - } - - float iio_float; - int iio_int; - mraa_result_t ret; - - ret = mraa_iio_write_float(iio_device0, "in_accel_scale", 0.019163); - if (ret == MRAA_SUCCESS) { - fprintf(stdout, "IIO write success\n"); - } - - ret = mraa_iio_read_float(iio_device0, "in_accel_scale", &iio_float); - if (ret == MRAA_SUCCESS) { - fprintf(stdout, "IIO read %f\n", iio_float); - } - - ret = mraa_iio_write_int(iio_device0, "scan_elements/in_accel_x_en", 1); - if (ret == MRAA_SUCCESS) { - fprintf(stdout, "IIO write success\n"); - } - - ret = mraa_iio_read_int(iio_device0, "scan_elements/in_accel_x_en", &iio_int); - if (ret == MRAA_SUCCESS) { - fprintf(stdout, "IIO read %d\n", iio_int); - } - - if (mraa_iio_trigger_buffer(iio_device0, interrupt, (void*)iio_device0) == MRAA_SUCCESS) { - sleep(100); - return EXIT_SUCCESS; - } - - /* - struct iio_event_data event; - iio_device6 = mraa_iio_init(6); - if (iio_device6 == NULL) { - return EXIT_FAILURE; - } - mraa_iio_write_int(iio_device6, "events/in_proximity2_thresh_either_en", 1); - - - // Blocking until event fired - if (mraa_iio_event_poll(iio_device6, &event) == MRAA_SUCCESS) { - event_interrupt(&event); //just to show data - } - - if (mraa_iio_event_setup_callback(iio_device6, event_interrupt, NULL) == MRAA_SUCCESS) { - sleep(100); - return EXIT_SUCCESS; - }*/ - - //! [Interesting] - return EXIT_FAILURE; -} diff --git a/examples/isr_pin6.c b/examples/isr_pin6.c deleted file mode 100644 index fa182f1..0000000 --- a/examples/isr_pin6.c +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Author: Brendan Le Foll - * Contributors: Alex Tereschenko - * 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 - -#include "mraa.h" - -static volatile int counter = 0; -static volatile int oldcounter = 0; - -void -interrupt(void* args) -{ - ++counter; -} - -int -main() -{ - mraa_init(); - mraa_gpio_context x; - - x = mraa_gpio_init(6); - if (x == NULL) { - return 1; - } - - mraa_gpio_dir(x, MRAA_GPIO_IN); - - mraa_gpio_edge_t edge = MRAA_GPIO_EDGE_BOTH; - - mraa_gpio_isr(x, edge, &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); - } - - mraa_gpio_close(x); - - return MRAA_SUCCESS; -} diff --git a/examples/mmap-io2.c b/examples/mmap-io2.c deleted file mode 100644 index 53c2432..0000000 --- a/examples/mmap-io2.c +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Author: Thomas Ingleby - * 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 "stdio.h" -#include - -#include "mraa.h" - -int -main(int argc, char** argv) -{ - //! [Interesting] - fprintf(stdout, "hello mraa\n Version: %s\n", mraa_get_version()); - mraa_gpio_context gpio; - gpio = mraa_gpio_init(2); - - mraa_gpio_use_mmaped(gpio, 1); - - for (;;) { - mraa_gpio_write(gpio, 1); - usleep(50000); - mraa_gpio_write(gpio, 0); - usleep(50000); - } - //! [Interesting] -} diff --git a/examples/platform/CMakeLists.txt b/examples/platform/CMakeLists.txt new file mode 100644 index 0000000..23006e8 --- /dev/null +++ b/examples/platform/CMakeLists.txt @@ -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() diff --git a/examples/firmata_curie_imu.c b/examples/platform/firmata_curie_imu.c similarity index 97% rename from examples/firmata_curie_imu.c rename to examples/platform/firmata_curie_imu.c index d5df3bd..094e11e 100644 --- a/examples/firmata_curie_imu.c +++ b/examples/platform/firmata_curie_imu.c @@ -44,9 +44,9 @@ main() mraa_init(); //! [Interesting] - /** - * This example reads from the FirmataCurieIMU plugin - */ + /** + * 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); diff --git a/examples/helloedison.c b/examples/platform/gpio_edison.c similarity index 100% rename from examples/helloedison.c rename to examples/platform/gpio_edison.c index 1169496..ae41309 100644 --- a/examples/helloedison.c +++ b/examples/platform/gpio_edison.c @@ -22,10 +22,10 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include -#include -#include #include +#include +#include +#include //! [Interesting] #include "mraa.h" diff --git a/examples/i2c_firmata.c b/examples/platform/i2c_firmata.c similarity index 99% rename from examples/i2c_firmata.c rename to examples/platform/i2c_firmata.c index e072ad1..863260b 100644 --- a/examples/i2c_firmata.c +++ b/examples/platform/i2c_firmata.c @@ -60,5 +60,4 @@ main(int argc, char** argv) printf("res is %d\n", res); #endif sleep(10); - } diff --git a/examples/initio.c b/examples/platform/initio.c similarity index 92% rename from examples/initio.c rename to examples/platform/initio.c index da0ca13..7ae71dc 100644 --- a/examples/initio.c +++ b/examples/platform/initio.c @@ -20,15 +20,21 @@ * 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 #include #include +/* mraa header */ #include "mraa.h" -int main() +int +main() { // GPIO-PIN mraa_gpio_context gpio = (mraa_gpio_context) init_io("gpio-1"); @@ -39,7 +45,7 @@ int main() // 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"); + 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 diff --git a/examples/samples.mapping.txt b/examples/samples.mapping.txt index 1f70e40..edae021 100644 --- a/examples/samples.mapping.txt +++ b/examples/samples.mapping.txt @@ -1,13 +1,3 @@ -analogin_a0.c java/AioA0.java -cycle-pwm3.c java/CyclePwm3.java -gpio_read6.c java/GpioRead6.java -helloedison.c java/HelloEdison.java -hellomraa.c java/Example.java -i2c_HMC5883L.c java/I2cCompass.java -mmap-io2.c java/GpioMmapped.java -spi_max7219.c java/SpiMAX7219.java -spi_mcp4261.c java/SpiMCP4261.java -uart.c java/UartExample.java AioA0.cpp java/AioA0.java Blink-IO.cpp java/BlinkIO.java I2c-compass.cpp java/I2cCompass.java diff --git a/examples/spi_max7219.c b/examples/spi_max7219.c deleted file mode 100644 index 276cff9..0000000 --- a/examples/spi_max7219.c +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Author: Michael Ring - * Author: Thomas Ingleby - * 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 "mraa.h" -#include -#include - -int -main(int argc, char** argv) -{ - //! [Interesting] - mraa_spi_context spi; - spi = mraa_spi_init(1); - if (spi == NULL) { - printf("Initialization of spi failed, check syslog for details, exit...\n"); - exit(1); - } - - printf("SPI initialised successfully\n"); - - mraa_spi_frequency(spi, 400000); - mraa_spi_lsbmode(spi, 0); - - // The MAX7219/21 Chip needs the data in word size - if (mraa_spi_bit_per_word(spi, 16) != MRAA_SUCCESS) { - printf("Could not set SPI Device to 16Bit mode, exit...\n"); - exit(1); - }; - - mraa_spi_write_word(spi, 0x0900); // Do not decode bits - mraa_spi_write_word(spi, 0x0a05); // Brightness of LEDs - mraa_spi_write_word(spi, 0x0b07); // Show all Scan Lines - mraa_spi_write_word(spi, 0x0c01); // Display on - mraa_spi_write_word(spi, 0x0f00); // Testmode off - - // Display Pattern on the display - uint16_t dataAA55[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 }; - mraa_spi_write_buf_word(spi, dataAA55, 16); - - sleep(2); - - // Display inverted Pattern - uint16_t data55AA[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa }; - mraa_spi_write_buf_word(spi, data55AA, 16); - - sleep(2); - - // Clear the display - uint16_t data[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 }; - mraa_spi_write_buf_word(spi, data, 16); - - int i; - int j; - // cycle through all LED's - for (i = 1; i <= 8; i++) { - for (j = 0; j < 8; j++) { - mraa_spi_write_word(spi, (i << 8) + (1 << j)); - sleep(1); - } - mraa_spi_write_word(spi, i << 8); - } - - mraa_spi_stop(spi); - - //! [Interesting] - return 0; -} diff --git a/examples/spi_mcp4261.c b/examples/spi_mcp4261.c deleted file mode 100644 index a2efa6c..0000000 --- a/examples/spi_mcp4261.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Author: Thomas Ingleby - * 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 "mraa.h" -#include -#include - -int -main(int argc, char** argv) -{ - mraa_init(); - //! [Interesting] - mraa_spi_context spi; - spi = mraa_spi_init(0); - printf("Hello, SPI initialised\n"); - uint8_t data[] = { 0x00, 100 }; - uint8_t* recv; - while (1) { - int i; - for (i = 90; i < 130; i++) { - data[1] = i; - recv = mraa_spi_write_buf(spi, data, 2); - printf("Writing -%i", i); - printf("RECIVED-%i-%i\n", recv[0], recv[1]); - usleep(100000); - } - for (i = 130; i > 90; i--) { - data[1] = i; - recv = mraa_spi_write_buf(spi, data, 2); - printf("Writing -%i", i); - printf("RECIVED-%i-%i\n", recv[0], recv[1]); - usleep(100000); - } - } - //! [Interesting] -}