waveshare_epaper

Waveshare e-paper display shenanigans
git clone git://bsandro.tech/waveshare_epaper
Log | Files | Refs | README

EPD_1in02d.c (12296B)


      1 /*****************************************************************************
      2 * | File      	:	EPD_1IN02_1in02.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   Electronic paper driver
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2019-09-27
      9 * | Info        :
     10 #
     11 # Permission is hereby granted, free of charge, to any person obtaining a copy
     12 # of this software and associated documnetation files (the "Software"), to deal
     13 # in the Software without restriction, including without limitation the rights
     14 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     15 # copies of the Software, and to permit persons to  whom the Software is
     16 # furished to do so, subject to the following conditions:
     17 #
     18 # The above copyright notice and this permission notice shall be included in
     19 # all copies or substantial portions of the Software.
     20 #
     21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     22 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     23 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     24 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     25 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     26 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     27 # THE SOFTWARE.
     28 #
     29 ******************************************************************************/
     30 #include "EPD_1in02d.h"
     31 /**
     32  * full screen update LUT
     33 **/
     34 const unsigned char lut_w1[] =
     35 {
     36 0x60	,0x5A	,0x5A	,0x00	,0x00	,0x01	,
     37 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     38 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     39 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     40 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     41 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     42 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     43  	
     44 };	
     45 const unsigned char lut_b1[] =
     46 {
     47 0x90	,0x5A	,0x5A	,0x00	,0x00	,0x01	,
     48 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     49 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     50 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     51 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     52 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     53 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     54 
     55 
     56 };
     57 /**
     58  * partial screen update LUT
     59 **/
     60 const unsigned char lut_w[] =
     61 {
     62 0x60	,0x01	,0x01	,0x00	,0x00	,0x01	,
     63 0x80	,0x1f	,0x00	,0x00	,0x00	,0x01	,
     64 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     65 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     66 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     67 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     68 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     69 
     70 };	
     71 const unsigned char lut_b[] =
     72 {
     73 0x90	,0x01	,0x01	,0x00	,0x00	,0x01	,
     74 0x40	,0x1f	,0x00	,0x00	,0x00	,0x01	,
     75 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     76 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     77 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     78 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     79 0x00	,0x00	,0x00	,0x00	,0x00	,0x00	,
     80 
     81 };
     82 /******************************************************************************
     83 function :	Software reset
     84 parameter:
     85 ******************************************************************************/
     86 static void EPD_1IN02_Reset(void)
     87 {
     88     DEV_Digital_Write(EPD_RST_PIN, 1);
     89 	DEV_Delay_ms(20);
     90 	DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
     91 	DEV_Delay_ms(20);
     92 	DEV_Digital_Write(EPD_RST_PIN, 1);
     93 	DEV_Delay_ms(20);
     94 	
     95 }
     96 
     97 /******************************************************************************
     98 function :	send command
     99 parameter:
    100      command : Command register
    101 ******************************************************************************/
    102 static void EPD_1IN02_SendCommand(UBYTE command)
    103 {
    104 	DEV_Digital_Write(EPD_CS_PIN, 0);
    105 	DEV_Digital_Write(EPD_DC_PIN, 0);// command write
    106 
    107 	DEV_SPI_WriteByte(command);
    108 	DEV_Digital_Write(EPD_CS_PIN, 1);
    109 }
    110 
    111 /******************************************************************************
    112 function :	send data
    113 parameter:
    114     Data : Write data
    115 ******************************************************************************/
    116 static void EPD_1IN02_SendData(UBYTE Data)
    117 {
    118 	DEV_Digital_Write(EPD_CS_PIN, 0);
    119 	DEV_Digital_Write(EPD_DC_PIN, 1);	// command write
    120 	DEV_SPI_WriteByte(Data);
    121 	DEV_Digital_Write(EPD_CS_PIN, 1);
    122 }
    123 
    124 /******************************************************************************
    125 function :	LUT download
    126 ******************************************************************************/
    127 void EPD_1IN02_SetFulltReg(void)
    128 {
    129 	unsigned int count;
    130 	EPD_1IN02_SendCommand(0x23);
    131 	for(count=0;count<42;count++)	     
    132 	{EPD_1IN02_SendData(lut_w1[count]);}    
    133 	
    134 	EPD_1IN02_SendCommand(0x24);
    135 	for(count=0;count<42;count++)	     
    136 	{EPD_1IN02_SendData(lut_b1[count]);}          
    137 }
    138 
    139 /******************************************************************************
    140 function :	LUT download
    141 ******************************************************************************/
    142 void EPD_1IN02_SetPartReg(void)
    143 {
    144 	unsigned int count;
    145 	EPD_1IN02_SendCommand(0x23);
    146 	for(count=0;count<42;count++){
    147 		EPD_1IN02_SendData(lut_w[count]);
    148 	}
    149 	
    150 	EPD_1IN02_SendCommand(0x24);
    151 	for(count=0;count<42;count++){
    152 		EPD_1IN02_SendData(lut_b[count]);
    153 	}          
    154 }
    155 
    156 /******************************************************************************
    157 function :	Wait until the busy_pin goes LOW
    158 ******************************************************************************/
    159 void EPD_1IN02_WaitUntilIdle(void)
    160 {
    161 	unsigned char busy;
    162 	do
    163 	{
    164 		EPD_1IN02_SendCommand(0x71);
    165 		busy = DEV_Digital_Read(EPD_BUSY_PIN);
    166 		busy =!(busy & 0x01);        
    167 	}
    168 	while(busy);
    169 	DEV_Delay_ms(800);                       
    170 }
    171 
    172 
    173 /******************************************************************************
    174 function :	Turn On Display
    175 ******************************************************************************/
    176 void EPD_1IN02_TurnOnDisplay(void)
    177 {
    178     // EPD_1IN02_SendCommand(0x04);  //power on
    179     // EPD_1IN02_WaitUntilIdle();
    180     EPD_1IN02_SendCommand(0x12);  //Start refreshing the screen
    181     DEV_Delay_ms(10);
    182     EPD_1IN02_WaitUntilIdle();
    183     // EPD_1IN02_SendCommand(0x02);
    184     // EPD_1IN02_WaitUntilIdle();    //power off
    185 }
    186 
    187 /******************************************************************************
    188 function :Initialize the e-Paper register
    189 ******************************************************************************/
    190 UBYTE EPD_1IN02_Init(void)
    191 {
    192 	EPD_1IN02_Reset(); 
    193 	
    194 	EPD_1IN02_SendCommand(0xD2);			
    195 	EPD_1IN02_SendData(0x3F);
    196 						 
    197 	EPD_1IN02_SendCommand(0x00);  			
    198 	EPD_1IN02_SendData (0x6F);  //from outside
    199 
    200 	EPD_1IN02_SendCommand(0x01);  //power setting
    201 	EPD_1IN02_SendData (0x03);	    
    202 	EPD_1IN02_SendData (0x00);
    203 	EPD_1IN02_SendData (0x2b);		
    204 	EPD_1IN02_SendData (0x2b); 
    205 	
    206 	EPD_1IN02_SendCommand(0x06);  //Configuring the charge pump
    207 	EPD_1IN02_SendData(0x3f);
    208 	
    209 	EPD_1IN02_SendCommand(0x2A);  //Setting XON and the options of LUT
    210 	EPD_1IN02_SendData(0x00); 
    211 	EPD_1IN02_SendData(0x00); 
    212 	
    213 	EPD_1IN02_SendCommand(0x30);  //Set the clock frequency
    214 	EPD_1IN02_SendData(0x17); //50Hz
    215 
    216 	EPD_1IN02_SendCommand(0x50);  //Set VCOM and data output interval
    217 	EPD_1IN02_SendData(0x57);			
    218 
    219 	EPD_1IN02_SendCommand(0x60);  //Set The non-overlapping period of Gate and Source.
    220 	EPD_1IN02_SendData(0x22);
    221 
    222     EPD_1IN02_SendCommand(0x61);  //resolution setting
    223     EPD_1IN02_SendData (0x50);    //source 128 	 
    224     EPD_1IN02_SendData (0x80);       
    225 
    226 	EPD_1IN02_SendCommand(0x82);  //sets VCOM_DC value
    227 	EPD_1IN02_SendData(0x12);  //-1v
    228 
    229 	EPD_1IN02_SendCommand(0xe3);//Set POWER SAVING
    230 	EPD_1IN02_SendData(0x33);
    231 	EPD_1IN02_SetFulltReg();	
    232     EPD_1IN02_SendCommand(0x04);     		//power on
    233     EPD_1IN02_WaitUntilIdle();
    234 	return 0;
    235 }
    236 
    237 /******************************************************************************
    238 function :Partial refresh initialization e-paper
    239 ******************************************************************************/
    240 void EPD_1IN02_Part_Init(void)
    241 {
    242 	EPD_1IN02_Reset();
    243 	EPD_1IN02_SendCommand(0xD2);			
    244 	EPD_1IN02_SendData(0x3F);
    245 						 
    246 	EPD_1IN02_SendCommand(0x00);  			
    247 	EPD_1IN02_SendData (0x6F);  //from outside
    248     
    249 	EPD_1IN02_SendCommand(0x01);  //power setting
    250 	EPD_1IN02_SendData (0x03);
    251 	EPD_1IN02_SendData (0x00);
    252 	EPD_1IN02_SendData (0x2b);		
    253 	EPD_1IN02_SendData (0x2b);
    254 	
    255 	EPD_1IN02_SendCommand(0x06);  //Configuring the charge pump
    256 	EPD_1IN02_SendData(0x3f);
    257 	
    258 	EPD_1IN02_SendCommand(0x2A);  //Setting XON and the options of LUT
    259 	EPD_1IN02_SendData(0x00);
    260 	EPD_1IN02_SendData(0x00);
    261 	
    262 	EPD_1IN02_SendCommand(0x30);  //Set the clock frequency
    263 	EPD_1IN02_SendData(0x17);
    264 				
    265 	EPD_1IN02_SendCommand(0x50);  //Set VCOM and data output interval
    266 	EPD_1IN02_SendData(0xf2);
    267 
    268 	EPD_1IN02_SendCommand(0x60);  //Set The non-overlapping period of Gate and Source.
    269 	EPD_1IN02_SendData(0x22);
    270 
    271 	EPD_1IN02_SendCommand(0x82);  //Set VCOM_DC value
    272     EPD_1IN02_SendData(0x12);//-1v
    273 
    274 	EPD_1IN02_SendCommand(0xe3);//Set POWER SAVING
    275 	EPD_1IN02_SendData(0x33);
    276 
    277 	EPD_1IN02_SetPartReg();	
    278     
    279     EPD_1IN02_SendCommand(0x04);//Set POWER SAVING	
    280     
    281     EPD_1IN02_WaitUntilIdle();
    282 }
    283 
    284 /******************************************************************************
    285 function :	Clear screen
    286 ******************************************************************************/
    287 void EPD_1IN02_Clear(void)
    288 { 
    289 	unsigned int i;
    290 	EPD_1IN02_SendCommand(0x10);
    291 	for(i=0;i<1280;i++){
    292 		EPD_1IN02_SendData(0X00);
    293 	}
    294 	EPD_1IN02_SendCommand(0x13);	       //Transfer new data
    295 	for(i=0;i<1280;i++){
    296 		EPD_1IN02_SendData(0xff);
    297 	}
    298 	EPD_1IN02_TurnOnDisplay();
    299 }
    300 
    301 
    302 
    303 /******************************************************************************
    304 function :	Sends the image buffer in RAM to e-Paper and displays
    305 parameter:
    306     Image :Displayed data
    307 ******************************************************************************/
    308 void EPD_1IN02_Display(UBYTE *Image)
    309 { 
    310 	UWORD Width;
    311 	Width = (EPD_1IN02_WIDTH % 8 == 0)? (EPD_1IN02_WIDTH / 8 ): (EPD_1IN02_WIDTH / 8 + 1);
    312 	//EPD_1IN02_Init();
    313 	EPD_1IN02_SendCommand(0x10);
    314 	for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
    315         for (UWORD i = 0; i < Width; i++) {
    316             EPD_1IN02_SendData(0xff);
    317         }
    318 	}
    319 
    320 	EPD_1IN02_SendCommand(0x13);
    321 	for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
    322         for (UWORD i = 0; i < Width; i++) {
    323             EPD_1IN02_SendData(Image[i + j * Width]);
    324         }
    325 	}
    326 	EPD_1IN02_TurnOnDisplay();
    327 }
    328 
    329 
    330 
    331 /******************************************************************************
    332 function :	Sends the image buffer in RAM to e-Paper and displays
    333 parameter:
    334     old_Image:  Last displayed data
    335     Image2   :  New data
    336 ******************************************************************************/
    337 void EPD_1IN02_DisplayPartial(UBYTE *old_Image, UBYTE *Image)
    338 {
    339     /* Set partial Windows */
    340     EPD_1IN02_SendCommand(0x91);		//This command makes the display enter partial mode
    341     EPD_1IN02_SendCommand(0x90);		//resolution setting
    342     EPD_1IN02_SendData(0);           //x-start
    343     EPD_1IN02_SendData(79);       //x-end
    344 
    345     EPD_1IN02_SendData(0);
    346     EPD_1IN02_SendData(127);  //y-end
    347     EPD_1IN02_SendData(0x00);
    348 
    349     UWORD Width;
    350     Width = (EPD_1IN02_WIDTH % 8 == 0)? (EPD_1IN02_WIDTH / 8 ): (EPD_1IN02_WIDTH / 8 + 1);
    351 
    352     /* send data */
    353     EPD_1IN02_SendCommand(0x10);
    354     for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
    355         for (UWORD i = 0; i < Width; i++) {
    356             EPD_1IN02_SendData(old_Image[i + j * Width]);
    357         }
    358     }
    359 
    360     EPD_1IN02_SendCommand(0x13);
    361     for (UWORD j = 0; j < EPD_1IN02_HEIGHT; j++) {
    362         for (UWORD i = 0; i < Width; i++) {
    363             EPD_1IN02_SendData(Image[i + j * Width]);
    364         }
    365     }
    366 
    367     /* Set partial refresh */
    368     EPD_1IN02_TurnOnDisplay();
    369 }
    370 
    371 /******************************************************************************
    372 function :	Enter sleep mode
    373 ******************************************************************************/
    374 void EPD_1IN02_Sleep(void)
    375 {
    376     EPD_1IN02_SendCommand(0X50);
    377     EPD_1IN02_SendData(0xf7);
    378     EPD_1IN02_SendCommand(0X02);  	//power off
    379     EPD_1IN02_WaitUntilIdle();
    380     EPD_1IN02_SendCommand(0X07);  	//deep sleep
    381     EPD_1IN02_SendData(0xA5);
    382     
    383     DEV_Delay_ms(200);
    384     printf("Turn off the power!!! \r\n");
    385     DEV_Digital_Write(EPD_RST_PIN, 0);// Module reset
    386 }
    387 
    388 
    389 
    390 
    391