piface.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * piFace.c:
  3. * Simple test for the PiFace interface board.
  4. *
  5. * Read the buttons and output the same to the LEDs
  6. *
  7. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  8. ***********************************************************************
  9. * This file is part of wiringPi:
  10. * https://projects.drogon.net/raspberry-pi/wiringpi/
  11. *
  12. * wiringPi is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * wiringPi is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public License
  23. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  24. ***********************************************************************
  25. */
  26. #include <wiringPi.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <stdint.h>
  30. int outputs [4] = { 0,0,0,0 } ;
  31. void scanButton (int button)
  32. {
  33. if (digitalRead (button) == LOW)
  34. {
  35. outputs [button] ^= 1 ;
  36. digitalWrite (button, outputs [button]) ;
  37. }
  38. while (digitalRead (button) == LOW)
  39. delay (1) ;
  40. }
  41. int main (void)
  42. {
  43. int pin, button ;
  44. printf ("Raspberry Pi wiringPiFace test program\n") ;
  45. if (wiringPiSetupPiFace () == -1)
  46. exit (1) ;
  47. // Enable internal pull-ups
  48. for (pin = 0 ; pin < 8 ; ++pin)
  49. pullUpDnControl (pin, PUD_UP) ;
  50. for (;;)
  51. {
  52. for (button = 0 ; button < 4 ; ++button)
  53. scanButton (button) ;
  54. delay (1) ;
  55. }
  56. return 0 ;
  57. }