#include "char.h"
#include "cmsis_os.h"
#include "main.h"
#include <string.h>
#include "variables.h"
#define OLED_CS   cs_Pin		
#define OLED_RST  RES_Pin
#define OLED_DC   dc_Pin
#define OLED_SCL  D0_Pin
#define OLED_SDA  D1_Pin
unsigned char OLED_GRAM[128][8];	 

void OLED_WR_Byte(unsigned char dat,unsigned char cmd)               ///////////////////////////////////////////1
{	
	taskENTER_CRITICAL();
	unsigned char i;		
  if(cmd==0)
	{
		HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_RESET);
	}
	  if(cmd==1)
	{
		HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_SET);
	}

	HAL_GPIO_WritePin(CS_GPIO_Port,CS_Pin,GPIO_PIN_RESET);

	for(i=0;i<8;i++)
	{			  
		HAL_GPIO_WritePin(D0_GPIO_Port,D0_Pin,GPIO_PIN_RESET);
		
		if(dat&0x80)
			HAL_GPIO_WritePin(D1_GPIO_Port,D1_Pin,GPIO_PIN_SET);
		else 
		   	HAL_GPIO_WritePin(D1_GPIO_Port,D1_Pin,GPIO_PIN_RESET);
		
		HAL_GPIO_WritePin(D0_GPIO_Port,D0_Pin,GPIO_PIN_SET);
		dat<<=1;   
	}				 

HAL_GPIO_WritePin(CS_GPIO_Port,CS_Pin,GPIO_PIN_SET);
HAL_GPIO_WritePin(DC_GPIO_Port,DC_Pin,GPIO_PIN_SET);
	taskEXIT_CRITICAL();
 	  
} 



void OLED_Refresh_Gram(void)                                                ///////////////////////////////////2
{
	unsigned char i,n;		    
	for(i=0;i<8;i++)  
	{  
		OLED_WR_Byte (0xb0+i,0);    //ÉèÖÃÒ³µØÖ·£¨0~7£©
		OLED_WR_Byte (0x00,0);      //ÉèÖÃÏÔÊ¾Î»ÖÃ¡ªÁÐµÍµØÖ·
		OLED_WR_Byte (0x10,0);      //ÉèÖÃÏÔÊ¾Î»ÖÃ¡ªÁÐ¸ßµØÖ·   
		for(n=0;n<128;n++)OLED_WR_Byte(OLED_GRAM[n][i],1); 
	}   
}

void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 
	OLED_WR_Byte(0xb0+y,0);
	OLED_WR_Byte(((x&0xf0)>>4)|0x10,0);
	OLED_WR_Byte((x&0x0f)|0x01,0); 
}   	  
//¿ªÆôOLEDÏÔÊ¾    





void OLED_Display_On(void)                                                ///////////////////////////////////3
{
	OLED_WR_Byte(0X8D,0);  //SET DCDCÃüÁî
	OLED_WR_Byte(0X14,0);  //DCDC ON
	OLED_WR_Byte(0XAF,0);  //DISPLAY ON
}

void OLED_DrawPoint(unsigned char x,unsigned char y,unsigned char t)
{
	unsigned char pos,bx,temp=0;
	if(x>127||y>63)return;//³¬³ö·¶Î§ÁË.
	pos=7-y/8;
	bx=y%8;
	temp=1<<(7-bx);
	if(t)OLED_GRAM[x][pos]|=temp;
	else OLED_GRAM[x][pos]&=~temp;
	
  
}




void OLED_Fill(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char dot)  ///////////////////////////4
{  
	unsigned char x,y;  
	for(x=x1;x<=x2;x++)
	{
		for(y=y1;y<=y2;y++)
		{ 	
			OLED_DrawPoint(x,y,dot);	
		}
	}													    
	OLED_Refresh_Gram();//¸üÐÂÏÔÊ¾
}




void OLED_Clear(void)   ////////////////////////////////////////////////////////////////////////////////5
{  
	unsigned char i,n;		    
	for(i=0;i<8;i++)  
	{    
		for(n=0;n<128;n++)
		{
			OLED_GRAM[n][i]=0;
		}
	}
	OLED_Refresh_Gram();//¸üÐÂÏÔÊ¾ 
}





