Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / examples / PiFace / reaction.c @ 32:cadb9025f1e0

History | View | Annotate | Download (3.96 KB)

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

    
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <stdint.h>
28

    
29
#include <wiringPi.h>
30
#include <piFace.h>
31

    
32

    
33
int outputs [4] = { 0,0,0,0 } ;
34

    
35
#define        PIFACE        200
36

    
37
/*
38
 * light:
39
 *        Light up the given LED - actually lights up a pair
40
 *********************************************************************************
41
 */
42

    
43
void light (int led, int value)
44
{
45
  led *= 2 ;
46
  digitalWrite (PIFACE + led + 0, value) ;
47
  digitalWrite (PIFACE + led + 1, value) ;
48
}
49

    
50
/*
51
 * lightAll:
52
 *        All On or Off
53
 *********************************************************************************
54
 */
55

    
56
void lightAll (int onoff)
57
{
58
  light (0, onoff) ;
59
  light (1, onoff) ;
60
  light (2, onoff) ;
61
  light (3, onoff) ;
62
}
63

    
64

    
65
/*
66
 * waitForNoButtons:
67
 *        Wait for all buttons to be released
68
 *********************************************************************************
69
 */
70

    
71
void waitForNoButtons (void)
72
{
73
  int i, button ;
74

    
75
  for (;;)
76
  {
77
    button = 0 ;
78
    for (i = 0 ; i < 4 ; ++i)
79
      button += digitalRead (PIFACE + i) ;
80

    
81
    if (button == 4)
82
      break ;
83
  }
84
}
85

    
86

    
87
void scanButton (int button)
88
{
89
  if (digitalRead (PIFACE + button) == LOW)
90
  {
91
    outputs [button] ^= 1 ;
92
    digitalWrite (PIFACE + button, outputs [button]) ;
93
  }
94

    
95
  while (digitalRead (PIFACE + button) == LOW)
96
    delay (1) ;
97
}
98

    
99

    
100
int main (void)
101
{
102
  int i, j ;
103
  int led, button ;
104
  unsigned int start, stop ;
105

    
106
  printf ("Raspberry Pi PiFace Reaction Timer\n") ;
107
  printf ("==================================\n") ;
108

    
109
  if (piFaceSetup (PIFACE) == -1)
110
    exit (1) ;
111

    
112
// Enable internal pull-ups
113

    
114
  for (i = 0 ; i < 8 ; ++i)
115
    pullUpDnControl (PIFACE + i, PUD_UP) ;
116

    
117

    
118
// Main game loop:
119
//        Put some random LED pairs up for a few seconds, then blank ...
120

    
121
  for (;;)
122
  {
123
    printf ("Press any button to start ... \n") ; fflush (stdout) ;
124

    
125
    for (;;)
126
    {
127
      led = rand () % 4 ;
128
      light (led, 1) ;
129
      delay (10) ;
130
      light (led, 0) ;
131

    
132
      button = 0 ;
133
      for (j = 0 ; j < 4 ; ++j)
134
        button += digitalRead (PIFACE + j) ;
135

    
136
      if (button != 4)
137
        break ;
138
    }
139

    
140
    waitForNoButtons () ;
141

    
142
    printf ("Wait for it ... ") ; fflush (stdout) ;
143

    
144
    led = rand () % 4 ;
145
    delay (rand () % 500 + 1000) ;
146
    light (led, 1) ;
147

    
148
    start = millis () ;
149
    for (button = -1 ; button == -1 ; )
150
    {
151
      for (j = 0 ; j < 4 ; ++j)
152
        if (digitalRead (PIFACE + j) == 0)        // Pushed
153
        {
154
          button = j ;
155
          break ;
156
        }
157
    }
158
    stop = millis () ;
159
    button = 3 - button ; // Correct for the buttons/LEDs reversed
160

    
161
    light (led, 0) ;
162

    
163
    waitForNoButtons () ;
164

    
165
    light (led, 1) ;
166

    
167
    if (button == led)
168
    {
169
      printf ("You got it in %3d mS\n", stop - start) ;
170
    }
171
    else
172
    {
173
      printf ("Missed: You pushed %d - LED was %d\n", button, led) ;
174
      for (;;)
175
      {
176
        light (button, 1) ;
177
        delay (100) ;
178
        light (button, 0) ;
179
        delay (100) ;
180
        i = 0 ;
181
        for (j = 0 ; j < 4 ; ++j)
182
          i += digitalRead (PIFACE + j) ;
183
        if (i != 4)
184
          break ;
185
      }
186

    
187
      waitForNoButtons () ;
188
    }
189
    light (led, 0) ;
190
    delay (4000) ;
191
  }
192

    
193
  return 0 ;
194
}