/*
 * Menu.c
 *
 *  Created on: May 5, 2022
 *      Author: Jano
 */

#include <stdio.h>
#include <stdbool.h>
#include "main.h"
#include "stm32l4xx.h"
#include "LCD.h"
#include "Vibration_Motor.h"
#include "Menu.h"

extern bool enc_up;
extern bool enc_down;
extern bool enc_sw;

extern uint16_t subs_size;
extern uint16_t vibration_pwm;
extern uint16_t pre_heat_temp;
extern uint16_t hardening_temp;
extern uint16_t pre_heat_time;
extern uint16_t in_fluid_time;
extern uint16_t hardening_time;
extern uint16_t iterations;
extern bool start_process;

char disp[20];

uint16_t menu_state;
uint16_t last_menu_state;
uint16_t start = 0;





void Encoder_HANDLER(uint16_t *variable, int up_limit, int down_limit){

	if (!HAL_GPIO_ReadPin(ENC_CLK_GPIO_Port, ENC_CLK_Pin)){

		if (!HAL_GPIO_ReadPin(ENC_DT_GPIO_Port, ENC_DT_Pin))
		{
			while (!HAL_GPIO_ReadPin(ENC_DT_GPIO_Port, ENC_DT_Pin)){
			}
			if(*variable > down_limit){
				*variable = *variable-1;
			}
			while (!HAL_GPIO_ReadPin(ENC_CLK_GPIO_Port, ENC_CLK_Pin)){
			}
			HAL_Delay (5);
		}

		else if (HAL_GPIO_ReadPin(ENC_DT_GPIO_Port, ENC_DT_Pin)){
			while (HAL_GPIO_ReadPin(ENC_DT_GPIO_Port, ENC_DT_Pin)){
			}
			if(*variable < up_limit){
				*variable = *variable+1;
			}
			while (!HAL_GPIO_ReadPin(ENC_CLK_GPIO_Port, ENC_CLK_Pin)){
			}
			while (!HAL_GPIO_ReadPin(ENC_DT_GPIO_Port,ENC_DT_Pin)){
			}
			HAL_Delay (5);
		}
	}
}

void Menu_INIT(){

	subs_size = 1;
	vibration_pwm = 1;
	pre_heat_temp = 50;
	hardening_temp = 50;
	pre_heat_time = 10;
	in_fluid_time = 10;
	hardening_time = 10;
	iterations = 1;
	start_process = false;
	menu_state = 1;

	lcd_clear();
	lcd_set_line_col(0, 3);
	lcd_send_string("Fluidized bed");
	lcd_set_line_col(1, 3);
	lcd_send_string("for hybrid IC");
	lcd_set_line_col(2, 5);
	lcd_send_string("packaging");
	lcd_set_line_col(3, 2);
	lcd_send_string("PRESS TO START");
	while(enc_sw == false){
	}
	enc_sw = false;
    lcd_clear();
    lcd_set_line_col(0,0);
    lcd_send_string(" Substrate size:2");
    lcd_set_line_col(0,17);
    sprintf(disp, "%c", (char)34);
    lcd_send_string(disp);
    lcd_set_line_col(1,0);
    lcd_send_string(" Vibrations:1%");
    lcd_set_line_col(2,0);
    lcd_send_string(" Pre-Heat temp:50");
    sprintf(disp,"%cC", (char)223);
    lcd_set_line_col(2,17);
    lcd_send_string(disp);
    lcd_set_line_col(3,0);
    lcd_send_string(" Pre-Heat time:10s");
}

