int main(void) { char buffer[20]; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize USART1, TX: PB6 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Initialize random number generator */ TM_RNG_Init(); while (1) { /* Get number */ sprintf(buffer, "Number: %u\n", TM_RNG_Get()); /* Put to USART */ TM_USART_Puts(USART1, buffer); Delayms(1000); } }
void TM_SNAKE_Start(void) { /* Initialize delay */ TM_DELAY_Init(); /* Initialize leds on board */ TM_DISCO_LedInit(); /* Turn off all leds */ TM_DISCO_LedOff(LED_ALL); /* Initialize True random number generator */ TM_RNG_Init(); /* Initialize ILI9341 LCD */ TM_ILI9341_Init(); TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); /* Initialize USB HID Host for keyboard */ TM_USB_HIDHOST_Init(); /* Set default options */ TM_SNAKE_SetFirstOptions(); /* Set default values */ TM_SNAKE_SetDefaultSnake(); /* Prepare display */ TM_SNAKE_PrepareDisplay(); /* Generate random target */ TM_SNAKE_GenerateTarget(); /* Set time to 0 */ TM_DELAY_SetTime(0); while (1) { /* Process USB HID host */ TM_USB_HIDHOST_Process(); /* Check for timeout, move snake here */ if (TM_DELAY_Time() >= Settings.Millis && !Settings.Pause && !GameOver) { /* Reset time */ TM_DELAY_SetTime(0); /* Get new direction value */ Snake.Direction = Snake1.Direction; /* Get last x/y value from snake array = snake head */ Snake_Head[0] = Snake.Snake[Snake.LastIndex][0]; Snake_Head[1] = Snake.Snake[Snake.LastIndex][1]; /* Store last value before update */ Snake_Head_Last[0] = Snake_Head[0]; Snake_Head_Last[1] = Snake_Head[1]; if (!Snake_FirstTime) { /* Move snake */ switch (Snake.Direction) { case SNAKE_DIRECTION_LEFT: Snake_Head[0] -= 1; break; case SNAKE_DIRECTION_RIGHT: Snake_Head[0] += 1; break; case SNAKE_DIRECTION_UP: Snake_Head[1] -= 1; break; case SNAKE_DIRECTION_DOWN: Snake_Head[1] += 1; break; default: break; } } /* Overflow is activated */ if (Settings.Overflow) { /* Check X */ if (Snake_Head[0] == -1) { Snake_Head[0] = SNAKE_PIXELS - 1; } else if (Snake_Head[0] == SNAKE_PIXELS) { Snake_Head[0] = 0; } /* Check Y */ if (Snake_Head[1] == -1) { Snake_Head[1] = SNAKE_PIXELS - 1; } else if (Snake_Head[1] == SNAKE_PIXELS) { Snake_Head[1] = 0; } } else { /* Check walls */ if ( Snake_Head[0] == -1 || Snake_Head[0] == SNAKE_PIXELS || Snake_Head[1] == -1 || Snake_Head[1] == SNAKE_PIXELS ) { /* We hit the wall somewhere */ GameOver = 1; } } if (!Snake_FirstTime) { /* Clear first value from array = snake foot */ TM_SNAKE_DeleteFromArray(0, Snake_Foot); /* Check if snake hit itself */ if (TM_SNAKE_MatchesSnakeLocations(Snake_Head)) { /* Set gameover flag */ GameOver = 1; } } /* Check if target is reached */ if ( !GameOver && Snake_Head[0] == Snake_Food[0] && Snake_Head[1] == Snake_Food[1] ) { /* Add new value to the array, increase snake */ TM_SNAKE_AddToArray(Snake_Head); /* Increase counter for snake hits */ Snake.Hits++; /* Generate new target */ TM_SNAKE_GenerateTarget(); } if (!GameOver) { if (!Snake_FirstTime) { /* Add new value to the array = new snake head */ TM_SNAKE_AddToArray(Snake_Head); } /* Clear pixel on LCD for foot */ /* First clear foot, maybe is new head on the same position */ TM_SNAKE_DrawPixel(Snake_Foot[0], Snake_Foot[1], 0); /* Draw pixel on LCD for new head position with head color */ TM_SNAKE_DrawPixel(Snake_Head[0], Snake_Head[1], 3); /* Draw new pixel for the second pixel after head with new color to delete head color */ TM_SNAKE_DrawPixel(Snake_Head_Last[0], Snake_Head_Last[1], 1); } /* Clear flag if needed */ if (Snake_FirstTime) { Snake_FirstTime = 0; } } if (GameOver) { /* Check flag */ if (!GameOverDisplay) { /* Set flag */ GameOverDisplay = 1; /* Show content to user */ TM_ILI9341_Puts(88, 120, "Game\nOVER", &TM_Font_16x26, ILI9341_COLOR_WHITE, ILI9341_COLOR_BLACK); TM_ILI9341_Puts(28, 180, "Press 'r' to start again!!", &TM_Font_7x10, ILI9341_COLOR_WHITE, ILI9341_COLOR_BLACK); } } else { /* Clear flag */ GameOverDisplay = 0; } /* Check if connected device is keyboard */ if (TM_USB_HIDHOST_Device() == TM_USB_HIDHOST_Result_KeyboardConnected) { /* Green LED ON */ TM_DISCO_LedOn(LED_GREEN); /* Read keyboard data */ TM_USB_HIDHOST_ReadKeyboard(&Keyboard); /* If any buttons active */ if (Keyboard.ButtonStatus == TM_USB_HIDHOST_Button_Pressed) { /* Check pressed button and do actions */ switch ((uint8_t)Keyboard.Button) { case SNAKE_KEY_LEFT: /* Change direction if possible */ if ( Snake.Direction == SNAKE_DIRECTION_UP || Snake.Direction == SNAKE_DIRECTION_DOWN || Snake.Direction == SNAKE_DIRECTION_LEFT ) { /* Disable pause mode */ Settings.Pause = 0; /* Set direction */ Snake1.Direction = SNAKE_DIRECTION_LEFT; } break; case SNAKE_KEY_RIGHT: /* Change direction if possible */ if ( Snake.Direction == SNAKE_DIRECTION_UP || Snake.Direction == SNAKE_DIRECTION_DOWN || Snake.Direction == SNAKE_DIRECTION_RIGHT ) { /* Disable pause mode */ Settings.Pause = 0; /* Set direction */ Snake1.Direction = SNAKE_DIRECTION_RIGHT; } break; case SNAKE_KEY_UP: /* Change direction if possible */ if ( Snake.Direction == SNAKE_DIRECTION_LEFT || Snake.Direction == SNAKE_DIRECTION_RIGHT || Snake.Direction == SNAKE_DIRECTION_UP ) { /* Disable pause mode */ Settings.Pause = 0; /* Set direction */ Snake1.Direction = SNAKE_DIRECTION_UP; } break; case SNAKE_KEY_DOWN: /* Change direction if possible */ if ( Snake.Direction == SNAKE_DIRECTION_LEFT || Snake.Direction == SNAKE_DIRECTION_RIGHT || Snake.Direction == SNAKE_DIRECTION_DOWN ) { /* Disable pause mode */ Settings.Pause = 0; /* Set direction */ Snake1.Direction = SNAKE_DIRECTION_DOWN; } break; case SNAKE_KEY_SPEED_UP: /* Increase speed if possible */ TM_SNAKE_SpeedUp(); break; case SNAKE_KEY_SPEED_DOWN: /* Decrease speed if possible */ TM_SNAKE_SpeedDown(); break; case SNAKE_KEY_PAUSE: /* Toggle pause */ if (Settings.Pause) { Settings.Pause = 0; } else { Settings.Pause = 1; } break; case SNAKE_KEY_RESET: /* Draw snake area */ TM_SNAKE_DrawArea(); /* Set default snake, leave as it was before */ TM_SNAKE_SetDefaultSnake(); /* Generate random target */ TM_SNAKE_GenerateTarget(); /* Disable gameover */ GameOver = 0; /* Reset first time flag */ Snake_FirstTime = 1; break; case SNAKE_KEY_OVERFLOW: /* Toggle overflow mode */ if (Settings.Overflow) { Settings.Overflow = 0; } else { Settings.Overflow = 1; } break; default: break; } } } else { /* Green LED OFF */ TM_DISCO_LedOff(LED_GREEN); } /* Update LCD with changed settings */ /* Check overflow */ if (Settings1.Overflow != Settings.Overflow || Settings1.Speed != Settings.Speed) { /* Save new */ Settings1.Overflow = Settings.Overflow; /* Save new */ Settings1.Speed = Settings.Speed; /* Display game mode and speed */ sprintf(Buffer, "Mode:%4d; Speed: %2d/%2d", Settings.Overflow, Settings.Speed, SNAKE_SPEED_MAX); TM_ILI9341_Puts(10, SNAKE_TEXT_LINE1, Buffer, &TM_Font_7x10, 0x0000, SNAKE_COLOR_MAIN_BCK); } /* Check snake hits */ if (Snake1.Hits != Snake.Hits) { /* Save new */ Snake1.Hits = Snake.Hits; /* Display Speed */ sprintf(Buffer, "Hits:%4d; Score: %5d", Snake.Hits, (2 - Settings.Overflow) * Snake.Hits * Settings.Speed); TM_ILI9341_Puts(10, SNAKE_TEXT_LINE2, Buffer, &TM_Font_7x10, 0x0000, SNAKE_COLOR_MAIN_BCK); } } }