blob: b696542e04c0566eeef4558ba8c7b8cffbf5df2d [file] [log] [blame]
Gordon Hendersonbf0ad862012-08-16 15:04:43 +01001/*
2 * gpio.c:
Gordon Henderson99095e32012-08-27 20:56:14 +01003 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
4 * Pi's GPIO.
Gordon Hendersonbf0ad862012-08-16 15:04:43 +01005 * Copyright (c) 2012 Gordon Henderson
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 *
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
23 */
24
Gordon Hendersonbf0ad862012-08-16 15:04:43 +010025
26#include <stdio.h>
27#include <stdlib.h>
28#include <stdint.h>
29#include <string.h>
30#include <unistd.h>
31#include <errno.h>
32#include <sys/types.h>
33#include <fcntl.h>
34
Gordon Henderson99095e32012-08-27 20:56:14 +010035#include <wiringPi.h>
36#include <gertboard.h>
37
Gordon Hendersonbf0ad862012-08-16 15:04:43 +010038#ifndef TRUE
39# define TRUE (1==1)
40# define FALSE (1==2)
41#endif
42
Gordon Henderson99095e32012-08-27 20:56:14 +010043#define VERSION "1.2"
Gordon Hendersonbf0ad862012-08-16 15:04:43 +010044
45static int wpMode ;
46
47char *usage = "Usage: gpio -v\n"
Gordon Henderson178ea082012-08-19 15:17:03 +010048 " gpio -h\n"
Gordon Hendersonbf0ad862012-08-16 15:04:43 +010049 " gpio [-g] <read/write/pwm/mode> ...\n"
50 " gpio [-p] <read/write/mode> ...\n"
51 " gpio export/edge/unexport/unexportall/exports ...\n"
52 " gpio drive <group> <value>\n"
Gordon Hendersonf99ffed2012-08-19 15:12:45 +010053 " gpio pwm-bal/pwm-ms \n"
54 " gpio pwmr <range> \n"
Gordon Henderson99095e32012-08-27 20:56:14 +010055 " gpio load spi/i2c\n"
56 " gpio gbr <channel>\n"
57 " gpio gbw <channel> <value>\n" ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +010058
59
Gordon Hendersonbf0ad862012-08-16 15:04:43 +010060/*
61 * changeOwner:
62 * Change the ownership of the file to the real userId of the calling
63 * program so we can access it.
64 *********************************************************************************
65 */
66
67static void changeOwner (char *cmd, char *file)
68{
69 uid_t uid = getuid () ;
70 uid_t gid = getgid () ;
71
72 if (chown (file, uid, gid) != 0)
73 {
74 if (errno == ENOENT) // Warn that it's not there
75 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
76 else
77 {
78 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
79 exit (1) ;
80 }
81 }
82}
83
84
85/*
86 * moduleLoaded:
87 * Return true/false if the supplied module is loaded
88 *********************************************************************************
89 */
90
91static int moduleLoaded (char *modName)
92{
93 int len = strlen (modName) ;
94 int found = FALSE ;
95 FILE *fd = fopen ("/proc/modules", "r") ;
96 char line [80] ;
97
98 if (fd == NULL)
99 {
100 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
101 exit (1) ;
102 }
103
104 while (fgets (line, 80, fd) != NULL)
105 {
106 if (strncmp (line, modName, len) != 0)
107 continue ;
108
109 found = TRUE ;
110 break ;
111 }
112
113 fclose (fd) ;
114
115 return found ;
116}
117
118
119/*
120 * doLoad:
121 * Load either the spi or i2c modules and change device ownerships, etc.
122 *********************************************************************************
123 */
124
125static void _doLoadUsage (char *argv [])
126{
127 fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
128 exit (1) ;
129}
130
131static void doLoad (int argc, char *argv [])
132{
133 char *module ;
134 char cmd [80] ;
135 char *file1, *file2 ;
136
137 if (argc != 3)
138 _doLoadUsage (argv) ;
139
140 /**/ if (strcasecmp (argv [2], "spi") == 0)
141 {
142 module = "spi_bcm2708" ;
143 file1 = "/dev/spidev0.0" ;
144 file2 = "/dev/spidev0.1" ;
145 }
146 else if (strcasecmp (argv [2], "i2c") == 0)
147 {
148 module = "i2c_bcm2708" ;
149 file1 = "/dev/i2c-0" ;
150 file2 = "/dev/i2c-1" ;
151 }
152 else
153 _doLoadUsage (argv) ;
154
155 if (!moduleLoaded (module))
156 {
157 sprintf (cmd, "modprobe %s", module) ;
158 system (cmd) ;
159 }
160
161 if (!moduleLoaded (module))
162 {
163 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
164 exit (1) ;
165 }
166
167 sleep (1) ; // To let things get settled
168
169 changeOwner (argv [0], file1) ;
170 changeOwner (argv [0], file2) ;
171}
172
173
174
175/*
176 * doExports:
177 * List all GPIO exports
178 *********************************************************************************
179 */
180
Gordon Henderson178ea082012-08-19 15:17:03 +0100181static void doExports (int argc, char *argv [])
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100182{
183 int fd ;
184 int i, l, first ;
185 char fName [128] ;
186 char buf [16] ;
187
188// Rather crude, but who knows what others are up to...
189
190 for (first = 0, i = 0 ; i < 64 ; ++i)
191 {
192
193// Try to read the direction
194
195 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
196 if ((fd = open (fName, O_RDONLY)) == -1)
197 continue ;
198
199 if (first == 0)
200 {
201 ++first ;
202 printf ("GPIO Pins exported:\n") ;
203 }
204
205 printf ("%4d: ", i) ;
206
207 if ((l = read (fd, buf, 16)) == 0)
208 sprintf (buf, "%s", "?") ;
209
210 buf [l] = 0 ;
211 if ((buf [strlen (buf) - 1]) == '\n')
212 buf [strlen (buf) - 1] = 0 ;
213
214 printf ("%-3s", buf) ;
215
216 close (fd) ;
217
218// Try to Read the value
219
220 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
221 if ((fd = open (fName, O_RDONLY)) == -1)
222 {
223 printf ("No Value file (huh?)\n") ;
224 continue ;
225 }
226
227 if ((l = read (fd, buf, 16)) == 0)
228 sprintf (buf, "%s", "?") ;
229
230 buf [l] = 0 ;
231 if ((buf [strlen (buf) - 1]) == '\n')
232 buf [strlen (buf) - 1] = 0 ;
233
234 printf (" %s", buf) ;
235
236// Read any edge trigger file
237
238 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
239 if ((fd = open (fName, O_RDONLY)) == -1)
240 {
241 printf ("\n") ;
242 continue ;
243 }
244
245 if ((l = read (fd, buf, 16)) == 0)
246 sprintf (buf, "%s", "?") ;
247
248 buf [l] = 0 ;
249 if ((buf [strlen (buf) - 1]) == '\n')
250 buf [strlen (buf) - 1] = 0 ;
251
252 printf (" %-8s\n", buf) ;
253
254 close (fd) ;
255 }
256}
257
258
259/*
260 * doExport:
261 * gpio export pin mode
262 * This uses the /sys/class/gpio device interface.
263 *********************************************************************************
264 */
265
266void doExport (int argc, char *argv [])
267{
268 FILE *fd ;
269 int pin ;
270 char *mode ;
271 char fName [128] ;
272
273 if (argc != 4)
274 {
275 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
276 exit (1) ;
277 }
278
279 pin = atoi (argv [2]) ;
280
281 mode = argv [3] ;
282
283 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
284 {
285 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
286 exit (1) ;
287 }
288
289 fprintf (fd, "%d\n", pin) ;
290 fclose (fd) ;
291
292 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
293 if ((fd = fopen (fName, "w")) == NULL)
294 {
295 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
296 exit (1) ;
297 }
298
299 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
300 fprintf (fd, "in\n") ;
301 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
302 fprintf (fd, "out\n") ;
303 else
304 {
305 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
306 exit (1) ;
307 }
308
309 fclose (fd) ;
310
311// Change ownership so the current user can actually use it!
312
313 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
314 changeOwner (argv [0], fName) ;
315
316 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
317 changeOwner (argv [0], fName) ;
318
319}
320
321
322/*
323 * doEdge:
324 * gpio edge pin mode
325 * Easy access to changing the edge trigger on a GPIO pin
326 * This uses the /sys/class/gpio device interface.
327 *********************************************************************************
328 */
329
330void doEdge (int argc, char *argv [])
331{
332 FILE *fd ;
333 int pin ;
334 char *mode ;
335 char fName [128] ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100336
337 if (argc != 4)
338 {
339 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
340 exit (1) ;
341 }
342
Gordon Henderson178ea082012-08-19 15:17:03 +0100343 pin = atoi (argv [2]) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100344 mode = argv [3] ;
345
346// Export the pin and set direction to input
347
348 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
349 {
350 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
351 exit (1) ;
352 }
353
354 fprintf (fd, "%d\n", pin) ;
355 fclose (fd) ;
356
357 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
358 if ((fd = fopen (fName, "w")) == NULL)
359 {
360 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
361 exit (1) ;
362 }
363
364 fprintf (fd, "in\n") ;
365 fclose (fd) ;
366
367 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
368 if ((fd = fopen (fName, "w")) == NULL)
369 {
370 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
371 exit (1) ;
372 }
373
Gordon Henderson178ea082012-08-19 15:17:03 +0100374 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
375 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
376 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
377 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100378 else
379 {
380 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
381 exit (1) ;
382 }
383
Gordon Henderson178ea082012-08-19 15:17:03 +0100384// Change ownership of the value and edge files, so the current user can actually use it!
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100385
386 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
Gordon Henderson178ea082012-08-19 15:17:03 +0100387 changeOwner (argv [0], fName) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100388
389 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
Gordon Henderson178ea082012-08-19 15:17:03 +0100390 changeOwner (argv [0], fName) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100391
392 fclose (fd) ;
393}
394
395
396/*
397 * doUnexport:
398 * gpio unexport pin
399 * This uses the /sys/class/gpio device interface.
400 *********************************************************************************
401 */
402
403void doUnexport (int argc, char *argv [])
404{
405 FILE *fd ;
406 int pin ;
407
408 if (argc != 3)
409 {
410 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
411 exit (1) ;
412 }
413
414 pin = atoi (argv [2]) ;
415
416 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
417 {
418 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
419 exit (1) ;
420 }
421
422 fprintf (fd, "%d\n", pin) ;
423 fclose (fd) ;
424}
425
426
427/*
428 * doUnexportAll:
429 * gpio unexportall
430 * Un-Export all the GPIO pins.
431 * This uses the /sys/class/gpio device interface.
432 *********************************************************************************
433 */
434
435void doUnexportall (int argc, char *argv [])
436{
437 FILE *fd ;
438 int pin ;
439
440 for (pin = 0 ; pin < 63 ; ++pin)
441 {
442 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
443 {
444 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
445 exit (1) ;
446 }
447 fprintf (fd, "%d\n", pin) ;
448 fclose (fd) ;
449 }
450}
451
452
453/*
454 * doMode:
455 * gpio mode pin mode ...
456 *********************************************************************************
457 */
458
459void doMode (int argc, char *argv [])
460{
461 int pin ;
462 char *mode ;
463
464 if (argc != 4)
465 {
466 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
467 exit (1) ;
468 }
469
470 pin = atoi (argv [2]) ;
471
472 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
473 return ;
474
475 mode = argv [3] ;
476
Gordon Henderson178ea082012-08-19 15:17:03 +0100477 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
478 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
479 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
480 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
481 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
482 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100483 else
484 {
485 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
486 exit (1) ;
487 }
488}
489
490
491/*
492 * doPadDrive:
493 * gpio drive group value
494 *********************************************************************************
495 */
496
Gordon Henderson1bb49892012-08-19 15:33:26 +0100497static void doPadDrive (int argc, char *argv [])
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100498{
499 int group, val ;
500
501 if (argc != 4)
502 {
503 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
504 exit (1) ;
505 }
506
507 group = atoi (argv [2]) ;
508 val = atoi (argv [3]) ;
509
510 if ((group < 0) || (group > 2))
511 {
512 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
513 exit (1) ;
514 }
515
516 if ((val < 0) || (val > 7))
517 {
518 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
519 exit (1) ;
520 }
521
522 setPadDrive (group, val) ;
523}
524
525
526/*
Gordon Henderson99095e32012-08-27 20:56:14 +0100527 * doGbw:
528 * gpio gbw channel value
529 *********************************************************************************
530 */
531
532static void doGbw (int argc, char *argv [])
533{
534 int channel, value ;
535
536 if (argc != 4)
537 {
538 fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
539 exit (1) ;
540 }
541
542 channel = atoi (argv [2]) ;
543 value = atoi (argv [3]) ;
544
545 if ((channel < 0) || (channel > 1))
546 {
547 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
548 exit (1) ;
549 }
550
551 if ((value < 0) || (value > 1023))
552 {
553 fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
554 exit (1) ;
555 }
556
557 if (gertboardSPISetup () == -1)
558 {
559 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
560 exit (1) ;
561 }
562
563 gertboardAnalogWrite (channel, value) ;
564}
565
566
567/*
568 * doGbr:
569 * gpio gbr channel
570 *********************************************************************************
571 */
572
573static void doGbr (int argc, char *argv [])
574{
575 int channel ;
576
577 if (argc != 3)
578 {
579 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
580 exit (1) ;
581 }
582
583 channel = atoi (argv [2]) ;
584
585 if ((channel < 0) || (channel > 1))
586 {
587 fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
588 exit (1) ;
589 }
590
591 if (gertboardSPISetup () == -1)
592 {
593 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
594 exit (1) ;
595 }
596
597 printf ("%d\n",gertboardAnalogRead (channel)) ;
598}
599
600
601
602/*
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100603 * doWrite:
604 * gpio write pin value
605 *********************************************************************************
606 */
607
Gordon Henderson1bb49892012-08-19 15:33:26 +0100608static void doWrite (int argc, char *argv [])
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100609{
610 int pin, val ;
611
612 if (argc != 4)
613 {
614 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
615 exit (1) ;
616 }
617
618 pin = atoi (argv [2]) ;
619
620 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
621 return ;
622
623 val = atoi (argv [3]) ;
624
625 /**/ if (val == 0)
626 digitalWrite (pin, LOW) ;
627 else
628 digitalWrite (pin, HIGH) ;
629}
630
631
632/*
633 * doRead:
634 * Read a pin and return the value
635 *********************************************************************************
636 */
637
638void doRead (int argc, char *argv [])
639{
640 int pin, val ;
641
642 if (argc != 3)
643 {
644 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
645 exit (1) ;
646 }
647
648 pin = atoi (argv [2]) ;
649
650 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
651 {
652 printf ("0\n") ;
653 return ;
654 }
655
656 val = digitalRead (pin) ;
657
658 printf ("%s\n", val == 0 ? "0" : "1") ;
659}
660
661
662/*
663 * doPwm:
664 * Output a PWM value on a pin
665 *********************************************************************************
666 */
667
668void doPwm (int argc, char *argv [])
669{
670 int pin, val ;
671
672 if (argc != 4)
673 {
674 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
675 exit (1) ;
676 }
677
678 pin = atoi (argv [2]) ;
679
680 if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
681 return ;
682
683 val = atoi (argv [3]) ;
684
685 pwmWrite (pin, val) ;
686}
687
688
689/*
Gordon Hendersonf99ffed2012-08-19 15:12:45 +0100690 * doPwmMode: doPwmRange:
691 * Change the PWM mode and Range values
692 *********************************************************************************
693 */
694
695static void doPwmMode (int mode)
696{
697 pwmSetMode (mode) ;
698}
699
700static void doPwmRange (int argc, char *argv [])
701{
702 unsigned int range ;
703
704 if (argc != 3)
705 {
706 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
707 exit (1) ;
708 }
709
710 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
711
712 if (range == 0)
713 {
714 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
715 exit (1) ;
716 }
717
718 pwmSetRange (range) ;
719}
720
721
722/*
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100723 * main:
724 * Start here
725 *********************************************************************************
726 */
727
728int main (int argc, char *argv [])
729{
730 int i ;
731
732 if (argc == 1)
733 {
734 fprintf (stderr, "%s: %s\n", argv [0], usage) ;
735 return 1 ;
736 }
737
Gordon Henderson178ea082012-08-19 15:17:03 +0100738 if (strcasecmp (argv [1], "-h") == 0)
739 {
740 printf ("%s: %s\n", argv [0], usage) ;
741 return 0 ;
742 }
743
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100744 if (strcasecmp (argv [1], "-v") == 0)
745 {
746 printf ("gpio version: %s\n", VERSION) ;
747 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
748 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
749 printf ("For details type: %s -warranty\n", argv [0]) ;
750 return 0 ;
751 }
752
753 if (strcasecmp (argv [1], "-warranty") == 0)
754 {
755 printf ("gpio version: %s\n", VERSION) ;
756 printf ("Copyright (c) 2012 Gordon Henderson\n") ;
757 printf ("\n") ;
758 printf (" This program is free software; you can redistribute it and/or modify\n") ;
759 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
760 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
761 printf (" (at your option) any later version.\n") ;
762 printf ("\n") ;
763 printf (" This program is distributed in the hope that it will be useful,\n") ;
764 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
765 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
766 printf (" GNU Lesser General Public License for more details.\n") ;
767 printf ("\n") ;
768 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
769 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
770 printf ("\n") ;
771 return 0 ;
772 }
773
774 if (geteuid () != 0)
775 {
776 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
777 return 1 ;
778 }
779
780// Initial test for /sys/class/gpio operations:
781
Gordon Henderson178ea082012-08-19 15:17:03 +0100782 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
783 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
784 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100785 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
Gordon Henderson178ea082012-08-19 15:17:03 +0100786 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
787
788// Check for drive or load commands:
789
790 if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
791 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100792
Gordon Henderson99095e32012-08-27 20:56:14 +0100793// Gertboard commands
794
795 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
796 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
797
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100798// Check for -g argument
799
800 if (strcasecmp (argv [1], "-g") == 0)
801 {
802 if (wiringPiSetupGpio () == -1)
803 {
Gordon Henderson178ea082012-08-19 15:17:03 +0100804 fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100805 exit (1) ;
806 }
807
808 for (i = 2 ; i < argc ; ++i)
809 argv [i - 1] = argv [i] ;
810 --argc ;
811 wpMode = WPI_MODE_GPIO ;
812 }
813
814// Check for -p argument for PiFace
815
816 else if (strcasecmp (argv [1], "-p") == 0)
817 {
818 if (wiringPiSetupPiFaceForGpioProg () == -1)
819 {
820 fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
821 exit (1) ;
822 }
823
824 for (i = 2 ; i < argc ; ++i)
825 argv [i - 1] = argv [i] ;
826 --argc ;
827 wpMode = WPI_MODE_PIFACE ;
828 }
829
830// Default to wiringPi mode
831
832 else
833 {
834 if (wiringPiSetup () == -1)
835 {
Gordon Henderson178ea082012-08-19 15:17:03 +0100836 fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100837 exit (1) ;
838 }
839 wpMode = WPI_MODE_PINS ;
840 }
841
Gordon Hendersonf99ffed2012-08-19 15:12:45 +0100842// Check for PWM operations
843
844 if (wpMode != WPI_MODE_PIFACE)
845 {
846 if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
847 if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
848 if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
849 }
850
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100851// Check for wiring commands
852
Gordon Henderson178ea082012-08-19 15:17:03 +0100853 /**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
854 else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
855 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
856 else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100857 else
858 {
Gordon Henderson178ea082012-08-19 15:17:03 +0100859 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
Gordon Hendersonbf0ad862012-08-16 15:04:43 +0100860 exit (1) ;
861 }
862 return 0 ;
863}