/*
 * APP_morsecode.c
 *
 *  Created on: 2022Äê3ÔÂ5ÈÕ
 *      Author: wuwen
 */

#include "APP_morsecode.h"

#define morse_t1 150
#define morse_t2 (morse_t1*3)
#define morse_hz 100

#define morse_play_t1 50
#define morse_play_t2 (morse_play_t1*3)
#define morse_play_hz 100

char push_key=0,last_key=1;
uint32_t morse_t=0;
uint32_t morse_time_out=0;
uint16_t morse_temp;

#define morse_input_buff_num 128
char morse_input_buff[morse_input_buff_num+1];
int morse_input_flag=0;
int morse_char_flag=0;
int morse_x,morse_y;
char morse_flah=0;
uint8_t morse_letter_temp=0;
uint8_t morse_letter_flag=0;

#define morsecodenum 38
const struct morsecode morsecodes[morsecodenum]=
{
		{
				2,0b01000000,'A'
		},
		{
				4,0b10000000,'B'
		},
		{
				4,0b10100000,'C'
		},
		{
				3,0b10000000,'D'
		},
		{
				1,0b00000000,'E'
		},
		{
				4,0b00100000,'F'
		},
		{
				3,0b11000000,'G'
		},
		{
				4,0b00000000,'H'
		},
		{
				2,0b00000000,'I'
		},
		{
				4,0b01110000,'J'
		},
		{
				3,0b10100000,'K'
		},
		{
				4,0b01000000,'L'
		},
		{
				2,0b11000000,'M'
		},
		{
				2,0b10000000,'N'
		},
		{
				3,0b11100000,'O'
		},
		{
				4,0b01100000,'P'
		},
		{
				4,0b11010000,'Q'
		},
		{
				3,0b01000000,'R'
		},
		{
				3,0b00000000,'S'
		},
		{
				1,0b10000000,'T'
		},
		{
				3,0b00100000,'U'
		},
		{
				4,0b00010000,'V'
		},
		{
				3,0b01100000,'W'
		},
		{
				4,0b10010000,'X'
		},
		{
				4,0b10110000,'Y'
		},
		{
				4,0b11000000,'Z'
		},
		{
				5,0b01111000,'1'
		},
		{
				5,0b00111000,'2'
		},
		{
				5,0b00011000,'3'
		},
		{
				5,0b00001000,'4'
		},
		{
				5,0b00000000,'5'
		},
		{
				5,0b10000000,'6'
		},
		{
				5,0b11000000,'7'
		},
		{
				5,0b11100000,'8'
		},
		{
				5,0b11110000,'9'
		},
		{
				5,0b11111000,'0'
		},
		{
				6,0b00110000,'?'
		},
		{
				5,0b10010000,'/'
		},

};

window *morsecode_window;

void APP_morsecode_init(window *a_window)
{
	morsecode_window=a_window;

	for(int a=0;a<65;a++)
	{
		morse_input_buff[a]='\0';
	}


}


char getmorsecode(uint8_t len,uint8_t code)
{
	for(int a=0;a<morsecodenum;a++)
	{
		if((morsecodes[a].len==len)&&(morsecodes[a].code==code))
		{
			return morsecodes[a].letter;
		}

	}
	return 0;
}



