waveshare_epaper

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

EPD_2in66.c (10534B)


      1 /*****************************************************************************
      2 * | File      	:  	EPD_2in66.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   2.66inch e-paper
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2020-05-25
      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_2in66.h"
     31 #include "Debug.h"
     32 
     33 const unsigned char WF_PARTIAL[159] ={
     34 0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     35 0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
     36 0x00,0x00,0x00,0x00,0x40,0x40,0x00,0x00,0x00,0x00,
     37 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,
     38 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     39 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     40 0x0A,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,
     41 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
     42 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     43 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     44 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     45 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     46 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     47 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
     48 0x00,0x00,0x00,0x00,0x22,0x22,0x22,0x22,0x22,0x22,
     49 0x00,0x00,0x00,0x22,0x17,0x41,0xB0,0x32,0x36,
     50 };
     51 
     52 
     53 /******************************************************************************
     54 function :	Software reset
     55 parameter:
     56 ******************************************************************************/
     57 static void EPD_2IN66_Reset(void)
     58 {
     59     DEV_Digital_Write(EPD_RST_PIN, 1);
     60     DEV_Delay_ms(200);
     61     DEV_Digital_Write(EPD_RST_PIN, 0);
     62     DEV_Delay_ms(10);
     63     DEV_Digital_Write(EPD_RST_PIN, 1);
     64     DEV_Delay_ms(200);
     65 }
     66 
     67 /******************************************************************************
     68 function :	send command
     69 parameter:
     70      Reg : Command register
     71 ******************************************************************************/
     72 static void EPD_2IN66_SendCommand(UBYTE Reg)
     73 {
     74     DEV_Digital_Write(EPD_DC_PIN, 0);
     75     DEV_Digital_Write(EPD_CS_PIN, 0);
     76     DEV_SPI_WriteByte(Reg);
     77     DEV_Digital_Write(EPD_CS_PIN, 1);
     78 }
     79 
     80 /******************************************************************************
     81 function :	send data
     82 parameter:
     83     Data : Write data
     84 ******************************************************************************/
     85 static void EPD_2IN66_SendData(UBYTE Data)
     86 {
     87     DEV_Digital_Write(EPD_DC_PIN, 1);
     88     DEV_Digital_Write(EPD_CS_PIN, 0);
     89     DEV_SPI_WriteByte(Data);
     90     DEV_Digital_Write(EPD_CS_PIN, 1);
     91 }
     92 
     93 /******************************************************************************
     94 function :	Wait until the busy_pin goes LOW
     95 parameter:
     96 ******************************************************************************/
     97 void EPD_2IN66_ReadBusy(void)
     98 {
     99     Debug("e-Paper busy\r\n");
    100     DEV_Delay_ms(200);
    101     while(DEV_Digital_Read(EPD_BUSY_PIN) == 1) {      //LOW: idle, HIGH: busy
    102         DEV_Delay_ms(5);
    103     }
    104     DEV_Delay_ms(100);
    105     Debug("e-Paper busy release\r\n");
    106 }
    107 
    108 
    109 /******************************************************************************
    110 function :	Turn On Display
    111 parameter:
    112 ******************************************************************************/
    113 static void EPD_2IN66_TurnOnDisplay(void)
    114 {
    115     EPD_2IN66_SendCommand(0x22);
    116     EPD_2IN66_SendData(0xF4);
    117     EPD_2IN66_SendCommand(0x20);
    118     EPD_2IN66_ReadBusy();
    119 
    120 }
    121 
    122 /******************************************************************************
    123 function :	Turn On Display
    124 parameter:
    125 ******************************************************************************/
    126 static void EPD_2IN66_SetLUA(void)
    127 {
    128     UWORD count;
    129     EPD_2IN66_SendCommand(0x32);
    130     for(count=0;count<153;count++){
    131         EPD_2IN66_SendData(WF_PARTIAL[count]);
    132     }    
    133     EPD_2IN66_ReadBusy();
    134 
    135 }
    136 
    137 
    138 
    139 /******************************************************************************
    140 function :	Initialize the e-Paper register
    141 parameter:
    142 ******************************************************************************/
    143 void EPD_2IN66_Init(void)
    144 {
    145     EPD_2IN66_Reset();
    146     EPD_2IN66_ReadBusy();
    147     EPD_2IN66_SendCommand(0x12);//soft  reset
    148     EPD_2IN66_ReadBusy();
    149     /* Y increment, X increment */
    150     // EPD_2IN66_SendCommand(0x11);
    151     // EPD_2IN66_SendData(0x03);
    152     // /*	Set RamX-address Start/End position	*/
    153     // EPD_2IN66_SendCommand(0x44);
    154     // EPD_2IN66_SendData(0x01); 
    155     // EPD_2IN66_SendData((EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1) );
    156     // /*	Set RamY-address Start/End position	*/
    157     // EPD_2IN66_SendCommand(0x45);
    158     // EPD_2IN66_SendData(0);
    159     // EPD_2IN66_SendData(0);
    160     // EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0xff));
    161     // EPD_2IN66_SendData((EPD_2IN66_HEIGHT&0x100)>>8);
    162 
    163 
    164 
    165     EPD_2IN66_SendCommand(0x3C);        // Border  设置  黑白一般设置为跟随白波形即 0x01        Border setting 
    166     EPD_2IN66_SendData(0x03);
    167     
    168     //EPD_2IN66_ReadBusy();
    169 }
    170 
    171 void EPD_2IN66_Part_Init(void)
    172 {
    173     // EPD_2IN66_Reset();
    174     // EPD_2IN66_ReadBusy();
    175     // EPD_2IN66_SendCommand(0x12);//soft  reset
    176     EPD_2IN66_ReadBusy();
    177    
    178      EPD_2IN66_SetLUA();
    179     EPD_2IN66_SendCommand(0x37); 
    180     EPD_2IN66_SendData(0x00);  
    181     EPD_2IN66_SendData(0x00);  
    182     EPD_2IN66_SendData(0x00);  
    183     EPD_2IN66_SendData(0x00); 
    184     EPD_2IN66_SendData(0x00);  	
    185     EPD_2IN66_SendData(0x40);  
    186     EPD_2IN66_SendData(0x00);  
    187     EPD_2IN66_SendData(0x00);   
    188     EPD_2IN66_SendData(0x00);  
    189     EPD_2IN66_SendData(0x00);
    190 
    191     EPD_2IN66_SendCommand(0x3C); 
    192     EPD_2IN66_SendData(0x80);   
    193 
    194         
    195     EPD_2IN66_SendCommand(0x22); 
    196     EPD_2IN66_SendData(0xC0);   
    197     EPD_2IN66_SendCommand(0x20); 
    198     EPD_2IN66_ReadBusy();
    199 }
    200 
    201 void EPD_2IN66_DisplayPart(UBYTE *Image, UWORD X_Start, UWORD Y_Start, UWORD X_End, UWORD Y_End)
    202 {
    203     // UWORD Width, Height;
    204     // Width = (X_End/8 - X_Start/8);
    205     // Height = Y_End - Y_Start;
    206     
    207     EPD_2IN66_SendCommand(0x44);         // set RAM x address start/end, in page 35
    208     EPD_2IN66_SendData(0x03);         // RAM x address start 
    209     EPD_2IN66_SendData(0x0E);           // RAM x address end  11
    210     EPD_2IN66_SendCommand(0x45);         // set RAM y address start/end, in page 35
    211     EPD_2IN66_SendData(0x29);    // RAM y address start 
    212     EPD_2IN66_SendData(0x00);
    213     EPD_2IN66_SendData(0x02);      // RAM y address end
    214     EPD_2IN66_SendData(0x00);            //39
    215 
    216     EPD_2IN66_SendCommand(0x4E);         // set RAM x address count to 0;
    217     EPD_2IN66_SendData(0x03);
    218     EPD_2IN66_SendCommand(0x4F);         // set RAM y address count to 0X127;    
    219     EPD_2IN66_SendData(0x29);
    220     EPD_2IN66_SendData(0x00);
    221     
    222     // UDOUBLE Addr = 0;
    223     // UDOUBLE Offset = ImageName;
    224      for(int i=0; i<480;i++){
    225         Image[i] =  0xf0;
    226     }
    227     // unsigned char tempOriginal;      
    228     // int tempcol=0;
    229     // int templine=0;   
    230     EPD_2IN66_SendCommand(0x24);
    231     for(int i=0;i<480;i++)//480次循环
    232     {
    233     
    234         EPD_2IN66_SendData(0xf0);
    235    }
    236     EPD_2IN66_TurnOnDisplay();
    237     DEV_Delay_ms(500);
    238 }
    239 /******************************************************************************
    240 function :	Clear screen
    241 parameter:
    242 ******************************************************************************/
    243 void EPD_2IN66_Clear(void)
    244 {
    245     UWORD Width, Height;
    246     Width = (EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1);
    247     Height = EPD_2IN66_HEIGHT;
    248 
    249     EPD_2IN66_SendCommand(0x4E);     
    250     EPD_2IN66_SendData(0x01);
    251 
    252     EPD_2IN66_SendCommand(0x4F);       
    253     EPD_2IN66_SendData(0x27);
    254     EPD_2IN66_SendData(0x01);
    255     
    256     EPD_2IN66_ReadBusy();
    257     EPD_2IN66_SendCommand(0x24);
    258     for (UWORD j = 0; j < Height; j++) {
    259         for (UWORD i = 0; i < Width; i++) {
    260             EPD_2IN66_SendData(0xff);
    261         }
    262     }
    263     
    264     EPD_2IN66_SendCommand(0x4E);     
    265     EPD_2IN66_SendData(0x01);
    266 
    267     EPD_2IN66_SendCommand(0x4F);       
    268     EPD_2IN66_SendData(0x27);
    269     EPD_2IN66_SendData(0x01);
    270     EPD_2IN66_ReadBusy();
    271     EPD_2IN66_SendCommand(0x26);
    272     for (UWORD j = 0; j < Height; j++) {
    273         for (UWORD i = 0; i < Width; i++) {
    274             EPD_2IN66_SendData(0xff);
    275         }
    276     }
    277     EPD_2IN66_TurnOnDisplay();
    278 }
    279 
    280 /******************************************************************************
    281 function :	Sends the image buffer in RAM to e-Paper and displays
    282 parameter:
    283 ******************************************************************************/
    284 void EPD_2IN66_Display(UBYTE *Image)
    285 {
    286     UWORD Width, Height;
    287     Width = (EPD_2IN66_WIDTH % 8 == 0)? (EPD_2IN66_WIDTH / 8 ): (EPD_2IN66_WIDTH / 8 + 1);
    288     Height = EPD_2IN66_HEIGHT;
    289    
    290     UDOUBLE Addr = 0;
    291     
    292     EPD_2IN66_SendCommand(0x4E);     
    293     EPD_2IN66_SendData(0x01);
    294 
    295     EPD_2IN66_SendCommand(0x4F);       
    296     EPD_2IN66_SendData(0x27);
    297     EPD_2IN66_SendData(0x01);
    298     
    299     EPD_2IN66_ReadBusy();
    300     // UDOUBLE Offset = ImageName;
    301     EPD_2IN66_SendCommand(0x24);
    302     for (UWORD j = 0; j < Height; j++) {
    303         for (UWORD i = 0; i < Width; i++) {
    304             Addr = i + j * Width;
    305             EPD_2IN66_SendData(Image[Addr]);
    306         }
    307     }
    308     EPD_2IN66_TurnOnDisplay();
    309 }
    310 
    311 
    312 
    313 /******************************************************************************
    314 function :	Enter sleep mode
    315 parameter:
    316 ******************************************************************************/
    317 void EPD_2IN66_Sleep(void)
    318 {
    319     EPD_2IN66_SendCommand(0x10);
    320     EPD_2IN66_SendData(0x01); 
    321     //EPD_2IN66_ReadBusy();
    322 }