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 ;
51 #define VERSION "2.07"
52 #define I2CDETECT "/usr/sbin/i2cdetect"
56 char *usage = "Usage: gpio -v\n"
58 " gpio [-g|-1] [-x extension:params] ...\n"
59 " gpio [-p] <read/write/wb> ...\n"
60 " gpio <read/write/aread/awritewb/pwm/clock/mode> ...\n"
61 " gpio readall/reset\n"
62 " gpio unexportall/exports\n"
63 " gpio export/edge/unexport ...\n"
64 " gpio wfi <pin> <mode>\n"
65 " gpio drive <group> <value>\n"
66 " gpio pwm-bal/pwm-ms \n"
67 " gpio pwmr <range> \n"
68 " gpio pwmc <divider> \n"
69 " gpio load spi/i2c\n"
70 " gpio i2cd/i2cdetect\n"
71 " gpio gbr <channel>\n"
72 " gpio gbw <channel> <value>" ; // No trailing newline needed here.
77 * Change the ownership of the file to the real userId of the calling
78 * program so we can access it.
79 *********************************************************************************
82 static void changeOwner (char *cmd, char *file)
84 uid_t uid = getuid () ;
85 uid_t gid = getgid () ;
87 if (chown (file, uid, gid) != 0)
89 if (errno == ENOENT) // Warn that it's not there
90 fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
93 fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
102 * Return true/false if the supplied module is loaded
103 *********************************************************************************
106 static int moduleLoaded (char *modName)
108 int len = strlen (modName) ;
110 FILE *fd = fopen ("/proc/modules", "r") ;
115 fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
119 while (fgets (line, 80, fd) != NULL)
121 if (strncmp (line, modName, len) != 0)
136 * Load either the spi or i2c modules and change device ownerships, etc.
137 *********************************************************************************
140 static void _doLoadUsage (char *argv [])
142 fprintf (stderr, "Usage: %s load <spi/i2c> [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ;
146 static void doLoad (int argc, char *argv [])
148 char *module1, *module2 ;
150 char *file1, *file2 ;
151 char args1 [32], args2 [32] ;
154 _doLoadUsage (argv) ;
156 args1 [0] = args2 [0] = 0 ;
158 /**/ if (strcasecmp (argv [2], "spi") == 0)
161 module2 = "spi_bcm2708" ;
162 file1 = "/dev/spidev0.0" ;
163 file2 = "/dev/spidev0.1" ;
165 sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
167 _doLoadUsage (argv) ;
169 else if (strcasecmp (argv [2], "i2c") == 0)
171 module1 = "i2c_dev" ;
172 module2 = "i2c_bcm2708" ;
173 file1 = "/dev/i2c-0" ;
174 file2 = "/dev/i2c-1" ;
176 sprintf (args2, " baudrate=%d", atoi (argv [3]) * 1000) ;
178 _doLoadUsage (argv) ;
181 _doLoadUsage (argv) ;
183 if (!moduleLoaded (module1))
185 sprintf (cmd, "modprobe %s%s", module1, args1) ;
189 if (!moduleLoaded (module2))
191 sprintf (cmd, "modprobe %s%s", module2, args2) ;
195 if (!moduleLoaded (module2))
197 fprintf (stderr, "%s: Unable to load %s\n", argv [0], module2) ;
201 sleep (1) ; // To let things get settled
203 changeOwner (argv [0], file1) ;
204 changeOwner (argv [0], file2) ;
210 * Run the i2cdetect command with the right runes for this Pi revision
211 *********************************************************************************
214 static void doI2Cdetect (int argc, char *argv [])
216 int port = piBoardRev () == 1 ? 0 : 1 ;
218 struct stat statBuf ;
220 if (stat (I2CDETECT, &statBuf) < 0)
222 fprintf (stderr, "%s: Unable to find i2cdetect command: %s\n", argv [0], strerror (errno)) ;
226 if (!moduleLoaded ("i2c_dev"))
228 fprintf (stderr, "%s: The I2C kernel module(s) are not loaded.\n", argv [0]) ;
232 sprintf (command, "%s -y %d", I2CDETECT, port) ;
233 if (system (command) < 0)
234 fprintf (stderr, "%s: Unable to run i2cdetect: %s\n", argv [0], strerror (errno)) ;
241 * Read all the GPIO pins
242 * We also want to use this to read the state of pins on an externally
243 * connected device, so we need to do some fiddling with the internal
244 * wiringPi node structures - since the gpio command can only use
245 * one external device at a time, we'll use that to our advantage...
246 *********************************************************************************
249 static char *pinNames [] =
251 "GPIO 0", "GPIO 1", "GPIO 2", "GPIO 3", "GPIO 4", "GPIO 5", "GPIO 6", "GPIO 7",
253 "CE0 ", "CE1 ", "MOSI ", "MISO ", "SCLK ",
255 "GPIO 8", "GPIO 9", "GPIO10", "GPIO11",
258 static char *alts [] =
260 "IN ", "OUT ", "ALT5", "ALT4", "ALT0", "ALT1", "ALT2", "ALT3"
263 static int wpiToPhys [64] =
265 11, 12, 13, 15, 16, 18, 22, 7, // 0...7
267 24, 26, 19, 21, 23, // 10..14
269 3, 4, 5, 6, // 17..20
270 0,0,0,0,0,0,0,0,0,0,0, // 20..31
271 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32..47
272 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 47..63
278 * A relatively crude way to read the pins on an external device.
279 * We don't know the input/output mode of pins, but we can tell
280 * if it's an analog pin or a digital one...
281 *********************************************************************************
284 static void doReadallExternal (void)
288 printf ("+------+---------+--------+\n") ;
289 printf ("| Pin | Digital | Analog |\n") ;
290 printf ("+------+---------+--------+\n") ;
292 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
293 printf ("| %4d | %4d | %4d |\n", pin, digitalRead (pin), analogRead (pin)) ;
295 printf ("+------+---------+--------+\n") ;
299 static void doReadall (void)
303 if (wiringPiNodes != NULL) // External readall
304 doReadallExternal () ;
307 printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
308 printf ("| wiringPi | GPIO | Phys | Name | Mode | Value |\n") ;
309 printf ("+----------+------+------+--------+------+-------+\n") ;
311 for (pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
313 if (wpiPinToGpio (pin) == -1)
316 printf ("| %6d | %3d | %3d | %s | %s | %s |\n",
317 pin, wpiPinToGpio (pin), wpiToPhys [pin],
320 digitalRead (pin) == HIGH ? "High" : "Low ") ;
323 printf ("+----------+------+------+--------+------+-------+\n") ;
330 * List all GPIO exports
331 *********************************************************************************
334 static void doExports (int argc, char *argv [])
341 for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective
344 // Try to read the direction
346 sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
347 if ((fd = open (fName, O_RDONLY)) == -1)
353 printf ("GPIO Pins exported:\n") ;
356 printf ("%4d: ", i) ;
358 if ((l = read (fd, buf, 16)) == 0)
359 sprintf (buf, "%s", "?") ;
362 if ((buf [strlen (buf) - 1]) == '\n')
363 buf [strlen (buf) - 1] = 0 ;
365 printf ("%-3s", buf) ;
369 // Try to Read the value
371 sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
372 if ((fd = open (fName, O_RDONLY)) == -1)
374 printf ("No Value file (huh?)\n") ;
378 if ((l = read (fd, buf, 16)) == 0)
379 sprintf (buf, "%s", "?") ;
382 if ((buf [strlen (buf) - 1]) == '\n')
383 buf [strlen (buf) - 1] = 0 ;
385 printf (" %s", buf) ;
387 // Read any edge trigger file
389 sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
390 if ((fd = open (fName, O_RDONLY)) == -1)
396 if ((l = read (fd, buf, 16)) == 0)
397 sprintf (buf, "%s", "?") ;
400 if ((buf [strlen (buf) - 1]) == '\n')
401 buf [strlen (buf) - 1] = 0 ;
403 printf (" %-8s\n", buf) ;
412 * gpio export pin mode
413 * This uses the /sys/class/gpio device interface.
414 *********************************************************************************
417 void doExport (int argc, char *argv [])
426 fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
430 pin = atoi (argv [2]) ;
434 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
436 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
440 fprintf (fd, "%d\n", pin) ;
443 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
444 if ((fd = fopen (fName, "w")) == NULL)
446 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
450 /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
451 fprintf (fd, "in\n") ;
452 else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
453 fprintf (fd, "out\n") ;
456 fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
462 // Change ownership so the current user can actually use it!
464 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
465 changeOwner (argv [0], fName) ;
467 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
468 changeOwner (argv [0], fName) ;
476 * Wait for Interrupt on a given pin.
477 * Slight cheat here - it's easier to actually use ISR now (which calls
478 * gpio to set the pin modes!) then we simply sleep, and expect the thread
479 * to exit the program. Crude but effective.
480 *********************************************************************************
483 static void wfi (void)
486 void doWfi (int argc, char *argv [])
492 fprintf (stderr, "Usage: %s wfi pin mode\n", argv [0]) ;
496 pin = atoi (argv [2]) ;
498 /**/ if (strcasecmp (argv [3], "rising") == 0) mode = INT_EDGE_RISING ;
499 else if (strcasecmp (argv [3], "falling") == 0) mode = INT_EDGE_FALLING ;
500 else if (strcasecmp (argv [3], "both") == 0) mode = INT_EDGE_BOTH ;
503 fprintf (stderr, "%s: wfi: Invalid mode: %s. Should be rising, falling or both\n", argv [1], argv [3]) ;
507 if (wiringPiISR (pin, mode, &wfi) < 0)
509 fprintf (stderr, "%s: wfi: Unable to setup ISR: %s\n", argv [1], strerror (errno)) ;
522 * Easy access to changing the edge trigger on a GPIO pin
523 * This uses the /sys/class/gpio device interface.
524 *********************************************************************************
527 void doEdge (int argc, char *argv [])
536 fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
540 pin = atoi (argv [2]) ;
543 // Export the pin and set direction to input
545 if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
547 fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
551 fprintf (fd, "%d\n", pin) ;
554 sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
555 if ((fd = fopen (fName, "w")) == NULL)
557 fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
561 fprintf (fd, "in\n") ;
564 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
565 if ((fd = fopen (fName, "w")) == NULL)
567 fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
571 /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
572 else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
573 else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
574 else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
577 fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
581 // Change ownership of the value and edge files, so the current user can actually use it!
583 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
584 changeOwner (argv [0], fName) ;
586 sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
587 changeOwner (argv [0], fName) ;
596 * This uses the /sys/class/gpio device interface.
597 *********************************************************************************
600 void doUnexport (int argc, char *argv [])
607 fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
611 pin = atoi (argv [2]) ;
613 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
615 fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
619 fprintf (fd, "%d\n", pin) ;
627 * Un-Export all the GPIO pins.
628 * This uses the /sys/class/gpio device interface.
629 *********************************************************************************
632 void doUnexportall (char *progName)
637 for (pin = 0 ; pin < 63 ; ++pin)
639 if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
641 fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
644 fprintf (fd, "%d\n", pin) ;
652 * Load readallExternal, we try to do this with an external device.
653 *********************************************************************************
656 static void doResetExternal (void)
660 for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
662 pinMode (pin, INPUT) ;
663 pullUpDnControl (pin, PUD_OFF) ;
670 * Reset the GPIO pins - as much as we can do
671 *********************************************************************************
674 static void doReset (char *progName)
678 if (wiringPiNodes != NULL) // External reset
682 doUnexportall (progName) ;
684 for (pin = 0 ; pin < 64 ; ++pin)
686 if (wpiPinToGpio (pin) == -1)
689 digitalWrite (pin, LOW) ;
690 pinMode (pin, INPUT) ;
691 pullUpDnControl (pin, PUD_OFF) ;
699 * gpio mode pin mode ...
700 *********************************************************************************
703 void doMode (int argc, char *argv [])
710 fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
714 pin = atoi (argv [2]) ;
718 /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
719 else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
720 else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
721 else if (strcasecmp (mode, "clock") == 0) pinMode (pin, GPIO_CLOCK) ;
722 else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
723 else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
724 else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
727 fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ;
735 * gpio drive group value
736 *********************************************************************************
739 static void doPadDrive (int argc, char *argv [])
745 fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
749 group = atoi (argv [2]) ;
750 val = atoi (argv [3]) ;
752 if ((group < 0) || (group > 2))
754 fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
758 if ((val < 0) || (val > 7))
760 fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
764 setPadDrive (group, val) ;
770 * gpio gbw channel value
771 * Gertboard Write - To the Analog output
772 *********************************************************************************
775 static void doGbw (int argc, char *argv [])
781 fprintf (stderr, "Usage: %s gbw <channel> <value>\n", argv [0]) ;
785 channel = atoi (argv [2]) ;
786 value = atoi (argv [3]) ;
788 if ((channel < 0) || (channel > 1))
790 fprintf (stderr, "%s: gbw: Channel number must be 0 or 1\n", argv [0]) ;
794 if ((value < 0) || (value > 1023))
796 fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
800 if (gertboardAnalogSetup (64) < 0)
802 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
806 analogWrite (64 + channel, value) ;
813 * From the analog input
814 *********************************************************************************
817 static void doGbr (int argc, char *argv [])
823 fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
827 channel = atoi (argv [2]) ;
829 if ((channel < 0) || (channel > 1))
831 fprintf (stderr, "%s: gbr: Channel number must be 0 or 1\n", argv [0]) ;
835 if (gertboardAnalogSetup (64) < 0)
837 fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
841 printf ("%d\n", analogRead (64 + channel)) ;
847 * gpio write pin value
848 *********************************************************************************
851 static void doWrite (int argc, char *argv [])
857 fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
861 pin = atoi (argv [2]) ;
863 /**/ if ((strcasecmp (argv [3], "up") == 0) || (strcasecmp (argv [3], "on") == 0))
865 else if ((strcasecmp (argv [3], "down") == 0) || (strcasecmp (argv [3], "off") == 0))
868 val = atoi (argv [3]) ;
871 digitalWrite (pin, LOW) ;
873 digitalWrite (pin, HIGH) ;
879 * gpio awrite pin value
880 *********************************************************************************
883 static void doAwrite (int argc, char *argv [])
889 fprintf (stderr, "Usage: %s awrite pin value\n", argv [0]) ;
893 pin = atoi (argv [2]) ;
895 val = atoi (argv [3]) ;
897 analogWrite (pin, val) ;
904 *********************************************************************************
907 static void doWriteByte (int argc, char *argv [])
913 fprintf (stderr, "Usage: %s wb value\n", argv [0]) ;
917 val = (int)strtol (argv [2], NULL, 0) ;
919 digitalWriteByte (val) ;
925 * Read a pin and return the value
926 *********************************************************************************
929 void doRead (int argc, char *argv [])
935 fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
939 pin = atoi (argv [2]) ;
940 val = digitalRead (pin) ;
942 printf ("%s\n", val == 0 ? "0" : "1") ;
948 * Read an analog pin and return the value
949 *********************************************************************************
952 void doAread (int argc, char *argv [])
956 fprintf (stderr, "Usage: %s aread pin\n", argv [0]) ;
960 printf ("%d\n", analogRead (atoi (argv [2]))) ;
967 *********************************************************************************
970 void doToggle (int argc, char *argv [])
976 fprintf (stderr, "Usage: %s toggle pin\n", argv [0]) ;
980 pin = atoi (argv [2]) ;
982 digitalWrite (pin, !digitalRead (pin)) ;
987 * Output a clock on a pin
988 *********************************************************************************
991 void doClock (int argc, char *argv [])
997 fprintf (stderr, "Usage: %s clock <pin> <freq>\n", argv [0]) ;
1001 pin = atoi (argv [2]) ;
1003 freq = atoi (argv [3]) ;
1005 gpioClockSet (pin, freq) ;
1011 * Output a PWM value on a pin
1012 *********************************************************************************
1015 void doPwm (int argc, char *argv [])
1021 fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
1025 pin = atoi (argv [2]) ;
1027 val = atoi (argv [3]) ;
1029 pwmWrite (pin, val) ;
1034 * doPwmMode: doPwmRange: doPwmClock:
1035 * Change the PWM mode, range and clock divider values
1036 *********************************************************************************
1039 static void doPwmMode (int mode)
1044 static void doPwmRange (int argc, char *argv [])
1046 unsigned int range ;
1050 fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
1054 range = (unsigned int)strtoul (argv [2], NULL, 10) ;
1058 fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
1062 pwmSetRange (range) ;
1065 static void doPwmClock (int argc, char *argv [])
1067 unsigned int clock ;
1071 fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
1075 clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
1077 if ((clock < 1) || (clock > 4095))
1079 fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
1083 pwmSetClock (clock) ;
1090 *********************************************************************************
1093 int main (int argc, char *argv [])
1097 if (getenv ("WIRINGPI_DEBUG") != NULL)
1099 printf ("gpio: wiringPi debug mode enabled\n") ;
1100 wiringPiDebug = TRUE ;
1105 fprintf (stderr, "%s\n", usage) ;
1111 if (strcasecmp (argv [1], "-h") == 0)
1113 printf ("%s: %s\n", argv [0], usage) ;
1117 // Sort of a special:
1119 if (strcmp (argv [1], "-R") == 0)
1121 printf ("%d\n", piBoardRev ()) ;
1125 // Version & Warranty
1127 if (strcmp (argv [1], "-V") == 0)
1129 printf ("%d\n", piBoardRev ()) ;
1133 if (strcmp (argv [1], "-v") == 0)
1135 printf ("gpio version: %s\n", VERSION) ;
1136 printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
1137 printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
1138 printf ("For details type: %s -warranty\n", argv [0]) ;
1140 printf ("This Raspberry Pi is a revision %d board.\n", piBoardRev ()) ;
1144 if (strcasecmp (argv [1], "-warranty") == 0)
1146 printf ("gpio version: %s\n", VERSION) ;
1147 printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
1149 printf (" This program is free software; you can redistribute it and/or modify\n") ;
1150 printf (" it under the terms of the GNU Leser General Public License as published\n") ;
1151 printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
1152 printf (" (at your option) any later version.\n") ;
1154 printf (" This program is distributed in the hope that it will be useful,\n") ;
1155 printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
1156 printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
1157 printf (" GNU Lesser General Public License for more details.\n") ;
1159 printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
1160 printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
1165 if (geteuid () != 0)
1167 fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
1171 // Initial test for /sys/class/gpio operations:
1173 /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
1174 else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
1175 else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
1176 else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
1177 else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argv [0]) ; return 0 ; }
1179 // Check for load command:
1181 if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
1183 // Gertboard commands
1185 if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
1186 if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
1188 // Check for -g argument
1190 /**/ if (strcasecmp (argv [1], "-g") == 0)
1192 wiringPiSetupGpio () ;
1194 for (i = 2 ; i < argc ; ++i)
1195 argv [i - 1] = argv [i] ;
1197 wpMode = WPI_MODE_GPIO ;
1200 // Check for -1 argument
1202 else if (strcasecmp (argv [1], "-1") == 0)
1204 wiringPiSetupPhys () ;
1206 for (i = 2 ; i < argc ; ++i)
1207 argv [i - 1] = argv [i] ;
1209 wpMode = WPI_MODE_PHYS ;
1212 // Check for -p argument for PiFace
1214 else if (strcasecmp (argv [1], "-p") == 0)
1218 for (i = 2 ; i < argc ; ++i)
1219 argv [i - 1] = argv [i] ;
1221 wpMode = WPI_MODE_PIFACE ;
1224 // Default to wiringPi mode
1229 wpMode = WPI_MODE_PINS ;
1232 // Check for -x argument to load in a new extension
1234 if (strcasecmp (argv [1], "-x") == 0)
1238 fprintf (stderr, "%s: -x missing extension specification.\n", argv [0]) ;
1239 exit (EXIT_FAILURE) ;
1242 if (!doExtension (argv [0], argv [2])) // Prints its own error messages
1243 exit (EXIT_FAILURE) ;
1245 for (i = 3 ; i < argc ; ++i)
1246 argv [i - 2] = argv [i] ;
1252 fprintf (stderr, "%s: no command given\n", argv [0]) ;
1253 exit (EXIT_FAILURE) ;
1256 // Core wiringPi functions
1258 /**/ if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
1259 else if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
1260 else if (strcasecmp (argv [1], "write" ) == 0) doWrite (argc, argv) ;
1261 else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
1262 else if (strcasecmp (argv [1], "awrite" ) == 0) doAwrite (argc, argv) ;
1263 else if (strcasecmp (argv [1], "aread" ) == 0) doAread (argc, argv) ;
1267 else if (strcasecmp (argv [1], "toggle" ) == 0) doToggle (argc, argv) ;
1271 else if (strcasecmp (argv [1], "pwm-bal" ) == 0) doPwmMode (PWM_MODE_BAL) ;
1272 else if (strcasecmp (argv [1], "pwm-ms" ) == 0) doPwmMode (PWM_MODE_MS) ;
1273 else if (strcasecmp (argv [1], "pwmr" ) == 0) doPwmRange (argc, argv) ;
1274 else if (strcasecmp (argv [1], "pwmc" ) == 0) doPwmClock (argc, argv) ;
1275 else if (strcasecmp (argv [1], "drive" ) == 0) doPadDrive (argc, argv) ;
1276 else if (strcasecmp (argv [1], "readall" ) == 0) doReadall () ;
1277 else if (strcasecmp (argv [1], "i2cdetect") == 0) doI2Cdetect (argc, argv) ;
1278 else if (strcasecmp (argv [1], "i2cd" ) == 0) doI2Cdetect (argc, argv) ;
1279 else if (strcasecmp (argv [1], "reset" ) == 0) doReset (argv [0]) ;
1280 else if (strcasecmp (argv [1], "wb" ) == 0) doWriteByte (argc, argv) ;
1281 else if (strcasecmp (argv [1], "clock" ) == 0) doClock (argc, argv) ;
1282 else if (strcasecmp (argv [1], "wfi" ) == 0) doWfi (argc, argv) ;
1285 fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
1286 exit (EXIT_FAILURE) ;