#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "../lvgl/lvgl.h"
#include "../ili9341/core.h"
#include "../ili9341/lv_driver.h"
#include "lv_conf.h"


short ReadX(){
	static short average[5] = {0};
	static unsigned char pos = 0;
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
	
  LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_1);

  LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_1);
			LL_mDelay(100);
	ADC1->CHSELR = LL_ADC_CHANNEL_8;
 ADC1->IER    = 0;
  ADC1->CR |= ADC_CR_ADSTART;

	while (ADC1->CR & ADC_CR_ADSTART)
	;
	average[pos]  = LL_ADC_REG_ReadConversionData32(ADC1);
	pos++;
	if (pos > 5) pos= 0;
	return (average[0] + average[1] + average[2] + average[3] + average[5])/3;
}


short ReadY(){
	static short average[3] = {0};
	static unsigned char pos = 0;
	
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
	
  LL_GPIO_SetOutputPin(GPIOB, LL_GPIO_PIN_0);

  LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_0);
			LL_mDelay(100);
	ADC1->CHSELR = LL_ADC_CHANNEL_9;
 ADC1->IER    = 0;
  ADC1->CR |= ADC_CR_ADSTART;

	while (ADC1->CR & ADC_CR_ADSTART)
	;
	average[pos]  = LL_ADC_REG_ReadConversionData32(ADC1);
	pos++;
	if (pos > 2) pos= 0;
	return (average[0] + average[1] + average[2])/3;
}


int main(void)
{
	char tmp[20] = "x: ";
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);

  /* SysTick_IRQn interrupt configuration */
  NVIC_SetPriority(SysTick_IRQn, 3);

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SPI2_Init();
	MX_ADC_Init();
	LL_mDelay(100);
	 LL_ADC_Enable(ADC1); 


	lv_init(); // LittleVgl (6.0)

  /* USER CODE BEGIN 2 */
	ILI9341_init();

  /* USER CODE END 2 */
		LL_mDelay(100);
		
		static lv_color_t disp_buf1[320 * 10] = {0};
		static lv_disp_draw_buf_t buf;
		lv_disp_draw_buf_init(&buf, disp_buf1, NULL, 320 * 10);


    lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    disp_drv.draw_buf = &buf;
    disp_drv.flush_cb = ILI9341_flush;
    disp_drv.hor_res = 320;                /*Set the horizontal resolution in pixels*/
    disp_drv.ver_res = 240;                /*Set the vertical resolution in pixels*/
    lv_disp_drv_register(&disp_drv);
 	LL_mDelay(100);
		lv_obj_t *src = lv_scr_act();//??????

	lv_obj_t *label1 = lv_label_create(src);//????
	lv_obj_t *label2 = lv_label_create(src);//????

		//LV_LABEL_LONG_EXPAND ??,?????????
		lv_obj_set_pos(label1,20,20);//????
		lv_obj_set_size(label1,100,50);//?????
		sprintf(tmp,"x:%d", ReadX());
		lv_label_set_text(label1,tmp);//??????
    //lv_obj_set_style_bg_color(lv_scr_act(), lv_palette_lighten(LV_PALETTE_GREY, 4), 0);
		
		lv_obj_set_pos(label2,20,50);//????
		lv_obj_set_size(label2,100,50);//?????
		sprintf(tmp,"y:%d", ReadY());
		lv_label_set_text(label2,tmp);//??????
//lv_obj_t* kb = lv_keyboard_create(lv_scr_act());
    //lv_obj_set_align(kb, LV_ALIGN_CENTER);
    //lv_obj_set_size(kb, 300, 150);

		/* Infinite loop */
		/* USER CODE BEGIN WHILE */
		while (1)
		{
				LL_mDelay(5);
			/* USER CODE END WHILE */
					lv_task_handler();
					lv_tick_inc(10);

		sprintf(tmp,"x:%d", ReadX());
		lv_label_set_text(label1,tmp);//??????
				LL_mDelay(1);
			
		sprintf(tmp,"y:%d", ReadY());
		lv_label_set_text(label2,tmp);//??????

			/* USER CODE BEGIN 3 */
		}
  /* USER CODE END 3 */
}
