-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.c
178 lines (139 loc) · 3.99 KB
/
display.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
/*Written by Fredrik Lundström and Michell Dib 2021 (c)*/
#include <stdint.h>
#include <pic32mx.h>
#include "snake.h" //Project header file
void *stdin, *stdout, *stderr;
#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)
/*If any point in the array is set to one the pixel att the same position be lit*/
uint8_t display[32][128]; //Human readable pixel position and activation
uint8_t oled_display[512]; //Computer readable pixel position and activation
/*(TAKEN FROM LAB) Quick sleep timer*/
void quicksleep(int cyc) {
int i;
for(i = cyc; i > 0; i--);
}
/*(TAKEN FROM LAB) Send data to the OLED display*/
uint8_t spi_send_recv(uint8_t data) { //LAB
while(!(SPI2STAT & 0x08));
SPI2BUF = data;
while(!(SPI2STAT & 1));
return SPI2BUF;
}
/*(TAKEN FROM LAB) Initialize OLED display*/
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);
}
/*(TAKEN FROM LAB) Display text*/
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] = ' ';
}
/*(TAKEN FROM LAB) Display text*/
void display_update(void) { //LAB
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]);
}
}
}
/*(TAKEN FROM LAB) This will print any image on the display with the help of an array containing the map of the active and inactive pixels*/
void display_image(int x, const uint8_t *data) { //LAB
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 < 128; j++)
spi_send_recv(data[i*128 + j]);
}
}
/*This function translates the display array to oled_display array.
It converts an x and y coordinate system into a readable array for the computer.*/
void translateToImage() {
int page, column, row, c, k;
uint8_t powerOfTwo = 1;
uint8_t oledNumber = 0;
int survivalMode = 0;
for(page = 0; page < 4; page++) {
for(column = 0; column < 128; column++) {
powerOfTwo = 1;
oledNumber = 0;
for(row = 0; row < 8; row++) {
if(display[8 * page + row][column]) {
oledNumber |= powerOfTwo;
}
powerOfTwo <<= 1;
}
oled_display[column + page * 128] = oledNumber;
}
}
}
/*This function sets all the values in the display array and oled display array into 0s*/
void clear_display() {
int row, column, i;
for(row = 0; row < 32; row++) {
for(column = 0; column < 128; column++) {
display[row][column] = 0;
}
}
for (i = 0; i < 512; i++) {
oled_display[i] = 0;
}
}
/*This function calls all the necessary functions for the game to start*/
void display_start(){
clear_display(); //This clear the screen everytime it loops. It will clear the last frame
game(); //Call game
translateToImage(); //Translate
display_image(0, oled_display); //And then display the frame
}