1 From e3e94bd49df4b4588cc5c95392b872eadb531fc4 Mon Sep 17 00:00:00 2001
2 From: Jason Kridner <jkridner@beagleboard.org>
3 Date: Thu, 20 May 2010 05:41:26 -0500
4 Subject: [PATCH 11/16] Add 'led' command
6 This patch allows any board implementing the coloured LED API
7 to control the LEDs from the console.
9 led [green | yellow | red | all ] [ on | off ]
11 or
13 led [ 1 | 2 | 3 | all ] [ on | off ]
15 Adds configuration item CONFIG_CMD_LED enabling the command.
17 Partially based on patch from Ulf Samuelsson:
18 http://www.mail-archive.com/u-boot@lists.denx.de/msg09593.html.
20 Updated based on feedback:
21 http://www.mail-archive.com/u-boot@lists.denx.de/msg41847.html
22 https://groups.google.com/d/topic/beagleboard/8Wf1HiK_QBo/discussion
23 * Fixed a handful of style issues.
24 * Significantly reduced the number of #ifdefs and redundant code
25 * Converted redundant code into loops test against a structure
26 * Made use of cmd_usage()
27 * Introduced a str_onoff() function, but haven't yet put it in common
28 * Eliminated trailing newline
30 v2 updates
31 * Test every LED in case "all" is used. Previously, the code broke from
32 the loop after setting the state of only one LED.
33 * Corrected swapped on/off in structure definition
34 * Removed trailing white space
35 ---
36 common/Makefile | 1 +
37 common/cmd_led.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
38 2 files changed, 153 insertions(+), 0 deletions(-)
39 create mode 100644 common/cmd_led.c
41 diff --git a/common/Makefile b/common/Makefile
42 index 048df0c..29a0ead 100644
43 --- a/common/Makefile
44 +++ b/common/Makefile
45 @@ -105,6 +105,7 @@ COBJS-$(CONFIG_CMD_IRQ) += cmd_irq.o
46 COBJS-$(CONFIG_CMD_ITEST) += cmd_itest.o
47 COBJS-$(CONFIG_CMD_JFFS2) += cmd_jffs2.o
48 COBJS-$(CONFIG_CMD_CRAMFS) += cmd_cramfs.o
49 +COBJS-$(CONFIG_CMD_LED) += cmd_led.o
50 COBJS-$(CONFIG_CMD_LICENSE) += cmd_license.o
51 COBJS-y += cmd_load.o
52 COBJS-$(CONFIG_LOGBUFFER) += cmd_log.o
53 diff --git a/common/cmd_led.c b/common/cmd_led.c
54 new file mode 100644
55 index 0000000..7f02fe6
56 --- /dev/null
57 +++ b/common/cmd_led.c
58 @@ -0,0 +1,152 @@
59 +/*
60 + * (C) Copyright 2010
61 + * Jason Kridner <jkridner@beagleboard.org>
62 + *
63 + * Based on cmd_led.c patch from:
64 + * http://www.mail-archive.com/u-boot@lists.denx.de/msg06873.html
65 + * (C) Copyright 2008
66 + * Ulf Samuelsson <ulf.samuelsson@atmel.com>
67 + *
68 + * See file CREDITS for list of people who contributed to this
69 + * project.
70 + *
71 + * This program is free software; you can redistribute it and/or
72 + * modify it under the terms of the GNU General Public License as
73 + * published by the Free Software Foundation; either version 2 of
74 + * the License, or (at your option) any later version.
75 + *
76 + * This program is distributed in the hope that it will be useful,
77 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
78 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79 + * GNU General Public License for more details.
80 + *
81 + * You should have received a copy of the GNU General Public License
82 + * along with this program; if not, write to the Free Software
83 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
84 + * MA 02111-1307 USA
85 + */
86 +
87 +#include <common.h>
88 +#include <config.h>
89 +#include <command.h>
90 +#include <status_led.h>
91 +
92 +struct led_tbl_s {
93 + char *string; /* String for use in the command */
94 + led_id_t mask; /* Mask used for calling __led_set() */
95 + void (*off)(void); /* Optional fucntion for turning LED off */
96 + void (*on)(void); /* Optional fucntion for turning LED on */
97 +};
98 +
99 +typedef struct led_tbl_s led_tbl_t;
100 +
101 +static const led_tbl_t led_commands[] = {
102 +#ifdef CONFIG_BOARD_SPECIFIC_LED
103 +#ifdef STATUS_LED_BIT
104 + { "0", STATUS_LED_BIT, NULL, NULL },
105 +#endif
106 +#ifdef STATUS_LED_BIT1
107 + { "1", STATUS_LED_BIT1, NULL, NULL },
108 +#endif
109 +#ifdef STATUS_LED_BIT2
110 + { "2", STATUS_LED_BIT2, NULL, NULL },
111 +#endif
112 +#ifdef STATUS_LED_BIT3
113 + { "3", STATUS_LED_BIT3, NULL, NULL },
114 +#endif
115 +#endif
116 +#ifdef STATUS_LED_GREEN
117 + { "green", STATUS_LED_GREEN, green_LED_off, green_LED_on },
118 +#endif
119 +#ifdef STATUS_LED_YELLOW
120 + { "yellow", STATUS_LED_YELLOW, yellow_LED_off, yellow_LED_on },
121 +#endif
122 +#ifdef STATUS_LED_RED
123 + { "red", STATUS_LED_RED, red_LED_off, red_LED_on },
124 +#endif
125 +#ifdef STATUS_LED_BLUE
126 + { "blue", STATUS_LED_BLUE, blue_LED_off, blue_LED_on },
127 +#endif
128 + { NULL, 0, NULL, NULL }
129 +};
130 +
131 +int str_onoff (char *var)
132 +{
133 + if (strcmp(var, "off") == 0) {
134 + return 0;
135 + }
136 + if (strcmp(var, "on") == 0) {
137 + return 1;
138 + }
139 + return -1;
140 +}
141 +
142 +int do_led (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
143 +{
144 + int state, i;
145 +
146 + /* Validate arguments */
147 + if ((argc != 3)) {
148 + return cmd_usage(cmdtp);
149 + }
150 +
151 + state = str_onoff(argv[2]);
152 + if (state < 0) {
153 + return cmd_usage(cmdtp);
154 + }
155 +
156 + for (i = 0; led_commands[i].string; i++) {
157 + if ((strcmp("all", argv[1]) == 0) ||
158 + (strcmp(led_commands[i].string, argv[1]) == 0)) {
159 + if (led_commands[i].on) {
160 + if (state) {
161 + led_commands[i].on();
162 + } else {
163 + led_commands[i].off();
164 + }
165 + } else {
166 + __led_set(led_commands[i].mask, state);
167 + }
168 + }
169 + }
170 +
171 + /* If we ran out of matches, print Usage */
172 + if (!led_commands[i].string && !(strcmp("all", argv[1]) == 0)) {
173 + return cmd_usage(cmdtp);
174 + }
175 +
176 + return 0;
177 +}
178 +
179 +U_BOOT_CMD(
180 + led, 3, 1, do_led,
181 + "led\t- ["
182 +#ifdef CONFIG_BOARD_SPECIFIC_LED
183 +#ifdef STATUS_LED_BIT
184 + "0|"
185 +#endif
186 +#ifdef STATUS_LED_BIT1
187 + "1|"
188 +#endif
189 +#ifdef STATUS_LED_BIT2
190 + "2|"
191 +#endif
192 +#ifdef STATUS_LED_BIT3
193 + "3|"
194 +#endif
195 +#endif
196 +#ifdef STATUS_LED_GREEN
197 + "green|"
198 +#endif
199 +#ifdef STATUS_LED_YELLOW
200 + "yellow|"
201 +#endif
202 +#ifdef STATUS_LED_RED
203 + "red|"
204 +#endif
205 +#ifdef STATUS_LED_BLUE
206 + "blue|"
207 +#endif
208 + "all] [on|off]\n",
209 + "led [led_name] [on|off] sets or clears led(s)\n"
210 +);
211 --
212 1.6.6.1