blink_onboard.c: explain use of calamari lure in example
[contrib/mraa.git] / examples / gpio.c
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 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 <string.h>
27 #include <unistd.h>
28
29 #include "mraa/gpio.h"
30
31 extern mraa_board_t* plat;
32
33 char board_name[] = "Some weird devboard that isn't recognised...";
34
35 void
36 print_version() {
37     fprintf(stdout, "Version %s on %s\n", mraa_get_version(), board_name);
38 }
39
40 void
41 print_help() {
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");
45 }
46
47 void
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");
53 }
54
55 void
56 list_pins() {
57     const mraa_board_t* platformInfo = plat;
58     int i;
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);
64             if (caps.gpio)
65                 fprintf(stdout, "GPIO");
66             if (caps.i2c)
67                 fprintf(stdout, "I2C");
68             if (caps.spi)
69                 fprintf(stdout, "SPI");
70             if (caps.pwm)
71                 fprintf(stdout, "PWM");
72             if (caps.uart)
73                 fprintf(stdout, "UART");
74             fprintf(stdout, "\n");
75         }
76     }
77 }
78
79 mraa_result_t
80 gpio_set(int pin, int level) {
81     mraa_gpio_context gpio = mraa_gpio_init(pin);
82     if (gpio != NULL) {
83         mraa_gpio_dir(gpio, MRAA_GPIO_OUT);
84         mraa_gpio_write(gpio, level);
85         return MRAA_SUCCESS;
86     } else
87         return MRAA_ERROR_INVALID_RESOURCE;
88 }
89
90 mraa_result_t
91 gpio_get(int pin, int* level) {
92     mraa_gpio_context gpio = mraa_gpio_init(pin);
93     if (gpio != NULL) {
94         mraa_gpio_dir(gpio, MRAA_GPIO_IN);
95         *level = mraa_gpio_read(gpio);
96         return MRAA_SUCCESS;
97     } else
98         return MRAA_ERROR_INVALID_RESOURCE;
99 }
100
101 int
102 main(int argc, char **argv)
103 {
104     mraa_platform_t platform = mraa_get_platform_type();
105
106     switch (platform) {
107         case MRAA_INTEL_GALILEO_GEN1:
108             strcpy(board_name, "Intel Galileo Gen1");
109             break;
110         case MRAA_INTEL_GALILEO_GEN2:
111             strcpy(board_name, "Intel Galileo Gen2");
112             break;
113         case MRAA_INTEL_EDISON_FAB_C:
114             strcpy(board_name, "Intel Edison");
115             break;
116         case MRAA_INTEL_MINNOWBOARD_MAX:
117             strcpy(board_name, "Intel Minnowboard Max");
118             break;
119         default:
120             fprintf(stdout, "Unknown platform code %d\n", platform);
121             return 1;
122     }
123
124
125     if (argc == 1) {
126         print_command_error();
127     }
128
129     if (argc > 1) {
130         if (strcmp(argv[1], "list") == 0) {
131             list_pins();
132         } else if (strcmp(argv[1], "help") == 0) {
133             print_help();
134         } else if (strcmp(argv[1], "set") == 0) {
135             if (argc == 4) {
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);
139             } else
140                 print_command_error();
141         } else if (strcmp(argv[1], "get") == 0) {
142             if (argc == 3) {
143                 int pin = atoi(argv[2]);
144                 int level;
145                 if (gpio_get(pin, &level) == MRAA_SUCCESS)
146                     fprintf(stdout, "Pin %d = %d\n", pin, level);
147                 else
148                     fprintf(stdout, "Could not initialize gpio %d\n", pin);
149             } else
150                 print_command_error();
151         } else {
152             print_command_error();
153         }
154     }
155     return 0;
156 }