#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"
#include "lmx2595.h"
#include <stdlib.h>
#include <stdio.h>              /* sprintf() */


bool gOut = true;
short gPosx,gPosy;
lv_indev_t * indev_touchpad;	// ?????????
lv_obj_t* kb;

char *freqset[20];

short ReadX(){
	static short average[2] = {0};
	static unsigned char pos = 0;
	short ret = 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(1);
	ADC1->CHSELR = LL_ADC_CHANNEL_8;
 ADC1->IER    = 0;
  ADC1->CR |= ADC_CR_ADSTART;

	while (ADC1->CR & ADC_CR_ADSTART)
	;

	if( LL_ADC_REG_ReadConversionData32(ADC1) < 500){
		gPosy = -1;
		return -1;
	}
	float s = (float)( LL_ADC_REG_ReadConversionData32(ADC1) -600)/11.65;
	ret = s;
	{
		gPosy = ret;
		return ret;
	}
}


short ReadY(){
	static short average[3] = {0};
	static unsigned char pos = 0;
	short ret = 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(1);
	ADC1->CHSELR = LL_ADC_CHANNEL_9;
 ADC1->IER    = 0;
  ADC1->CR |= ADC_CR_ADSTART;

	while (ADC1->CR & ADC_CR_ADSTART)
	;

	ret = LL_ADC_REG_ReadConversionData32(ADC1);
	if(ret < 500) {
		gPosx = -1;
		return -1;
	}
	float tmp = (float)(3560 - ret)/9.3;
	ret = (short)tmp;
	if(ret < 0)
		return 0;
	else {
		gPosx = ret;
		return ret;
	}
	
}


static bool touchpad_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data)
{
    static lv_coord_t last_x = 0;
    static lv_coord_t last_y = 0;
	
	
    if( (gPosy > 0 )&&(gPosx > 0)) {
        last_x = gPosx;
        last_y = gPosy;
 
        data->point.x = last_x;
        data->point.y = last_y;
        data->state = LV_INDEV_STATE_PR;
    }
    else
    {
        data->point.x = last_x;
        data->point.y = last_y;
        data->state = LV_INDEV_STATE_REL;
    }

    return false;
}

static void ta_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * ta = lv_event_get_target(e);
    lv_obj_t * kb = lv_event_get_user_data(e);
    if(code == LV_EVENT_FOCUSED) {
        lv_keyboard_set_textarea(kb, ta);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
    if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(kb, NULL);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
		
		if(code == LV_EVENT_INSERT){
			 const char * txt = lv_textarea_get_text(ta);
		}
}

static void event_cb_start(lv_event_t* e)
{
    static uint32_t cnt = 1;
		lv_obj_t* labl = lv_event_get_user_data(e); //??????
	  float freq = atof(lv_textarea_get_text(labl));
		if(freq == 0.0){
			
		}else{
			if(freq > 25.0)
				LMX2592_SETOUT(1,1);
				LMX2592_SETOUT(2,1);
				gOut = true;
				 LMX2592_WRITE_FREQ(freq);
		}
}


int fputc(int ch, FILE *p) {
		LL_mDelay(1);
		LL_USART_TransmitData8(USART1, ch);
		return ch;
}

static void event_cb_stop(lv_event_t* e)
{
	gOut = false;
	LMX2592_SETOUT(1,0);
	LMX2592_SETOUT(2,0);

}

#pragma import(__use_no_semihosting)
struct __FILE 
{
  int handle; 
};

FILE __stdout;
void _sys_exit(int x) 
{ 
  x = x; 
}
void _ttywrch(int ch)
{
	ch = ch;
}

