
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdlib.h"

void SH1107_IIC_Start(void);
void SH1107_IIC_Stop(void);
void SH1107_IIC_ByteWrite(uint8_t dat);
char SH1107_IIC_ByteRead(uint8_t ack);
void SH1107_comm_out(char data);
void SH1107_data_out(char data);

void SH1107_show_8x16(char y, char x, char word) ;
void SH1107_show_variable(char y, char x, char l, int word);

 //模组屏
#define SH1107_SCL_SetOutput()        LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_0, LL_GPIO_MODE_OUTPUT);//SDA
#define SH1107_SCL_SetVal()           LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_0);
#define SH1107_SCL_ClrVal()           LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_0);

#define SH1107_SDA_SetOutput()        LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_1, LL_GPIO_MODE_OUTPUT);//SDA
#define SH1107_SDA_SetInput()         LL_GPIO_SetPinMode(GPIOC, LL_GPIO_PIN_1, LL_GPIO_MODE_INPUT);//SDA 
#define SH1107_SDA_SetVal()           LL_GPIO_SetOutputPin(GPIOC, LL_GPIO_PIN_1);
#define SH1107_SDA_ClrVal()           LL_GPIO_ResetOutputPin(GPIOC, LL_GPIO_PIN_1);

#define SH1107_073_xlen 88
#define SH1107_073_ylen 128

void SH1107_073_test(void)
{
   int i,j;
  char sentence01[]  = "0123456789;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789;<=>?@ABCDEFGHIJKLMNOPQRSTUV";
      switch( Sub_Menu )
          {
              case 0: //白色           
                SH1107_comm_out(0xa6);
                for(i=0;i<16;i++)
                {
                     SH1107_comm_out(0xb0+i);//Set Row Address of Display RAM
                     SH1107_comm_out(0x00);//Set Lower Column Address of display RAM
                     SH1107_comm_out(0x10);//Set Higher Column Address of display RAM
                     for(j=1;j<=88;j++)    {     SH1107_data_out(0xFF);  }
                }
                      break;
              case 1:    //黑色
                    
                         SH1107_comm_out(0xb0);//Set Row Address of Display RAM
                         SH1107_comm_out(0x01);//Set Lower Column Address of display RAM
                         SH1107_comm_out(0x10);//Set Higher Column Address of display RAM
                         for(j=1;j<=86;j++)   {    SH1107_data_out(0x01);  }
                    for(i=1;i<15;i++)
                    {
                         SH1107_comm_out(0xb0+i);//Set Row Address of Display RAM
                         SH1107_comm_out(0x01);//Set Lower Column Address of display RAM
                         SH1107_comm_out(0x10);//Set Higher Column Address of display RAM
                         for(j=1;j<=86;j++)   {     SH1107_data_out(0x00);      }
                    }
                         SH1107_comm_out(0xbf);//Set Row Address of Display RAM
                         SH1107_comm_out(0x01);//Set Lower Column Address of display RAM
                         SH1107_comm_out(0x10);//Set Higher Column Address of display RAM
                         for(j=1;j<=86;j++)   {     SH1107_data_out(0x80);       }
                break;
              case 2:   //50%  
                     for(i=0;i<16;i++)
                     {     
                        SH1107_comm_out(0xb0+i);//Set Row Address of Display RAM
                         SH1107_comm_out(0x00);//Set Lower Column Address of display RAM
                         SH1107_comm_out(0x10);//Set Higher Column Address of display RAM
                         for(j=0;j<88;j++)       
                         {    SH1107_data_out(0xAA);
                              SH1107_data_out(0x55);
                         }  

                     }
                break;
              case 3:  //LOG 
                   for(i=0;i<88;i++)   {      SH1107_show_8x16(i/(SH1107_073_xlen/8)*2, (i%(SH1107_073_xlen/8))*8, sentence01[i]) ;   }
           

                break;
              case 4:  
                  SH1107_comm_out(0xa7);//Set Reverse Display
                  
                   Sub_Menu = 8;
                break;
              case 5:    
                break;

          default:break;
           }
    _Delay_us(80000);
    
}

