This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakesrv.c
221 lines (196 loc) · 5.63 KB
/
snakesrv.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
#include <funk/nrf24l01p.h>
#include <funk/rftransfer.h>
#include "snake_shared.c"
void host (void);
// radio function prototypes
void initRadio(void);
uint8_t switchToHostModeAndWaitForClients(int timeout); // returns -1 if timeout (no host found), gameID (bit 2-5), bacon x (6-10) and bacon y (11-15) else
uint8_t receiveKeyPressed(int timeout); // to be used by host in wait loop
void sendKeyPressed(uint8_t keyPressed, int timeout); // to be used by client in wait loop
void receiveMove(uint8_t * display, uint8_t * baconx, uint8_t * bacony, int timeout); // to be used by client when game should be received (display must be uint8_t[52], baconx and y uint8_t)
void sendMove(uint8_t * display, uint8_t baconx, uint8_t bacony, int timeout); // to be used by host when game display must be sent (display must be uint8_t[52])
struct NRF_CFG config =
{
.channel= 81,
.txmac= "\x1\x2\x3\x5\x5",
.nrmacs=1,
.mac0= "\x1\x2\x3\x5\x5",
.maclen ="\x20",
};
vringpbuf snake2;
point bacon;
int8_t direction2;
void ram(void) {
//if (switchToHostModeAndWaitForClients (10000)) { // wait for 10 secs
//someone joined
nrf_config_set(&config);
nrf_set_strength(3);
host();
//}
}
inline void host (void) {
uint8_t key = BTN_RIGHT, button;
uint8_t p2key = BTN_LEFT;
lcdClear();
initSnake (&snake2, 3, GAME_HEIGHT-1, 0, GAME_HEIGHT-1, DIRECTION_LEFT);
direction2=DIRECTION_LEFT;
uint8_t dsp [GAME_SIZE/8];
for (i=0; i<GAME_SIZE/8; i++)
dsp[i] = 0;
while (1) {
sendMove (dsp, bacon.x, bacon.y, 0);
switch (key) {
case BTN_ENTER:
// exit
return;
case BTN_RIGHT:
if (direction2 != DIRECTION_LEFT)
direction2 = DIRECTION_RIGHT;
break;
case BTN_UP:
if (direction2 != DIRECTION_DOWN)
direction2 = DIRECTION_UP;
break;
case BTN_LEFT:
if (direction2 != DIRECTION_RIGHT)
direction2 = DIRECTION_LEFT;
break;
case BTN_DOWN:
if (direction2 != DIRECTION_UP)
direction2 = DIRECTION_DOWN;
break;
case BTN_NONE:
break;
default:
direction2 = (direction2 + 1) % 4;
break;
//Default: No keystroke received. Assuming last keystroke.
}
point newendpoint = snake2.endpoint;
shiftPoint (&newendpoint, direction2);
bool resetBacon = false;
if (newendpoint.x == bacon.x && newendpoint.y == bacon.y) {
growBuf (&snake2, direction2);
resetBacon = true;
} else {
setGamePixel(snake2.startpoint.x, snake2.startpoint.y, 0);
shiftBuf (&snake2, direction2);
}
if (getGamePixel(snake2.endpoint.x, snake2.endpoint.y))
break;
setGamePixel(snake2.endpoint.x, snake2.endpoint.y, 1);
switch (p2key) {
case BTN_ENTER:
// exit
return;
case BTN_RIGHT:
if (direction != DIRECTION_LEFT)
direction = DIRECTION_RIGHT;
break;
case BTN_UP:
if (direction != DIRECTION_DOWN)
direction = DIRECTION_UP;
break;
case BTN_LEFT:
if (direction != DIRECTION_RIGHT)
direction = DIRECTION_LEFT;
break;
case BTN_DOWN:
if (direction != DIRECTION_UP)
direction = DIRECTION_DOWN;
break;
//Default: No keystroke received. Assuming last keystroke.
}
newendpoint = snake.endpoint;
shiftPoint (&newendpoint, direction);
resetBacon = false;
if (newendpoint.x == bacon.x && newendpoint.y == bacon.y) {
growBuf (&snake, direction);
resetBacon = true;
} else {
setGamePixel(snake.startpoint.x, snake.startpoint.y, 0);
shiftBuf (&snake, direction);
}
if (getGamePixel(snake.endpoint.x, snake.endpoint.y))
break;
setGamePixel(snake.endpoint.x, snake.endpoint.y, 1);
while (resetBacon) {
bacon.x = getRandom() % GAME_WIDTH;
bacon.y = getRandom() % GAME_HEIGHT;
if (!getGamePixel(bacon.x, bacon.y))
resetBacon = false;
}
drawFood (bacon.x, bacon.y);
lcdRefresh();
key = BTN_NONE;
p2key = BTN_NONE;
for (i=0; i < ( TIME_PER_MOVE / 100 ); ++i) {
p2key=receiveKeyPressed(100);
if ((button = getInputRaw()) != BTN_NONE)
key = button;
}
}
delayms(500);
while(getInputRaw() == BTN_NONE)
delayms(25);
}
// returns 0 if timeout, gameID (!= 0) else
uint8_t switchToHostModeAndWaitForClients(int timeout) {
// Broadcast Message format: 1 Byte
// 00 01 02 03 04 05 06 07
// 00-07: gameID
uint8_t gameID = 0, buf;
uint32_t k;
for (k = 0; k < timeout / 64; ++k) {
//while (!gameID)
// gameID = getRandom() & 0xff;
gameID = 0xff;
config.mac0[4] = 0xff;
config.txmac[4] = 0xff;
nrf_config_set(&config);
nrf_snd_pkt_crc(1, &gameID);
//config.mac0[4] = gameID;
//config.txmac[4] = gameID;
//nrf_config_set(&config);
uint8_t len = nrf_rcv_pkt_time(64, 1, &buf);
lcdPrintInt(len);
lcdNl();
lcdRefresh();
if (len == 1 && buf == BTN_NONE)
return gameID;
gameID = 0;
}
return 0;
}
// to be used by host in wait loop
uint8_t receiveKeyPressed(int timeout) {
uint8_t buf [32];
nrf_config_set(&config);
if (nrf_rcv_pkt_time(100,32,buf) == 32)
delayms(timeout);
return buf[0];
}
// to be used by client in wait loop
void sendKeyPressed(uint8_t keyPressed, int timeout) {
nrf_snd_pkt_crc(1, &keyPressed);
delayms(timeout);
}
// to be used by client when game must be received (display must be uint8_t[52], bacony and y uint8_t)
void receiveMove(uint8_t * display, uint8_t * baconx, uint8_t * bacony, int timeout) {
uint8_t buf[53];
if (nrf_rcv_pkt_time(timeout, 53, buf) != 53)
return;
delayms(timeout);
memcpy(display, buf, 51);
*baconx = buf[51];
*bacony = buf[52];
}
// to be used by host when game display must be sent (display must be uint8_t[52])
void sendMove(uint8_t * display, uint8_t baconx, uint8_t bacony, int timeout) {
uint8_t buf[53];
memcpy(buf, display, 51);
buf[51] = baconx;
buf[52] = bacony;
nrf_snd_pkt_crc(53, buf);
delayms(timeout);
}