-
Notifications
You must be signed in to change notification settings - Fork 0
/
shapes.c
405 lines (358 loc) · 15.3 KB
/
shapes.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
/************************************************************************************************************************
* [FILE NAME] : <shapes.c> *
* [AUTHOR] : <Eslam EL-Naggar> *
* [DATE CREATED]: <JAN 26, 2020> *
* [Description} : <source files contains the functions definitions related to the shapse> *
************************************************************************************************************************/
/*------------------------------------------------------INCLUDES-------------------------------------------------------*/
#include "shapes.h"
/*----------------------------------------------------------------------------------------------------
[Function Name]: DrawShape
[Description] : This function is responsible for drawing the shape at the top of the game zone
[Args] : uint8 *shape_Ptr
this argument shall contains the address of the array of the shapes formation
uint8 * rowIndicator_Ptr
this argument shall contains the address of the rowIndicator variable
uint8 * upperPageIndicator_Ptr
this argument shall contains the address of the upperPageIndicator variable
uint8 * lowerPageIndicator_Ptr
this argument shall contains the address of the lowerPageIndicator variable
uint8 * columnIndicator_Ptr
this argument shall contains the address of the columnIndicator variable
uint8 * nextShapeFlag_Ptr
this argument shall contains the address of the nextShapeFlag variable
uint8 * lowerState_Ptr
this argument shall contains the address of the array of lower page state of columns
uint8 * upperState_Ptr
this argument shall contains the address of the array of upper page state of columns
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void DrawShape(uint8 *shape_Ptr, uint8 *rowIndicator_Ptr,
uint8 *columnIndicator_Ptr, uint8 *lowerPageIndicator_Ptr,
uint8 *upperPageIndicator_Ptr, uint8 *nextShapeFlag_Ptr,
uint8 *lowerState_Ptr, uint8 *upperState_Ptr) {
uint8 loopCounter = ZERO;
cli();
/* Select Right half of display */
GLCD_CTRL_PORT |= (ONE << CS1);
GLCD_CTRL_PORT &= ~(ONE << CS2);
/* check on the page number to be sure that is a valid page number */
if (*upperPageIndicator_Ptr <= LAST_PAGE) {
/* set the page number */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *upperPageIndicator_Ptr);
/* looping along the shape width */
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + *columnIndicator_Ptr + loopCounter);
/* read the state of the column */
upperState_Ptr[loopCounter] = GLCD_readData();
/* one dummy read is required to get the state*/
upperState_Ptr[loopCounter] = GLCD_readData();
/* check if the upper part of the shape collides with another shape or not */
if (checkUpper(upperState_Ptr[loopCounter], *rowIndicator_Ptr)
& (((shapes[*shape_Ptr][loopCounter]) << (*rowIndicator_Ptr)))) {
/* set the nextShapeFlag to ONE ( indication of the collision ) */
*nextShapeFlag_Ptr = ONE;
/* break the loop */
break;
}
}
}
/* check if the shape collides or not */
if (*nextShapeFlag_Ptr == ONE) {
} else {
/* if it doesn't collide, check the lower part of the shape collides or not */
/* check on the page number to be sure that is a valid page number */
if (*lowerPageIndicator_Ptr <= LAST_PAGE) {
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *lowerPageIndicator_Ptr);
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + *columnIndicator_Ptr + loopCounter);
/* read the state of the column */
lowerState_Ptr[loopCounter] = GLCD_readData();
/* one dummy read is required to get the state*/
lowerState_Ptr[loopCounter] = GLCD_readData();
/* check if the upper part of the shape collides with another shape or not */
if (checkLower(lowerState_Ptr[loopCounter], *rowIndicator_Ptr)
& (((shapes[*shape_Ptr][loopCounter])
>> ((NUM_BITS_IN_BYTE - *rowIndicator_Ptr))))) {
/* set the nextShapeFlag to ONE ( indication of the collision ) */
*nextShapeFlag_Ptr = ONE;
/* break the loop */
break;
}
}
}
}
/* check if the shape collides or not
* if yes, skip display the current shape
* if not, displaying the current shape
*/
if (*nextShapeFlag_Ptr == ONE) {
/* if current page is the top page, this means that the player losses the game */
if (*upperPageIndicator_Ptr == STARTED_UPPER_PAGE) {
/* set the loseFlag to ONE ( indication of losing the game ) */
loseFlag = ONE;
}
} else {
/* check on the page number to be sure that is a valid page number */
if ((*upperPageIndicator_Ptr <= LAST_PAGE)) {
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *upperPageIndicator_Ptr);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + *columnIndicator_Ptr);
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
/*display each column of the shape */
GLCD_sendData(
(upperState_Ptr[loopCounter]
& ~((shapes[*shape_Ptr][loopCounter])
<< (*rowIndicator_Ptr)))
| (((shapes[*shape_Ptr][loopCounter])
<< (*rowIndicator_Ptr))));
}
}
/* check on the page number to be sure that is a valid page number */
if ((*lowerPageIndicator_Ptr <= LAST_PAGE)) {
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *lowerPageIndicator_Ptr);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + *columnIndicator_Ptr);
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
/*display each column of the shape */
GLCD_sendData(
(lowerState_Ptr[loopCounter]
& ~((shapes[*shape_Ptr][loopCounter])
>> ((NUM_BITS_IN_BYTE
- *rowIndicator_Ptr))))
| (((shapes[*shape_Ptr][loopCounter])
>> ((NUM_BITS_IN_BYTE
- *rowIndicator_Ptr)))));
}
}
_delay_ms(DELAY_AFTER_DRAWING_TIME);
}
sei();
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: EraseShape
[Description] : This function is responsible for drawing the shape at the top of the game zone
[Args] : uint8 *shape_Ptr
this argument shall contains the address of the array of the shapes formation
uint8 * rowIndicator_Ptr
this argument shall contains the address of the rowIndicator variable
uint8 * upperPageIndicator_Ptr
this argument shall contains the address of the upperPageIndicator variable
uint8 * lowerPageIndicator_Ptr
this argument shall contains the address of the lowerPageIndicator variable
uint8 * columnIndicator_Ptr
this argument shall contains the address of the columnIndicator variable
uint8 * lowerState_Ptr
this argument shall contains the address of the array of lower page state of columns
uint8 * upperState_Ptr
this argument shall contains the address of the array of upper page state of columns
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void EraseShape(uint8 *shape_Ptr, uint8 *rowIndicator_Ptr,
uint8 *columnIndicator_Ptr, uint8 *lowerPageIndicator_Ptr,
uint8 *upperPageIndicator_Ptr, uint8 *lowerState_Ptr,
uint8 *upperState_Ptr) {
uint8 loopCounter = ZERO;
cli();
GLCD_CTRL_PORT |= (ONE << CS1); /* Select Right half of display */
GLCD_CTRL_PORT &= ~(ONE << CS2);
/* check on the page number to be sure that is a valid page number */
if ((*upperPageIndicator_Ptr <= LAST_PAGE)) {
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *upperPageIndicator_Ptr);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + *columnIndicator_Ptr);
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
/* erasing the upper part of the shape */
GLCD_sendData(
(upperState_Ptr[loopCounter]
& ~((shapes[*shape_Ptr][loopCounter])
<< (*rowIndicator_Ptr))));
}
}
/* check on the page number to be sure that is a valid page number */
if ((*lowerPageIndicator_Ptr <= LAST_PAGE)) {
/* set the page address */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + *lowerPageIndicator_Ptr);
/* set the column address */
GLCD_sendCommand(
COLUMN_SETTING_ADDRESS + *columnIndicator_Ptr);
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
/* erasing the lower part of the shape */
GLCD_sendData(
(lowerState_Ptr[loopCounter]
& ~((shapes[*shape_Ptr][loopCounter])
>> ((NUM_BITS_IN_BYTE - *rowIndicator_Ptr)))));
}
}
sei();
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: GenerateLocation
[Description] : This function is responsible for drawing the shape at the top of the game zone
[Args] : uint8 *shape_Ptr
this argument shall contains the address of the array of the shapes formation
uint8 * rowIndicator_Ptr
this argument shall contains the address of the rowIndicator variable
uint8 * upperPageIndicator_Ptr
this argument shall contains the address of the upperPageIndicator variable
uint8 * lowerPageIndicator_Ptr
this argument shall contains the address of the lowerPageIndicator variable
uint8 * columnIndicator_Ptr
this argument shall conatins the address of the columnIndicator variable
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void GenerateLocation(uint8 *shape_Ptr, uint8 *rowIndicator_Ptr,
uint8 *upperPageIndicator_Ptr, uint8 *lowerPageIndicator_Ptr,
uint8 *columnIndicator_Ptr) {
cli();
/* row numbers will be 1,3,5,7 */
(*rowIndicator_Ptr) = (*rowIndicator_Ptr) + TWO;
/* check if the row number is out of range
* if yes, we have to increment the pages number
*/
if ((*rowIndicator_Ptr) == NINE) {
(*rowIndicator_Ptr) = ONE;
(*upperPageIndicator_Ptr)++;
(*lowerPageIndicator_Ptr)++;
}
/* check if the row number is last row and the last upper page
* and the lower page is out of the number
* this means, the current shape reaches to the bottom of the border
* so, we have to start again with new shape
*/
if ((*rowIndicator_Ptr == SEVEN) && (*upperPageIndicator_Ptr == SEVEN)
&& (*lowerPageIndicator_Ptr == EIGHT)) {
completeCheck(rowIndicator_Ptr, (uint8) (*upperPageIndicator_Ptr));
*upperPageIndicator_Ptr = STARTED_UPPER_PAGE;
*lowerPageIndicator_Ptr = STARTED_LOWER_PAGE;
*rowIndicator_Ptr = STARTED_ROW;
(*columnIndicator_Ptr) = STARTED_COLUMN;
*shape_Ptr = nextShape;
nextShape = NextShapeView(&shape);
if ((*shape_Ptr) == NUM_SHAPES) {
(*shape_Ptr) = ZERO;
}
}
sei();
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: RotateShape
[Description] : This function is responsible for setting the number of the new shape after rotating
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void RotateShape(uint8 *columnIndicator_Ptr) {
switch (shape) {
case FIRST_SHAPE:
shape = SECOND_SHAPE;
break;
case SECOND_SHAPE:
shape = FIRST_SHAPE;
break;
case THIRD_SHAPE:
break;
case FOURTH_SHAPE:
shape = FIFTH_SHAPE;
break;
case FIFTH_SHAPE:
shape = FOURTH_SHAPE;
break;
case SIXTH_SHAPE:
shape = EIGHTH_SHAPE;
break;
case SEVENTH_SHAPE:
shape = NINGTH_SHAPE;
break;
case EIGHTH_SHAPE:
shape = SEVENTH_SHAPE;
break;
case NINGTH_SHAPE:
shape = SIXTH_SHAPE;
break;
}
checkAfterMoving(columnIndicator_Ptr);
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: NextShapeView
[Description] : This function is responsible for drawing the next shape
[Args] : uint8 * shape_Ptr
this argument shall contains the address of the next shape
[Returns] : This function returns value of the next shape
----------------------------------------------------------------------------------------------------*/
uint8 NextShapeView(uint8 *shape_Ptr) {
uint8 loopCounter = ZERO;
static uint8 shapeNext = ZERO;
GLCD_CTRL_PORT |= (ONE << CS2); /* Select Left half of display */
GLCD_CTRL_PORT &= ~(ONE << CS1);
GLCD_sendCommand(COLUMN_SETTING_ADDRESS + SHAPE_OUTPUT_ADDRESS); /* Set column address) */
GLCD_sendCommand(PAGE_SETTING_ADDRESS + PAGE_2); /* Set page address */
if ((*shape_Ptr) + ONE == NUM_SHAPES) {
shapeNext = FIRST_SHAPE;
} else {
shapeNext = (*shape_Ptr) + ONE;
}
/* displaying the current shape */
for (loopCounter = ZERO; loopCounter < SHAPE_WIDTH; loopCounter++) {
GLCD_sendData(shapes[(shapeNext)][loopCounter]);
}
return shapeNext;
}
/*----------------------------------------------------------------------------------------------------
[Function Name]: checkAfterMoving
[Description] : This function is responsible for checking and adjusting
t the correct number of columns of each shape
[Args] : uint8 *columnIndicator_Ptr
this argument shall contains the address of the columnIndicator variable
[Returns] : This function returns void
----------------------------------------------------------------------------------------------------*/
void checkAfterMoving(uint8 *columnIndicator_Ptr) {
switch (shape) {
case THIRD_SHAPE:
if (*columnIndicator_Ptr > RIGHT_MARGIN_THIRD_SHAPE) {
*columnIndicator_Ptr = RIGHT_MARGIN_THIRD_SHAPE;
}
if (*columnIndicator_Ptr < LEFT_MARGIN_ALL_SHAPES) {
*columnIndicator_Ptr = LEFT_MARGIN_ALL_SHAPES;
}
break;
case FOURTH_SHAPE:
if (*columnIndicator_Ptr > RIGHT_MARGIN_FOURTH_SHAPE) {
*columnIndicator_Ptr = RIGHT_MARGIN_FOURTH_SHAPE;
}
if (*columnIndicator_Ptr < LEFT_MARGIN_ALL_SHAPES) {
*columnIndicator_Ptr = LEFT_MARGIN_ALL_SHAPES;
}
break;
case SIXTH_SHAPE:
case SEVENTH_SHAPE:
if (*columnIndicator_Ptr > RIGHT_MARGIN_SIXTH_SEVENTH_SHAPE) {
*columnIndicator_Ptr = RIGHT_MARGIN_SIXTH_SEVENTH_SHAPE;
}
if (*columnIndicator_Ptr < LEFT_MARGIN_ALL_SHAPES) {
*columnIndicator_Ptr = LEFT_MARGIN_ALL_SHAPES;
}
break;
case FIRST_SHAPE:
case SECOND_SHAPE:
case FIFTH_SHAPE:
case EIGHTH_SHAPE:
case NINGTH_SHAPE:
if (*columnIndicator_Ptr > RIGHT_MARGINE_REST_SHAPES) {
*columnIndicator_Ptr = RIGHT_MARGINE_REST_SHAPES;
}
if (*columnIndicator_Ptr < LEFT_MARGIN_ALL_SHAPES) {
*columnIndicator_Ptr = LEFT_MARGIN_ALL_SHAPES;
}
break;
}
}