//ÔÚÖ¸¶¨Î»ÖÃÏÔÊ¾Ò»¸ö×Ö·û,°üÀ¨²¿·Ö×Ö·û
//x:0~127
//y:0~63
//mode:0,·´°×ÏÔÊ¾;1,Õý³£ÏÔÊ¾				 
//size:Ñ¡Ôñ×ÖÌå 12/16/24
void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char ucWid,unsigned char mode) ///////////////////////////6
{      			    
	unsigned char temp,t,t1;
	unsigned char y0=y;
	unsigned char cucWid=(ucWid/8+((ucWid%8)?1:0))*(ucWid/2);		//µÃµ½×ÖÌåÒ»¸ö×Ö·û¶ÔÓ¦µãÕó¼¯ËùÕ¼µÄ×Ö½ÚÊý
	chr=chr-' ';//µÃµ½Æ«ÒÆºóµÄÖµ		 
    for(t=0;t<cucWid;t++)
    {   
		if(ucWid==12)temp=ascii_1206[chr][t]; 	 	//µ÷ÓÃ1206×ÖÌå
		else if(ucWid==16)temp=ascii_1608[chr][t];	//µ÷ÓÃ1608×ÖÌå
		else if(ucWid==24)temp=ascii_2412[chr][t];	//µ÷ÓÃ2412×ÖÌå
		else return;								//Ã»ÓÐµÄ×Ö¿â
        for(t1=0;t1<8;t1++)
		{
			if(temp&0x80)OLED_DrawPoint(x,y,mode);
			else OLED_DrawPoint(x,y,!mode);
			temp<<=1;
			y++;
			if((y-y0)==ucWid)
			{
				y=y0;
				x++;
				break;
			}
		}  	 
    }       
   
}

////////////////////////////////////////////////////////////////////////////////



void OLED_ShowFontHZ(unsigned char x,unsigned char y,unsigned char pos,unsigned char size,unsigned char mode)
{
	unsigned char temp,t,t1;
	unsigned char y0=y;  
	unsigned char csize=(size/8+((size%8)?1:0))*(size);//µÃµ½×ÖÌåÒ»¸ö×Ö·û¶ÔÓ¦µãÕó¼¯ËùÕ¼µÄ×Ö½ÚÊý	 
	if(size!=12&&size!=16&&size!=24)return;	//²»Ö§³ÖµÄsize
	 
	for(t=0;t<csize;t++)
	{   												   
		if(size==12)temp=FontHzk[pos][t]; 	 	//µ÷ÓÃ1206×ÖÌå
		else if(size==16)temp=FontHzk[pos][t];	//µ÷ÓÃ1608×ÖÌå
		else if(size==24)temp=FontHzk[pos][t];	//µ÷ÓÃ2412×ÖÌå
		else return;								//Ã»ÓÐµÄ×Ö¿â
        for(t1=0;t1<8;t1++)
		{
			if(temp&0x80)OLED_DrawPoint(x,y,mode);
			else OLED_DrawPoint(x,y,!mode);
			temp<<=1;
			y++;
			if((y-y0)==size)
			{
				y=y0;
				x++;
				break;
			}
		}  	 
	}  	
} 	
////////////////////////////////////////////

void showstring(char A[], unsigned char x ,unsigned char y)  ///////////////////////////////////////////7
{
	OLED_Clear();
	int len,i=0;
	unsigned a=1,b=1;
	len=strlen(A);
	a=x+a;
	b=y+b;
	while(i<len)
	{
		OLED_ShowChar(a,b,A[i],16,1);
		a=a+9;
		if((i>12)&&(((i-12)%13)==1))
		{
			a=1+x;
			b=b+16;
		}
		i=i+1;
	}
	
	OLED_Refresh_Gram()	;	
	
}

void plusstring(char A[], unsigned char x ,unsigned char y) ///////////////////////////////////////////8
{
	int len,i=0;
	unsigned a=1,b=1;
	len=strlen(A);
	a=x+a;
	b=y+b;
	while(i<len)
	{
		OLED_ShowChar(a,b,A[i],16,1);
		a=a+9;
		if((i>12)&&(((i-12)%13)==1))
		{
			a=1+x;
			b=b+16;
		}
		i=i+1;
	}
	
	OLED_Refresh_Gram()	;	
	
}


void roll_x_string(char A[], unsigned char x1 ,unsigned char y1, unsigned char x2)   /////////////////////////////////9
{
	if(x1>x2)
	{
		while((x1-x2)>10)
		{
			showstring(A,x1,y1);
			x1=x1-10;
			
		}
		showstring(A,x2,y1);
		
	}
	else
	{
		while((x2-x1)>5)
		{
			showstring(A,x1,y1);
			x1=x1+5;
			
		}
		showstring(A,x2,y1);
	}
	
}


void roll_y_string(char A[], unsigned char x1 ,unsigned char y1, unsigned char y2)   /////////////////////////////////10
{
	if(y1>y2)
	{
		while(y1>y2)
		{
			showstring(A,x1,y1);
			y1=y1-1;
			
		}
	}
	else
	{
		while(y1<y2)
		{
			showstring(A,x1,y1);
			y1=y1+1;
			
		}
	}
	
}


	


