From 1434f8f3179c8ba4f11bf34733d704553f2f4842 Mon Sep 17 00:00:00 2001 From: Thomas Ingleby Date: Fri, 16 May 2014 16:56:48 +0100 Subject: [PATCH] blink-io-cpp: example gpio using cpp wrappers. Signed-off-by: Thomas Ingleby Signed-off-by: Brendan Le Foll --- examples/c++/Blink-IO.cpp | 74 +++++++++++++++++++++++++++++++++++++ examples/c++/CMakeLists.txt | 2 + 2 files changed, 76 insertions(+) create mode 100644 examples/c++/Blink-IO.cpp diff --git a/examples/c++/Blink-IO.cpp b/examples/c++/Blink-IO.cpp new file mode 100644 index 0000000..1e3066b --- /dev/null +++ b/examples/c++/Blink-IO.cpp @@ -0,0 +1,74 @@ +/* + * Author: Brendan Le Foll + * 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 +#include +#include +#include + +#include "gpio.hpp" + +#define DEFAULT_IOPIN 8 + +static int iopin; +int running = 0; + +void +sig_handler(int signo) +{ + if (signo == SIGINT) { + printf("closing IO%d nicely\n", iopin); + running = -1; + } +} + +int main (int argc, char **argv) +{ + if (argc < 2) { + printf("Provide an int arg if you want to flash on something other than %d\n", DEFAULT_IOPIN); + iopin = DEFAULT_IOPIN; + } else { + iopin = strtol(argv[1], NULL, 10); + } + maa::Gpio* gpio = new maa::Gpio(iopin); + if (gpio == NULL) { + return MAA_ERROR_UNSPECIFIED; + } + int response = gpio->dir(MAA_GPIO_OUT); + if (response != MAA_SUCCESS) + maa_result_print((maa_result_t) MAA_SUCCESS); + + signal(SIGINT, sig_handler); + + while (running == 0) { + response = gpio->write(1); + sleep(1); + response = gpio->write(0); + sleep(1); + } + delete gpio; + return response; +} diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index 879b3ba..6c5bdba 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -1,5 +1,7 @@ add_executable (AioA0 AioA0.cpp) +add_executable (blink-io-cpp Blink-IO.cpp) include_directories(${PROJECT_SOURCE_DIR}/api) target_link_libraries (AioA0 maa stdc++) +target_link_libraries (blink-io-cpp maa stdc++)