#include "stm32f1xx_hal.h"
#include "lcd.h"  // Make sure to include your LCD library

// Define button and LED pins
#define BUTTON1_PIN GPIO_PIN_0
#define BUTTON2_PIN GPIO_PIN_1
#define LED1_PIN GPIO_PIN_0
#define LED2_PIN GPIO_PIN_1

// Function prototypes
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
void displayFeedback(char *feedback);

I2C_HandleTypeDef hi2c1;

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    MX_I2C1_Init();
    LCD_Init(); // Initialize the LCD display

    while (1)
    {
        if (HAL_GPIO_ReadPin(GPIOA, BUTTON1_PIN) == GPIO_PIN_SET)
        {
            displayFeedback("Good");
            HAL_GPIO_WritePin(GPIOB, LED1_PIN, GPIO_PIN_SET);
            HAL_GPIO_WritePin(GPIOB, LED2_PIN, GPIO_PIN_RESET);
        }
        else if (HAL_GPIO_ReadPin(GPIOA, BUTTON2_PIN) == GPIO_PIN_SET)
        {
            displayFeedback("Bad");
            HAL_GPIO_WritePin(GPIOB, LED1_PIN, GPIO_PIN_RESET);
            HAL_GPIO_WritePin(GPIOB, LED2_PIN, GPIO_PIN_SET);
        }
        else
        {
            // Turn off both LEDs if no button is pressed
            HAL_GPIO_WritePin(GPIOB, LED1_PIN | LED2_PIN, GPIO_PIN_RESET);
            displayFeedback("No Feedback");
        }
        HAL_Delay(200); // Simple debounce delay
    }
}

void displayFeedback(char *feedback)
{
    LCD_Clear();
    LCD_SetCursor(0, 0);
    LCD_Print(feedback);
}

static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();

    // Configure Button pins as input
    GPIO_InitStruct.Pin = BUTTON1_PIN | BUTTON2_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // Configure LED pins as output
    GPIO_InitStruct.Pin = LED1_PIN | LED2_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}

static void MX_I2C1_Init(void)
{
    hi2c1.Instance = I2C1;
    hi2c1.Init.ClockSpeed = 100000;  // 100 kHz
    hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
    hi2c1.Init.OwnAddress1 = 0;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    HAL_I2C_Init(&hi2c1);
}

// Add your SystemClock_Config function here, as well as any other necessary functions