void OLED_Init()                                                    /////////////////////////////////12
{

	
	
	
	HAL_GPIO_WritePin(RES_GPIO_Port,RES_Pin,GPIO_PIN_SET);
	osDelay(100);
	
	HAL_GPIO_WritePin(RES_GPIO_Port,RES_Pin,GPIO_PIN_RESET);
	osDelay(100);
	
	HAL_GPIO_WritePin(RES_GPIO_Port,RES_Pin,GPIO_PIN_SET);
					  
	OLED_WR_Byte(0xAE,0); //¹Ø±ÕÏÔÊ¾
	OLED_WR_Byte(0xD5,0); //ÉèÖÃÊ±ÖÓ·ÖÆµÒò×Ó,Õðµ´ÆµÂÊ
	OLED_WR_Byte(80,0);   //[3:0],·ÖÆµÒò×Ó;[7:4],Õðµ´ÆµÂÊ
	OLED_WR_Byte(0xA8,0); //ÉèÖÃÇý¶¯Â·Êý
	OLED_WR_Byte(0X3F,0); //Ä¬ÈÏ0X3F(1/64) 
	OLED_WR_Byte(0xD3,0); //ÉèÖÃÏÔÊ¾Æ«ÒÆ
	OLED_WR_Byte(0X00,0); //Ä¬ÈÏÎª0

	OLED_WR_Byte(0x40,0); //ÉèÖÃÏÔÊ¾¿ªÊ¼ÐÐ [5:0],ÐÐÊý.
													    
	OLED_WR_Byte(0x8D,0); //µçºÉ±ÃÉèÖÃ
	OLED_WR_Byte(0x14,0); //bit2£¬¿ªÆô/¹Ø±Õ
	OLED_WR_Byte(0x20,0); //ÉèÖÃÄÚ´æµØÖ·Ä£Ê½
	OLED_WR_Byte(0x02,0); //[1:0],00£¬ÁÐµØÖ·Ä£Ê½;01£¬ÐÐµØÖ·Ä£Ê½;10,Ò³µØÖ·Ä£Ê½;Ä¬ÈÏ10;
	OLED_WR_Byte(0xA1,0); //¶ÎÖØ¶¨ÒåÉèÖÃ,bit0:0,0->0;1,0->127;
	OLED_WR_Byte(0xC0,0); //ÉèÖÃCOMÉ¨Ãè·½Ïò;bit3:0,ÆÕÍ¨Ä£Ê½;1,ÖØ¶¨ÒåÄ£Ê½ COM[N-1]->COM0;N:Çý¶¯Â·Êý
	OLED_WR_Byte(0xDA,0); //ÉèÖÃCOMÓ²¼þÒý½ÅÅäÖÃ
	OLED_WR_Byte(0x12,0); //[5:4]ÅäÖÃ
		 
	OLED_WR_Byte(0x81,0); //¶Ô±È¶ÈÉèÖÃ
	OLED_WR_Byte(0x7F,0); //1~255;Ä¬ÈÏ0X7F (ÁÁ¶ÈÉèÖÃ,Ô½´óÔ½ÁÁ)
	OLED_WR_Byte(0xD9,0); //ÉèÖÃÔ¤³äµçÖÜÆÚ
	OLED_WR_Byte(0xf1,0); //[3:0],PHASE 1;[7:4],PHASE 2;
	OLED_WR_Byte(0xDB,0); //ÉèÖÃVCOMH µçÑ¹±¶ÂÊ
	OLED_WR_Byte(0x30,0); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;

	OLED_WR_Byte(0xA4,0); //È«¾ÖÏÔÊ¾¿ªÆô;bit0:1,¿ªÆô;0,¹Ø±Õ;(°×ÆÁ/ºÚÆÁ)
	OLED_WR_Byte(0xA6,0); //ÉèÖÃÏÔÊ¾·½Ê½;bit0:1,·´ÏàÏÔÊ¾;0,Õý³£ÏÔÊ¾	    						   
	OLED_WR_Byte(0xAF,0); //¿ªÆôÏÔÊ¾	 
	OLED_Clear();
}

	void sciishow(char *box, unsigned char A)
	{
		unsigned char high, low;
		high=(A&0xf0)>>4;
		low=A&0x0f;
	if(high>=0x0A)
	{
		box[0]=high+0x37;
	}
	else
	{
		box[0]=high+0x30;
	}
		if(low>=0x0A)
	{
		box[1]=low+0x37;
	}
	else
	{
		box[1]=low+0x30;
	}
		
	}
