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