main.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
  15. *
  16. * Feedback, Bugs... [email protected]
  17. *
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24. #include <linux/gpio_dev.h>
  25. #include <linux/ioctl.h>
  26. void
  27. print_usage()
  28. {
  29. printf("gpioctl dirin|dirout|get|set|clear gpio\n");
  30. exit(0);
  31. }
  32. int
  33. main(int argc, char **argv)
  34. {
  35. int gpio_pin;
  36. int fd;
  37. int result = 0;
  38. if (argc != 3)
  39. {
  40. print_usage();
  41. }
  42. if ((fd = open("/dev/gpio", O_RDWR)) < 0)
  43. {
  44. printf("Error whilst opening /dev/gpio\n");
  45. return -1;
  46. }
  47. gpio_pin = atoi(argv[2]);
  48. printf("using gpio pin %d\n", gpio_pin);
  49. if (!strcmp(argv[1], "dirin"))
  50. {
  51. ioctl(fd, GPIO_DIR_IN, gpio_pin);
  52. } else if (!strcmp(argv[1], "dirout"))
  53. {
  54. ioctl(fd, GPIO_DIR_OUT, gpio_pin);
  55. } else if (!strcmp(argv[1], "get"))
  56. {
  57. result = ioctl(fd, GPIO_GET, gpio_pin);
  58. printf("Pin %d is %s\n", gpio_pin, (result ? "HIGH" : "LOW"));
  59. } else if (!strcmp(argv[1], "set"))
  60. {
  61. ioctl(fd, GPIO_SET, gpio_pin);
  62. } else if (!strcmp(argv[1], "clear"))
  63. {
  64. ioctl(fd, GPIO_CLEAR, gpio_pin);
  65. } else print_usage();
  66. return result;
  67. }