#include "ublox.h"

//remember to enable uart interrupts, set 9600boaud and change HAL_UART_Receive_IT(&GPS_Uart_Inst,UBX_CFG_MSG,36); and extern UART_HandleTypeDef GPS_Uart_Inst; in ublox.h if using different uart
//up untill ublox fixes, it returns 0 in all fields.

volatile uint8_t GPS_data_buff[150]; // 136 exact length
//remember to enable uart interrupts, set 9600boaud and change HAL_UART_Receive_IT(&GPS_Uart_Inst,UBX_CFG_MSG,36); and extern UART_HandleTypeDef GPS_Uart_Inst; in ublox.h if using different uart
//up untill ublox fixes, it returns 0 in all fields.

volatile uint8_t GPS_data_buff[150]; // 136 exact length

void UBLOX_init()
{
	HAL_Delay(1000);

	uint8_t UBX_CFG_MSG[]    = {0xb5,0x62,0x06,0x01,0x08,0x00,   0xf0,0x00, 0x01,0x00,0x01,0x01,0x01,0x01,   0x04,0x33};
	uint8_t UBX_NAV_POSLLH[] = {0xb5,0x62,0x06,0x01,0x08,0x00,   0x01,0x02, 0x00,0x01,0x00,0x00,0x00,0x00,   0x13,0xbe};
	uint8_t UBX_NAV_PVT[]    = {0xb5,0x62,0x06,0x01,0x08,0x00,   0x01,0x07, 0x00,0x01,0x00,0x00,0x00,0x00,   0x18,0xE1};

	// disable MNEMEA messages  one by one by setting update rate to 0 in USART interface

	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GGA

	UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x3a;
	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GLL

	UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x41;
	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GSA

	UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x48;
	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // GSV

	UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x4f;
	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // RMC

	UBX_CFG_MSG[7]++; UBX_CFG_MSG[14]++; UBX_CFG_MSG[15]=0x56;
	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_CFG_MSG,16,20); // VTG


	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_NAV_POSLLH,16,20); ///UBX-NAV-POSLLH
	HAL_UART_Transmit(&GPS_Uart_Inst,UBX_NAV_PVT,16,20); ///UBX-NAV-PVT

	// CONFIG
	HAL_UART_Transmit(&GPS_Uart_Inst,(uint8_t[]){0xb5,0x62,0x06,0x24,0x24,0x00,0xff,0xff,0x08,0x03,0x00,0x00,0x00,0x00,0x10,0x27,0x00,0x00,0x05,0x00,0xfa,0x00,0xfa,0x00,0x64,0x00,0x2c,0x01,0x00,0x3c,0x00,0x00,0x00,0x00,0xc8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1c,0x6c},44,55);
}


void UBLOX_get_data_from_buff(GPS_Data_t * GPS_Data_pointer)
{
	GPS_Data_pointer->year = ((GPS_data_buff[5+6] << 8)|(GPS_data_buff[4+6]));
	GPS_Data_pointer->month =  GPS_data_buff[6+6];
	GPS_Data_pointer->day =  GPS_data_buff[7+6];
	GPS_Data_pointer->hour =  GPS_data_buff[8+6];
	GPS_Data_pointer->min =  GPS_data_buff[9+6];
	GPS_Data_pointer->sec =  GPS_data_buff[10+6];
	GPS_Data_pointer->num_of_sat =  GPS_data_buff[23+6];
	GPS_Data_pointer->longitude =  ((GPS_data_buff[100+6+7] << 24)|(GPS_data_buff[100+6+6] << 16)|(GPS_data_buff[100+6+5] << 8)|(GPS_data_buff[100+6+4]));
	GPS_Data_pointer->latitude =  ((GPS_data_buff[100+6+11] << 24)|(GPS_data_buff[100+6+10] << 16)|(GPS_data_buff[100+6+9] << 8)|(GPS_data_buff[100+6+8]));
	GPS_Data_pointer->height_sea_lvl =  ((GPS_data_buff[100+6+19] << 24)|(GPS_data_buff[100+6+18] << 16)|(GPS_data_buff[100+6+17] << 8)|(GPS_data_buff[100+6+16]));
    printf("%d     %d      %d    \r\n",GPS_Data_pointer->longitude,GPS_Data_pointer->latitude,GPS_Data_pointer->height_sea_lvl);
}

void UBLOX_receive(void)
{
	//printf("dupa1\r\n");
	//HAL_UART_Receive(&GPS_Uart_Inst,GPS_data_buff,1,1);
	HAL_UART_Receive_DMA(&GPS_Uart_Inst,GPS_data_buff,136);
}



/*void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) // interupt on rising edge
{
	if(GPIO_Pin == TIMEPULSE_Pin)
	{
	    UBLOX_receive();
	    printf("dupa\r\n");
	}
}*/

//https://www.u-blox.com/sites/default/files/products/documents/u-blox8-M8_ReceiverDescrProtSpec_%28UBX-13003221%29_Public.pdf#page=370&zoom=100,0,0
//str 371 -> num of sat
//time hh:mm:ss
