/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "Button Navigation"
	- Source Code NOT compiled for: Arduino Uno
	- Source Code created on: 2024-06-07 10:28:23

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* lcd display i2c  20x4  button pin11 UP LOW menu up */
	/* button pin10 UP LOW menu enter  button pin12UP LOW */
	/* menu down  Temperatur  submenu   MaxTemp */
	/* ->max_temp -= 0.1;  MinTeamp->min_temp -= 0.1; */
	/* Durchfluss(Flow)  submenu   MaxFlow ->  MinFlow -> */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>  //https://github.com/marcoschwartz/LiquidCrystal_I2C

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void displayMenu(void);
void handleButtonPress(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t UP_BUTTON_PushButton_PIN_D11        = 11;
const uint8_t DOWN_BUTTON_PushButton_PIN_D12      = 12;
const uint8_t ENTER_BUTTON_PushButton_PIN_D10     = 10;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
LiquidCrystal_I2C lcd(0x27, 20, 4);  // set the LCD address to 0x27 for a 20 chars and 4 line display

/****** MENU VARIABLES *****/
enum MenuState { MAIN_MENU, TEMP_SUBMENU, FLOW_SUBMENU };
MenuState currentMenu = MAIN_MENU;
int menuIndex = 0;
float maxTemp = 100.0;
float minTemp = 0.0;
float maxFlow = 10.0;
float minFlow = 0.0;

void setup(void)
{
    // put your setup code here, to run once:

    pinMode(UP_BUTTON_PushButton_PIN_D11,    INPUT_PULLUP);
    pinMode(DOWN_BUTTON_PushButton_PIN_D12,  INPUT_PULLUP);
    pinMode(ENTER_BUTTON_PushButton_PIN_D10, INPUT_PULLUP);

    // Initialize the LCD
    lcd.init();
    lcd.backlight();

    // Display the initial menu
    displayMenu();
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    handleButtonPress();
}

void displayMenu(void)
{
    lcd.clear();
    switch (currentMenu)
    {
        case MAIN_MENU:
            lcd.setCursor(0, 0);
            lcd.print("1. Temperatur");
            lcd.setCursor(0, 1);
            lcd.print("2. Durchfluss");
            break;
        case TEMP_SUBMENU:
            lcd.setCursor(0, 0);
            lcd.print("MaxTemp: ");
            lcd.print(maxTemp, 1);
            lcd.setCursor(0, 1);
            lcd.print("MinTemp: ");
            lcd.print(minTemp, 1);
            break;
        case FLOW_SUBMENU:
            lcd.setCursor(0, 0);
            lcd.print("MaxFlow: ");
            lcd.print(maxFlow, 1);
            lcd.setCursor(0, 1);
            lcd.print("MinFlow: ");
            lcd.print(minFlow, 1);
            break;
    }
}

void handleButtonPress(void)
{
    static uint32_t lastDebounceTime = 0;
    const uint32_t debounceDelay = 50;

    if (millis() - lastDebounceTime > debounceDelay)
    {
        if (digitalRead(UP_BUTTON_PushButton_PIN_D11) == LOW)
        {
            lastDebounceTime = millis();
            if (currentMenu == MAIN_MENU)
            {
                menuIndex = (menuIndex == 0) ? 1 : 0;
            }
            else if (currentMenu == TEMP_SUBMENU)
            {
                maxTemp -= 0.1;
                minTemp -= 0.1;
            }
            else if (currentMenu == FLOW_SUBMENU)
            {
                maxFlow -= 0.1;
                minFlow -= 0.1;
            }
            displayMenu();
        }
        else if (digitalRead(DOWN_BUTTON_PushButton_PIN_D12) == LOW)
        {
            lastDebounceTime = millis();
            if (currentMenu == MAIN_MENU)
            {
                menuIndex = (menuIndex == 1) ? 0 : 1;
            }
            else if (currentMenu == TEMP_SUBMENU)
            {
                maxTemp += 0.1;
                minTemp += 0.1;
            }
            else if (currentMenu == FLOW_SUBMENU)
            {
                maxFlow += 0.1;
                minFlow += 0.1;
            }
            displayMenu();
        }
        else if (digitalRead(ENTER_BUTTON_PushButton_PIN_D10) == LOW)
        {
            lastDebounceTime = millis();
            if (currentMenu == MAIN_MENU)
            {
                if (menuIndex == 0)
                {
                    currentMenu = TEMP_SUBMENU;
                }
                else if (menuIndex == 1)
                {
                    currentMenu = FLOW_SUBMENU;
                }
            }
            else
            {
                currentMenu = MAIN_MENU;
            }
            displayMenu();
        }
    }
}

/* END CODE */