You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gpio.c 20 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * gpio.c:
  3. * Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
  4. * Pi's GPIO.
  5. * 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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <sys/types.h>
  31. #include <fcntl.h>
  32. #include <wiringPi.h>
  33. #include <gertboard.h>
  34. #ifndef TRUE
  35. # define TRUE (1==1)
  36. # define FALSE (1==2)
  37. #endif
  38. #define VERSION "1.2"
  39. static int wpMode ;
  40. char *usage = "Usage: gpio -v\n"
  41. " gpio -h\n"
  42. " gpio [-g] <read/write/pwm/mode> ...\n"
  43. " gpio [-p] <read/write/mode> ...\n"
  44. " gpio export/edge/unexport/unexportall/exports ...\n"
  45. " gpio drive <group> <value>\n"
  46. " gpio pwm-bal/pwm-ms \n"
  47. " gpio pwmr <range> \n"
  48. " gpio pwmc <divider> \n"
  49. " gpio load spi/i2c\n"
  50. " gpio gbr <channel>\n"
  51. " gpio gbw <channel> <value>" ; // No trailing newline needed here.
  52. /*
  53. * changeOwner:
  54. * Change the ownership of the file to the real userId of the calling
  55. * program so we can access it.
  56. *********************************************************************************
  57. */
  58. static void changeOwner (char *cmd, char *file)
  59. {
  60. uid_t uid = getuid () ;
  61. uid_t gid = getgid () ;
  62. if (chown (file, uid, gid) != 0)
  63. {
  64. if (errno == ENOENT) // Warn that it's not there
  65. fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
  66. else
  67. {
  68. fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
  69. exit (1) ;
  70. }
  71. }
  72. }
  73. /*
  74. * moduleLoaded:
  75. * Return true/false if the supplied module is loaded
  76. *********************************************************************************
  77. */
  78. static int moduleLoaded (char *modName)
  79. {
  80. int len = strlen (modName) ;
  81. int found = FALSE ;
  82. FILE *fd = fopen ("/proc/modules", "r") ;
  83. char line [80] ;
  84. if (fd == NULL)
  85. {
  86. fprintf (stderr, "gpio: Unable to check modules: %s\n", strerror (errno)) ;
  87. exit (1) ;
  88. }
  89. while (fgets (line, 80, fd) != NULL)
  90. {
  91. if (strncmp (line, modName, len) != 0)
  92. continue ;
  93. found = TRUE ;
  94. break ;
  95. }
  96. fclose (fd) ;
  97. return found ;
  98. }
  99. /*
  100. * doLoad:
  101. * Load either the spi or i2c modules and change device ownerships, etc.
  102. *********************************************************************************
  103. */
  104. static void _doLoadUsage (char *argv [])
  105. {
  106. fprintf (stderr, "Usage: %s load <spi/i2c>\n", argv [0]) ;
  107. exit (1) ;
  108. }
  109. static void doLoad (int argc, char *argv [])
  110. {
  111. char *module ;
  112. char cmd [80] ;
  113. char *file1, *file2 ;
  114. if (argc != 3)
  115. _doLoadUsage (argv) ;
  116. /**/ if (strcasecmp (argv [2], "spi") == 0)
  117. {
  118. module = "spi_bcm2708" ;
  119. file1 = "/dev/spidev0.0" ;
  120. file2 = "/dev/spidev0.1" ;
  121. }
  122. else if (strcasecmp (argv [2], "i2c") == 0)
  123. {
  124. module = "i2c_bcm2708" ;
  125. file1 = "/dev/i2c-0" ;
  126. file2 = "/dev/i2c-1" ;
  127. }
  128. else
  129. _doLoadUsage (argv) ;
  130. if (!moduleLoaded (module))
  131. {
  132. sprintf (cmd, "modprobe %s", module) ;
  133. system (cmd) ;
  134. }
  135. if (!moduleLoaded (module))
  136. {
  137. fprintf (stderr, "%s: Unable to load %s\n", argv [0], module) ;
  138. exit (1) ;
  139. }
  140. sleep (1) ; // To let things get settled
  141. changeOwner (argv [0], file1) ;
  142. changeOwner (argv [0], file2) ;
  143. }
  144. /*
  145. * doExports:
  146. * List all GPIO exports
  147. *********************************************************************************
  148. */
  149. static void doExports (int argc, char *argv [])
  150. {
  151. int fd ;
  152. int i, l, first ;
  153. char fName [128] ;
  154. char buf [16] ;
  155. // Rather crude, but who knows what others are up to...
  156. for (first = 0, i = 0 ; i < 64 ; ++i)
  157. {
  158. // Try to read the direction
  159. sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ;
  160. if ((fd = open (fName, O_RDONLY)) == -1)
  161. continue ;
  162. if (first == 0)
  163. {
  164. ++first ;
  165. printf ("GPIO Pins exported:\n") ;
  166. }
  167. printf ("%4d: ", i) ;
  168. if ((l = read (fd, buf, 16)) == 0)
  169. sprintf (buf, "%s", "?") ;
  170. buf [l] = 0 ;
  171. if ((buf [strlen (buf) - 1]) == '\n')
  172. buf [strlen (buf) - 1] = 0 ;
  173. printf ("%-3s", buf) ;
  174. close (fd) ;
  175. // Try to Read the value
  176. sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ;
  177. if ((fd = open (fName, O_RDONLY)) == -1)
  178. {
  179. printf ("No Value file (huh?)\n") ;
  180. continue ;
  181. }
  182. if ((l = read (fd, buf, 16)) == 0)
  183. sprintf (buf, "%s", "?") ;
  184. buf [l] = 0 ;
  185. if ((buf [strlen (buf) - 1]) == '\n')
  186. buf [strlen (buf) - 1] = 0 ;
  187. printf (" %s", buf) ;
  188. // Read any edge trigger file
  189. sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ;
  190. if ((fd = open (fName, O_RDONLY)) == -1)
  191. {
  192. printf ("\n") ;
  193. continue ;
  194. }
  195. if ((l = read (fd, buf, 16)) == 0)
  196. sprintf (buf, "%s", "?") ;
  197. buf [l] = 0 ;
  198. if ((buf [strlen (buf) - 1]) == '\n')
  199. buf [strlen (buf) - 1] = 0 ;
  200. printf (" %-8s\n", buf) ;
  201. close (fd) ;
  202. }
  203. }
  204. /*
  205. * doExport:
  206. * gpio export pin mode
  207. * This uses the /sys/class/gpio device interface.
  208. *********************************************************************************
  209. */
  210. void doExport (int argc, char *argv [])
  211. {
  212. FILE *fd ;
  213. int pin ;
  214. char *mode ;
  215. char fName [128] ;
  216. if (argc != 4)
  217. {
  218. fprintf (stderr, "Usage: %s export pin mode\n", argv [0]) ;
  219. exit (1) ;
  220. }
  221. pin = atoi (argv [2]) ;
  222. mode = argv [3] ;
  223. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  224. {
  225. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  226. exit (1) ;
  227. }
  228. fprintf (fd, "%d\n", pin) ;
  229. fclose (fd) ;
  230. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  231. if ((fd = fopen (fName, "w")) == NULL)
  232. {
  233. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  234. exit (1) ;
  235. }
  236. /**/ if ((strcasecmp (mode, "in") == 0) || (strcasecmp (mode, "input") == 0))
  237. fprintf (fd, "in\n") ;
  238. else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
  239. fprintf (fd, "out\n") ;
  240. else
  241. {
  242. fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
  243. exit (1) ;
  244. }
  245. fclose (fd) ;
  246. // Change ownership so the current user can actually use it!
  247. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  248. changeOwner (argv [0], fName) ;
  249. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  250. changeOwner (argv [0], fName) ;
  251. }
  252. /*
  253. * doEdge:
  254. * gpio edge pin mode
  255. * Easy access to changing the edge trigger on a GPIO pin
  256. * This uses the /sys/class/gpio device interface.
  257. *********************************************************************************
  258. */
  259. void doEdge (int argc, char *argv [])
  260. {
  261. FILE *fd ;
  262. int pin ;
  263. char *mode ;
  264. char fName [128] ;
  265. if (argc != 4)
  266. {
  267. fprintf (stderr, "Usage: %s edge pin mode\n", argv [0]) ;
  268. exit (1) ;
  269. }
  270. pin = atoi (argv [2]) ;
  271. mode = argv [3] ;
  272. // Export the pin and set direction to input
  273. if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
  274. {
  275. fprintf (stderr, "%s: Unable to open GPIO export interface: %s\n", argv [0], strerror (errno)) ;
  276. exit (1) ;
  277. }
  278. fprintf (fd, "%d\n", pin) ;
  279. fclose (fd) ;
  280. sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
  281. if ((fd = fopen (fName, "w")) == NULL)
  282. {
  283. fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  284. exit (1) ;
  285. }
  286. fprintf (fd, "in\n") ;
  287. fclose (fd) ;
  288. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  289. if ((fd = fopen (fName, "w")) == NULL)
  290. {
  291. fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
  292. exit (1) ;
  293. }
  294. /**/ if (strcasecmp (mode, "none") == 0) fprintf (fd, "none\n") ;
  295. else if (strcasecmp (mode, "rising") == 0) fprintf (fd, "rising\n") ;
  296. else if (strcasecmp (mode, "falling") == 0) fprintf (fd, "falling\n") ;
  297. else if (strcasecmp (mode, "both") == 0) fprintf (fd, "both\n") ;
  298. else
  299. {
  300. fprintf (stderr, "%s: Invalid mode: %s. Should be none, rising, falling or both\n", argv [1], mode) ;
  301. exit (1) ;
  302. }
  303. // Change ownership of the value and edge files, so the current user can actually use it!
  304. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  305. changeOwner (argv [0], fName) ;
  306. sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ;
  307. changeOwner (argv [0], fName) ;
  308. fclose (fd) ;
  309. }
  310. /*
  311. * doUnexport:
  312. * gpio unexport pin
  313. * This uses the /sys/class/gpio device interface.
  314. *********************************************************************************
  315. */
  316. void doUnexport (int argc, char *argv [])
  317. {
  318. FILE *fd ;
  319. int pin ;
  320. if (argc != 3)
  321. {
  322. fprintf (stderr, "Usage: %s unexport pin\n", argv [0]) ;
  323. exit (1) ;
  324. }
  325. pin = atoi (argv [2]) ;
  326. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  327. {
  328. fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
  329. exit (1) ;
  330. }
  331. fprintf (fd, "%d\n", pin) ;
  332. fclose (fd) ;
  333. }
  334. /*
  335. * doUnexportAll:
  336. * gpio unexportall
  337. * Un-Export all the GPIO pins.
  338. * This uses the /sys/class/gpio device interface.
  339. *********************************************************************************
  340. */
  341. void doUnexportall (int argc, char *argv [])
  342. {
  343. FILE *fd ;
  344. int pin ;
  345. for (pin = 0 ; pin < 63 ; ++pin)
  346. {
  347. if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
  348. {
  349. fprintf (stderr, "%s: Unable to open GPIO export interface\n", argv [0]) ;
  350. exit (1) ;
  351. }
  352. fprintf (fd, "%d\n", pin) ;
  353. fclose (fd) ;
  354. }
  355. }
  356. /*
  357. * doMode:
  358. * gpio mode pin mode ...
  359. *********************************************************************************
  360. */
  361. void doMode (int argc, char *argv [])
  362. {
  363. int pin ;
  364. char *mode ;
  365. if (argc != 4)
  366. {
  367. fprintf (stderr, "Usage: %s mode pin mode\n", argv [0]) ;
  368. exit (1) ;
  369. }
  370. pin = atoi (argv [2]) ;
  371. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  372. return ;
  373. mode = argv [3] ;
  374. /**/ if (strcasecmp (mode, "in") == 0) pinMode (pin, INPUT) ;
  375. else if (strcasecmp (mode, "out") == 0) pinMode (pin, OUTPUT) ;
  376. else if (strcasecmp (mode, "pwm") == 0) pinMode (pin, PWM_OUTPUT) ;
  377. else if (strcasecmp (mode, "up") == 0) pullUpDnControl (pin, PUD_UP) ;
  378. else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ;
  379. else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ;
  380. else
  381. {
  382. fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/up/down/tri\n", argv [1], mode) ;
  383. exit (1) ;
  384. }
  385. }
  386. /*
  387. * doPadDrive:
  388. * gpio drive group value
  389. *********************************************************************************
  390. */
  391. static void doPadDrive (int argc, char *argv [])
  392. {
  393. int group, val ;
  394. if (argc != 4)
  395. {
  396. fprintf (stderr, "Usage: %s drive group value\n", argv [0]) ;
  397. exit (1) ;
  398. }
  399. group = atoi (argv [2]) ;
  400. val = atoi (argv [3]) ;
  401. if ((group < 0) || (group > 2))
  402. {
  403. fprintf (stderr, "%s: drive group not 0, 1 or 2: %d\n", argv [0], group) ;
  404. exit (1) ;
  405. }
  406. if ((val < 0) || (val > 7))
  407. {
  408. fprintf (stderr, "%s: drive value not 0-7: %d\n", argv [0], val) ;
  409. exit (1) ;
  410. }
  411. setPadDrive (group, val) ;
  412. }
  413. /*
  414. * doGbw:
  415. * gpio gbw channel value
  416. *********************************************************************************
  417. */
  418. static void doGbw (int argc, char *argv [])
  419. {
  420. int channel, value ;
  421. if (argc != 4)
  422. {
  423. fprintf (stderr, "Usage: %s gbr <channel> <value>\n", argv [0]) ;
  424. exit (1) ;
  425. }
  426. channel = atoi (argv [2]) ;
  427. value = atoi (argv [3]) ;
  428. if ((channel < 0) || (channel > 1))
  429. {
  430. fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
  431. exit (1) ;
  432. }
  433. if ((value < 0) || (value > 1023))
  434. {
  435. fprintf (stderr, "%s: value must be from 0 to 255\n", argv [0]) ;
  436. exit (1) ;
  437. }
  438. if (gertboardSPISetup () == -1)
  439. {
  440. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  441. exit (1) ;
  442. }
  443. gertboardAnalogWrite (channel, value) ;
  444. }
  445. /*
  446. * doGbr:
  447. * gpio gbr channel
  448. *********************************************************************************
  449. */
  450. static void doGbr (int argc, char *argv [])
  451. {
  452. int channel ;
  453. if (argc != 3)
  454. {
  455. fprintf (stderr, "Usage: %s gbr <channel>\n", argv [0]) ;
  456. exit (1) ;
  457. }
  458. channel = atoi (argv [2]) ;
  459. if ((channel < 0) || (channel > 1))
  460. {
  461. fprintf (stderr, "%s: channel must be 0 or 1\n", argv [0]) ;
  462. exit (1) ;
  463. }
  464. if (gertboardSPISetup () == -1)
  465. {
  466. fprintf (stderr, "Unable to initialise the Gertboard SPI interface: %s\n", strerror (errno)) ;
  467. exit (1) ;
  468. }
  469. printf ("%d\n",gertboardAnalogRead (channel)) ;
  470. }
  471. /*
  472. * doWrite:
  473. * gpio write pin value
  474. *********************************************************************************
  475. */
  476. static void doWrite (int argc, char *argv [])
  477. {
  478. int pin, val ;
  479. if (argc != 4)
  480. {
  481. fprintf (stderr, "Usage: %s write pin value\n", argv [0]) ;
  482. exit (1) ;
  483. }
  484. pin = atoi (argv [2]) ;
  485. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  486. return ;
  487. val = atoi (argv [3]) ;
  488. /**/ if (val == 0)
  489. digitalWrite (pin, LOW) ;
  490. else
  491. digitalWrite (pin, HIGH) ;
  492. }
  493. /*
  494. * doRead:
  495. * Read a pin and return the value
  496. *********************************************************************************
  497. */
  498. void doRead (int argc, char *argv [])
  499. {
  500. int pin, val ;
  501. if (argc != 3)
  502. {
  503. fprintf (stderr, "Usage: %s read pin\n", argv [0]) ;
  504. exit (1) ;
  505. }
  506. pin = atoi (argv [2]) ;
  507. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  508. {
  509. printf ("0\n") ;
  510. return ;
  511. }
  512. val = digitalRead (pin) ;
  513. printf ("%s\n", val == 0 ? "0" : "1") ;
  514. }
  515. /*
  516. * doPwm:
  517. * Output a PWM value on a pin
  518. *********************************************************************************
  519. */
  520. void doPwm (int argc, char *argv [])
  521. {
  522. int pin, val ;
  523. if (argc != 4)
  524. {
  525. fprintf (stderr, "Usage: %s pwm <pin> <value>\n", argv [0]) ;
  526. exit (1) ;
  527. }
  528. pin = atoi (argv [2]) ;
  529. if ((wpMode == WPI_MODE_PINS) && ((pin < 0) || (pin >= NUM_PINS)))
  530. return ;
  531. val = atoi (argv [3]) ;
  532. pwmWrite (pin, val) ;
  533. }
  534. /*
  535. * doPwmMode: doPwmRange: doPwmClock:
  536. * Change the PWM mode, range and clock divider values
  537. *********************************************************************************
  538. */
  539. static void doPwmMode (int mode)
  540. {
  541. pwmSetMode (mode) ;
  542. }
  543. static void doPwmRange (int argc, char *argv [])
  544. {
  545. unsigned int range ;
  546. if (argc != 3)
  547. {
  548. fprintf (stderr, "Usage: %s pwmr <range>\n", argv [0]) ;
  549. exit (1) ;
  550. }
  551. range = (unsigned int)strtoul (argv [2], NULL, 10) ;
  552. if (range == 0)
  553. {
  554. fprintf (stderr, "%s: range must be > 0\n", argv [0]) ;
  555. exit (1) ;
  556. }
  557. pwmSetRange (range) ;
  558. }
  559. static void doPwmClock (int argc, char *argv [])
  560. {
  561. unsigned int clock ;
  562. if (argc != 3)
  563. {
  564. fprintf (stderr, "Usage: %s pwmc <clock>\n", argv [0]) ;
  565. exit (1) ;
  566. }
  567. clock = (unsigned int)strtoul (argv [2], NULL, 10) ;
  568. if ((clock < 1) || (clock > 4095))
  569. {
  570. fprintf (stderr, "%s: clock must be between 0 and 4096\n", argv [0]) ;
  571. exit (1) ;
  572. }
  573. pwmSetClock (clock) ;
  574. }
  575. /*
  576. * main:
  577. * Start here
  578. *********************************************************************************
  579. */
  580. int main (int argc, char *argv [])
  581. {
  582. int i ;
  583. if (argc == 1)
  584. {
  585. fprintf (stderr, "%s: %s\n", argv [0], usage) ;
  586. return 1 ;
  587. }
  588. if (strcasecmp (argv [1], "-h") == 0)
  589. {
  590. printf ("%s: %s\n", argv [0], usage) ;
  591. return 0 ;
  592. }
  593. if (strcasecmp (argv [1], "-v") == 0)
  594. {
  595. printf ("gpio version: %s\n", VERSION) ;
  596. printf ("Copyright (c) 2012 Gordon Henderson\n") ;
  597. printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
  598. printf ("For details type: %s -warranty\n", argv [0]) ;
  599. return 0 ;
  600. }
  601. if (strcasecmp (argv [1], "-warranty") == 0)
  602. {
  603. printf ("gpio version: %s\n", VERSION) ;
  604. printf ("Copyright (c) 2012 Gordon Henderson\n") ;
  605. printf ("\n") ;
  606. printf (" This program is free software; you can redistribute it and/or modify\n") ;
  607. printf (" it under the terms of the GNU Leser General Public License as published\n") ;
  608. printf (" by the Free Software Foundation, either version 3 of the License, or\n") ;
  609. printf (" (at your option) any later version.\n") ;
  610. printf ("\n") ;
  611. printf (" This program is distributed in the hope that it will be useful,\n") ;
  612. printf (" but WITHOUT ANY WARRANTY; without even the implied warranty of\n") ;
  613. printf (" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n") ;
  614. printf (" GNU Lesser General Public License for more details.\n") ;
  615. printf ("\n") ;
  616. printf (" You should have received a copy of the GNU Lesser General Public License\n") ;
  617. printf (" along with this program. If not, see <http://www.gnu.org/licenses/>.\n") ;
  618. printf ("\n") ;
  619. return 0 ;
  620. }
  621. if (geteuid () != 0)
  622. {
  623. fprintf (stderr, "%s: Must be root to run. Program should be suid root. This is an error.\n", argv [0]) ;
  624. return 1 ;
  625. }
  626. // Initial test for /sys/class/gpio operations:
  627. /**/ if (strcasecmp (argv [1], "exports" ) == 0) { doExports (argc, argv) ; return 0 ; }
  628. else if (strcasecmp (argv [1], "export" ) == 0) { doExport (argc, argv) ; return 0 ; }
  629. else if (strcasecmp (argv [1], "edge" ) == 0) { doEdge (argc, argv) ; return 0 ; }
  630. else if (strcasecmp (argv [1], "unexportall") == 0) { doUnexportall (argc, argv) ; return 0 ; }
  631. else if (strcasecmp (argv [1], "unexport" ) == 0) { doUnexport (argc, argv) ; return 0 ; }
  632. // Check for drive or load commands:
  633. if (strcasecmp (argv [1], "drive") == 0) { doPadDrive (argc, argv) ; return 0 ; }
  634. if (strcasecmp (argv [1], "load" ) == 0) { doLoad (argc, argv) ; return 0 ; }
  635. // Gertboard commands
  636. if (strcasecmp (argv [1], "gbr" ) == 0) { doGbr (argc, argv) ; return 0 ; }
  637. if (strcasecmp (argv [1], "gbw" ) == 0) { doGbw (argc, argv) ; return 0 ; }
  638. // Check for -g argument
  639. if (strcasecmp (argv [1], "-g") == 0)
  640. {
  641. if (wiringPiSetupGpio () == -1)
  642. {
  643. fprintf (stderr, "%s: Unable to initialise GPIO mode.\n", argv [0]) ;
  644. exit (1) ;
  645. }
  646. for (i = 2 ; i < argc ; ++i)
  647. argv [i - 1] = argv [i] ;
  648. --argc ;
  649. wpMode = WPI_MODE_GPIO ;
  650. }
  651. // Check for -p argument for PiFace
  652. else if (strcasecmp (argv [1], "-p") == 0)
  653. {
  654. if (wiringPiSetupPiFaceForGpioProg () == -1)
  655. {
  656. fprintf (stderr, "%s: Unable to initialise PiFace.\n", argv [0]) ;
  657. exit (1) ;
  658. }
  659. for (i = 2 ; i < argc ; ++i)
  660. argv [i - 1] = argv [i] ;
  661. --argc ;
  662. wpMode = WPI_MODE_PIFACE ;
  663. }
  664. // Default to wiringPi mode
  665. else
  666. {
  667. if (wiringPiSetup () == -1)
  668. {
  669. fprintf (stderr, "%s: Unable to initialise wiringPi mode\n", argv [0]) ;
  670. exit (1) ;
  671. }
  672. wpMode = WPI_MODE_PINS ;
  673. }
  674. // Check for PWM operations
  675. if (wpMode != WPI_MODE_PIFACE)
  676. {
  677. if (strcasecmp (argv [1], "pwm-bal") == 0) { doPwmMode (PWM_MODE_BAL) ; return 0 ; }
  678. if (strcasecmp (argv [1], "pwm-ms") == 0) { doPwmMode (PWM_MODE_MS) ; return 0 ; }
  679. if (strcasecmp (argv [1], "pwmr") == 0) { doPwmRange (argc, argv) ; return 0 ; }
  680. if (strcasecmp (argv [1], "pwmc") == 0) { doPwmClock (argc, argv) ; return 0 ; }
  681. }
  682. // Check for wiring commands
  683. /**/ if (strcasecmp (argv [1], "read" ) == 0) doRead (argc, argv) ;
  684. else if (strcasecmp (argv [1], "write") == 0) doWrite (argc, argv) ;
  685. else if (strcasecmp (argv [1], "pwm" ) == 0) doPwm (argc, argv) ;
  686. else if (strcasecmp (argv [1], "mode" ) == 0) doMode (argc, argv) ;
  687. else
  688. {
  689. fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [1]) ;
  690. exit (1) ;
  691. }
  692. return 0 ;
  693. }