#include "stm32g4xx_hal.h"
#include "oled_common.h"
#include "i2c_bridge.h"
#include "logo_grayscale.h"
#include "fonts.h"

extern "C" {
#include "OLED_SSD1327.h"
#include "GFX_SSD1327.h"
}
  

I2C_HandleTypeDef *i2c_bridge;


//
void init_oled(void)
{
    init_gpio_oled();
    
    i2c_bridge_init();
      
    i2c_bridge = i2c_get_bridge();
      
    SSD1327_I2cInit(i2c_bridge);
    
    SSD1327_Clear(BLACK);

    //GFX_Image(15, 0, (uint8_t*)logo_grayscale, 96, 96);
        
    SSD1327_Display();
    
    test_oled();
}


//
void test_oled(void)
{
    //SSD1327_DrawPixel(int16_t x, int16_t y, uint8_t Color);
  
    for (int i = 0; i < 16; i++)
    {
        SSD1327_DrawPixel(10, 20 + i, i);
    }
  
    SSD1327_DrawPixel(20, 20, 15);
    
    SSD1327_DrawPixel(30, 20, 15);
    
    SSD1327_DrawPixel(40, 20, 5);
    
    //void GFX_DrawLine(int x_start, int y_start, int x_end, int y_end, uint8_t color);
    // Верхняя 
    GFX_DrawLine(0, 0, 127, 0, 15);
    
    // Левая
    GFX_DrawLine(0, 0, 0, 127, 15);
    
    // Правая
    GFX_DrawLine(127, 0, 127, 127, 15);
    
    // Нижняя 
    GFX_DrawLine(0, 127, 127, 127, 15);
    
    GFX_SetFont(font_8x5);
    GFX_SetFontSize(2);
    
    GFX_DrawChar(50, 50, 'F', 15, 0);
    GFX_DrawString(10, 100, "Hellow world", 15, 0);
    
    
#if 0    
    uint8_t GFX_GetFontHeight(void);
    uint8_t GFX_GetFontWidth(void);
    uint8_t  GFX_GetFontSize(void);
    void GFX_DrawChar(int x, int y, char chr, uint8_t color, uint8_t background);
    void GFX_DrawString(int x, int y, char* str, uint8_t color, uint8_t background);
#endif    

    SSD1327_Display();
    
}


//
void init_gpio_oled(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
        
    __HAL_RCC_GPIOA_CLK_ENABLE();
    
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
    
#if 0    
    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET);
    
    /*Configure GPIO pin : PC13 */
    GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
    /*Configure GPIO pin : PC6 */
    GPIO_InitStruct.Pin = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
#endif    
}


