/
utilites.c
574 lines (479 loc) · 12.1 KB
/
utilites.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* utilities.c
*
* Created on: 2014-01-30
* Author: Tommy Cheang, Joanna Leung, Ellina Sergeyeva, Kris_Mazurek
*/
#include "tanks.h"
// *********************** Audio related functions *************************************
//-----------------------------------------------------------------------------------------------------------------
/*
* Overview: This configures the audio and video.
*
*/
void av_config_setup()
{
alt_up_av_config_dev * av_config =
alt_up_av_config_open_dev("/dev/audio_video_config");
if(av_config == NULL)
{
//printf("Configuration failed\n");
}
while (!alt_up_av_config_read_ready(av_config)){}
//printf("Audio video configuration complete\n");
}
//---------------------------------------------------------------------------------------------------------------------------------------
/*
* Overview: This checks if the sdcard is connected or not
* This also checks if the SDCard is of the correct format of FAT16
*/
void sdcardcheck(alt_up_sd_card_dev *device_reference)
{
char file_name[10];
int connected = 0;
if (device_reference != NULL)
{
while(1)
{
if ((connected == 0) && (alt_up_sd_card_is_Present()))
{
//printf("Card connected.\n");
if (alt_up_sd_card_is_FAT16())
{
//printf("FAT16 file system detected.\n");
if(alt_up_sd_card_find_first(".", (char*) file_name)==0)
{
//printf("%s\n",file_name);
while(alt_up_sd_card_find_next((char*)file_name)==0)
{
//printf("%s\n", file_name);
alt_up_sd_card_read(file_name);
}
}
break;
}
else
{
//printf("Unknown file system.\n");
}
connected = 1;
}
else if ((connected == 1) && (alt_up_sd_card_is_Present() == false))
{
//printf("Card disconnected.\n"); connected = 0;
}
}
}
else
{
//printf("Card could not be opened\n");
}
}
/*
* Overview: This is the function for reading the bytes of the coin sound for when the bullet is shot out of the cannon.
*
*/
int readCoin(unsigned int *buffer, char* filename, short int handle)
{
short int data;
unsigned char upper;
unsigned char lower;
int size;
alt_up_audio_dev *audio=alt_up_audio_open_dev("/dev/audio");
//printf("Now loading: %s\n", filename);
handle = alt_up_sd_card_fopen(filename, false);
if (handle < 0)
{
//printf("Failed to open the file");
return;
}
int i;
//printf("first for loop\n");
for (i = 0; i < 46; i++)
{
alt_up_sd_card_read(handle);
}
size = 0;
while (1)
{
////printf("inside the while loop\n");
data = alt_up_sd_card_read(handle);
////printf("data is 0x%x", data);
if (data == -1)
{
break;
}
else
{
lower = data;
}
data = alt_up_sd_card_read(handle);
if (data == -1)
{
break;
}
else
{
upper = data;
}
buffer[size] = ((upper << 8) | lower);
////printf("buffer is 0x%x\n", buffer[size]);
size++;
if (size + 1 > COIN_SIZE)
{//7720 for cha ching sound
break;
}
//play_audio(audio, buffer, handle);
}
/*while(1)//do this to loop
{
play_audio(audio, buffer, handle);
}*/
////printf("Song size: %d\n", size);
alt_up_sd_card_fclose(handle);
return size;
}
/*
* Overview: This is the function for reading the bytes of the boom sound for when the bullet hits a tank
*
*/
int readDoh(unsigned int *buffer, char* filename, short int handle)
{
short int data;
unsigned char upper;
unsigned char lower;
int size;
alt_up_audio_dev *audio=alt_up_audio_open_dev("/dev/audio");
//printf("Now loading: %s\n", filename);
handle = alt_up_sd_card_fopen(filename, false);
if (handle < 0) {
printf("Failed to open the file");
return;
}
int i;
//printf("first for loop\n");
for (i = 0; i < 46; i++) {
alt_up_sd_card_read(handle);
}
size = 0;
while (1) {
//printf("inside the while loop\n");
data = alt_up_sd_card_read(handle);
//printf("data is 0x%x", data);
if (data == -1) {
break;
} else {
lower = data;
}
data = alt_up_sd_card_read(handle);
if (data == -1) {
break;
} else {
upper = data;
}
buffer[size] = ((upper << 8) | lower);
//printf("buffer is 0x%x\n", buffer[size]);
size++;
if (size + 1 > DOH_SIZE) {//7720 for cha ching sound
break;
}
//play_doh(audio, buffer, handle);
}
//break;
//printf("Song size: %d\n", size);
alt_up_sd_card_fclose(handle);
return size;
}
/*
* Overview: This plays the audio for the coin sound
*
*/
void play_audio(alt_up_audio_dev *audio, unsigned int *buffer, short int handle)
{
int lbytes;
int rbytes;
int lindex=22;
int rindex=22;
alt_up_audio_dev* aud=audio;
int i=0;
while(i<COIN_LENGTH) //200 for cha ching
{
if (alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_LEFT) > 22)
{
lbytes = alt_up_audio_write_fifo(aud, &buffer[lindex], 96, ALT_UP_AUDIO_LEFT);
rbytes = alt_up_audio_write_fifo(aud, &buffer[rindex], 96, ALT_UP_AUDIO_RIGHT);
lindex += lbytes;
rindex += rbytes;
i++;
}
}
//printf("all good things come to an end\n");
alt_up_sd_card_fclose(handle);
}
/*
* Overview: This plays the boom sound when the tanks are hit
*
*/
void play_doh(alt_up_audio_dev *audio, unsigned int *buffer, short int handle)
{
int lbytes;
int rbytes;
int lindex=22;
int rindex=22;
alt_up_audio_dev* aud=audio;
int i=0;
while(i<DOH_LENGTH) //200 for cha ching
{
if (alt_up_audio_write_fifo_space(audio, ALT_UP_AUDIO_LEFT) > 22)
{
lbytes = alt_up_audio_write_fifo(aud, &buffer[lindex], 96, ALT_UP_AUDIO_LEFT);
rbytes = alt_up_audio_write_fifo(aud, &buffer[rindex], 96, ALT_UP_AUDIO_RIGHT);
lindex += lbytes;
rindex += rbytes;
i++;
}
}
//printf("all good things come to an end\n");
alt_up_sd_card_fclose(handle);
}
//----------------------------------------------------------------------------------------------------------------------------
/*
* Overview: The origins of the coin sound
*
*/void sound()
{
short int handle;
//printf("SDCARD CONNECTION CHECK\n");
alt_up_sd_card_dev *device_reference=alt_up_sd_card_open_dev("/dev/Altera_UP_SD_Card_Avalon_Interface_0");
alt_up_audio_dev *audio;
sdcardcheck(device_reference);
//printf("AV CONFIG SETUP\n");
av_config_setup();
//printf("AUDIO OPEN\n");
audio=alt_up_audio_open_dev("/dev/audio");
unsigned int buffer[COIN_SIZE];
char *filename="coin.wav";
readCoin(buffer, filename, handle);
play_audio(audio, buffer, handle);
alt_up_sd_card_fclose(filename);
}
/*
* Overview: The origins of the boom sound
*
*/
void sound_doh()
{
short int handle;
short int doh_handle;
int check;
//printf("SDCARD CONNECTION CHECK\n");
alt_up_sd_card_dev *device_reference=alt_up_sd_card_open_dev("/dev/Altera_UP_SD_Card_Avalon_Interface_0");
alt_up_audio_dev *audio;
sdcardcheck(device_reference);
//printf("AV CONFIG SETUP\n");
av_config_setup();
//printf("AUDIO OPEN\n");
audio=alt_up_audio_open_dev("/dev/audio");
unsigned int buffer[DOH_SIZE];
char *filename="boom1.wav";
readDoh(buffer, filename, doh_handle);
play_audio(audio, buffer, handle);
alt_up_sd_card_fclose(filename);
}
// *********************** High Score related functions ********************************
/*
* Overview: Stores score on SD card.
*
*/
void store_high_score_sd_card( alt_up_sd_card_dev * sd, char * filename, struct tank * t ){
struct tank* tank = (struct tank *) t;
int file_handle; // write to this file
int i = 0; // index
char buffer[SD_CHAR_BUF_LENGTH]; // buffer
sprintf( buffer, "%d", tank->score); // put integer to a char buffer
if(sd != NULL) {
if(alt_up_sd_card_is_Present()) {
file_handle =alt_up_sd_card_fopen(filename, false);
if(file_handle < 0)
printf("Text file open failed\n");
else {
//printf("\nStoring data...\n");
for(i =0; i < SD_CHAR_BUF_LENGTH; i++) {
alt_up_sd_card_write(file_handle, buffer[i]);
}
//printf("Done storing and Closing File...\n");
alt_up_sd_card_fclose(file_handle);
//printf("Closed the file\n\n");
}
}
}
}
/*
* Overview: Helper function to print out the contents of the given file to console.
*
*/
void print_file( alt_up_sd_card_dev * sd, char * filename ){
char buf_read [SD_CHAR_BUF_LENGTH_LONG];
int file;
int i;
if(sd != NULL){
if(alt_up_sd_card_is_Present()) {
file = alt_up_sd_card_fopen(filename, false);
if(file < 0)
printf("Text File open failed\n");
else {
//printf("Reading from file...\n");
for(i=0; i<SD_CHAR_BUF_LENGTH_LONG; i++) {
buf_read[i]=alt_up_sd_card_read(file);
}
//printf("Done reading\n");
alt_up_sd_card_fclose(file);
//printf("Closed the file\n");
printf("\nPrinting out file contents\n");
for (i = 0; i <SD_CHAR_BUF_LENGTH_LONG; i++ ) {
printf("%c", buf_read[i] );
}
printf("\n");
}
}
}
}
/*
* Overview: Read high score of the previous game
*
*/
void load_high_score( alt_up_sd_card_dev * sd, struct high_score * highscore )
{
struct high_score* scores = (struct high_score *) highscore;
char buf1[SD_CHAR_BUF_LENGTH_SHORT]; // player 1 buffer
char buf2[SD_CHAR_BUF_LENGTH_SHORT]; // player 2 buffer
int intBuf1[SD_CHAR_BUF_LENGTH_SHORT];
int intBuf2[SD_CHAR_BUF_LENGTH_SHORT];
int file_player1, file_player2; // file handles
int i = 0; //index
if(sd != NULL){
if(alt_up_sd_card_is_Present()) {
// Player 1 previous score
file_player1 = alt_up_sd_card_fopen(FILENAME_SCORE1, false);
if(file_player1 < 0) {
printf("Text File open failed\n");
return;
}
else{
//printf("\nPlayer 1: Reading prev score\n");
for(i=0; i<SD_CHAR_BUF_LENGTH_SHORT; i++) {
buf1[i]=alt_up_sd_card_read(file_player1);
}
alt_up_sd_card_fclose(file_player1);
}
// Player 2 previous score
file_player2 = alt_up_sd_card_fopen(FILENAME_SCORE2, false);
if(file_player2 < 0) {
printf("Text File open failed\n");
return;
}
else{
//printf("Player 2: Reading prev score\n");
for(i=0; i<SD_CHAR_BUF_LENGTH_SHORT; i++) {
buf2[i]=alt_up_sd_card_read(file_player2);
}
alt_up_sd_card_fclose(file_player2);
}
}
}
if(buf1[0] != NULL ){
scores->highscore_p1 = buf1[0]; // load the value to player 1 highscore struct
}
else
printf("Null reference p1\n");
if(buf2[0] != NULL ){
scores->highscore_p2 = buf2[0]; // load the value to player 2 highscore struct
}
else
printf("Null reference p1\n");
return;
}
/*
* Overview: Helper function to test SD card.
* Check connectivity, file system, and any present files.
*
*/
void testsdcard(alt_up_sd_card_dev *device_reference)
{
char file_name[10];
int connected = 0;
if (device_reference != NULL)
{
while(1)
{
if ((connected == 0) && (alt_up_sd_card_is_Present()))
{
printf("Card connected.\n");
if (alt_up_sd_card_is_FAT16())
{
printf("FAT16 file system detected.\n");
if(alt_up_sd_card_find_first(".", (char*) file_name)==0)
{
printf("%s\n",file_name);
while(alt_up_sd_card_find_next((char*)file_name)==0)
{
printf("%s\n", file_name);
alt_up_sd_card_read(file_name);
}
}
break;
}
else
{
printf("Unknown file system.\n");
}
connected = 1;
}
else if ((connected == 1) && (alt_up_sd_card_is_Present() == false))
{
printf("Card disconnected.\n"); connected = 0;
}
}
}
else
{
printf("Card could not be opened\n");
}
}
/*
* Overview: exercise 3 reads sd_card and list its file.
*
*/
void sd_card(void){
char file_name[10];
alt_up_sd_card_dev *device_reference = NULL;
int connected = 0;
device_reference = alt_up_sd_card_open_dev(ALTERA_UP_SD_CARD_AVALON_INTERFACE_0_NAME);
if (device_reference != NULL) {
while(1) {
if ((connected == 0) && (alt_up_sd_card_is_Present())) {
//printf("Card connected.\n");
if (alt_up_sd_card_is_FAT16()) {
//printf("FAT16 file system detected.\n");
if(alt_up_sd_card_find_first(".", (char*) file_name)==0){
//printf("%s\n",file_name);
while(alt_up_sd_card_find_next((char*)file_name)==0)
{
//printf("%s\n", file_name);
}
}
} else {
//printf("Unknown file system.\n");
}
connected = 1;
} else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)) {
//printf("Card disconnected.\n");
connected = 0;
}
break;
}
} else {
//printf("Card could not be opened\n");
}
//printf("sd_card function finished\n");
}