-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab9Main.c
More file actions
809 lines (625 loc) · 16.9 KB
/
Lab9Main.c
File metadata and controls
809 lines (625 loc) · 16.9 KB
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
// Lab9Main.c
// Runs on MSPM0G3507
// Lab 9 ECE319K
// Your name
// Last Modified: 12/31/2023
#include <stdio.h>
#include <stdint.h>
#include <ti/devices/msp/msp.h>
#include "../inc/ST7735.h"
#include "../inc/Clock.h"
#include "../inc/LaunchPad.h"
#include "../inc/TExaS.h"
#include "../inc/Timer.h"
#include "../inc/ADC1.h"
#include "../inc/DAC5.h"
#include "SmallFont.h"
#include "LED.h"
#include "Switch.h"
#include "Sound.h"
#include "images/images.h"
// ****note to ECE319K students****
// the data sheet says the ADC does not work when clock is 80 MHz
// however, the ADC seems to work on my boards at 80 MHz
// I suggest you try 80MHz, but if it doesn't work, switch to 40MHz
uint32_t Data, Flag, Position, index2;
int32_t Timer=40;
uint32_t Sub=0;
# define GREENN 12
# define YELLOWW 17
# define REDD 13
int32_t timecounter;
uint32_t Zero;
uint32_t FakeTime;
uint32_t GameOver;
uint32_t HoopFlag;
uint32_t i=0;
uint32_t Count;
uint32_t xarray[4];
uint32_t z=0;
uint32_t PlayerScore;
const uint16_t *imagearray[4];
uint16_t sizearray [4];
struct sprite{
int32_t x, y;
int32_t w, h;
int32_t vx,vy;
int32_t oldx, oldy;
const uint16_t *blank;
const uint16_t *image;
const uint16_t *ball;
const uint16_t *white;
int life;
int32_t size;
};
typedef struct sprite sprite_t;
sprite_t ball;
struct hoop{
int32_t x, y;
int32_t w, h;
int32_t vx,vy;
int32_t oldx, oldy;
const uint16_t *blank;
const uint16_t *image;
int32_t size;
};
typedef struct hoop hoop_t;
hoop_t hoop;
void HoopInit1(void){
hoop.oldx=30;
hoop.oldy=60;
}
void HoopInit(void){
hoop.y=60;
hoop.w=60;
hoop.h=60;
hoop.vy=0;
hoop.vx=0;
hoop.blank=blackhoop;
hoop.size=60;
xarray[0]=15;
xarray[1]=30;
xarray[2]=50;
}
void BallInit(void){
ball.x=0;
ball.y=0;
ball.w=32;
ball.h=32;
ball.vy=-3;
ball.vx=0;
ball.oldx=50;
ball.oldy=159;
ball.image=hoop40;
ball.blank=black;
ball.life=1;
ball.size=20;
ball.ball=basketblack20;
// ball.white=basketwhite;
}
void BallInit2(void){
ball.x=ball.x;
ball.y=0;
ball.w=32;
ball.h=32;
ball.vy=-3;
ball.vx=0;
ball.oldx=50;
ball.oldy=159;
ball.image=hoop40;
ball.blank=black;
ball.life=1;
ball.size=20;
ball.ball=basketblack20;
// ball.white=basketwhite;
}
void ArrayInit(void){
imagearray[0]=basketblack30;
imagearray[1]=basketblack28;
imagearray[2]=basketblack25;
imagearray[3]=basketblack20;
xarray[0]=50;
/// xarray[1]=70;
/// xarray[2]=90;
/// xarray[3]=40;
/// xarray[4]=20;
sizearray[0]=30;
sizearray[1]=28;
sizearray[2]=25;
sizearray[3]=20;
}
void BallMove(void){
ball.y=ball.oldy;
// ball.x=ball.oldx+ball.vx;
ball.image=basketblack20;
ball.size=20;
i++;
if(i>=4){
i=3;
}
}
void BallMove2(void){
ball.y=ball.oldy+ball.vy;
ball.x=ball.oldx+ball.vx;
ball.image=basketblack20;
ball.size=20;
i++;
if(i>=4){
i=3;
}
}
void ChangeHoopCoord(void){
hoop.oldx=hoop.x;
}
void ChangeCoord(void){
ball.oldy=ball.y;
ball.oldx=ball.x;
ball.size=ball.size;
//ball.ball=ball.image;
hoop.x=xarray[z];
z++;
if(z>3){
z=0;
}
}
uint32_t ScoreCounter=0; //score for Player 1
uint32_t ScoreCounter2=0; //score for Player 2
char Player1[] = "Score:";
char Time[]="Clock:";
char TimeSpan[]="Jam:";
char Player2[]="Score:";
char Jugador1[] = "Skor:";
char Jugador2[] = "Skor:";
char *playerarrayspan[2];
char *playerarray[2];
// do not need to use IIDX when there is a single interrupt source
//}
//get data from adc and pixels to move
//make old pixels black
//put ball on new pixels
//}
uint32_t noslide;
void PLL_Init(void){ // set phase lock loop (PLL)
// Clock_Init40MHz(); // run this line for 40MHz
Clock_Init80MHz(0); // run this line for 80MHz
}
uint32_t M=1;
uint32_t Random32(void){
M = 1664525*M+1013904223;
return M;
}
uint32_t Random(uint32_t n){
return (Random32()>>16)%n;
}
uint32_t shootball, stopshoot, shootballleft, shootballright;
// games engine runs at 30Hz
uint32_t ConvertXPos(uint32_t Data){
if (noslide==1){
return ball.x;
}
return (128*Data)/4096;
}
uint32_t beforetime;
void Sound_Buzzer2(void);
void TIMG12_IRQHandler(void){uint32_t pos,msg,Ps;
static uint32_t last0=0;
uint32_t now0 = Switch_In();
if((TIMG12->CPU_INT.IIDX) == 1){ // this will acknowledge
// game engine goes here
timecounter++;
ball.x=ConvertXPos(ADCin());
Flag=1;
if((now0 == 0x80000)&&(last0 == 0)){
// just touched, runs once per touch
shootball=1;
}
if(((GPIOB->DIN31_0 & (1<<7)) == 0x80)&&(last0 == 0)){
shootballleft=1;
// just released, runs once per release
}
if(((GPIOA->DIN31_0 & (1<<8)) == 0x100)&&(last0 == 0)){
shootballright=1;
// just released, runs once per release
}
last0 = now0;
}
// 2) read input switches
// 3) move sprites
// 4) start sounds
// 5) set semaphore
int Lang_Flag;
// NO LCD OUTPUT IN INTERRUPT SERVICE ROUTINES
if(timecounter>=1212){
if(Lang_Flag==1){
if(playerarray[index2]==Player2){
//index2--;
GameOver=1;
noslide=0;
ArrayInit();
BallInit2();
shootball=0;
shootballleft=0;
shootballright=0;
timecounter=0;
Zero=1;
Sound_Buzzer2();
}
else if(playerarray[index2]!=Player2){
index2++;
noslide=0;
ArrayInit();
BallInit2();
shootball=0;
shootballleft=0;
shootballright=0;
timecounter=0;
beforetime=(timecounter*33)/1000;
Zero=1;
PlayerScore=1;
Sound_Buzzer2();
}
}
if(Lang_Flag==2){
if(playerarrayspan[index2]==Jugador2){
// index2--;
GameOver=1;
noslide=0;
ArrayInit();
BallInit2();
shootball=0;
shootballleft=0;
shootballright=0;
timecounter=0;
Zero=1;
Sound_Buzzer2();
}
else if(playerarrayspan[index2]!=Jugador2){
index2++;
noslide=0;
ArrayInit();
BallInit2();
shootball=0;
shootballleft=0;
shootballright=0;
timecounter=0;
beforetime=(timecounter*33)/1000;
Zero=1;
PlayerScore=1;
Sound_Buzzer2();
}
}
}
}
void ChangeHoopCoord(void);
uint8_t TExaS_LaunchPadLogicPB27PB26(void){
return (0x80|((GPIOB->DOUT31_0>>26)&0x03));
}
int Lang_Flag=0;
int main2(void){ // main2
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
ST7735_InitPrintf();
//note: if you colors are weird, see different options for
// ST7735_InitR(INITR_REDTAB); inside ST7735_InitPrintf()
ST7735_FillScreen(ST7735_BLACK);
ST7735_InvertDisplay(1);
ST7735_DrawBitmap(110, 110, players2, 16, 50); // player ship bottom
for(uint32_t t=500;t>0;t=t-5){
SmallFont_OutVertical(t,104,6); // top left
Clock_Delay1ms(50); // delay 50 msec
}
ST7735_FillScreen(0x0000); // set screen to black
ST7735_SetCursor(1, 1);
ST7735_OutString("GAME OVER");
ST7735_SetCursor(1, 2);
ST7735_OutString("Nice try,");
ST7735_SetCursor(1, 3);
ST7735_OutString("Earthling!");
ST7735_SetCursor(2, 4);
ST7735_OutUDec(1234);
while(1){
}
}
// ALL ST7735 OUTPUT MUST OCCUR IN MAIN
int main(void){ // final main
uint32_t timethen,timenow;
uint32_t scoredone, scoredone2;
__disable_irq();
PLL_Init(); // set bus speed
LaunchPad_Init();
ST7735_InitPrintf();
//note: if you colors are weird, see different options for
// ST7735_InitR(INITR_REDTAB); inside ST7735_InitPrintf()
ST7735_FillScreen(ST7735_BLACK);
ST7735_InvertDisplay(1);
ADCinit(); //PB18 = ADC1 channel 5, slidepot
Switch_Init(); // initialize switches
LED_Init(); // initialize LED
Sound_Init(); // initialize sound
//positioninit();
TExaS_Init(0,0,&TExaS_LaunchPadLogicPB27PB26); // PB27 and PB26
// initialize interrupts on TimerG12 at 30 Hz
TimerG12_IntArm(80000000/30,2);
// TimerG0_IntArm(period2, 1,3);
ST7735_InvertDisplay(1);
// initialize all data structures
ArrayInit();
BallInit2();
HoopInit1();
HoopInit();
playerarray[0]= Player1;
playerarray[1]= Player2;
playerarrayspan[0]=Jugador1;
playerarrayspan[1]=Jugador2;
uint32_t Switch_InA(void);
ST7735_SetTextColor(62266);
ST7735_SetCursor(1, 1);
ST7735_OutString("Welcome to Arcade");
ST7735_SetCursor(1, 2);
ST7735_OutString("Hoops!");
ST7735_SetTextColor(64640);
ST7735_SetCursor(1, 3);
ST7735_OutString("Selamat datang di");
ST7735_SetCursor(1, 4);
ST7735_OutString("Arcade Hoops!");
ST7735_SetCursor(1, 6);
ST7735_SetTextColor(65480);
ST7735_OutString("Push top button");
ST7735_SetCursor(1, 7);
ST7735_OutString("for English");
ST7735_SetCursor(1, 8);
ST7735_OutString("and bottom button");
ST7735_SetCursor(1, 9);
ST7735_OutString("for Indonesian.");
ST7735_SetTextColor(9920);
ST7735_SetCursor(1, 11);
ST7735_OutString("Tekan tombol");
ST7735_SetCursor(1, 12);
ST7735_OutString("atas untuk");
ST7735_SetCursor(1, 13);
ST7735_OutString("Bahasa Inggris dan");
ST7735_SetCursor(1, 14);
ST7735_OutString("tombol bawah untuk");
ST7735_SetCursor(1, 15);
ST7735_OutString("Bahasa Indonesia.");
uint32_t lang_select;
while(Lang_Flag==0){
lang_select = Switch_InA();
if(lang_select == 0x8000){
Lang_Flag=2;
}
if(lang_select == 0x20000){ //PA17
Lang_Flag=1;
}
}
ST7735_FillScreen(0x0000);
void Ding(void);
__enable_irq();
beforetime=(timecounter*33)/1000;
while(1){
moveagain:
if(GameOver==1){
ST7735_FillScreen(0x0000);
ST7735_SetCursor(1, 1);
if(Lang_Flag==1){
ST7735_OutString("Game Over");
if(ScoreCounter>ScoreCounter2){
ST7735_SetCursor(1, 2);
ST7735_SetTextColor(64640);
ST7735_OutString("Player 1 Wins!");
}
else if(ScoreCounter<ScoreCounter2){
ST7735_SetCursor(1, 2);
ST7735_SetTextColor(64640);
ST7735_OutString("Player 2 Wins");
}
else if(ScoreCounter==ScoreCounter2){
ST7735_SetCursor(1, 2);
ST7735_SetTextColor(64640);
ST7735_OutString("Tie");
}
}
if(Lang_Flag==2){
ST7735_OutString("Permainan berakhir");
if(ScoreCounter>ScoreCounter2){
ST7735_SetCursor(1, 2);
ST7735_SetTextColor(64640);
ST7735_OutString("Pemain 1 Menang");
}
else if(ScoreCounter<ScoreCounter2){
ST7735_SetCursor(1, 2);
ST7735_SetTextColor(64640);
ST7735_OutString("Pemain 2 Menang");
}
else if(ScoreCounter==ScoreCounter2){
ST7735_SetCursor(1, 2);
ST7735_SetTextColor(64640);
ST7735_OutString("Dasi");
}
}
while(1){}
}
if(Zero==0){
if(Lang_Flag==1){
ST7735_SetCursor(2, 12);
if(((timecounter*33)/1000)<=9){
printf("%s", Time);
printf("%s", "00");
printf("%d", (timecounter*33)/1000);
}
else if (((timecounter*33)/1000)>9){
if((timecounter*33)/1000==37){
LED_On(GREENN);
}
if((timecounter*33)/1000==38){
LED_Off(GREENN);
LED_On(YELLOWW);
}
if((timecounter*33)/1000==39){
LED_Off(YELLOWW);
LED_On(REDD);
ST7735_DrawBitmap(ball.x, ball.y, ball.blank, ball.size, ball.size);
}
printf("%s", Time);
printf("%s", "0");
printf("%d", (timecounter*33)/1000);
}
}
if(Lang_Flag==2){
ST7735_SetCursor(2, 12);
if(((timecounter*33)/1000)<=9){
printf("%s", TimeSpan);
printf("%s", "00");
printf("%d", (timecounter*33)/1000);
}
else if (((timecounter*33)/1000)>9){
if((timecounter*33)/1000==37){
LED_On(GREENN);
}
if((timecounter*33)/1000==38){
LED_Off(GREENN);
LED_On(YELLOWW);
}
if((timecounter*33)/1000==39){
LED_Off(YELLOWW);
LED_On(REDD);
ST7735_DrawBitmap(ball.x, ball.y, ball.blank, ball.size, ball.size);
}
printf("%s", TimeSpan);
printf("%s", "0");
printf("%d", (timecounter*33)/1000);
}
}
}
else if(Zero==1){
ST7735_DrawBitmap(ball.x, ball.y, ball.blank, ball.size, ball.size);
LED_Off(REDD);
if(Lang_Flag==1){
Zero=0;
ST7735_SetCursor(2, 12);
printf("%s", Time);
printf("%s", "000");
}
if(Lang_Flag==2){
Zero=0;
ST7735_SetCursor(2, 12);
printf("%s", TimeSpan);
printf("%s", "000");
}
}
ST7735_SetCursor(2, 10);
if(Lang_Flag==1){
printf("%s", playerarray[index2] );
if(playerarray[index2]==Player2){
printf("%2d", ScoreCounter2);
}
else if(playerarray[index2]!=Player2){
printf("%2d", ScoreCounter);
}
}
if(Lang_Flag==2){
printf("%s", playerarrayspan[index2] );
if(playerarrayspan[index2]==Jugador2){
printf("%2d", ScoreCounter2);
}
else if(playerarrayspan[index2]!=Jugador2){
printf("%2d", ScoreCounter);
}
}
while(Flag==0){}
Flag=0;
if((((timecounter*33)/1000) - beforetime) == 2){
beforetime=((timecounter*33)/1000);
ST7735_DrawBitmap(hoop.oldx, hoop.oldy, blackhoop, hoop.size, hoop.size); // player ship bottom
ST7735_DrawBitmap(hoop.x, hoop.y, hoop40, hoop.size, hoop.size); // player ship bottom
ChangeHoopCoord();
}
// ChangeCoord();
BallMove();
/// while(PS){ }
// ST7735_DrawBitmap(hoop.oldx, hoop.oldy, hoop40, hoop.size, hoop.size); // player ship bottom
//ST7735_DrawBitmap(hoop.x, hoop.y, hoop40, hoop.size, hoop.size); // player ship bottom
ST7735_DrawBitmap(ball.oldx, ball.oldy, ball.blank, ball.size, ball.size);
ST7735_DrawBitmap(ball.x, ball.y, ball.image, ball.size, ball.size);
//HoopInit();
ChangeCoord();
ST7735_DrawBitmap(110, 110, players1, 16, 50); // player ship bottom
if(PlayerScore){
ST7735_DrawBitmap(110, 110, BLACK, 16, 50); // player ship bottom
ST7735_DrawBitmap(110, 110, players2, 16, 50); // player ship bottom
}
while(shootball==0 & shootballleft==0 & shootballright==0){goto moveagain;}
if(shootball==1){
shootball=1;
noslide=1;
ball.vy=-3;
}
else if(shootballleft==1){
shootballleft=1;
ball.vx=-2;
ball.vy=-3;
}
else if(shootballright==1){
shootballright=1;
ball.vx=+2;
ball.vy=-3;
}
if(ball.y<=34){
ST7735_DrawBitmap(hoop.oldx, hoop.oldy, hoop40, 60, 60); // player ship bottom
if((ball.y==33) && (ball.x>=(hoop.oldx+5)) && (ball.x<=(hoop.oldx+25))){
if(Lang_Flag == 1){
if(playerarray[index2]==Player2){
ScoreCounter2++;
Ding();
}
ScoreCounter++;
Ding();
}
if(Lang_Flag == 2){
if(playerarrayspan[index2]==Jugador2){
ScoreCounter2++;
Ding();
}
ScoreCounter++;
Ding();
}
}
uint32_t wait=10000;
ST7735_DrawBitmap(ball.oldx, ball.oldy, ball.blank, ball.size, ball.size );
/// ST7735_DrawBitmap(0, 159, brickwall, 128, 160);
ball.y=79;
ST7735_DrawBitmap(ball.x, ball.y, ball.image, ball.size, ball.size );
ball.vy=-ball.vy;
ChangeCoord();
BallMove();
while(wait!=0){
wait--;
}
}
// ST7735_DrawBitmap(ball.oldx, ball.oldy, ball.ball, ball.size, ball.size);
noslide=1;
ST7735_DrawBitmap(hoop.oldx, hoop.oldy, hoop40, 60, 60); // player ship bottom
ST7735_DrawBitmap(ball.oldx, ball.oldy, ball.blank, ball.size, ball.size);
BallMove2();
ST7735_DrawBitmap(ball.x, ball.y, ball.image, ball.size, ball.size);
ChangeCoord();
if(ball.vy==+3){
ST7735_DrawBitmap(hoop.oldx, hoop.oldy, blackhoop, 60, 60); // player ship bottom
ST7735_DrawBitmap(hoop.oldx, hoop.oldy, hoop40, 60, 60); // player ship bottom !!
///ST7735_DrawBitmap(0, 159, brickwall, 128, 160);
ST7735_DrawBitmap(ball.oldx, ball.oldy, ball.blank, ball.size, ball.size );
ArrayInit();
BallInit2();
shootball=0;
shootballleft=0;
shootballright=0;
noslide=0;
}
}
}
// delay 50 msec
// wait for semaphore
// clear semaphore
// update ST7735R
// check for end game or level switch