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.
 
 
 
 
 

195 rivejä
4.0 KiB

  1. /*
  2. * reaction.c:
  3. * Simple test for the PiFace interface board.
  4. *
  5. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  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 <wiringPi.h>
  28. #include <piFace.h>
  29. int outputs [4] = { 0,0,0,0 } ;
  30. #define PIFACE 200
  31. /*
  32. * light:
  33. * Light up the given LED - actually lights up a pair
  34. *********************************************************************************
  35. */
  36. void light (int led, int value)
  37. {
  38. led *= 2 ;
  39. digitalWrite (PIFACE + led + 0, value) ;
  40. digitalWrite (PIFACE + led + 1, value) ;
  41. }
  42. /*
  43. * lightAll:
  44. * All On or Off
  45. *********************************************************************************
  46. */
  47. void lightAll (int onoff)
  48. {
  49. light (0, onoff) ;
  50. light (1, onoff) ;
  51. light (2, onoff) ;
  52. light (3, onoff) ;
  53. }
  54. /*
  55. * waitForNoButtons:
  56. * Wait for all buttons to be released
  57. *********************************************************************************
  58. */
  59. void waitForNoButtons (void)
  60. {
  61. int i, button ;
  62. for (;;)
  63. {
  64. button = 0 ;
  65. for (i = 0 ; i < 4 ; ++i)
  66. button += digitalRead (PIFACE + i) ;
  67. if (button == 4)
  68. break ;
  69. }
  70. }
  71. void scanButton (int button)
  72. {
  73. if (digitalRead (PIFACE + button) == LOW)
  74. {
  75. outputs [button] ^= 1 ;
  76. digitalWrite (PIFACE + button, outputs [button]) ;
  77. }
  78. while (digitalRead (PIFACE + button) == LOW)
  79. delay (1) ;
  80. }
  81. int main (void)
  82. {
  83. int i, j ;
  84. int led, button ;
  85. unsigned int start, stop ;
  86. printf ("Raspberry Pi PiFace Reaction Timer\n") ;
  87. printf ("==================================\n") ;
  88. if (piFaceSetup (PIFACE) == -1)
  89. exit (1) ;
  90. // Enable internal pull-ups
  91. for (i = 0 ; i < 8 ; ++i)
  92. pullUpDnControl (PIFACE + i, PUD_UP) ;
  93. // Main game loop:
  94. // Put some random LED pairs up for a few seconds, then blank ...
  95. for (;;)
  96. {
  97. printf ("Press any button to start ... \n") ; fflush (stdout) ;
  98. for (;;)
  99. {
  100. led = rand () % 4 ;
  101. light (led, 1) ;
  102. delay (10) ;
  103. light (led, 0) ;
  104. button = 0 ;
  105. for (j = 0 ; j < 4 ; ++j)
  106. button += digitalRead (PIFACE + j) ;
  107. if (button != 4)
  108. break ;
  109. }
  110. waitForNoButtons () ;
  111. printf ("Wait for it ... ") ; fflush (stdout) ;
  112. led = rand () % 4 ;
  113. delay (rand () % 500 + 1000) ;
  114. light (led, 1) ;
  115. start = millis () ;
  116. for (button = -1 ; button == -1 ; )
  117. {
  118. for (j = 0 ; j < 4 ; ++j)
  119. if (digitalRead (PIFACE + j) == 0) // Pushed
  120. {
  121. button = j ;
  122. break ;
  123. }
  124. }
  125. stop = millis () ;
  126. button = 3 - button ; // Correct for the buttons/LEDs reversed
  127. light (led, 0) ;
  128. waitForNoButtons () ;
  129. light (led, 1) ;
  130. if (button == led)
  131. {
  132. printf ("You got it in %3d mS\n", stop - start) ;
  133. }
  134. else
  135. {
  136. printf ("Missed: You pushed %d - LED was %d\n", button, led) ;
  137. for (;;)
  138. {
  139. light (button, 1) ;
  140. delay (100) ;
  141. light (button, 0) ;
  142. delay (100) ;
  143. i = 0 ;
  144. for (j = 0 ; j < 4 ; ++j)
  145. i += digitalRead (PIFACE + j) ;
  146. if (i != 4)
  147. break ;
  148. }
  149. waitForNoButtons () ;
  150. }
  151. light (led, 0) ;
  152. delay (4000) ;
  153. }
  154. return 0 ;
  155. }