-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitches.c
216 lines (198 loc) · 5.8 KB
/
switches.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include "portable.h"
#include "stewie-lamps.h"
#include "lamps.h"
#include "sounds.h"
#include "game.h"
#include "display.h"
#include "switches.h"
static char* switchName(int gpio) {
switch( gpio ) {
case SWITCH_START: return "start";
case SWITCH_FLIPPER_LEFT: return "flipper_left";
case SWITCH_FLIPPER_RIGHT: return "flipper_right";
case SWITCH_LOIS: return "louis";
case SWITCH_MEG: return "meg";
case SWITCH_PETER: return "peter";
case SWITCH_SHOOTER: return "shooter";
case SWITCH_BRIAN_CHRIS: return "brian_chris";
case SWITCH_CHRIS_BRIAN: return "chris_brian";
default: return "?";
}
}
static void shooterOff(void) {
gpioWrite( SOLENOID_SHOOTER, 0 );
gpioCancelTimer( TIMER_SHOOTER );
}
static void switchCallback(int gpio, int level, uint32_t tick) {
static bool brian_sw = false;
static bool chris_sw = false;
static int balls = 0;
printf("switch %s %d\n", switchName(gpio), level);
if ( balls <= 0 ) {
if ( gpio == SWITCH_START ) {
if ( level == 0 ) {
balls = 5;
printf("balls:%d\n", balls);
soundPlay( sound_start );
gameStart();
}
}
}
switch ( gpio ) {
case SWITCH_START:
if ( level == 0 ) {
if ( gpioRead( SWITCH_SHOOTER ) == 0 ) {
soundPlay( sound_launch );
gpioWrite( SOLENOID_SHOOTER, 1 );
gpioSetTimerFunc( TIMER_SHOOTER, 100, shooterOff );
}
}
break;
case SWITCH_SHOOTER:
if ( level == 0 ) {
balls--;
printf("balls:%d\n", balls);
if ( balls > 0 ) {
lampsBlink( blink_shoot_again );
soundPlay( sound_drain );
displayBall( balls );
} else {
lampsBlink( blink_none );
gameStop();
soundPlay( sound_game_over );
displayText( "Damn" );
}
} else {
lampsBlink( blink_none );
}
break;
case SWITCH_LOIS: {
if ( level != 0 ) {
break;
}
static int j = 0;
j |= 0x01;
ledMatrixLois( j );
j <<= 1;
if ( j > 0x0F ) {
j = 0;
soundPlay( sound_lois );
lampsBlink( blink_lois );
displayScore( 100 );
} else {
displayScore( 10 );
}
soundPlay( sound_hit );
break; }
case SWITCH_MEG: {
if ( level != 0 ) {
break;
}
static int j = 0;
j |= 0x01;
ledMatrixMeg( j );
j <<= 1;
if ( j > 0x07 ) {
j = 0;
soundPlay( sound_meg );
lampsBlink( blink_meg );
displayScore( 100 );
} else {
displayScore( 10 );
}
soundPlay( sound_hit );
break; }
case SWITCH_PETER: {
if ( level != 0 ) {
break;
}
static int j = 0;
j |= 0x01;
ledMatrixPeter( j );
j <<= 1;
if ( j > 0x1F ) {
j = 0;
soundPlay( sound_peter );
lampsBlink( blink_peter );
displayScore( 100 );
} else {
displayScore( 10 );
}
soundPlay( sound_hit );
break; }
case SWITCH_BRIAN_CHRIS: {
if ( level != 0 ) {
break;
}
if ( !brian_sw ) {
chris_sw = true;
break;
}
brian_sw = false;
static int j = 0;
j |= 0x01;
ledMatrixChris( j );
j <<= 1;
if ( j > 0x1F ) {
j = 0;
soundPlay( sound_chris );
lampsBlink( blink_chris );
displayScore( 100 );
} else {
displayScore( 10 );
}
soundPlay( sound_hit );
break; }
case SWITCH_CHRIS_BRIAN: {
if ( level != 0 ) {
break;
}
if ( !chris_sw ) {
brian_sw = true;
break;
}
chris_sw = false;
static int j = 0;
j |= 0x01;
ledMatrixBrian( j );
j <<= 1;
if ( j > 0x1F ) {
j = 0;
soundPlay( sound_brian );
lampsBlink( blink_brian );
displayScore( 100 );
} else {
displayScore( 10 );
}
soundPlay( sound_hit );
break; }
}
}
void switchesInit(void) {
printf("switchesInit()\n");
int switches[] = {
SWITCH_START,
SWITCH_LOIS,
SWITCH_MEG,
SWITCH_PETER,
SWITCH_SHOOTER,
SWITCH_BRIAN_CHRIS,
SWITCH_CHRIS_BRIAN
};
for (int i=0; i<sizeof(switches)/sizeof(switches[0]); i++) {
int gpio = switches[i];
gpioSetMode( gpio, PI_INPUT );
gpioSetPullUpDown( gpio, PI_PUD_UP );
gpioGlitchFilter( gpio, 250 );
gpioSetAlertFunc( gpio, switchCallback );
}
gpioGlitchFilter( SWITCH_SHOOTER, 2000 );
gpioSetMode( SWITCH_DRIVE_SIGNAL, PI_OUTPUT );
gpioWrite( SWITCH_DRIVE_SIGNAL, 0 );
}