#include "OLED.h"
/*********************************************************/
void OLED_WrDat(unsigned char dat)
{
  if(SPI_IIC)
	 {
	     HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET);
	     HAL_SPI_Transmit(&hspi1,&dat,sizeof(dat),100);
	 }
  else
  {
	   HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x40, 1, &dat, 1, 10);
  }

}

	/*********************OLEDÐ´ÃüÁî************************************/
void OLED_WrCmd(unsigned char cmd)
{
		if(SPI_IIC)
			 {
			    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET);
				HAL_SPI_Transmit(&hspi1,&cmd,sizeof(cmd),100);
			 }
		  else
		  {
			  HAL_I2C_Mem_Write(&hi2c1, 0x78, 0x00, 1, &cmd, 1, 10);
		  }



	/*unsigned char i;
 HAL_GPIO_WritePin(GPIOB, DC_Pin, GPIO_PIN_RESET);
	for(i=0;i<8;i++)
	{
		if((cmd << i) & 0x80)
		{
			 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
		}
		else
		{
			 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
		}
		OLED_SCL = 0;
		OLED_SCL = 1;
	}*/
}



/*********************OLED ÉèÖÃ×ø±ê************************************/
void OLED_SetPos(unsigned char x, unsigned char y)
{
	OLED_WrCmd(0xb0+y);
	OLED_WrCmd(((x&0xf0)>>4)|0x10);
	OLED_WrCmd((x&0x0f)|0x01);
}

/*********************OLEDÈ«ÆÁ************************************/
void OLED_Fill(unsigned char bmp_dat)
{
	unsigned char y,x;
	for(y=0;y<8;y++)
	{
		OLED_WrCmd(0xb0U+y);
		OLED_WrCmd(0x01U);
		OLED_WrCmd(0x10U);
		for(x=0;x<X_WIDTH;x++)
		OLED_WrDat(bmp_dat);
	}
}

/*********************OLEDÇåÆÁ************************/
void OLED_CLS(void)
{
	OLED_Fill(0x00);
}

/*************¿ªÆôOLEDÏÔÊ¾*************/
void OLED_ON(void)
{
	OLED_WrCmd(0X8D);  //ÉèÖÃµçºÉ±Ã
	OLED_WrCmd(0X14);  //¿ªÆôµçºÉ±Ã
	OLED_WrCmd(0XAF);  //OLED»½ÐÑ
}

/*************¹Ø±ÕOLEDÏÔÊ¾ -- Ê¹OLED½øÈëÐÝÃßÄ£Ê½*************/
void OLED_OFF(void)
{
	OLED_WrCmd(0X8D);  //ÉèÖÃµçºÉ±Ã
	OLED_WrCmd(0X10);  //¹Ø±ÕµçºÉ±Ã
	OLED_WrCmd(0XAE);  //OLEDÐÝÃß
}

