-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisplay.c
More file actions
292 lines (257 loc) · 6.76 KB
/
display.c
File metadata and controls
292 lines (257 loc) · 6.76 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
#include <stdint.h> /* Declarations of uint_32 and the like */
#include <pic32mx.h> /* Declarations of system-specific addresses etc */
#include "flappybird.h"
#define DISPLAY_CHANGE_TO_COMMAND_MODE (PORTFCLR = 0x10)
#define DISPLAY_CHANGE_TO_DATA_MODE (PORTFSET = 0x10)
#define DISPLAY_ACTIVATE_RESET (PORTGCLR = 0x200)
#define DISPLAY_DO_NOT_RESET (PORTGSET = 0x200)
#define DISPLAY_ACTIVATE_VDD (PORTFCLR = 0x40)
#define DISPLAY_ACTIVATE_VBAT (PORTFCLR = 0x20)
#define DISPLAY_TURN_OFF_VDD (PORTFSET = 0x40)
#define DISPLAY_TURN_OFF_VBAT (PORTFSET = 0x20)
/* quicksleep:
A simple function to create a small delay.
Very inefficient use of computing resources,
but very handy in some special cases. */
void quicksleep(int cyc)
{
int i;
for (i = cyc; i > 0; i--)
;
} // Directly copied from lab 3
uint8_t spi_send_recv(uint8_t data)
{
while (!(SPI2STAT & 0x08))
;
SPI2BUF = data;
while (!(SPI2STAT & 1))
;
return SPI2BUF;
} // Directly copied from lab 3
void displayMenu()
{
if (currentmenu == 0) // Main Menu
{
display_string(0, "1.Start Game");
display_string(1, "2.High Score");
display_string(2, "3.Difficulty");
display_string(3, "4.Help Menu");
}
else if (currentmenu == 1) // Change Difficulty
{
display_string(0, "1.Current: Easy");
if (difficulty == 2)
{
display_string(0, "1.Current: Normal");
}
if (difficulty == 3)
{
display_string(0, "1.Current: Hard");
}
display_string(1, "2. -- Difficulty");
display_string(2, "3. ++ Difficulty");
display_string(3, "4.Back");
}
else if (currentmenu == 2) // High Score
{
read_scoreboard();
display_string(3, "4.Back");
}
else if (currentmenu == 3) // Help
{
display_string(0, "Menu - BTN 1-4");
display_string(1, "Jump - BTN 1/4");
display_string(2, "x- BTN 3, x+ BTN 2");
display_string(3, "4. Back");
}
return;
} // Shows variable strings depending on currentmenu chosen in menu
// Erik Paulinder 2023-02-27
void displayGame(int ObstacleX[], int ObstacleY[])
{
int i, j, g; // Declaring for-loop variables
int yCount = 4;
int holesize = 0xff;
if (difficulty == 2)
{
holesize = 0xFC;
}
if (difficulty == 3)
{
holesize = 0xF0;
}
// Sets size/value of hole, for this display the binary value is reversed, 1 = 0
// FF = 8 bits zero, 3F = 6 bits zero, ,0F = 4 bits zero,
for (i = 0; i < 4; i++) // Because it will only write on 1/4 of the screen with display image, create it 4 times
{
yCount = 4;
birdelex1 = 0;
birdelex2 = 0;
for (j = 0; j < 128; j++) // Fill every position in the array
{
game[j]=0xFF;//Sets every position to 1, which is default which corresponds to black
if (j % 32 == 0) // Increments y-value, since each position is 1 byte, and it goes to next page each 32 steps
{
yCount -= 1;
} // Increments yCount, for which 1/4 currently located in
for (g = 0; g < 4; g++) // Check each obstacle
{
if (((ObstacleX[g] < (i + 1) * 32) && (ObstacleX[g] >= (i * 32)))) // Checks if this obstacle x is located in current zone
{
if (j % 32 == (ObstacleX[g]) % 32) // If so, checks that current value ObstacleX is the same as j, in intervall of 32
{
if (yCount == ObstacleY[g])
{
game[j] = holesize;
}
else if (yCount == ObstacleY[g])
{
game[j] = holesize;
}
else if (yCount == ObstacleY[g])
{
game[j] = holesize;
}
else if (yCount == ObstacleY[g])
{
game[j] = holesize;
} // Above checks so that the hole is placed in correct y-value, aka flag(?)
else
{
game[j] = 0x00;
} // If not correct y value, but correct x-value, fill the entire thing
}
}
}
if ((birdx < ((i + 1) * 32)) && (birdx >= (i * 32)))
{
if ((j % 32) == (birdx % 32))
{
if ((birdelex1 == 1))
{
game[j] = ((game[j] & 0xFE) + ((0x00)&(0x01)));
birdelex1 = 0;
}
if ((((yCount + 1) * 8) > birdy && birdy >= ((yCount)*8)))
{
if (((birdy) > ((yCount)*8)))
{
game[j] = ((game[j] & (0xFE7F >> ((birdy) % 8))));
}
else if ((birdy) == ((yCount)*8))
{
game[j] = (game[j]&0x7F) + ((0x00)& (0x80));
birdelex1 = 1;
}
}
}
}
// Similar to obstacles, first checks if correct interval of x, then if correct x, then correct y, if so set one pixel, with y value offset from top
if (((birdx + 1) < (i + 1) * 32) && ((birdx + 1) >= (i * 32)))
{
if ((j % 32) == ((birdx + 1) % 32))
{
if ((birdelex2 == 1))
{
game[j] = ((game[j] & 0xFE) + ((0x00)&(0x01)));
birdelex2 = 0;
}
if ((((yCount + 1) * 8) > birdy && birdy >= ((yCount)*8)))
{
if (((birdy) > ((yCount)*8)))
{
game[j] = ((game[j] & (0xFE7F >> ((birdy) % 8))));
}
else if ((birdy) == ((yCount)*8))
{
game[j] = (game[j]&0x7F) + ((0x00)& (0x80));
birdelex2 = 1;
}
}
}
}
}
display_image(32 * i, game); // Send current array, with current 1/4 of map, to display_image, to show on OLED-Screen
}
// Creates a Bit-Map Dependent on current game status, setting obstacle and bird x and y to one.
// Erik Paulinder 2023-02-27
return;
}
// Erik Paulinder 2023-02-27
// Above generates a bitmap depending on current game data, and then passes that one to display image
void display_image(int x, const uint8_t *data)
{
int i, j;
for (i = 0; i < 4; i++)
{
DISPLAY_CHANGE_TO_COMMAND_MODE;
spi_send_recv(0x22);
spi_send_recv(i);
spi_send_recv(x & 0xF);
spi_send_recv(0x10 | ((x >> 4) & 0xF));
DISPLAY_CHANGE_TO_DATA_MODE;
for (j = 0; j < 32; j++)
spi_send_recv(~data[i * 32 + j]);
}
} // Directly copied from lab 3
void display_init(void)
{
DISPLAY_CHANGE_TO_COMMAND_MODE;
quicksleep(10);
DISPLAY_ACTIVATE_VDD;
quicksleep(1000000);
spi_send_recv(0xAE);
DISPLAY_ACTIVATE_RESET;
quicksleep(10);
DISPLAY_DO_NOT_RESET;
quicksleep(10);
spi_send_recv(0x8D);
spi_send_recv(0x14);
spi_send_recv(0xD9);
spi_send_recv(0xF1);
DISPLAY_ACTIVATE_VBAT;
quicksleep(10000000);
spi_send_recv(0xA1);
spi_send_recv(0xC8);
spi_send_recv(0xDA);
spi_send_recv(0x20);
spi_send_recv(0xAF);
} // Directly copied from lab 3
void display_update(void)
{
int i, j, k;
int c;
for (i = 0; i < 4; i++)
{
DISPLAY_CHANGE_TO_COMMAND_MODE;
spi_send_recv(0x22);
spi_send_recv(i);
spi_send_recv(0x0);
spi_send_recv(0x10);
DISPLAY_CHANGE_TO_DATA_MODE;
for (j = 0; j < 16; j++)
{
c = textbuffer[i][j];
if (c & 0x80)
continue;
for (k = 0; k < 8; k++)
spi_send_recv(font[c * 8 + k]);
}
}
} // Directly copied from lab 3
void display_string(int line, char *s)
{
int i;
if (line < 0 || line >= 4)
return;
if (!s)
return;
for (i = 0; i < 16; i++)
if (*s)
{
textbuffer[line][i] = *s;
s++;
}
else
textbuffer[line][i] = ' ';
} // Directly copied from lab 3