#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_fsmc.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_it.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_tim.h"
#include "misc.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_usart.h"
#include <math.h>

#include "cp850-8x16.dat"

#define TFT_ADR 0x60000001

#define BLACK      					0x0
#define DIM_GREEN      				0x1
#define MEDIUM_GREEN				0x2
#define GREEN      					0x3
#define DIM_RED      				0x4
#define DIM_YELLOW      			0x5
#define GREENISH_YELLOW      		0x6
#define REDDISH_GREEN      			0x7
#define MEDIUM_RED      			0x8
#define REDDISH_YELLOW     			0x9
#define MEDIUM_YELLOW      			0xa
#define YELLOWISH_GREEN     		0xb
#define RED     					0xc
#define GREENISH_RED     			0xd
#define YELLOWISH_RED      			0xe
#define YELLOW      				0xf

#define SIZE_X      320
#define SIZE_Y      240
#define X_START_POS    53
#define Y_START_POS    7
#define THP      2    // Hsync length
#define TVP      4    // Vsync length in H lines

#define MAX_X      (SIZE_X - 1 )
#define MAX_Y      (SIZE_Y - 1 )

#define BUFFER_X    0               // when needed
#define IMAGEWIDTH    (SIZE_X + BUFFER_X)

#define H_LINE_LEN    	(IMAGEWIDTH+X_START_POS) // entire row
#define V_LEN      		(SIZE_Y+Y_START_POS)
#define HSYNC_ADR_MASK    0x20000                  // A16 active when 0
#define VSYNC_ADR_MASK    0x40000                  // A17 active when 0

#define VG  1                   // Foreground is drawn
#define HG  2                   // Background is drawn

uint8_t draw_mode = VG+HG;  // Default: foreground + background
uint16_t line_cnt;              // for DMA
uint32_t akt_picture_address;      // for DMA
int8_t black_pixel= 0 ;     // for DMA

uint8_t *active_font = (uint8_t *)cp850_font_8x16;  // active font

#define CHARACTER_LENGTH          active_font[ 0 ]   // character length
#define CHARACTER_HEIGHT    		active_font[ 1 ] // character height

uint8_t foreground = BLACK, background = YELLOW;   // default
int8_t zoom_x = 1 , zoom_y = 1 ;
int16_t akt_x= 0 , akt_y= 0 ;      // for writing output

uint8_t imagememory[X_START_POS + SIZE_X*SIZE_Y];  // with H-sync lead
uint8_t *image = &imagememory[X_START_POS-1];    // eff. picture Start

char buff[4];

int32_t hodnota1 = 0, hodnota2 = 0, wid = 0;
const char id_atmel = 'U';
const char id_pc = 'P';
char r_data;
int state_id = 0;
int start = 0;
int32_t OffsetLeft = 0, OffsetRight = 0;
int usart_connected =0;

/////for pong game
int l_ball_pos_x = 150;
int l_ball_pos_y = 120;
int ball_pos_x = 150;
int ball_pos_y = 120;
int pong_start[2] = {0,0};
int course = 0;
int plate1, plate2 = 0;
int l_plate1, l_plate2 = 1;
int plate_start = 0;
int pong_out, pong_outt = 0;	//signalizuje naraz do steny, pri neodrazenem ball
int pong_size_y = 21;
int pong_score[2] = {0,0};
int measure_game = 0;
int pong_game = 0;
int pong_velocity = 20000;
int pong = 20000; //rychlost
uint8_t pong_game_active = 0;
uint8_t pong_game_mode = 0;
int press_counter = 0;
uint8_t menu_press = 50;


int png_cnt = 0;
int pong_ball = 10;
int pong_step = 1;
int plate_height = 10 * 7;
int plate_width = 10;
#define CNT_LN	2
int ey[CNT_LN];
int ey_last[CNT_LN];



//POMOCNE PROMENNE//

int my_counter_1 = 0;
int usart_counter_1 = 0;
int comp_counter_1 = 0;

//////////////////////////////////
////	KOREKCE	//////////////////
int32_t copy_hodnota1 = 0;
int32_t copy_hodnota2 = 0;
int hodnota1_prumer = 0;
int hodnota2_prumer = 0;
int min_hodnota1 = 100;
int min_hodnota2 = 100;
int max_hodnota1 = 0;
int max_hodnota2 = 0;
int memlen = 40;
int32_t mem1[40];
int32_t mem2[40];
int kompenzace1 = 0;
int kompenzace2 = 0;
int kompenzovana1 = 0;
int kompenzovana2 = 0;
int stisk1 = 0;
int stisk2 = 0;
int fall1 = 0;
int fall2 = 0;
int32_t copy_hodnota1_minula = 0;
int32_t copy_hodnota2_minula = 0;
int fall1_counter = 0;
int fall2_counter = 0;
///pro cykly
int i =0;

int PROGRAM_STATE = 0;
int LAST_PROGRAM_STATE = 20;

int SECONDS_TIMER_STATE = 0;

int count_seconds = 0;
int TEN_SECONDS = 0;
int ONE_SECOND = 0;
int counter_to_start = 5;


//ostatn
int cnt_ln = CNT_LN;
int q = 0, qq = 0, qqq = 0, qmax = 1000;
int mf1 = 0, mf2 = 0; //pocitadla pro zobrazovani maximalnich hodnot
int ln_width = 20;
int ln_space = 20;
int bar_start = 0;
int top_bound = 20;
int bar1 = 0;
int bar2 = 0;
int l_bar1 = 20;//hodnota musi byt vetsi nez nula, aby se na zacatku neprekreslil ramecek
int l_bar2 = 20;//hodnota musi byt vetsi nez nula, aby se na zacatku neprekreslil ramecek
int actual_bar = 0;

int max_high = 20;
int low_high = 15;
int8_t ln_color;
int8_t ln_col;

int actual_force_cnt = 0;
int actual_force_cnt2 = 0;
int max_force1 = 0, max_force2 = 0;
int max_force1_active = 0;
int max_force2_active = 0;
int rec = 0;
int h1last = 0;
int h2last = 0;

int tmp;
int tmp2, tmp3;

int DISPLAY_DIRECT_DRIVE = 10;
int temp_done = 0;
int temp2_done = 0;

int temp4, temp5;
int command_code = 0;
int command_color = 0;
uint8_t cmd_color = BLACK;
int command_x1 = 0;
int command_x2 = 0;
int command_y1 = 0;
int command_y2 = 0;

#define TEXT_LENGTH 40
#define CMD_TEXT (TEXT_LENGTH+1)//delka samotneho textu v prikazu(delka realneho textu - 1)35slo ok
#define BUFF_CMD_LENGTH (20+CMD_TEXT)

char buff_command[BUFF_CMD_LENGTH];//puvodne18

char command_text[CMD_TEXT];
int command_zoom_x = 1;
int command_ready = 0;

int command_err_warn = 0;

//Nastaveni hodnot pro prepinani Modul_State
#define NO_CONNECTION 	0
#define MEASURING_CARD 	1
#define PC_CONNECTED	2
int Modul_State = NO_CONNECTION;
int Last_Modul_State = NO_CONNECTION;
int no_connection_first_run = 0;

char checksum_received[2];//zasobnik pro prijaty checksum
//pro vypocet checksum prijate zpravy
int for_chcksum = 0;
int chsum = 0;
char checksum_computed[2];
char dh_vysledek[2];

int zero = 0;

char cb1[25];
char cb2[CMD_TEXT-5];//pro 35 znaku je 31
int xxx = 0;