void OLED_Init(void)
{

     HAL_Delay(500);
	 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, GPIO_PIN_RESET);

	    HAL_Delay(500);//初始化之前的延时很重要！
	 	OLED_WrCmd(0xae);//--turn off oled panel
	 	OLED_WrCmd(0x00);//---set low column address
	 	OLED_WrCmd(0x10);//---set high column address
	 	OLED_WrCmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
	 	OLED_WrCmd(0x81);//--set contrast control register
	 	OLED_WrCmd(Brightness); // Set SEG Output Current Brightness
	 	OLED_WrCmd(0xa1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
	 	OLED_WrCmd(0xc8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
	 	OLED_WrCmd(0xa6);//--set normal display
	 	OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
	 	OLED_WrCmd(0x3f);//--1/64 duty
	 	OLED_WrCmd(0xd3);//-set display offset	Shift Mapping RAM Counter (0x00~0x3F)
	 	OLED_WrCmd(0x00);//-not offset
	 	OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
	 	OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
	 	OLED_WrCmd(0xd9);//--set pre-charge period
	 	OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
	 	OLED_WrCmd(0xda);//--set com pins hardware configuration
	 	OLED_WrCmd(0x12);
	 	OLED_WrCmd(0xdb);//--set vcomh
	 	OLED_WrCmd(0x40);//Set VCOM Deselect Level
	 	OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
	 	OLED_WrCmd(0x02);//
	 //	OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
	 //	OLED_WrCmd(0x10);//--0x10 -- disable; 0x14 -- enable
	 	OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
	 	OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
	 	OLED_WrCmd(0xaf);//--turn on oled panel
	 	OLED_Fill(0x00); //初始清屏
	 	OLED_SetPos(0,0);


}

//--------------------------------------------------------------
// Prototype      : void OLED_DrawPoint(unsigned char x,unsigned char y,unsigned char t)
// Calls          :
// Parameters     : x:0~127,y:0~63,t:1 Ìî³ä 0,Çå¿Õ
// Description    : »­µã
//--------------------------------------------------------------
///*void OLED_DrawPoint(unsigned char x,unsigned char y,unsigned char t)
/*{
	unsigned char pos,bx,temp=0;
	unsigned char OLED_GRAM[128][8];

	if(x>127||y>63)
		return;//³¬³ö·¶Î§ÁË
	pos = y/8;
	bx = y%8;
	temp = 1<<bx;
	if(t)
		OLED_GRAM[x][pos] |= temp;
	else
		OLED_GRAM[x][pos] &= ~temp;
}*/

//--------------------------------------------------------------
// Prototype      : void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
// Calls          :
// Parameters     : x,y -- ÆðÊ¼µã×ø±ê(x:0~127, y:0~7); ch[]:ÒªÏÔÊ¾µÄ×Ö·û´®; TextSize:1 -- 6*8µãÕó,2 -- 8*16µãÕó
// Description    : ÏÔÊ¾²»Í¬×ÖÌåµÄASCII×Ö·û
//--------------------------------------------------------------
void OLED_Char(unsigned char x, unsigned char y, unsigned char ch)
{
	unsigned char c = 0,i = 0;
	while(ch != '\0')
	{
		c = ch - 32;
		OLED_SetPos(x,y);
		for(i=0;i<6;i++)
			OLED_WrDat(F6x8[c][i]);
	}
}

void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
	unsigned char c = 0,i = 0,j = 0;
	switch(TextSize)
	{
		case 1:
		{
			while(ch[j] != '\0')
			{
				c = ch[j] - 32;
				if(x > 126)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<6;i++)
					OLED_WrDat(F6x8[c][i]);
				x += 6;
				j++;
			}
		}break;
		case 2:
		{
			while(ch[j] != '\0')
			{
				c = ch[j] - 32;
				if(x > 120)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<8;i++)
					OLED_WrDat(F8X16[c*16+i]);
				OLED_SetPos(x,y+1);
				for(i=0;i<8;i++)
					OLED_WrDat(F8X16[c*16+i+8]);
				x += 8;
				j++;
			}
		}break;
	}
}

//--------------------------------------------------------------
// Prototype      : void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
// Calls          :
// Parameters     : x,y -- ÆðÊ¼µã×ø±ê(x:0~127, y:0~7); N:ºº×ÖÔÚcodetab.hÖÐµÄË÷Òý
// Description    : ÏÔÊ¾codetab.hÖÐµÄºº×Ö,16*16µãÕó
//--------------------------------------------------------------
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
	unsigned char wm=0;
	unsigned int  address=32*N;
	OLED_SetPos(x , y);
	for(wm = 0;wm < 16;wm++)
	{
		OLED_WrDat(F16x16[address]);
		address += 1;
	}
	OLED_SetPos(x,y + 1);
	for(wm = 0;wm < 16;wm++)
	{
		OLED_WrDat(F16x16[address]);
		address += 1;
	}
}

//--------------------------------------------------------------
// Prototype      : void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);
// Calls          :
// Parameters     : x0,y0 -- ÆðÊ¼µã×ø±ê(x0:0~127, y0:0~7); x1,y1 -- Æðµã¶Ô½ÇÏß(½áÊøµã)µÄ×ø±ê(x1:1~128,y1:1~8)
// Description    : ÏÔÊ¾BMPÎ»Í¼
//--------------------------------------------------------------
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
	unsigned int j=0;
	unsigned char x,y;

  if(y1%8==0)
		y = y1/8;
  else
		y = y1/8 + 1;
	for(y=y0;y<y1;y++)
	{
		OLED_SetPos(x0,y);
    for(x=x0;x<x1;x++)
		{
			OLED_WrDat(BMP[j++]);
		}
	}
}