void Menu_LOOP(){

  switch (menu_state){

	  case 1:
		  if(menu_state != last_menu_state){
			  lcd_set_line_col(0,0);
			  lcd_send_string(">");
			  lcd_set_line_col(1,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(2,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(3,0);
			  lcd_send_string(" ");

			  last_menu_state = menu_state;
		  }
		  if(enc_sw == true){
			  enc_sw = false;

			  while(enc_sw == false){
					  Encoder_HANDLER(&subs_size, 2, 1);
					  if(subs_size == 1){
						  sprintf(disp,"%d%c", 2, (char)34);
					  }
					  if(subs_size == 2){
						  sprintf(disp,"%d%c", 4, (char)34);
					  }
					  lcd_set_line_col(0,16);
					  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 2:
		  if(menu_state != last_menu_state){
			  lcd_set_line_col(0,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(1,0);
			  lcd_send_string(">");
			  lcd_set_line_col(2,0);
			  lcd_send_string(" ");

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;
			  //Vibration_START();

			  while(enc_sw == false){
				  Encoder_HANDLER(&vibration_pwm, 100, 1);
				  Vibration_SET(vibration_pwm);
				  lcd_set_line_col(1,12);
				  sprintf(disp,"%d%c ", vibration_pwm, '%');
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
			  // Vibration_STOP();
		  }
		  break;

	  case 3:
		  if(menu_state != last_menu_state){
			  lcd_set_line_col(0,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(1,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(2,0);
			  lcd_send_string(">");
			  lcd_set_line_col(3,0);
			  lcd_send_string(" ");

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;

			  while(enc_sw == false){
				  Encoder_HANDLER(&pre_heat_temp, 250, 50);
				  lcd_set_line_col(2,15);
				  sprintf(disp,"%d%cC ", pre_heat_temp, (char)223);
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 4:
		  if(menu_state != last_menu_state){
			  if(last_menu_state > 4){
				    lcd_clear();
				    lcd_set_line_col(0,0);
				    lcd_send_string(" Substrate size: ");
				    lcd_set_line_col(0,16);
				    if(subs_size == 1){
				    	sprintf(disp,"%d%c", 2, (char)34);
				    }
				    if(subs_size == 2){
				    	sprintf(disp,"%d%c", 4, (char)34);
				    }
				    lcd_send_string(disp);
				    lcd_set_line_col(1,0);
				    lcd_send_string(" Vibrations: ");
				    sprintf(disp, "%d%c ", vibration_pwm, '%');
				    lcd_set_line_col(1,12);
				    lcd_send_string(disp);
				    lcd_set_line_col(2,0);
				    lcd_send_string(" Pre-Heat temp: ");
				    sprintf(disp,"%d%cC ", pre_heat_temp, (char)223);
				    lcd_set_line_col(2,15);
				    lcd_send_string(disp);
				    lcd_set_line_col(3,0);
				    lcd_send_string(">Pre-Heat time: ");
				    sprintf(disp,"%ds", pre_heat_time);
				    lcd_set_line_col(3,15);
				    lcd_send_string(disp);
			  }

			  else{
				  lcd_set_line_col(2,0);
				  lcd_send_string(" ");
				  lcd_set_line_col(3,0);
				  lcd_send_string(">");
			  }

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;

			  while(enc_sw == false){
				  Encoder_HANDLER(&pre_heat_time, 999, 10);
				  lcd_set_line_col(3,15);
				  sprintf(disp,"%ds ", pre_heat_time);
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 5:
		  if(menu_state != last_menu_state){
			  if(last_menu_state < 5){
				  lcd_clear();
				  lcd_set_line_col(0,0);
				  lcd_send_string(">In-Fluid time: ");
				  sprintf(disp,"%ds", in_fluid_time);
				  lcd_set_line_col(0,15);
				  lcd_send_string(disp);
				  lcd_set_line_col(1,0);
				  lcd_send_string(" Harden temp: ");
				  sprintf(disp,"%d%cC", hardening_temp, (char)223);
				  lcd_set_line_col(1,13);
				  lcd_send_string(disp);
				  lcd_set_line_col(2,0);
				  lcd_send_string(" Harden time: ");
				  sprintf(disp,"%ds", hardening_time);
				  lcd_set_line_col(2,13);
				  lcd_send_string(disp);
				  lcd_set_line_col(3,0);
				  lcd_send_string(" Iterations: ");
				  lcd_set_line_col(3,12);
				  sprintf(disp,"%d", iterations);
				  lcd_send_string(disp);

			  }
			  else{
				  lcd_set_line_col(0,0);
				  lcd_send_string(">");
				  lcd_set_line_col(1,0);
				  lcd_send_string(" ");
				  lcd_set_line_col(2,0);
				  lcd_send_string(" ");
				  lcd_set_line_col(3,0);
				  lcd_send_string(" ");
			  }

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;

			  while(enc_sw == false){
				  Encoder_HANDLER(&in_fluid_time, 999, 10);
				  lcd_set_line_col(0,15);
				  sprintf(disp,"%ds ", in_fluid_time);
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 6:
		  if(menu_state != last_menu_state){
			  lcd_set_line_col(0,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(1,0);
			  lcd_send_string(">");
			  lcd_set_line_col(2,0);
			  lcd_send_string(" ");

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;

			  while(enc_sw == false){
				  Encoder_HANDLER(&hardening_temp, 255, 50);
				  lcd_set_line_col(1,13);
				  sprintf(disp,"%d%cC", hardening_temp,  (char)223);
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 7:
		  if(menu_state != last_menu_state){
			  lcd_set_line_col(1,0);
			  lcd_send_string(" ");
			  lcd_set_line_col(2,0);
			  lcd_send_string(">");
			  lcd_set_line_col(3,0);
			  lcd_send_string(" ");

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;

			  while(enc_sw == false){
				  Encoder_HANDLER(&hardening_time, 999, 10);
				  lcd_set_line_col(2,13);
				  sprintf(disp,"%ds ", hardening_time);
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 8:
		  if(menu_state != last_menu_state){
			  if(last_menu_state > 8){
				  lcd_clear();
				  lcd_set_line_col(0,0);
				  lcd_send_string(" In-Fluid time: ");
				  sprintf(disp,"%ds", in_fluid_time);
				  lcd_set_line_col(0,15);
				  lcd_send_string(disp);
				  lcd_set_line_col(1,0);
				  lcd_send_string(" Harden temp: ");
				  sprintf(disp,"%d%cC", hardening_temp, (char)223);
				  lcd_set_line_col(1,13);
				  lcd_send_string(disp);
				  lcd_set_line_col(2,0);
				  lcd_send_string(" Harden time: ");
				  sprintf(disp,"%ds", hardening_time);
				  lcd_set_line_col(2,13);
				  lcd_send_string(disp);
				  lcd_set_line_col(3,0);
				  lcd_send_string(">Iterations: ");
				  lcd_set_line_col(3,12);
				  sprintf(disp,"%d", iterations);
				  lcd_send_string(disp);
			  }
			  else{
				  lcd_set_line_col(2,0);
				  lcd_send_string(" ");
				  lcd_set_line_col(3,0);
				  lcd_send_string(">");
			  }

			  last_menu_state = menu_state;
		  }

		  if(enc_sw){
			  enc_sw = false;

			  while(enc_sw == false){
				  Encoder_HANDLER(&iterations, 50, 1);
				  lcd_set_line_col(3,12);
				  sprintf(disp,"%d ", iterations);
				  lcd_send_string(disp);
			  }
			  enc_sw = false;
		  }
		  break;

	  case 9:
		  if(menu_state != last_menu_state){
			  if(last_menu_state < 9){
				  lcd_clear();
				  lcd_set_line_col(0,0);
				  lcd_send_string(">Start Process");
			  }
			  else{
				  lcd_set_line_col(0,0);
				  lcd_send_string(">");
				  lcd_set_line_col(1,0);
				  lcd_send_string(" ");
				  lcd_set_line_col(2,0);
				  lcd_send_string(" ");
				  lcd_set_line_col(3,0);
				  lcd_send_string(" ");

			  }
			  last_menu_state = menu_state;
		  }

		  if(enc_sw){

			  enc_sw = false;
			  lcd_clear();
			  lcd_set_line_col(0,2);
			  lcd_send_string("Do You really");
			  lcd_set_line_col(1,2);
			  lcd_send_string("want to start?");
			  start = 1;

			  while(enc_sw == false){
				  Encoder_HANDLER(&start, 2, 1);
				  if(start == 1){
					  lcd_set_line_col(2,9);
					  lcd_send_string("NO ");
				  }
				  if(start == 2){
					  lcd_set_line_col(2,9);
					  lcd_send_string("YES");
				  }
			  }
		  }
		  enc_sw = false;

		  if(start == 1){
			  start = 0;
			  lcd_clear();
			  lcd_set_line_col(0,0);
			  lcd_send_string(">Start Process");
		  }
		  if(start == 2){
			  start = 0;
			  Fluidization_PROCESS();
			  lcd_clear();
			  lcd_set_line_col(0,0);
			  lcd_send_string(">Start Process");
		  }

		  break;

  }
}
