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 set/clear a GPIO pin using the MRAA library.
19  * This may be tested with any of many GPIO devices such as an LED
20  * directly connected to the GPIO pin and ground,
21  * the Grove buzzer or Grove LED board.
22  *
23  * The on-board LED on the Edison Arudino expansion board may be
24  * accessed with Digital I/O 13 (mapped from Linux GPIO 243):
25  *   example-gpio-output -p 13 -s
26  *
27  * See the following link for a table to map from the numbers on the
28  * board silk screen to the libmraa GPIO numbers:
29  *   https://learn.sparkfun.com/tutorials/installing-libmraa-on-ubilinux-for-edison
30  */
31 #include <getopt.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 #include <mraa.h>
36 
37 // Structure to hold the decoded command line options
38 struct pgm_options {
39   int        pin;
40   bool       set;
41   bool       clear;
42 };
43 
44 // Be sure to keep the options for longopts and shortopts in the same order
45 // so that Usage() is correct.
46 static struct option longopts[] = {
47   {"help",  no_argument,        NULL,   '?'},
48   {"pin",   no_argument,        NULL,   'p'},
49   {"set",   no_argument,        NULL,   's'},
50   {"clear", no_argument,        NULL,   'c'},
51   {NULL,    0,                  NULL,    0 }
52 };
53 static char shortopts[] = "?p:sc";
54 
55 // Describes the options for this program.
Usage(char * pgm_name)56 void Usage(char *pgm_name) {
57   printf("Usage: %s [options...]\n", pgm_name);
58   printf("Manipulate a GPIO pin\n");
59   printf("Options:\n");
60   for (int i = 0, j = 0; longopts[i].name; i++, j++) {
61     if (shortopts[j] == ':')
62       j++;
63     printf(" --%-6s%s or -%c%s""\n",
64         longopts[i].name,
65         (shortopts[j+1] == ':') ? " <arg> " : "",
66         shortopts[j],
67         (shortopts[j+1] == ':') ? " <arg> " : "");
68   }
69   return;
70 }
71 
72 // Processes all command line options.
73 //   sets the options members for commnd line options
74 //   returns (0) on success, -1 otherwise.
ReadOpts(int argc,char ** argv,struct pgm_options * options)75 int ReadOpts(int argc, char **argv, struct pgm_options *options) {
76   int  ch = 0;
77 
78   while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
79     switch (ch) {
80       case 'p':
81         options->pin = atoi(optarg);
82         break;
83       case 's':
84         options->set = true;
85         break;
86       case 'c':
87         options->clear = true;
88         break;
89       default:
90         Usage(argv[0]);
91         return -1;
92     }
93   }
94   argc -= optind;
95   argv += optind;
96 
97   return 0;
98 }
99 
main(int argc,char * argv[])100 int main(int argc, char* argv[]) {
101   const unsigned kDefaultPinGPIO = 7;
102   pgm_options options = {kDefaultPinGPIO, false, false};
103 
104   if (ReadOpts(argc, argv, &options) < 0)
105     return 1;
106 
107   mraa_init();
108   mraa_gpio_context m_gpio = mraa_gpio_init(options.pin);
109   if (!m_gpio) {
110     fprintf(stderr, "Unable to initialize GPIO, invalid pin number?\n");
111     return 1;
112   }
113   mraa_gpio_dir(m_gpio, MRAA_GPIO_OUT);
114 
115   if (options.set)
116     mraa_gpio_write(m_gpio, 1);
117 
118   // If both options were specified, wait a few seconds before clearing
119   if (options.clear && options.set)
120     sleep(5);
121 
122   if (options.clear)
123     mraa_gpio_write(m_gpio, 0);
124 
125   mraa_gpio_close(m_gpio);
126   return 0;
127 }
128