/*
 * oled.c
 *
 *  Created on: Nov 17, 2021
 *      Author: wvv
 */
#include "oled.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <main.h>
extern I2C_HandleTypeDef hi2c1;

void oled_write_cmd(uint8_t cmd)
{
	HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, I2C_MEMADD_SIZE_8BIT, &cmd, 1, 0x100);
}

uint8_t oled_buf[OLED_HEIGHT * OLED_WIDTH] = { 0 };

void oled_clear(void)
{
	memset(oled_buf, 0x00, sizeof(oled_buf));
}
void oled_refresh(void)
{
	for (uint8_t i = 0; i < OLED_HEIGHT; i++)
	{
		oled_write_cmd(0xb0 + i);
		oled_write_cmd(0x00);
		oled_write_cmd(0x10);
		HAL_StatusTypeDef ret = HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, 1,
				(uint8_t*) &oled_buf[i * OLED_WIDTH], OLED_WIDTH, 100);
		if (ret != HAL_OK)
		{
			HAL_I2C_DeInit(&hi2c1);
			HAL_I2C_Init(&hi2c1);
		}
	}
}
void oled_init(void)
{
	oled_write_cmd(0xAE);
	oled_write_cmd(0x20);
	oled_write_cmd(0x10);

	oled_write_cmd(0xB0);
	oled_write_cmd(0xC8);
	oled_write_cmd(0x00);
	oled_write_cmd(0x10);
	oled_write_cmd(0x40);

	oled_write_cmd(0x81);
	oled_write_cmd(0xff);

	oled_write_cmd(0xa1);
	oled_write_cmd(0xa6);

	oled_write_cmd(0xa8);
	oled_write_cmd(0x1f);

	oled_write_cmd(0xd3);
	oled_write_cmd(0x00);

	oled_write_cmd(0xd5);
	oled_write_cmd(0xf0);

	oled_write_cmd(0xd9);
	oled_write_cmd(0x22);

	oled_write_cmd(0xda);
	oled_write_cmd(0x02);

	oled_write_cmd(0xdb);
	oled_write_cmd(0x20);

	oled_write_cmd(0x8d);
	oled_write_cmd(0x14);

	oled_write_cmd(0xaf);
	HAL_Delay(100);
	oled_clear();
	oled_refresh();
}

