waveshare_epaper

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

EPD_3in7_V2.c (11667B)


      1 /*****************************************************************************
      2 * | File      :   EPD_3in7_V2.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   3.7inch e-paper
      5 * | Info        :
      6 *----------------
      7 * |This version:   V1.1
      8 * | Date        :   2020-04-24
      9 * | Info        :
     10 * -----------------------------------------------------------------------------
     11 #
     12 # Permission is hereby granted, free of charge, to any person obtaining a copy
     13 # of this software and associated documnetation files (the "Software"), to deal
     14 # in the Software without restriction, including without limitation the rights
     15 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     16 # copies of the Software, and to permit persons to  whom the Software is
     17 # furished to do so, subject to the following conditions:
     18 #
     19 # The above copyright notice and this permission notice shall be included in
     20 # all copies or substantial portions of the Software.
     21 #
     22 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     23 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     24 # FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     25 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     26 # LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     27 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     28 # THE SOFTWARE.
     29 #
     30 ******************************************************************************/
     31 #include "EPD_3in7_V2.h"
     32 #include "Debug.h"
     33 
     34 //////////////////////////////////////full screen update LUT////////////////////////////////////////////
     35 
     36 //1.82s
     37 const unsigned char lut_vcom[] ={
     38     0x00,0x15,0x13,0x00,0x00,0x01,
     39     0x00,0x0A,0x28,0x01,0x00,0x01,
     40     0x00,0x00,0x00,0x00,0x00,0x00,
     41     0x00,0x00,0x00,0x00,0x00,0x00,
     42     0x00,0x00,0x00,0x00,0x00,0x00,
     43     0x00,0x00,0x00,0x00,0x00,0x00,
     44     0x00,0x00,0x00,0x00,0x00,0x00,
     45     0x00,0x00,};
     46 const unsigned char lut_ww[] ={
     47     0x50,0x15,0x13,0x00,0x00,0x01,
     48     0x20,0x0A,0x28,0x01,0x00,0x01,
     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 const unsigned char lut_bw[] ={
     55     0x10,0x0F,0x0F,0x00,0x00,0x01,
     56     0x28,0x05,0x1E,0x19,0x01,0x01,
     57     0x00,0x00,0x00,0x00,0x00,0x00,
     58     0x00,0x00,0x00,0x00,0x00,0x00,
     59     0x00,0x00,0x00,0x00,0x00,0x00,
     60     0x00,0x00,0x00,0x00,0x00,0x00,
     61     0x00,0x00,0x00,0x00,0x00,0x00,};
     62 const unsigned char lut_wb[] ={
     63     0x80,0x0F,0x0F,0x00,0x00,0x01,
     64     0x14,0x05,0x1E,0x19,0x01,0x01,
     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     0x00,0x00,0x00,0x00,0x00,0x00,};
     70 const unsigned char lut_bb[] ={
     71     0xA0,0x15,0x13,0x00,0x00,0x01,
     72     0x10,0x0A,0x28,0x01,0x00,0x01,
     73     0x00,0x00,0x00,0x00,0x00,0x00,
     74     0x00,0x00,0x00,0x00,0x00,0x00,
     75     0x00,0x00,0x00,0x00,0x00,0x00,
     76     0x00,0x00,0x00,0x00,0x00,0x00,
     77     0x00,0x00,0x00,0x00,0x00,0x00,};
     78 
     79 
     80 /////////////////////////////////////partial screen update LUT///////////////////////////////////////////
     81 //300ms
     82 const unsigned char lut_vcom1[] ={
     83     0x00,0x01,0x0E,0x00,0x00,0x01,
     84     0x00,0x00,0x00,0x00,0x00,0x00,
     85     0x00,0x00,0x00,0x00,0x00,0x00,
     86     0x00,0x00,0x00,0x00,0x00,0x00,
     87     0x00,0x00,0x00,0x00,0x00,0x00,
     88     0x00,0x00,0x00,0x00,0x00,0x00,
     89     0x00,0x00,0x00,0x00,0x00,0x00
     90     ,0x00,0x00,};
     91 const unsigned char lut_ww1[] ={
     92     0x00,0x01,0x0E,0x00,0x00,0x01,
     93     0x00,0x00,0x00,0x00,0x00,0x00,
     94     0x00,0x00,0x00,0x00,0x00,0x00,
     95     0x00,0x00,0x00,0x00,0x00,0x00,
     96     0x00,0x00,0x00,0x00,0x00,0x00,
     97     0x00,0x00,0x00,0x00,0x00,0x00,
     98     0x00,0x00,0x00,0x00,0x00,0x00,};
     99 const unsigned char lut_bw1[] ={
    100     0x20,0x01,0x0E,0x00,0x00,0x01,
    101     0x00,0x00,0x00,0x00,0x00,0x00,
    102     0x00,0x00,0x00,0x00,0x00,0x00,
    103     0x00,0x00,0x00,0x00,0x00,0x00,
    104     0x00,0x00,0x00,0x00,0x00,0x00,
    105     0x00,0x00,0x00,0x00,0x00,0x00,
    106     0x00,0x00,0x00,0x00,0x00,0x00,};
    107 const unsigned char lut_wb1[] ={
    108     0x10,0x01,0x0E,0x00,0x00,0x01,
    109     0x00,0x00,0x00,0x00,0x00,0x00,
    110     0x00,0x00,0x00,0x00,0x00,0x00,
    111     0x00,0x00,0x00,0x00,0x00,0x00,
    112     0x00,0x00,0x00,0x00,0x00,0x00,
    113     0x00,0x00,0x00,0x00,0x00,0x00,
    114     0x00,0x00,0x00,0x00,0x00,0x00,};
    115 const unsigned char lut_bb1[] ={
    116     0x00,0x01,0x0E,0x00,0x00,0x01,
    117     0x00,0x00,0x00,0x00,0x00,0x00,
    118     0x00,0x00,0x00,0x00,0x00,0x00,
    119     0x00,0x00,0x00,0x00,0x00,0x00,
    120     0x00,0x00,0x00,0x00,0x00,0x00,
    121     0x00,0x00,0x00,0x00,0x00,0x00,
    122     0x00,0x00,0x00,0x00,0x00,0x00,};
    123 
    124 /******************************************************************************
    125 function :Software reset
    126 parameter:
    127 ******************************************************************************/
    128 static void EPD_3IN7_V2_Reset(void)
    129 {
    130     DEV_Digital_Write(EPD_RST_PIN, 1);
    131     DEV_Delay_ms(200);
    132     DEV_Digital_Write(EPD_RST_PIN, 0);
    133     DEV_Delay_ms(2);
    134     DEV_Digital_Write(EPD_RST_PIN, 1);
    135     DEV_Delay_ms(200);
    136 }
    137 
    138 /******************************************************************************
    139 function :send command
    140 parameter:
    141      Reg : Command register
    142 ******************************************************************************/
    143 static void EPD_3IN7_V2_SendCommand(UBYTE Reg)
    144 {
    145     DEV_Digital_Write(EPD_DC_PIN, 0);
    146     DEV_Digital_Write(EPD_CS_PIN, 0);
    147     DEV_SPI_WriteByte(Reg);
    148     DEV_Digital_Write(EPD_CS_PIN, 1);
    149 }
    150 
    151 /******************************************************************************
    152 function :send data
    153 parameter:
    154     Data : Write data
    155 ******************************************************************************/
    156 static void EPD_3IN7_V2_SendData(UBYTE Data)
    157 {
    158     DEV_Digital_Write(EPD_DC_PIN, 1);
    159     DEV_Digital_Write(EPD_CS_PIN, 0);
    160     DEV_SPI_WriteByte(Data);
    161     DEV_Digital_Write(EPD_CS_PIN, 1);
    162 }
    163 
    164 /******************************************************************************
    165 function :Wait until the busy_pin goes LOW
    166 parameter:
    167 ******************************************************************************/
    168 static void EPD_3IN7_V2_ReadBusy(void)
    169 {
    170     unsigned char busy;
    171     printf("ReadBusy\r\n");
    172     DEV_Delay_ms(1200); 
    173     do{
    174         EPD_3IN7_V2_SendCommand(0x71);
    175         busy = DEV_Digital_Read(EPD_BUSY_PIN);
    176         busy =!(busy & 0x01);        
    177     }while(busy);
    178     printf("ReadBusy END\r\n");
    179     DEV_Delay_ms(200); 
    180 }
    181 
    182 /******************************************************************************
    183 function :set the look-up tables
    184 parameter:
    185 ******************************************************************************/
    186 void EPD_3IN7_V2_Lut(void)
    187 {
    188     // UWORD count;
    189     // EPD_3IN7_V2_SendCommand(0x20);
    190     // for(count=0;count<44;count++)
    191         // {EPD_3IN7_V2_SendData(lut_vcom[count]);}
    192 
    193     // EPD_3IN7_V2_SendCommand(0x21);
    194     // for(count=0;count<42;count++)
    195         // {EPD_3IN7_V2_SendData(lut_ww[count]);}   
    196 
    197     // EPD_3IN7_V2_SendCommand(0x22);
    198     // for(count=0;count<42;count++)
    199         // {EPD_3IN7_V2_SendData(lut_bw[count]);} 
    200 
    201     // EPD_3IN7_V2_SendCommand(0x23);
    202     // for(count=0;count<42;count++)
    203         // {EPD_3IN7_V2_SendData(lut_wb[count]);} 
    204 
    205     // EPD_3IN7_V2_SendCommand(0x24);
    206     // for(count=0;count<42;count++)
    207         // {EPD_3IN7_V2_SendData(lut_bb[count]);} 
    208     
    209     
    210         UWORD count;
    211     EPD_3IN7_V2_SendCommand(0x20);
    212     for(count=0;count<44;count++)
    213         {EPD_3IN7_V2_SendData(lut_vcom[count]);}
    214 
    215     EPD_3IN7_V2_SendCommand(0x21);
    216     for(count=0;count<42;count++)
    217         {EPD_3IN7_V2_SendData(lut_ww[count]);}   
    218 
    219     EPD_3IN7_V2_SendCommand(0x22);
    220     for(count=0;count<42;count++)
    221         {EPD_3IN7_V2_SendData(lut_bw[count]);} 
    222 
    223     EPD_3IN7_V2_SendCommand(0x23);
    224     for(count=0;count<42;count++)
    225         {EPD_3IN7_V2_SendData(lut_wb[count]);} 
    226 
    227     EPD_3IN7_V2_SendCommand(0x24);
    228     for(count=0;count<42;count++)
    229         {EPD_3IN7_V2_SendData(lut_bb[count]);} 
    230 }
    231 
    232 /******************************************************************************
    233 function :Initialize the e-Paper register
    234 parameter:
    235 ******************************************************************************/
    236 void EPD_3IN7_V2_Init(void)
    237 {
    238     EPD_3IN7_V2_Reset();
    239     
    240     EPD_3IN7_V2_SendCommand(0x01); //POWER SETTING
    241     EPD_3IN7_V2_SendData (0x07);
    242     EPD_3IN7_V2_SendData (0x07);   
    243     EPD_3IN7_V2_SendData (0x3f);   
    244     EPD_3IN7_V2_SendData (0x3f);   
    245 
    246     EPD_3IN7_V2_SendCommand(0x06); //booster soft start
    247     EPD_3IN7_V2_SendData (0x17);   //A
    248     EPD_3IN7_V2_SendData (0x17);   //B
    249     EPD_3IN7_V2_SendData (0x1d);   //C 
    250 
    251     EPD_3IN7_V2_SendCommand(0x04);
    252     EPD_3IN7_V2_ReadBusy();
    253 
    254     EPD_3IN7_V2_SendCommand(0x00); //panel setting
    255     EPD_3IN7_V2_SendData(0x3f);    //KW-3f   KWR-2F	BWROTP 0f	BWOTP 1f
    256 
    257     EPD_3IN7_V2_SendCommand(0x30); //PLL setting
    258     EPD_3IN7_V2_SendData (0x04);   //100hz 
    259 
    260     EPD_3IN7_V2_SendCommand(0x61); //resolution setting
    261     EPD_3IN7_V2_SendData (0xf0);   
    262     EPD_3IN7_V2_SendData (0x01);   
    263     EPD_3IN7_V2_SendData (0xa0);
    264 
    265     EPD_3IN7_V2_SendCommand(0x82); //vcom_DC setting
    266     EPD_3IN7_V2_SendData (0x12);
    267 
    268     EPD_3IN7_V2_SendCommand(0X50); //VCOM AND DATA INTERVAL SETTING
    269     EPD_3IN7_V2_SendData(0x21);
    270     EPD_3IN7_V2_SendData(0x07);
    271 }
    272 
    273 /******************************************************************************
    274 function :Clear screen
    275 parameter:
    276 ******************************************************************************/
    277 void EPD_3IN7_V2_Clear(void)
    278 {
    279     UWORD Width, Height;
    280     Width = (EPD_3IN7_V2_WIDTH % 8 == 0)? (EPD_3IN7_V2_WIDTH / 8 ): (EPD_3IN7_V2_WIDTH / 8 + 1);
    281     Height = EPD_3IN7_V2_HEIGHT;
    282     
    283     printf("Width = %d , Height = %d\r\n",Width,Height);
    284     
    285     EPD_3IN7_V2_SendCommand(0x10);
    286     for (UWORD j = 0; j < Height; j++) {
    287         for (UWORD i = 0; i < Width; i++) {
    288             EPD_3IN7_V2_SendData(0xff);
    289         }
    290     }
    291     EPD_3IN7_V2_SendCommand(0x13);
    292     for (UWORD j = 0; j < Height; j++) {
    293         for (UWORD i = 0; i < Width; i++) {
    294             EPD_3IN7_V2_SendData(0xff);
    295         }
    296     }
    297     EPD_3IN7_V2_Lut();
    298     EPD_3IN7_V2_SendCommand(0x12); //DISPLAY REFRESH
    299     DEV_Delay_ms(200);  //!!!The delay here is necessary, 200uS at least!!!     
    300     EPD_3IN7_V2_ReadBusy();
    301     
    302 }
    303 
    304 /******************************************************************************
    305 function :Sends the image buffer in RAM to e-Paper and displays
    306 parameter:
    307 ******************************************************************************/
    308 void EPD_3IN7_V2_Display(const UBYTE *Image)
    309 {
    310     UWORD Width, Height;
    311     Width = (EPD_3IN7_V2_WIDTH % 8 == 0)? (EPD_3IN7_V2_WIDTH / 8 ): (EPD_3IN7_V2_WIDTH / 8 + 1);
    312     Height = EPD_3IN7_V2_HEIGHT;
    313     
    314     EPD_3IN7_V2_SendCommand(0x10);
    315     for (UWORD j = 0; j < Height; j++) {
    316         for (UWORD i = 0; i < Width; i++) {
    317             EPD_3IN7_V2_SendData(0xff);
    318         }
    319     }
    320     EPD_3IN7_V2_SendCommand(0x13);
    321     for (UWORD j = 0; j < Height; j++) {
    322         for (UWORD i = 0; i < Width; i++) {
    323             EPD_3IN7_V2_SendData(Image[i + j * Width]);
    324         }
    325     }
    326     EPD_3IN7_V2_Lut();
    327     EPD_3IN7_V2_SendCommand(0x12); //DISPLAY REFRESH
    328     DEV_Delay_ms(200);  //!!!The delay here is necessary, 200uS at least!!!     
    329     EPD_3IN7_V2_ReadBusy();
    330 }
    331 
    332 /******************************************************************************
    333 function :Enter sleep mode
    334 parameter:
    335 ******************************************************************************/
    336 void EPD_3IN7_V2_Sleep(void)
    337 {
    338     EPD_3IN7_V2_SendCommand(0X50);
    339     EPD_3IN7_V2_SendData(0xf7);
    340     
    341     EPD_3IN7_V2_SendCommand(0X02); //power off
    342     EPD_3IN7_V2_ReadBusy();
    343     EPD_3IN7_V2_SendCommand(0X07); //deep sleep
    344     EPD_3IN7_V2_SendData(0xA5);
    345 }