2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
29 #include "mraa/gpio.h"
31 extern mraa_board_t* plat;
33 char board_name[] = "Some weird devboard that isn't recognised...";
37 fprintf(stdout, "Version %s on %s\n", mraa_get_version(), board_name);
42 fprintf(stdout, "list List pins\n");
43 fprintf(stdout, "set pin level Set pin to level (0/1)\n");
44 fprintf(stdout, "get pin Get pin level\n");
48 print_command_error() {
49 fprintf(stdout, "Invalid command, options are:\n");
50 fprintf(stdout, "list List pins\n");
51 fprintf(stdout, "set pin level Set pin to level (0/1)\n");
52 fprintf(stdout, "get pin Get pin level\n");
57 const mraa_board_t* platformInfo = plat;
59 for (i = 0; i < platformInfo->phy_pin_count; ++i) {
60 const char *mraa_name = platformInfo->pins[i].name;
61 if (strcmp(mraa_name, "INVALID") != 0) {
62 mraa_pincapabilities_t caps = platformInfo->pins[i].capabilites;
63 fprintf(stdout, "%02d %-8s ", i, mraa_name);
65 fprintf(stdout, "GPIO");
67 fprintf(stdout, "I2C");
69 fprintf(stdout, "SPI");
71 fprintf(stdout, "PWM");
73 fprintf(stdout, "UART");
74 fprintf(stdout, "\n");
80 gpio_set(int pin, int level) {
81 mraa_gpio_context gpio = mraa_gpio_init(pin);
83 mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
84 mraa_gpio_write(gpio, level);
87 return MRAA_ERROR_INVALID_RESOURCE;
91 gpio_get(int pin, int* level) {
92 mraa_gpio_context gpio = mraa_gpio_init(pin);
94 mraa_gpio_dir(gpio, MRAA_GPIO_IN);
95 *level = mraa_gpio_read(gpio);
98 return MRAA_ERROR_INVALID_RESOURCE;
102 main(int argc, char **argv)
104 mraa_platform_t platform = mraa_get_platform_type();
107 case MRAA_INTEL_GALILEO_GEN1:
108 strcpy(board_name, "Intel Galileo Gen1");
110 case MRAA_INTEL_GALILEO_GEN2:
111 strcpy(board_name, "Intel Galileo Gen2");
113 case MRAA_INTEL_EDISON_FAB_C:
114 strcpy(board_name, "Intel Edison");
116 case MRAA_INTEL_MINNOWBOARD_MAX:
117 strcpy(board_name, "Intel Minnowboard Max");
120 fprintf(stdout, "Unknown platform code %d\n", platform);
126 print_command_error();
130 if (strcmp(argv[1], "list") == 0) {
132 } else if (strcmp(argv[1], "help") == 0) {
134 } else if (strcmp(argv[1], "set") == 0) {
136 int pin = atoi(argv[2]);
137 if (gpio_set(pin, atoi(argv[3])) != MRAA_SUCCESS)
138 fprintf(stdout, "Could not initialize gpio %d\n", pin);
140 print_command_error();
141 } else if (strcmp(argv[1], "get") == 0) {
143 int pin = atoi(argv[2]);
145 if (gpio_get(pin, &level) == MRAA_SUCCESS)
146 fprintf(stdout, "Pin %d = %d\n", pin, level);
148 fprintf(stdout, "Could not initialize gpio %d\n", pin);
150 print_command_error();
152 print_command_error();