void APP_morsecode_loop()
{
	/*
	char str[64],get_char_temp;
	sprintf(str,"%d %d  ",morse_input_flag,morse_char_flag);
	LCD_ShowString(morsecode_window->x, morsecode_window->y+100, str, 16, GREEN, RED);
*/
	if(k3.code==1)
	{
		for(int a=0;a<64;a++)
		{

			if(morse_input_buff[a]=='\0')
			{
				break;
			}

			if(morse_input_buff[a]==' ')
			{
				add_a_note(0, 50, morse_play_t2);
			}else
			{
				for(int b=0;b<morsecodenum;b++)
				{
					if(morsecodes[b].letter==morse_input_buff[a])
					{
						for(int c=0;c<morsecodes[b].len;c++)
						{
							if(morsecodes[b].code&(0x80>>c))
							{
								add_a_note(morse_play_hz, 50, morse_play_t2);
							}else
							{
								add_a_note(morse_play_hz, 50, morse_play_t1);
							}
							add_a_note(0, 50, morse_play_t1);
						}
						break;
					}

				}
				add_a_note(0, 50, morse_play_t2);
			}
		}

	}
	if(k2.code==3)
	{
		sprintf(morse_input_buff,"EISH5  E I S H 5");
	}
	if(k2.code==2)
	{
		sprintf(morse_input_buff,"HELLO WORLD");
	}
	if(k2.code==3)
	{
		sprintf(morse_input_buff,"CQCQ DE HELLO");
	}
	if(k2.code==1)
	{
		morse_x=morsecode_window->x;
		morse_y=morsecode_window->y+16;

		for(int a=0;a<morse_input_buff_num;a++)
		{
			morse_input_buff[a]='\0';
			if(morse_x>=(morsecode_window->x+morsecode_window->width))
			{
				morse_x=morsecode_window->x;
				morse_y+=16;
				if(morse_y>=(morsecode_window->y+morsecode_window->high))
				{
					morse_y=morsecode_window->y+16;
				}
			}
			LCD_ShowChar(morse_x,morse_y,' ',16,CYAN,RED);

			morse_x+=8;
		}
		morse_input_flag=0;
		morse_char_flag=0;
		morse_flah=0;
	}

	char get_char_temp;
	push_key=KEY1;

	if(push_key!=last_key)
	{

		if(push_key==0)
		{
			morse_t=HAL_GetTick();
			play_ones(morse_hz,50);
		}else
		{
			morse_temp=HAL_GetTick()-morse_t;
			if(morse_temp>morse_t2)
			{
				//time out error
			}else if(morse_temp>morse_t1)
			{
				//-
				morse_input_buff[morse_input_flag]='-';
				morse_letter_temp|=(0x80>>morse_letter_flag);
			}else
			{
				//.
				morse_input_buff[morse_input_flag]='.';

			}
			morse_input_flag++;
			morse_letter_flag++;
			//if(morse_input_flag>=morse_char_flag+8){morse_input_flag=morse_char_flag;morse_letter_flag=0;}
			play_ones(0,0);
		}


		last_key=push_key;
		morse_time_out=HAL_GetTick();
	}

	if(push_key==1)
	{
		//Get cursor on the screen
		if((HAL_GetTick()-morse_time_out)>morse_t1)
		{
				if((morse_input_flag-morse_char_flag)>0)
				{

				get_char_temp=getmorsecode(morse_letter_flag,morse_letter_temp);

				morse_x=morsecode_window->x;
				morse_y=morsecode_window->y+16;
				for(int a=0;a<morse_char_flag;a++)
				{

					if(morse_x>=(morsecode_window->x+morsecode_window->width))
					{
						morse_x=morsecode_window->x;
						morse_y+=16;
						if(morse_y>=(morsecode_window->y+morsecode_window->high))
						{
							morse_y=morsecode_window->y+16;
						}
					}
					morse_x+=8;
				}

				for(int a=morse_char_flag;a<morse_input_flag;a++)
				{

					if(morse_x>=(morsecode_window->x+morsecode_window->width))
					{
						morse_x=morsecode_window->x;
						morse_y+=16;
						if(morse_y>=(morsecode_window->y+morsecode_window->high))
						{
							morse_y=morsecode_window->y+16;
						}
					}

					if((a==morse_char_flag)&&(get_char_temp!=0))
					{

						LCD_ShowChar(morse_x,morse_y,get_char_temp,16,CYAN,RED);
						morse_input_buff[a]=get_char_temp;

					}else
					{
						LCD_ShowChar(morse_x,morse_y,' ',16,CYAN,RED);
						morse_input_buff[a]='\0';
					}

					morse_x+=8;


				}
				morse_letter_flag=0;
				morse_letter_temp=0;
				if(get_char_temp!=0){morse_flah=1;morse_char_flag+=1;}

				morse_input_flag=morse_char_flag;


			}
		}
		if((HAL_GetTick()-morse_time_out)>morse_t2)
		{
			if(morse_flah==1)
			{
				morse_input_buff[morse_char_flag]=' ';
/*
				morse_x=morsecode_window->x;
				morse_y=morsecode_window->y+16;
				for(int a=0;a<morse_char_flag;a++)
				{

					if(morse_x>=(morsecode_window->x+morsecode_window->width))
					{
						morse_x=morsecode_window->x;
						morse_y+=16;
						if(morse_y>=(morsecode_window->y+morsecode_window->high))
						{
							morse_y=morsecode_window->y+16;
						}
					}
					morse_x+=8;
				}
				LCD_ShowChar(morse_x,morse_y,morse_input_buff[morse_char_flag],16,CYAN,RED);
*/
				morse_char_flag+=1;
				morse_input_flag=morse_char_flag;
				morse_flah=0;
			}
		}
	}

	morse_x=morsecode_window->x;
	morse_y=morsecode_window->y+16;

	for(int a=0;a<morse_input_buff_num;a++)
	{

		if(morse_input_buff[a]=='\0')
		{
			break;
		}

		if(morse_x>=(morsecode_window->x+morsecode_window->width))
		{
			morse_x=morsecode_window->x;
			morse_y+=16;
			//if(morse_y>=(morsecode_window->y+morsecode_window->high))
			//{
				//morse_y=morsecode_window->y+16;
			//}
		}
		LCD_ShowChar(morse_x,morse_y,morse_input_buff[a],16,CYAN,RED);

		morse_x+=8;
	}

}
