Mercurial > hg > beaglert
comparison projects/tank_wars/render.cpp @ 22:fbfeb5895efd matrix_gpio
Updated tank wars demo for new API
author | andrewm |
---|---|
date | Sun, 03 May 2015 01:10:17 +0100 |
parents | 06f93bef7dd2 |
children | 3c3a1357657d |
comparison
equal
deleted
inserted
replaced
20:58eb99dac921 | 22:fbfeb5895efd |
---|---|
12 #include <rtdk.h> | 12 #include <rtdk.h> |
13 #include <cmath> | 13 #include <cmath> |
14 #include <cstdlib> | 14 #include <cstdlib> |
15 #include <time.h> | 15 #include <time.h> |
16 | 16 |
17 int gAudioFramesPerMatrixFrame = 2; // Ratio in audio to matrix sample rate | |
18 | |
17 int gInputTank1Angle = 0; // Inputs for the cannon angles | 19 int gInputTank1Angle = 0; // Inputs for the cannon angles |
18 int gInputTank2Angle = 1; | 20 int gInputTank2Angle = 1; |
19 int gInputLauncher = 2; // Input for launcher FSR | 21 int gInputLauncher = 2; // Input for launcher FSR |
20 | 22 |
21 int gOutputX = 0; // Outputs for the scope | 23 int gOutputX = 0; // Outputs for the scope |
35 // 1st-order filter and peak detector for launcher input | 37 // 1st-order filter and peak detector for launcher input |
36 float gLauncherLastSample = 0; | 38 float gLauncherLastSample = 0; |
37 float gLauncherFilterPole = 0.8; | 39 float gLauncherFilterPole = 0.8; |
38 float gLauncherPeakValue = 0; | 40 float gLauncherPeakValue = 0; |
39 float gLauncherPeakFilterPole = 0.999; | 41 float gLauncherPeakFilterPole = 0.999; |
40 float gLauncherNoiseThreshold = 0.01 * MATRIX_MAX; | 42 float gLauncherNoiseThreshold = 0.01; |
41 float gLauncherMinimumPeak = 0.1 * MATRIX_MAX; | 43 float gLauncherMinimumPeak = 0.1; |
42 bool gLauncherTriggered = false; | 44 bool gLauncherTriggered = false; |
43 | 45 |
44 // Screen update rate; affects buffer size. Actual contents of buffer | 46 // Screen update rate; affects buffer size. Actual contents of buffer |
45 // may be smaller than this | 47 // may be smaller than this |
46 int gScreenWidth = 512; | 48 int gScreenWidth = 512; |
59 bool gScreenNextBufferReady; // Is the next buffer ready to go? | 61 bool gScreenNextBufferReady; // Is the next buffer ready to go? |
60 | 62 |
61 // Auxiliary (low-priority) task for updating the screen | 63 // Auxiliary (low-priority) task for updating the screen |
62 AuxiliaryTask gScreenUpdateTask; | 64 AuxiliaryTask gScreenUpdateTask; |
63 | 65 |
66 // Buffers for music and sound effects | |
67 extern float *gMusicBuffer; | |
68 extern int gMusicBufferLength; | |
69 extern float *gSoundBoomBuffer; | |
70 extern int gSoundBoomBufferLength; | |
71 | |
72 // Current state for sound and music | |
73 int gMusicBufferPointer = 0; // 0 means start of buffer... | |
74 int gSoundBoomBufferPointer = -1; // -1 means don't play... | |
75 float gSoundProjectileOscillatorPhase = 0; | |
76 float gSoundProjectileOscillatorGain = 0.2; | |
77 float gOscillatorPhaseScaler = 0; | |
78 | |
64 void screen_update(); | 79 void screen_update(); |
65 | 80 |
66 // initialise_render() is called once before the audio rendering starts. | 81 // initialise_render() is called once before the audio rendering starts. |
67 // Use it to perform any initialisation and allocation which is dependent | 82 // Use it to perform any initialisation and allocation which is dependent |
68 // on the period size or sample rate. | 83 // on the period size or sample rate. |
70 // userData holds an opaque pointer to a data structure that was passed | 85 // userData holds an opaque pointer to a data structure that was passed |
71 // in from the call to initAudio(). | 86 // in from the call to initAudio(). |
72 // | 87 // |
73 // Return true on success; returning false halts the program. | 88 // Return true on success; returning false halts the program. |
74 | 89 |
75 bool initialise_render(int numMatrixChannels, int numAudioChannels, | 90 bool initialise_render(int numAnalogChannels, int numDigitalChannels, int numAudioChannels, |
76 int numMatrixFramesPerPeriod, | 91 int numAnalogFramesPerPeriod, |
77 int numAudioFramesPerPeriod, | 92 int numAudioFramesPerPeriod, |
78 float matrixSampleRate, float audioSampleRate, | 93 float analogSampleRate, float audioSampleRate, |
79 void *userData) | 94 void *userData) |
80 { | 95 { |
81 srandom(time(NULL)); | 96 srandom(time(NULL)); |
82 | 97 |
83 // Verify we are running with matrix enabled | 98 // Verify we are running with matrix enabled |
84 if(numMatrixFramesPerPeriod == 0 || numMatrixChannels < 4) { | 99 if(numAnalogFramesPerPeriod == 0 || numAnalogChannels < 4) { |
85 rt_printf("Error: this example needs the matrix enabled with at least 4 channels\n"); | 100 rt_printf("Error: this example needs the matrix enabled with at least 4 channels\n"); |
86 return false; | 101 return false; |
87 } | 102 } |
88 | 103 |
104 // Initialise audio variables | |
105 gAudioFramesPerMatrixFrame = numAudioFramesPerPeriod / numAnalogFramesPerPeriod; | |
106 gOscillatorPhaseScaler = 2.0 * M_PI / audioSampleRate; | |
107 | |
89 // Initialise the screen buffers | 108 // Initialise the screen buffers |
90 gScreenBufferMaxLength = 2 * matrixSampleRate / gScreenFramesPerSecond; | 109 gScreenBufferMaxLength = 2 * analogSampleRate / gScreenFramesPerSecond; |
91 gScreenBuffer1 = new float[gScreenBufferMaxLength]; | 110 gScreenBuffer1 = new float[gScreenBufferMaxLength]; |
92 gScreenBuffer2 = new float[gScreenBufferMaxLength]; | 111 gScreenBuffer2 = new float[gScreenBufferMaxLength]; |
93 if(gScreenBuffer1 == 0 || gScreenBuffer2 == 0) { | 112 if(gScreenBuffer1 == 0 || gScreenBuffer2 == 0) { |
94 rt_printf("Error initialising screen buffers\n"); | 113 rt_printf("Error initialising screen buffers\n"); |
95 return false; | 114 return false; |
102 gScreenBufferNextUpdateLocation = 0; | 121 gScreenBufferNextUpdateLocation = 0; |
103 gScreenNextBufferReady = false; | 122 gScreenNextBufferReady = false; |
104 | 123 |
105 // Initialise the game | 124 // Initialise the game |
106 setupGame(gScreenWidth, gScreenHeight); | 125 setupGame(gScreenWidth, gScreenHeight); |
107 gGameFrameInterval = matrixSampleRate / gGameFramesPerSecond; | 126 gGameFrameInterval = analogSampleRate / gGameFramesPerSecond; |
108 gSamplesUntilNextFrame = gGameFrameInterval; | 127 gSamplesUntilNextFrame = gGameFrameInterval; |
109 | 128 |
110 // Initialise auxiliary tasks | 129 // Initialise auxiliary tasks |
111 if((gScreenUpdateTask = createAuxiliaryTaskLoop(&screen_update, 90, | 130 if((gScreenUpdateTask = createAuxiliaryTaskLoop(&screen_update, 90, |
112 "beaglert-screen-update")) == 0) | 131 "beaglert-screen-update")) == 0) |
138 // render() is called regularly at the highest priority by the audio engine. | 157 // render() is called regularly at the highest priority by the audio engine. |
139 // Input and output are given from the audio hardware and the other | 158 // Input and output are given from the audio hardware and the other |
140 // ADCs and DACs (if available). If only audio is available, numMatrixFrames | 159 // ADCs and DACs (if available). If only audio is available, numMatrixFrames |
141 // will be 0. | 160 // will be 0. |
142 | 161 |
143 void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut, | 162 void render(int numAnalogFrames, int numDigitalFrames, int numAudioFrames, float *audioIn, float *audioOut, |
144 uint16_t *matrixIn, uint16_t *matrixOut) | 163 float *analogIn, float *analogOut, uint32_t *digital) |
145 { | 164 { |
146 for(int n = 0; n < numMatrixFrames; n++) { | 165 int audioIndex = 0; |
166 | |
167 for(int n = 0; n < numAnalogFrames; n++) { | |
168 for(int k = 0; k < gAudioFramesPerMatrixFrame; k++) { | |
169 // Render music and sound | |
170 float audioSample = 0; | |
171 | |
172 // Music plays in a loop | |
173 if(gMusicBuffer != 0 && gMusicBufferPointer >= 0) { | |
174 audioSample += gMusicBuffer[gMusicBufferPointer++]; | |
175 if(gMusicBufferPointer >= gMusicBufferLength) | |
176 gMusicBufferPointer = 0; | |
177 } | |
178 | |
179 // Sound effect plays until finished, then stops | |
180 if(gSoundBoomBuffer != 0 && gSoundBoomBufferPointer >= 0) { | |
181 audioSample += gSoundBoomBuffer[gSoundBoomBufferPointer++]; | |
182 if(gSoundBoomBufferPointer >= gSoundBoomBufferLength) | |
183 gSoundBoomBufferPointer = -1; | |
184 } | |
185 | |
186 // Oscillator plays to indicate projectile height | |
187 if(gameStatusProjectileInMotion()) { | |
188 audioSample += gSoundProjectileOscillatorGain * sinf(gSoundProjectileOscillatorPhase); | |
189 | |
190 gSoundProjectileOscillatorPhase += gOscillatorPhaseScaler * constrain(map(gameStatusProjectileHeight(), | |
191 1.0, 0, 300, 2000), 200, 6000); | |
192 if(gSoundProjectileOscillatorPhase > 2.0 * M_PI) | |
193 gSoundProjectileOscillatorPhase -= 2.0 * M_PI; | |
194 } | |
195 | |
196 audioOut[2*audioIndex] = audioOut[2*audioIndex + 1] = audioSample; | |
197 audioIndex++; | |
198 } | |
199 | |
147 // First-order lowpass filter to remove noise on launch FSR | 200 // First-order lowpass filter to remove noise on launch FSR |
148 float rawSample = analogRead(gInputLauncher, n); | 201 float rawSample = AnalogRead(gInputLauncher, n); |
149 float launchSample = gLauncherFilterPole * gLauncherLastSample + | 202 float launchSample = gLauncherFilterPole * gLauncherLastSample + |
150 (1.0f - gLauncherFilterPole) * rawSample; | 203 (1.0f - gLauncherFilterPole) * rawSample; |
151 gLauncherLastSample = launchSample; | 204 gLauncherLastSample = launchSample; |
152 | 205 |
153 // Peak-detect on launch signal | 206 // Peak-detect on launch signal |
162 gLauncherTriggered = true; | 215 gLauncherTriggered = true; |
163 // Peak detected-- fire!! | 216 // Peak detected-- fire!! |
164 // Set both cannon strengths but only one will | 217 // Set both cannon strengths but only one will |
165 // fire depending on whose turn it is | 218 // fire depending on whose turn it is |
166 float strength = map(gLauncherPeakValue, | 219 float strength = map(gLauncherPeakValue, |
167 gLauncherMinimumPeak, MATRIX_MAX, | 220 gLauncherMinimumPeak, 1.0, |
168 0.5f, 10.0f); | 221 0.5f, 10.0f); |
169 setTank1CannonStrength(strength); | 222 setTank1CannonStrength(strength); |
170 setTank2CannonStrength(strength); | 223 setTank2CannonStrength(strength); |
171 fireProjectile(); | 224 fireProjectile(); |
172 } | 225 } |
177 | 230 |
178 if(--gSamplesUntilNextFrame <= 0) { | 231 if(--gSamplesUntilNextFrame <= 0) { |
179 // Update game physics and cannon angles | 232 // Update game physics and cannon angles |
180 gSamplesUntilNextFrame = gGameFrameInterval; | 233 gSamplesUntilNextFrame = gGameFrameInterval; |
181 | 234 |
182 setTank1CannonAngle(map(analogRead(gInputTank1Angle, n), | 235 setTank1CannonAngle(map(AnalogRead(gInputTank1Angle, n), |
183 0, MATRIX_MAX, M_PI, 0)); | 236 0, 1.0, M_PI, 0)); |
184 setTank2CannonAngle(map(analogRead(gInputTank2Angle, n), | 237 setTank2CannonAngle(map(AnalogRead(gInputTank2Angle, n), |
185 0, MATRIX_MAX, M_PI, 0)); | 238 0, 1.0, M_PI, 0)); |
186 nextGameFrame(); | 239 nextGameFrame(); |
240 | |
241 // Check for collision and start sound accordingly | |
242 if(gameStatusCollisionOccurred()) { | |
243 gSoundBoomBufferPointer = 0; | |
244 } | |
187 } | 245 } |
188 | 246 |
189 if(gScreenBufferReadPointer >= gScreenBufferReadLength - 1 | 247 if(gScreenBufferReadPointer >= gScreenBufferReadLength - 1 |
190 && gScreenNextBufferReady) { | 248 && gScreenNextBufferReady) { |
191 // Got to the end; swap buffers | 249 // Got to the end; swap buffers |
197 float x = gScreenBufferRead[gScreenBufferReadPointer++]; | 255 float x = gScreenBufferRead[gScreenBufferReadPointer++]; |
198 float y = gScreenBufferRead[gScreenBufferReadPointer++]; | 256 float y = gScreenBufferRead[gScreenBufferReadPointer++]; |
199 | 257 |
200 // Rescale screen coordinates to matrix ranges; invert the Y | 258 // Rescale screen coordinates to matrix ranges; invert the Y |
201 // coordinate to go from normal screen coordinates to scope coordinates | 259 // coordinate to go from normal screen coordinates to scope coordinates |
202 analogWrite(gOutputX, n, constrain(map(x, 0, gScreenWidth, 0, MATRIX_MAX), 0, MATRIX_MAX)); | 260 AnalogWriteFrame(gOutputX, n, constrain(map(x, 0, gScreenWidth, 0, 1.0), 0, 1.0)); |
203 analogWrite(gOutputY, n, constrain(map(y, 0, gScreenHeight, MATRIX_MAX, 0), 0, MATRIX_MAX)); | 261 AnalogWriteFrame(gOutputY, n, constrain(map(y, 0, gScreenHeight, 1.0, 0), 0, 1.0)); |
204 } | 262 } |
205 else { | 263 else { |
206 // Still not ready! Write 0 until something happens | 264 // Still not ready! Write 0 until something happens |
207 analogWrite(gOutputX, n, 0); | 265 AnalogWriteFrame(gOutputX, n, 0); |
208 analogWrite(gOutputY, n, 0); | 266 AnalogWriteFrame(gOutputY, n, 0); |
209 } | 267 } |
210 | 268 |
211 if(gameStatusWinner() != 0) { | 269 if(gameStatusWinner() != 0) { |
212 // Blink one LED to show who won | 270 // Blink one LED to show who won |
213 // Blink both LEDs when projectile is in motion | 271 // Blink both LEDs when projectile is in motion |
214 uint16_t val = (gSampleCounter % 4000 > 2000) ? MATRIX_MAX : 0; | 272 float val = (gSampleCounter % 4000 > 2000) ? 1.0 : 0; |
215 analogWrite(gOutputPlayer1LED, n, gameStatusWinner() == 1 ? val : 0); | 273 AnalogWriteFrame(gOutputPlayer1LED, n, gameStatusWinner() == 1 ? val : 0); |
216 analogWrite(gOutputPlayer2LED, n, gameStatusWinner() == 2 ? val : 0); | 274 AnalogWriteFrame(gOutputPlayer2LED, n, gameStatusWinner() == 2 ? val : 0); |
217 | 275 |
218 // After 5 seconds, restart the game | 276 // After 5 seconds, restart the game |
219 gSamplesSinceFinish++; | 277 gSamplesSinceFinish++; |
220 if(gSamplesSinceFinish > 22050*5) | 278 if(gSamplesSinceFinish > 22050*5) |
221 gGameShouldRestart = true; | 279 gGameShouldRestart = true; |
222 } | 280 } |
223 else if(gameStatusProjectileInMotion()) { | 281 else if(gameStatusProjectileInMotion()) { |
224 // Blink both LEDs when projectile is in motion | 282 // Blink both LEDs when projectile is in motion |
225 uint16_t val = (gSampleCounter % 2000 > 1000) ? MATRIX_MAX : 0; | 283 float val = (gSampleCounter % 2000 > 1000) ? 1.0 : 0; |
226 analogWrite(gOutputPlayer1LED, n, val); | 284 AnalogWriteFrame(gOutputPlayer1LED, n, val); |
227 analogWrite(gOutputPlayer2LED, n, val); | 285 AnalogWriteFrame(gOutputPlayer2LED, n, val); |
228 } | 286 } |
229 else if(gameStatusPlayer1Turn()) { | 287 else if(gameStatusPlayer1Turn()) { |
230 analogWrite(gOutputPlayer1LED, n, MATRIX_MAX); | 288 AnalogWriteFrame(gOutputPlayer1LED, n, 1.0); |
231 analogWrite(gOutputPlayer2LED, n, 0); | 289 AnalogWriteFrame(gOutputPlayer2LED, n, 0); |
232 } | 290 } |
233 else { | 291 else { |
234 analogWrite(gOutputPlayer2LED, n, MATRIX_MAX); | 292 AnalogWriteFrame(gOutputPlayer2LED, n, 1.0); |
235 analogWrite(gOutputPlayer1LED, n, 0); | 293 AnalogWriteFrame(gOutputPlayer1LED, n, 0); |
236 } | 294 } |
237 | 295 |
238 // Check if we have reached the point where we should next update | 296 // Check if we have reached the point where we should next update |
239 if(gScreenBufferReadPointer >= gScreenBufferNextUpdateLocation && | 297 if(gScreenBufferReadPointer >= gScreenBufferNextUpdateLocation && |
240 !gScreenNextBufferReady) { | 298 !gScreenNextBufferReady) { |