aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: ee6125f2dc16743b2d133a6ef7e90e4880670e8b (plain)
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
#include <stdlib.h>

#include "main.h"
#include "demo.h"

bool g_hh_run = true;

static int8_t buttonDPAD[] = {0,0,0,0}; //1left 2right 3down 4up

struct playerData{
	uint16_t posX;
	uint16_t posY;
	uint8_t radius;
	uint8_t rotation; //45 degrees steps 0 == right 2 == down 4 == left 6 == up
	uint8_t directionX; //direction where its looking at in case no input;
	int8_t speed;	//10 default L/R MODifier
	bool inAir;

};

struct playerData player1;

uint8_t tileMap[30][40] =
  {
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,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1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 },
		  {1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 ,1,1,1,1,1,1,1,1,1,1 }



  };

void buttonRead();
void playerMovement();
void sendData(uint8_t, uint16_t);

  uint16_t pos_x; //0b0000 0001 0011 0110
  uint16_t pos_y;

  uint8_t left = 0;
  uint8_t right = 0;
  uint8_t up = 0;
  uint8_t down = 0;



  uint8_t pos_x_bit[2];
  uint8_t pos_y_bit[2];
  uint8_t data_send[3];

  int tileX;
  int tileY;
//  struct playerData player1;
 //int buttons[] = {GPIO_PIN_4,GPIO_PIN_5,GPIO_PIN_6,GPIO_PIN_8}; // 1 left // 2 right // 3 up // 4 down


int main() {
	hh_setup();

/// init struct
  player1.posX = 31000; //0b0000 0001 0011 0110
  player1.posY = 21000;
  player1.radius = 8;
  player1.speed = 1;
  player1.directionX = 1;
  player1.rotation = 8;
  player1.inAir = false;

	hh_loop();
	hh_exit();
	return 0;
}

void hh_ppu_vblank_interrupt() {
	static unsigned long frame = 0;
	frame++;

	buttonRead();
	playerMovement();


	// send data via SPI //adjust map size
	pos_x = player1.posX / 100;
	pos_y = player1.posY / 100;

	sendData(0b01000000,pos_x);
	sendData(0b00000000,pos_y);

	// hh_demo_loop(frame);
}

void sendData(uint8_t address, uint16_t data)
{
			uint8_t bitData[3];
			bitData[2] = data & 0xff;
			bitData[1] = (data >> 8);
			bitData[0] = address; // first byte is address



			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
			HAL_SPI_Transmit(&hspi1, bitData, 3, 100); //2*8 bit data
			HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);

}

void buttonRead()
{
	//int buttons[] = {GPIO_PIN_4,GPIO_PIN_5,GPIO_PIN_6,GPIO_PIN_8}; // 1 left // 2 right // 3 down // 4 up  8-6-4-5
	uint16_t buttons[] = {0x0100U,0x0040U,0x0010U,0x0020U}; // 1 left // 2 right // 3 down // 4 up

	for(int i = 0; i < 4; i++)
	{
		if(HAL_GPIO_ReadPin(GPIOB, buttons[i]) == 1)
		{
			if((i+2) % 2 == 0)
			{
				buttonDPAD[i] = 1;
			}
			else
			{
				buttonDPAD[i] = -1;
			}
		}
		else
		{
			buttonDPAD[i] = 0;
		}
	}
}

void playerMovement()
{

	int8_t directionX = 0; // -1 = L || 1 == R
	int8_t directionY = 0; // -1 = D || 1 == U

	uint8_t i,j;
	uint8_t rotation = 0; // 0-7
	//temp var for testing



	// direction calc
	directionX = buttonDPAD[0] + buttonDPAD[1];
	directionY = buttonDPAD[2] + buttonDPAD[3];

	//rotation calc
	for(i = -1; i < 2;i++)
	{
		for(j = -1; j < 2; j++)
		{
			if(directionX == i)
			{
				if(directionY == j)
				{
					if(i != 0 && j != 0) //dont update when player idle
					{
						player1.rotation = rotation;
					}
				}
			}
		rotation++;
		}
	}
	//direction calc
	if(directionX != 0) //update direction if player is not idle
	{
		player1.directionX = directionX;
	}
	//collision map x-axis

	//tile calc including radius and direction for background coliision

	uint16_t tileColX;
	uint16_t tileColY = ( player1.posY / 100) / 16; ;

	// remaining space between grid and exact
	uint8_t modTileX;
	uint8_t modTileY;



	if(player1.inAir == false && directionX != 0)
	{
		if(directionX == 1)
		{
			tileColX = ( ( player1.posX / 100) + player1.radius ) / 20;
			modTileX = ( player1.posX + ( 100 * player1.radius ) ) % 2000;
		}
		else if(directionX == -1)
		{
			tileColX = ( ( player1.posX / 100) - player1.radius ) / 20;
			modTileX = ( player1.posX - ( 100 * player1.radius ) ) % 2000;
		}

			if(tileMap[tileColY][tileColX + directionX] != 1)
			{
				player1.posX = player1.posX + (directionX * player1.speed); // NEW x set
			}

			else if(tileMap[tileColY][tileColX + directionX] == 1)
			{
				if(modTileX < player1.speed)
				{
					player1.posX = player1.posX + (directionX * modTileX); // NEW x set
				}
				else
				{
					player1.posX = player1.posX + (directionX * player1.speed); // NEW x set
				}
			}

	}
	else //if in air different all borders have to be checked
	{

	}

	//collision map floor (y-axis) (falling)
	// if falling no jump press (implement)
	/*
	tileColY = (( player1.posY / 100) + player1.radius) / 16; //bottom of player box
	modTileY = 1;
	if(tileMap[tileColY+1][tileColX] != 1) //rework after jumping
	{
		player1.posY = player1.posY + 5 ;// NEW y set //makew var gravity
		//playerStat = falling; //for later use of graphics/sound
	}
	*/
	//else if(tileMap[])





}