packaging: also support rpmbuild from Tizen:2.3
[contrib/mraa.git] / examples / helloedison.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <stdio.h>
26 #include <syslog.h>
27 #include <string.h>
28 #include <stdbool.h>
29 //! [Interesting]
30 #include "mraa.h"
31
32 int
33 main(int argc, char** argv)
34 {
35     mraa_result_t ret = MRAA_SUCCESS;
36     mraa_platform_t platform_type = mraa_get_platform_type();
37
38     if (platform_type != MRAA_INTEL_EDISON_FAB_C) {
39         fprintf(stderr, "Error: This program can only run on an edison\n");
40         ret = MRAA_ERROR_INVALID_PLATFORM;
41         goto end;
42     }
43
44     // MRAA_INTEL_EDISON_GP182 == 0 so this will initialise pin0 on arduino
45     // which is hardware gpio 130 and not 182
46     mraa_gpio_context gpio182 = mraa_gpio_init(MRAA_INTEL_EDISON_GP182);
47     if (gpio182 == NULL) {
48         fprintf(stderr, "Error: Failed to open gpio182\n");
49         ret = MRAA_ERROR_INVALID_PLATFORM;
50         goto end;
51     }
52     mraa_gpio_dir(gpio182, MRAA_GPIO_OUT);
53
54     // we set the owner to false here, this makes sure that we do not close the
55     // gpio from sysfs in mraa_gpio_close meaning it will stay as an output and
56     // we will not always transition from 0->1 as gpio182 as output has the
57     // default position of '0'. Note that the value could change as a result of
58     // a mraa_gpio_dir however meaning we always go from 0->1 or 1->0
59     mraa_gpio_owner(gpio182, false);
60     int val = mraa_gpio_read(gpio182);
61     printf("GPIO%d (mraa pin %d) was: %d, will set to %d\n", 182, mraa_gpio_get_pin(gpio182), val, !val);
62     mraa_gpio_write(gpio182, !val);
63     mraa_gpio_close(gpio182);
64
65 end:
66     mraa_deinit();
67     return ret;
68 }
69 //! [Interesting]