3 * Simple test for the PiFace interface board.
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/
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.
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.
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 ***********************************************************************
33 int outputs [4] = { 0,0,0,0 } ;
39 * Light up the given LED - actually lights up a pair
40 *********************************************************************************
43 void light (int led, int value)
46 digitalWrite (PIFACE + led + 0, value) ;
47 digitalWrite (PIFACE + led + 1, value) ;
53 *********************************************************************************
56 void lightAll (int onoff)
67 * Wait for all buttons to be released
68 *********************************************************************************
71 void waitForNoButtons (void)
78 for (i = 0 ; i < 4 ; ++i)
79 button += digitalRead (PIFACE + i) ;
87 void scanButton (int button)
89 if (digitalRead (PIFACE + button) == LOW)
91 outputs [button] ^= 1 ;
92 digitalWrite (PIFACE + button, outputs [button]) ;
95 while (digitalRead (PIFACE + button) == LOW)
104 unsigned int start, stop ;
106 printf ("Raspberry Pi PiFace Reaction Timer\n") ;
107 printf ("==================================\n") ;
109 if (piFaceSetup (PIFACE) == -1)
112 // Enable internal pull-ups
114 for (i = 0 ; i < 8 ; ++i)
115 pullUpDnControl (PIFACE + i, PUD_UP) ;
119 // Put some random LED pairs up for a few seconds, then blank ...
123 printf ("Press any button to start ... \n") ; fflush (stdout) ;
133 for (j = 0 ; j < 4 ; ++j)
134 button += digitalRead (PIFACE + j) ;
140 waitForNoButtons () ;
142 printf ("Wait for it ... ") ; fflush (stdout) ;
145 delay (rand () % 500 + 1000) ;
149 for (button = -1 ; button == -1 ; )
151 for (j = 0 ; j < 4 ; ++j)
152 if (digitalRead (PIFACE + j) == 0) // Pushed
159 button = 3 - button ; // Correct for the buttons/LEDs reversed
163 waitForNoButtons () ;
169 printf ("You got it in %3d mS\n", stop - start) ;
173 printf ("Missed: You pushed %d - LED was %d\n", button, led) ;
181 for (j = 0 ; j < 4 ; ++j)
182 i += digitalRead (PIFACE + j) ;
187 waitForNoButtons () ;