-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
157 lines (131 loc) · 4.06 KB
/
Copy pathmain.c
File metadata and controls
157 lines (131 loc) · 4.06 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
/*
* XBeeTest
*
* Tests XBee module on AT91 board
* Print any messages received and transmits if a key is depressed.
*
*/
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "timer.h"
#include "uart.h"
#include "AT91PIO.h"
#include "pg12864.h"
#include "keypad.h"
#include "Delay.h"
#include "sound.h"
#include "XBee.h"
#include "GameHeader.h"
void main(void) {
int key; /* keycode */
int XBeeCode; /* XBee initialisation code */
SnakeMove snakeBuffer;
/* Initialise GPIO */
AT91InitialisePIO();
/* Initialize UART module and register getchar/putchar callbacks. */
UartInit(AT91UartGetchar, AT91UartPutchar);
/* First disable interrupts. */
__disable_interrupt();
/* Setup interrupt controller - for timer and UART Rx services */
AT91InitInterrupt(TimerBeat, UartRxrdy);
/* Periodic timer initialization. */
AT91InitTimer();
/* Setup serial port - baud rate, etc. */
AT91UartInit();
/* Enable interrupts - timer and UART Rx services */
__enable_interrupt();
/* Start periodic timer. */
AT91StartTimer();
/* Initialise sound interface */
soundInit();
/* Initialise LCD and clear display */
LCD_Init();
Delay_ms(500);
LCD_ClearDisplay();
/* Title string */
LCD_PutString("Starting Snake \n");
/* Initialise XBee module
Channel= 0x0C
PAN ID= 0x3330
Source address= 0x0001
Destination address= 0x0000 0000 0000 FFFF - a broadcast address */
XBeeCode= XBeeInit( 0x0C, 0x3330, 0x0001, 0x00000000, 0x0000FFFF );
if ( XBeeCode == 0 )
LCD_PutString("XBee OK\n");
else if ( XBeeCode == -2 )
LCD_PutString("XBee init error\n");
else if ( XBeeCode == -1 )
LCD_PutString("XBee param error\n");
else
LCD_PutString("XBee FAILED! \n");
s_GameInstance.m_updateCount = 0;
s_GameInstance.m_currState = STATE_WAITING_FOR_HOST;
/* Main loop - print anything received and send message if key is pressed */
while (TRUE)
{
switch(s_GameInstance.m_currState)
{
case STATE_WAITING_FOR_HOST:
{
// Waiting Indicator
LCD_PositionCursor(0,40);
switch(s_GameInstance.m_updateCount % 10)
{
case 0: LCD_PutString("-^-_______"); break;
case 1: LCD_PutString("_-^-______"); break;
case 2: LCD_PutString("__-^-_____"); break;
case 3: LCD_PutString("___-^-____"); break;
case 4: LCD_PutString("____-^-___"); break;
case 5: LCD_PutString("_____-^-__"); break;
case 6: LCD_PutString("______-^-_"); break;
case 7: LCD_PutString("_______-^-"); break;
case 8: LCD_PutString("-_______-^"); break;
case 9: LCD_PutString("^-_______-"); break;
}
// Press a button to claim hostship
if ( ( key= keyPress() ) == 0x0F )
{
LCD_ClearDisplay();
LCD_PutString("Game Started \n");
StartGame(key);
}
// Check if someone is hosting Game
else if( RecvData((char*)&snakeBuffer, sizeof(SnakeMove)) > 0)
{
// Check its a game starting and not a game in progress
if(snakeBuffer.m_updateCount == 1)
{
LCD_ClearDisplay();
LCD_PutString("Game Joined \n");
JoinGame(&snakeBuffer);
}
}
// DO NOT CLEAR SCREEN!!!
Sleep(500);
}
break;
case STATE_PLAYING:
{
// Normal Game loop
UpdateNetwork();
if(s_GameInstance.m_currState == STATE_PLAYING)
{
UpdateGame();
UpdateScreen();
UpdateInput();
}
}
break;
case STATE_GAME_OVER:
{
// Display Game Over Message
// Waiting
memset(&s_GameInstance, 0, sizeof(SnakeGame));
s_GameInstance.m_currState = STATE_WAITING_FOR_HOST;
}
break;
}
s_GameInstance.m_updateCount += 1;
}
}