1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * This is an example to flash a number of LED lights in a pattern.
19  * Connect your LEDs to appropriate GPIOs (as many as you like)
20  * then modify gpio[] and m_gpio[] to specify the pins.
21  */
22 #include <unistd.h>
23 #include <stdio.h>
24 
25 #include <mraa.h>
26 
main(int argc,char * argv[])27 int main(int argc, char* argv[]) {
28   const unsigned gpio[] = {0, 1, 2};
29   const int gpio_count = sizeof(gpio)/sizeof(*gpio);
30   mraa_gpio_context m_gpio[] = {NULL, NULL, NULL};
31 
32   mraa_init();
33   for (int i = 0; i < gpio_count; i++) {
34     m_gpio[i] = mraa_gpio_init(gpio[i]);
35     if (!m_gpio[i]) {
36       fprintf(stderr, "Unable to initialize GPIO %d, invalid pin number?\n",
37           gpio[i]);
38       return 1;
39     }
40     mraa_gpio_dir(m_gpio[i], MRAA_GPIO_OUT);
41   }
42 
43   for (int iterations = 0; iterations < 3; iterations++) {
44     for (int i = 0; i < gpio_count; i++) {
45     mraa_gpio_write(m_gpio[i], 1);
46       sleep(1);
47     }
48     for (int i = gpio_count-1; i >= 0; i--) {
49       mraa_gpio_write(m_gpio[i], 0);
50       sleep(1);
51     }
52   }
53 
54   for (int i = 0; i < gpio_count; i++)
55     mraa_gpio_close(m_gpio[i]);
56   return 0;
57 }
58