int main(void)
{

	char tmp[20] = "x: ";
	static lv_style_t style;

  /* 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();
	 MX_USART1_UART_Init();

	LL_mDelay(100);
	 LL_ADC_Enable(ADC1); 
	Lmx_PortIntit();
	lv_init(); // LittleVgl (6.0)
	SetLmxParameter();
  /* USER CODE BEGIN 2 */
	ILI9341_init();


	short read111 = lmxRead(68);
		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;              
    disp_drv.ver_res = 240;               

    lv_disp_drv_register(&disp_drv);
		LL_mDelay(100);

		
    lv_indev_drv_t indev_drv;

    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);

		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,120,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,120,50);
		//lv_obj_set_size(label2,100,50);
		//sprintf(tmp,"y:%d", ReadY());
		//lv_label_set_text(label2,tmp);
		
		lv_obj_t *label = lv_label_create(lv_scr_act());
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0xaaaaa), 0); //0x000000 = ??
    lv_label_set_recolor(label, true);                                  //??????
    lv_obj_set_pos(label, 10, 40);                                      //????
    lv_obj_set_size(label, 60, 20);                                    //?????
    LV_FONT_DECLARE(lv_font_montserrat_20)                              //????
		sprintf(tmp,"%d",read111);
    lv_label_set_text(label, "freq:");           //????
    lv_obj_set_style_text_font(label, &lv_font_montserrat_20, 0);


		lv_obj_t *label2 = lv_label_create(lv_scr_act());
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0xaaaaa), 0); //0x000000 = ??
    lv_label_set_recolor(label2, true);                                  //??????
    lv_obj_set_pos(label2, 10, 10);                                      //????
    lv_obj_set_size(label2, 200, 20);                                    //?????
    LV_FONT_DECLARE(lv_font_montserrat_20)                              //????
    lv_label_set_text(label2, "#000000 state: # #ff0000 off #");           //????
    lv_obj_set_style_text_font(label2, &lv_font_montserrat_20, 0);
		
		kb = lv_keyboard_create(lv_scr_act());
		lv_keyboard_set_mode(kb, LV_KEYBOARD_MODE_NUMBER);
    lv_obj_t * ta;
    ta = lv_textarea_create(lv_scr_act());
		lv_textarea_set_one_line(ta, true);
    lv_obj_add_event_cb(ta, ta_event_cb, LV_EVENT_ALL, kb);
		lv_obj_set_pos(ta, 70, 40);                                      //????
    lv_obj_set_size(ta, 110, 60);
		lv_style_init(&style);
    lv_obj_set_style_bg_color(ta, lv_palette_main(LV_PALETTE_BLUE), LV_PART_CURSOR);
    lv_obj_set_style_bg_opa(ta, LV_OPA_COVER, LV_PART_CURSOR);
    lv_obj_add_flag(ta, LV_STATE_FOCUSED);
 
		lv_textarea_add_text(ta, "50.0");

		label = lv_label_create(lv_scr_act());
    lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0xaaaaa), 0); //0x000000 = ??
    lv_label_set_recolor(label, true);                                  //??????
    lv_obj_set_pos(label, 180, 40);                                      //????
    lv_obj_set_size(label, 40, 20);                                    //?????
    LV_FONT_DECLARE(lv_font_montserrat_20)                              //????
    lv_label_set_text(label, "#ff0080 mhz#");           //????
    lv_obj_set_style_text_font(label, &lv_font_montserrat_20, 0);
		
		lv_obj_t *btn1 = lv_btn_create(lv_scr_act());
		lv_obj_set_pos(btn1, 240, 40);                                      //????
    lv_obj_set_size(btn1, 60, 20);
		lv_style_init(&style);

    lv_obj_add_event_cb(btn1, event_cb_start, LV_EVENT_CLICKED, ta); // ??CLICK??

    lv_obj_t* label1 = lv_label_create(btn1);       
		
		lv_obj_t *btn2 = lv_btn_create(lv_scr_act());
		lv_obj_set_pos(btn2, 240, 10);                                      //????
    lv_obj_set_size(btn2, 60, 20);
		lv_obj_add_event_cb(btn2, event_cb_stop, LV_EVENT_CLICKED, ta); // ??CLICK??


    lv_obj_t* label_btn2 = lv_label_create(btn2);       
    lv_obj_set_style_text_font(label_btn2, &lv_font_montserrat_20, LV_PART_MAIN); 
    lv_label_set_text(label_btn2, "stop");                  
    lv_obj_set_align(label_btn2,LV_ALIGN_CENTER);             
		
    lv_obj_set_style_text_font(label1, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_label_set_text(label1, "start");                 
    lv_obj_set_align(label1,LV_ALIGN_CENTER);              
    lv_style_init(&style);

    lv_style_set_radius(&style, 3);

    lv_style_set_bg_opa(&style, LV_OPA_100);
    lv_style_set_bg_color(&style, lv_palette_main(LV_PALETTE_BLUE));
    lv_style_set_bg_grad_color(&style, lv_palette_darken(LV_PALETTE_BLUE, 2));
    lv_style_set_bg_grad_dir(&style, LV_GRAD_DIR_VER);

    lv_style_set_border_opa(&style, LV_OPA_40);
    lv_style_set_border_width(&style, 2);
    lv_style_set_border_color(&style, lv_palette_main(LV_PALETTE_GREY));

    lv_style_set_shadow_width(&style, 8);
    lv_style_set_shadow_color(&style, lv_palette_main(LV_PALETTE_GREY));
    lv_style_set_shadow_ofs_y(&style, 8);

    lv_style_set_outline_opa(&style, LV_OPA_COVER);
    lv_style_set_outline_color(&style, lv_palette_main(LV_PALETTE_BLUE));

    lv_style_set_text_color(&style, lv_color_white());
    lv_style_set_pad_all(&style, 10);

    static lv_style_t style_pr;
    lv_style_init(&style_pr);

    lv_style_set_outline_width(&style_pr, 30);
    lv_style_set_outline_opa(&style_pr, LV_OPA_TRANSP);

    lv_style_set_translate_y(&style_pr, 5);
    lv_style_set_shadow_ofs_y(&style_pr, 3);
    lv_style_set_bg_color(&style_pr, lv_palette_darken(LV_PALETTE_BLUE, 2));
    lv_style_set_bg_grad_color(&style_pr, lv_palette_darken(LV_PALETTE_BLUE, 4));

    static lv_style_transition_dsc_t trans;
    static lv_style_prop_t props[] = {LV_STYLE_OUTLINE_WIDTH, LV_STYLE_OUTLINE_OPA, 0};

    lv_style_set_transition(&style_pr, &trans);

    lv_obj_add_style(btn1, &style, 0);
    lv_obj_add_style(btn1, &style_pr, LV_STATE_PRESSED);


    lv_obj_add_style(btn2, &style, 0);
    lv_obj_add_style(btn2, &style_pr, LV_STATE_PRESSED);

		
		
		LL_mDelay(250);
		SetLmxFreq();

		LMX2592_WRITE_FREQ(50.0);

		int i = 0;
		
		while (1)
		{
			i++;
			if(i > 30){
				read111 = lmxRead(68);
				if((read111&(0x03<<9) == 0) || (read111&(0x03<<9) == 1) || (read111&(0x03<<9) == 2)){
					if(gOut)
						lv_label_set_text(label2, "#000000 state: # #ff0000 unlock # # #00ff00 enable #");           //????
					else
						lv_label_set_text(label2, "#000000 state: # #ff0000 unlock # # #ff0000 disable #");           //????
				}else{
					if(gOut)
						lv_label_set_text(label2, "#000000 state:# #00ff00 locked OK# ");           //????
					else
						lv_label_set_text(label2, "#000000 state: # #00ff00 locked# #ff0000 off#");           //????
				}
				i = 0;
			}
			
			LL_mDelay(5);
			/* USER CODE END WHILE */
			lv_task_handler();
			lv_tick_inc(10);
			ReadX();
			ReadY();
		//sprintf(tmp,"y:%d", ReadX());
		//lv_label_set_text(label1,tmp);//??????
		//LL_mDelay(1);
		//sprintf(tmp,"x:%d", ReadY());
		//lv_label_set_text(label2,tmp);//??????

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