3 * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
5 * Copyright (c) 2012-2013 Gordon Henderson
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
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.
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.
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 ***********************************************************************
34 #include <sys/types.h>
39 #include <gertboard.h>
42 #include "extensions.h"
44 extern int wiringPiDebug ;
46 extern void doReadall (void) ;
47 extern void doReadallOld (void) ;
54 #define VERSION "2.13"
55 #define I2CDETECT "/usr/sbin/i2cdetect"
59 char *usage = "Usage: gpio -v\n"
61 " gpio [-g|-1] [-x extension:params] ...\n"
62 " gpio [-p] <read/write/wb> ...\n"
63 " gpio <read/write/aread/awritewb/pwm/clock/mode> ...\n"
64 " gpio readall/reset\n"
65 " gpio unexportall/exports\n"
66 " gpio export/edge/unexport ...\n"
67 " gpio wfi <pin> <mode>\n"
68 " gpio drive <group> <value>\n"
69 " gpio pwm-bal/pwm-ms \n"
70 " gpio pwmr <range> \n"
71 " gpio pwmc <divider> \n"
72 " gpio load spi/i2c\n"
73 " gpio i2cd/i2cdetect\n"
74 " gpio gbr <channel>\n"
75 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
80 * Decode a pin "number" which can actually be a pin name to represent
81 * one of the Pi's on-board pins.
82 *********************************************************************************
85 static int decodePin (const char *str)
88 // The first case - see if it's a number:
90 if (isdigit (str [0]))
99 * Change the ownership of the file to the real userId of the calling
100 * program so we can access it.
101 *********************************************************************************
104 static void changeOwner (char *cmd, char *file)
106 uid_t uid = getuid () ;
107 uid_t gid = getgid () ;
109 if (chown (file, uid, gid) != 0)
111 if (errno == ENOENT) // Warn that it's not there
112 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
115 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
124 * Return true/false if the supplied module is loaded
125 *********************************************************************************
128 static int moduleLoaded (char *modName)
130 int len = strlen (modName) ;
132 FILE *fd = fopen ("/proc/modules", "r") ;
137 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
141 while (fgets (line, 80, fd) != NULL)
143 if (strncmp (line, modName, len) != 0)
158 * Load either the spi or i2c modules and change device ownerships, etc.
159 *********************************************************************************
162 static void _doLoadUsage (char *argv [])
164 fprintf (stderr, "Usage: %s load <spi/i2c> [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ;
168 static void doLoad (int argc, char *argv [])
170 char *module1, *module2 ;
172 char *file1, *file2 ;
173 char args1 [32], args2 [32] ;
176 _doLoadUsage (argv) ;
178 args1 [0] = args2 [0] = 0 ;
180 /**/ if (strcasecmp (argv [2], "spi") == 0)
183 module2 = "spi_bcm2708" ;
184 file1 = "/dev/spidev0.0" ;
185 file2 = "/dev/spidev0.1" ;
187 sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
189 _doLoadUsage (argv) ;
191 else if (strcasecmp (argv [2], "i2c") == 0)
193 module1 = "i2c_dev" ;
194 module2 = "i2c_bcm2708" ;
195 file1 = "/dev/i2c-0" ;
196 file2 = "/dev/i2c-1" ;
198 sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
200 _doLoadUsage (argv) ;
203 _doLoadUsage (argv) ;
205 if (!moduleLoaded (module1))
207 sprintf (cmd, "modprobe %s%s", module1, args1) ;
211 if (!moduleLoaded (module2))
213 sprintf (cmd, "modprobe %s%s", module2, args2) ;
217 if (!moduleLoaded (module2))
219 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
223 sleep (1) ; // To let things get settled
225 changeOwner (argv [0], file1) ;
226 changeOwner (argv [0], file2) ;
232 * Run the i2cdetect command with the right runes for this Pi revision
233 *********************************************************************************
236 static void doI2Cdetect (int argc, char *argv [])
238 int port = piBoardRev () == 1 ? 0 : 1 ;
240 struct stat statBuf ;
242 if (stat (I2CDETECT, &statBuf) < 0)
244 fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
248 if (!moduleLoaded ("i2c_dev"))
250 fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
254 sprintf (command, "%s -y %d", I2CDETECT, port) ;
255 if (system (command) < 0)
256 fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
263 * List all GPIO exports
264 *********************************************************************************
267 static void doExports (int argc, char *argv [])
274 for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
277 // Try to read the direction
279 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
280 if ((fd = open (fName, O_RDONLY)) == -1)
286 printf ("GPIO Pins exported:\n") ;
289 printf ("%4d: ", i) ;
291 if ((l = read (fd, buf, 16)) == 0)
292 sprintf (buf, "%s", "?") ;
295 if ((buf [strlen (buf) - 1]) == '\n')
296 buf [strlen (buf) - 1] = 0 ;
298 printf ("%-3s", buf) ;
302 // Try to Read the value
304 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
305 if ((fd = open (fName, O_RDONLY)) == -1)
307 printf ("No Value file (huh?)\n") ;
311 if ((l = read (fd, buf, 16)) == 0)
312 sprintf (buf, "%s", "?") ;
315 if ((buf [strlen (buf) - 1]) == '\n')
316 buf [strlen (buf) - 1] = 0 ;
318 printf (" %s", buf) ;
320 // Read any edge trigger file
322 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
323 if ((fd = open (fName, O_RDONLY)) == -1)
329 if ((l = read (fd, buf, 16)) == 0)
330 sprintf (buf, "%s", "?") ;
333 if ((buf [strlen (buf) - 1]) == '\n')
334 buf [strlen (buf) - 1] = 0 ;
336 printf (" %-8s\n", buf) ;
345 * gpio export pin mode
346 * This uses the /sys/class/gpio device interface.
347 *********************************************************************************
350 void doExport (int argc, char *argv [])
359 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
363 pin = atoi (argv [2]) ;
367 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
369 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
373 fprintf (fd, "%d\n", pin) ;
376 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
377 if ((fd = fopen (fName, "w")) == NULL)
379 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
383 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
384 fprintf (fd, "in\n") ;
385 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
386 fprintf (fd, "out\n") ;
389 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
395 // Change ownership so the current user can actually use it!
397 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
398 changeOwner (argv [0], fName) ;
400 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
401 changeOwner (argv [0], fName) ;
409 * Wait for Interrupt on a given pin.
410 * Slight cheat here - it's easier to actually use ISR now (which calls
411 * gpio to set the pin modes!) then we simply sleep, and expect the thread
412 * to exit the program. Crude but effective.
413 *********************************************************************************
416 static void wfi (void)
419 void doWfi (int argc, char *argv [])
425 fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
429 pin = atoi (argv [2]) ;
431 /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
432 else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
433 else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
436 fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
440 if (wiringPiISR (pin, mode, &wfi) < 0)
442 fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
455 * Easy access to changing the edge trigger on a GPIO pin
456 * This uses the /sys/class/gpio device interface.
457 *********************************************************************************
460 void doEdge (int argc, char *argv [])
469 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
473 pin = atoi (argv [2]) ;
476 // Export the pin and set direction to input
478 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
480 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
484 fprintf (fd, "%d\n", pin) ;
487 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
488 if ((fd = fopen (fName, "w")) == NULL)
490 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
494 fprintf (fd, "in\n") ;
497 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
498 if ((fd = fopen (fName, "w")) == NULL)
500 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
504 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
505 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
506 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
507 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
510 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
514 // Change ownership of the value and edge files, so the current user can actually use it!
516 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
517 changeOwner (argv [0], fName) ;
519 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
520 changeOwner (argv [0], fName) ;
529 * This uses the /sys/class/gpio device interface.
530 *********************************************************************************
533 void doUnexport (int argc, char *argv [])
540 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
544 pin = atoi (argv [2]) ;
546 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
548 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
552 fprintf (fd, "%d\n", pin) ;
560 * Un-Export all the GPIO pins.
561 * This uses the /sys/class/gpio device interface.
562 *********************************************************************************
565 void doUnexportall (char *progName)
570 for (pin = 0 ; pin < 63 ; ++pin)
572 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
574 fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
577 fprintf (fd, "%d\n", pin) ;
585 * Load readallExternal, we try to do this with an external device.
586 *********************************************************************************
589 static void doResetExternal (void)
593 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
595 pinMode (pin, INPUT) ;
596 pullUpDnControl (pin, PUD_OFF) ;
603 * Reset the GPIO pins - as much as we can do
604 *********************************************************************************
607 static void doReset (char *progName)
611 if (wiringPiNodes != NULL) // External reset
615 doUnexportall (progName) ;
617 for (pin = 0 ; pin < 64 ; ++pin)
619 if (wpiPinToGpio (pin) == -1)
622 digitalWrite (pin, LOW) ;
623 pinMode (pin, INPUT) ;
624 pullUpDnControl (pin, PUD_OFF) ;
632 * gpio mode pin mode ...
633 *********************************************************************************
636 void doMode (int argc, char *argv [])
643 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
647 pin = atoi (argv [2]) ;
651 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
652 else if (strcasecmp (mode, "input") == 0) pinMode (pin, INPUT) ;
653 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
654 else if (strcasecmp (mode, "output") == 0) pinMode (pin, OUTPUT) ;
655 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
656 else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
657 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
658 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
659 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
660 else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ;
664 else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ;
665 else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ;
666 else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ;
667 else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ;
668 else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ;
669 else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ;
672 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
680 * gpio drive group value
681 *********************************************************************************
684 static void doPadDrive (int argc, char *argv [])
690 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
694 group = atoi (argv [2]) ;
695 val = atoi (argv [3]) ;
697 if ((group < 0) || (group > 2))
699 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
703 if ((val < 0) || (val > 7))
705 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
709 setPadDrive (group, val) ;
715 * gpio gbw channel value
716 * Gertboard Write - To the Analog output
717 *********************************************************************************
720 static void doGbw (int argc, char *argv [])
726 fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
730 channel = atoi (argv [2]) ;
731 value = atoi (argv [3]) ;
733 if ((channel < 0) || (channel > 1))
735 fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
739 if ((value < 0) || (value > 1023))
741 fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
745 if (gertboardAnalogSetup (64) < 0)
747 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
751 analogWrite (64 + channel, value) ;
758 * From the analog input
759 *********************************************************************************
762 static void doGbr (int argc, char *argv [])
768 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
772 channel = atoi (argv [2]) ;
774 if ((channel < 0) || (channel > 1))
776 fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
780 if (gertboardAnalogSetup (64) < 0)
782 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
786 printf ("%d\n", analogRead (64 + channel)) ;
792 * gpio write pin value
793 *********************************************************************************
796 static void doWrite (int argc, char *argv [])
802 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
806 pin = atoi (argv [2]) ;
808 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
810 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
813 val = atoi (argv [3]) ;
816 digitalWrite (pin, LOW) ;
818 digitalWrite (pin, HIGH) ;
824 * gpio awrite pin value
825 *********************************************************************************
828 static void doAwrite (int argc, char *argv [])
834 fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
838 pin = atoi (argv [2]) ;
840 val = atoi (argv [3]) ;
842 analogWrite (pin, val) ;
849 *********************************************************************************
852 static void doWriteByte (int argc, char *argv [])
858 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
862 val = (int)strtol (argv [2], NULL, 0) ;
864 digitalWriteByte (val) ;
870 * Read a pin and return the value
871 *********************************************************************************
874 void doRead (int argc, char *argv [])
880 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
884 pin = atoi (argv [2]) ;
885 val = digitalRead (pin) ;
887 printf ("%s\n", val == 0 ? "0" : "1") ;
893 * Read an analog pin and return the value
894 *********************************************************************************
897 void doAread (int argc, char *argv [])
901 fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
905 printf ("%d\n", analogRead (atoi (argv [2]))) ;
912 *********************************************************************************
915 void doToggle (int argc, char *argv [])
921 fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
925 pin = atoi (argv [2]) ;
927 digitalWrite (pin, !digitalRead (pin)) ;
932 * Output a clock on a pin
933 *********************************************************************************
936 void doClock (int argc, char *argv [])
942 fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
946 pin = atoi (argv [2]) ;
948 freq = atoi (argv [3]) ;
950 gpioClockSet (pin, freq) ;
956 * Output a PWM value on a pin
957 *********************************************************************************
960 void doPwm (int argc, char *argv [])
966 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
970 pin = atoi (argv [2]) ;
972 val = atoi (argv [3]) ;
974 pwmWrite (pin, val) ;
979 * doPwmMode: doPwmRange: doPwmClock:
980 * Change the PWM mode, range and clock divider values
981 *********************************************************************************
984 static void doPwmMode (int mode)
989 static void doPwmRange (int argc, char *argv [])
995 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
999 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
1003 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
1007 pwmSetRange (range) ;
1010 static void doPwmClock (int argc, char *argv [])
1012 unsigned int clock ;
1016 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
1020 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
1022 if ((clock < 1) || (clock > 4095))
1024 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
1028 pwmSetClock (clock) ;
1035 *********************************************************************************
1038 int main (int argc, char *argv [])
1042 if (getenv ("WIRINGPI_DEBUG") != NULL)
1044 printf ("gpio: wiringPi debug mode enabled\n") ;
1045 wiringPiDebug = TRUE ;
1050 fprintf (stderr, "%s\n", usage) ;
1056 if (strcasecmp (argv [1], "-h") == 0)
1058 printf ("%s: %s\n", argv [0], usage) ;
1062 // Sort of a special:
1064 if (strcmp (argv [1], "-R") == 0)
1066 printf ("%d\n", piBoardRev ()) ;
1070 // Version & Warranty
1072 if (strcmp (argv [1], "-V") == 0)
1074 printf ("%d\n", piBoardRev ()) ;
1078 if (strcmp (argv [1], "-v") == 0)
1080 printf ("gpio version: %s\n", VERSION) ;
1081 printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
1082 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
1083 printf ("For details type: %s -warranty\n", argv [0]) ;
1085 printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
1089 if (strcasecmp (argv [1], "-warranty") == 0)
1091 printf ("gpio version: %s\n", VERSION) ;
1092 printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
1094 printf (" This program is free software; you can redistribute it and/or modify\n") ;
1095 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
1096 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
1097 printf (" (at your option) any later version.\n") ;
1099 printf (" This program is distributed in the hope that it will be useful,\n") ;
1100 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
1101 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
1102 printf (" GNU Lesser General Public License for more details.\n") ;
1104 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
1105 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
1110 if (geteuid () != 0)
1112 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
1116 // Initial test for /sys/class/gpio operations:
1118 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
1119 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
1120 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
1121 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
1122 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
1124 // Check for load command:
1126 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
1128 // Gertboard commands
1130 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
1131 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
1133 // Check for -g argument
1135 /**/ if (strcasecmp (argv [1], "-g") == 0)
1137 wiringPiSetupGpio () ;
1139 for (i = 2 ; i < argc ; ++i)
1140 argv [i - 1] = argv [i] ;
1142 wpMode = WPI_MODE_GPIO ;
1145 // Check for -1 argument
1147 else if (strcasecmp (argv [1], "-1") == 0)
1149 wiringPiSetupPhys () ;
1151 for (i = 2 ; i < argc ; ++i)
1152 argv [i - 1] = argv [i] ;
1154 wpMode = WPI_MODE_PHYS ;
1157 // Check for -p argument for PiFace
1159 else if (strcasecmp (argv [1], "-p") == 0)
1163 for (i = 2 ; i < argc ; ++i)
1164 argv [i - 1] = argv [i] ;
1166 wpMode = WPI_MODE_PIFACE ;
1169 // Default to wiringPi mode
1174 wpMode = WPI_MODE_PINS ;
1177 // Check for -x argument to load in a new extension
1179 if (strcasecmp (argv [1], "-x") == 0)
1183 fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
1184 exit (EXIT_FAILURE) ;
1187 if (!doExtension (argv [0], argv [2])) // Prints its own error messages
1188 exit (EXIT_FAILURE) ;
1190 for (i = 3 ; i < argc ; ++i)
1191 argv [i - 2] = argv [i] ;
1197 fprintf (stderr, "%s: no command given\n", argv [0]) ;
1198 exit (EXIT_FAILURE) ;
1201 // Core wiringPi functions
1203 /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
1204 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
1205 else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
1206 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
1207 else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
1208 else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
1212 else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
1216 else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
1217 else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
1218 else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
1219 else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
1220 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
1221 else if (strcasecmp (argv [1], "readall" ) == 0) doReadallOld () ;
1222 else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall () ;
1223 else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
1224 else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
1225 else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
1226 else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
1227 else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
1228 else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
1231 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
1232 exit (EXIT_FAILURE) ;