aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlenk008 <frenk_0_0@hotmail.com>2023-03-04 00:14:50 +0100
committerGitHub <noreply@github.com>2023-03-04 00:14:50 +0100
commitb82afe9a241fc61c9b4cd64676c3ed804e240ae1 (patch)
treea592fdc9c83e6c7bf7eb8553bcecd093ecf6ee68
parent5dd15ae43082f1898e63c97d7691cf4721d86a07 (diff)
Cleaned main.c
-rw-r--r--GameLogic/stm32/main.c234
1 files changed, 103 insertions, 131 deletions
diff --git a/GameLogic/stm32/main.c b/GameLogic/stm32/main.c
index 97180ff..edabde5 100644
--- a/GameLogic/stm32/main.c
+++ b/GameLogic/stm32/main.c
@@ -138,22 +138,14 @@ int main(void)
};
- int pos_x_max = 410;
- int pos_y_max = 310;
- int pos_x_min = 210;
- int pos_y_min = 110;
uint8_t pos_x_bit[2];
uint8_t pos_y_bit[2];
uint8_t data_send[3];
- uint8_t led1Uit = 0b00100001 ;
- uint8_t led2Uit = 0x9D ;
+
int tileX;
int tileY;
- // int to pointer
- // uint8_t * ledTest1 = &led1Aan;
-// uint8_t * ledTest2 = &led2Aan;
- uint8_t * ledTest3 = &led1Uit;
- uint8_t * ledTest4 = &led2Uit;
+
+
@@ -163,134 +155,114 @@ int main(void)
/* USER CODE BEGIN WHILE */
while (1)
{
- // pos_x_bit[0] = 0b00000001;
- // pos_x_bit[1] = 0b00110110;
-
- pos_x_bit[1] = pos_x & 0xff;
- pos_x_bit[0] = (pos_x >> 8);
- pos_y_bit[1] = pos_y & 0xff;
- pos_y_bit[0] = (pos_y >> 8);
+ //shift int into 2 8bit u_int
+ pos_x_bit[1] = pos_x & 0xff;
+ pos_x_bit[0] = (pos_x >> 8);
- tileX = pos_x / 20;
+ pos_y_bit[1] = pos_y & 0xff;
+ pos_y_bit[0] = (pos_y >> 8);
+ // simplify coor for tilemap
+ tileX = pos_x / 20;
+ tileY = pos_y / 16;
- tileY = pos_y / 16;
-
-
- if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == 1)
- {
+ //read buttons 4 times
+ if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4) == 1)
+ {
left++;
-
- }
- else
- {
- left = 0;
- }
- if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == 1)
- {
- right++;
- }
- else
- {
- right = 0;
- }
- if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6) == 1)
- {
- up++;
- }
- else
- {
- up = 0;
- }
- if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == 1)
- {
- down++;
- }
- else
- {
- down = 0;
- }
-
- // if(pos_x % 8 == 0 && pos_y % 8 == 0 && tileMap[tileY][tileX] == 1)
- // {
-
- // }
- // else
- // {
- if(left > 10)
- {
- if(tileMap[tileY][tileX] == 1)
- {
- left = 0;
- }
- else
- {
- pos_x--;
- }
- left = 0;
- }
- if(right > 10)
- {
- if(tileMap[tileY][tileX+1] == 1)
- {
- right = 0;
- }
- else
- {
- pos_x++;
- }
-
-
- right = 0;
- }
-
- if(up > 10)
- {
- if(tileMap[tileY+1][tileX] == 1)
- {
- up = 0;
- }
- else
- {
- pos_y++;
- }
- up = 0;
-
-
-
- }
- if(down > 10)
+ }
+ else
+ {
+ left = 0;
+ }
+ if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5) == 1)
{
- if( tileMap[tileY][tileX] == 1)
- {
- down = 0;
- }
- else
- {
- pos_y--;
- }
-
- down = 0;
+ right++;
}
- // }
-
-
-
-
- data_send[0] = 0b00000000;
- data_send[1] = pos_y_bit[0] ;
- data_send[2] = pos_y_bit[1];
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
- HAL_SPI_Transmit(&hspi1, data_send, 3, HAL_MAX_DELAY); //2*8 bit
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
- data_send[0] = 0b01000000;
- data_send[1] = pos_x_bit[0] ;
- data_send[2] = pos_x_bit[1];
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
- HAL_SPI_Transmit(&hspi1, data_send, 3, HAL_MAX_DELAY); //2*8 bit
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
+ else
+ {
+ right = 0;
+ }
+ if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6) == 1)
+ {
+ up++;
+ }
+ else
+ {
+ up = 0;
+ }
+ if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_8) == 1)
+ {
+ down++;
+ }
+ else
+ {
+ down = 0;
+ }
+ // compare position x/y with tilemap for collision
+ if(left > 10)
+ {
+ if(tileMap[tileY][tileX] == 1)
+ {
+ left = 0;
+ }
+ else
+ {
+ pos_x--;
+ }
+ left = 0;
+ }
+ if(right > 10)
+ {
+ if(tileMap[tileY][tileX+1] == 1)
+ {
+ right = 0;
+ }
+ else
+ {
+ pos_x++;
+ }
+ right = 0;
+ }
+ if(up > 10)
+ {
+ if(tileMap[tileY+1][tileX] == 1)
+ {
+ up = 0;
+ }
+ else
+ {
+ pos_y++;
+ }
+ up = 0;
+ }
+ if(down > 10)
+ {
+ if( tileMap[tileY][tileX] == 1)
+ {
+ down = 0;
+ }
+ else
+ {
+ pos_y--;
+ }
+ down = 0;
+ }
+ // send data via SPI
+ data_send[0] = 0b00000000; // first byte is address
+ data_send[1] = pos_y_bit[0] ;
+ data_send[2] = pos_y_bit[1];
+ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
+ HAL_SPI_Transmit(&hspi1, data_send, 3, HAL_MAX_DELAY); //3*8 bit
+ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
+ data_send[0] = 0b01000000; // first byte is address
+ data_send[1] = pos_x_bit[0] ;
+ data_send[2] = pos_x_bit[1];
+ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET);
+ HAL_SPI_Transmit(&hspi1, data_send, 3, HAL_MAX_DELAY); //3*8 bit
+ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_SET);
/* USER CODE END WHILE */