您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

104 行
2.7 KiB

  1. /*
  2. * buttons.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 <stdio.h>
  27. #include <wiringPi.h>
  28. #include <piFace.h>
  29. int outputs [4] = { 0,0,0,0 } ;
  30. // Use 200 as the pin-base for the PiFace board
  31. #define PIFACE_BASE 200
  32. /*
  33. * scanButton:
  34. * Read the guiven button - if it's pressed, then flip the state
  35. * of the correspoinding output pin
  36. *********************************************************************************
  37. */
  38. void scanButton (int button)
  39. {
  40. if (digitalRead (PIFACE_BASE + button) == LOW)
  41. {
  42. outputs [button] ^= 1 ;
  43. digitalWrite (PIFACE_BASE + button, outputs [button]) ;
  44. printf ("Button %d pushed - output now: %s\n",
  45. button, (outputs [button] == 0) ? "Off" : "On") ;
  46. }
  47. while (digitalRead (PIFACE_BASE + button) == LOW)
  48. delay (1) ;
  49. }
  50. /*
  51. * start here
  52. *********************************************************************************
  53. */
  54. int main (void)
  55. {
  56. int pin, button ;
  57. printf ("Raspberry Pi wiringPi + PiFace test program\n") ;
  58. printf ("===========================================\n") ;
  59. printf ("\n") ;
  60. printf (
  61. "This program reads the buttons and uses them to toggle the first 4\n"
  62. "outputs. Push a button once to turn an output on, and push it again to\n"
  63. "turn it off again.\n\n") ;
  64. // Always initialise wiringPi. Use wiringPiSys() if you don't need
  65. // (or want) to run as root
  66. wiringPiSetupSys () ;
  67. piFaceSetup (PIFACE_BASE) ;
  68. // Enable internal pull-ups & start with all off
  69. for (pin = 0 ; pin < 8 ; ++pin)
  70. {
  71. pullUpDnControl (PIFACE_BASE + pin, PUD_UP) ;
  72. digitalWrite (PIFACE_BASE + pin, 0) ;
  73. }
  74. // Loop, scanning the buttons
  75. for (;;)
  76. {
  77. for (button = 0 ; button < 4 ; ++button)
  78. scanButton (button) ;
  79. delay (5) ;
  80. }
  81. return 0 ;
  82. }