void SH1107_073_I2C_initial(void)
{
    LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
    
    SH1107_SCL_SetOutput();
    SH1107_SDA_SetOutput();

    SH1107_comm_out(0xae);//Display OFF
    SH1107_comm_out(0x00);//Lower column
    SH1107_comm_out(0x10);//Higher column
    
    SH1107_comm_out(0xB0);//page address
    SH1107_comm_out(0xd5);//Set DCLK
    SH1107_comm_out(0xb1);//
    SH1107_comm_out(0x20);//ROW address
    SH1107_comm_out(0x81);
    SH1107_comm_out(0x10);//
    SH1107_comm_out(0xA0);//Segment remap
    SH1107_comm_out(0xA4);//
    SH1107_comm_out(0xA6);//
    SH1107_comm_out(0xA8);//
    
    SH1107_comm_out(0x57);
    SH1107_comm_out(0xdc);
    SH1107_comm_out(0x00);
    SH1107_comm_out(0xd3);//Set offset
    SH1107_comm_out(0x6c);
    SH1107_comm_out(0xad);//VPP
    SH1107_comm_out(0x80);
    SH1107_comm_out(0xc0);
    SH1107_comm_out(0xd9);//
    SH1107_comm_out(0x22);
    SH1107_comm_out(0xdb);
    SH1107_comm_out(0x3f);
    
    SH1107_comm_out(0xaf);//Set Display ON
    
    _Delay_us(600);
}


/******************************************************************************
*  y: 列位置，取值范围0-63
*  x: 行位置，取值范围0-127
* 
* 
*******************************************************************************/
void SH1107_show_8x16(char y, char x, char word) 
{ 
    int i,j;    
    
    SH1107_comm_out(0xb0+y);      //Set  page行地址
    SH1107_comm_out(0x00+x%16);   //Set 低列地址
    SH1107_comm_out(0x10+x/16);   //Set 高列地址
    for(i=0;i<8;i++) 
    { 
      j = word<<4 | i<<1;
      SH1107_data_out(Arial_8x16[j]); 
    }
    SH1107_comm_out(0xb0+y+1);    //Set  page行地址
    SH1107_comm_out(0x00+x%16);   //Set 低列地址
    SH1107_comm_out(0x10+x/16);   //Set 高列地址
    for(i=0;i<8;i++) 
    { 
      j= word<<4 | i<<1 | 1;
      SH1107_data_out(Arial_8x16[j] ); 
    }
}
void SH1107_comm_out(char data)
{
   SH1107_IIC_Start(); 
   SH1107_IIC_ByteWrite(0x78); 
   SH1107_IIC_ByteWrite(0x00); 
   SH1107_IIC_ByteWrite(data); 
   SH1107_IIC_Stop(); 
}
void SH1107_data_out(char data)
{
   SH1107_IIC_Start(); 
   SH1107_IIC_ByteWrite(0x78); 
   SH1107_IIC_ByteWrite(0x40); 
   SH1107_IIC_ByteWrite(data); 
   SH1107_IIC_Stop(); 
}

void SH1107_IIC_Start(void)
{
	SH1107_SCL_SetVal();
	SH1107_SDA_SetVal();	_Delay_us(2);
	SH1107_SDA_ClrVal();	_Delay_us(2);
	SH1107_SCL_ClrVal();	_Delay_us(2);
}
void SH1107_IIC_Stop(void)
{
	SH1107_SCL_ClrVal();
	SH1107_SDA_ClrVal();	_Delay_us(2);
	SH1107_SCL_SetVal();	_Delay_us(2);
	SH1107_SDA_SetVal();	_Delay_us(2);
}
void SH1107_IIC_ByteWrite(uint8_t dat)
{
	uint8_t i;
	for(i=0; i<8; i++)
	{
		if(dat & 0x80)	{	SH1107_SDA_SetVal();		}
		else		{	SH1107_SDA_ClrVal();		}
		dat <<= 1;		_Delay_us(2);
		SH1107_SCL_SetVal();		_Delay_us(2);
		SH1107_SCL_ClrVal();		_Delay_us(2);
	}
	SH1107_SDA_SetInput();
	SH1107_SCL_SetVal();		_Delay_us(2);
	SH1107_SCL_ClrVal();		_Delay_us(2);
	SH1107_SDA_SetOutput();
}
