waveshare_epaper

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

EPD_7in5b_HD.c (9800B)


      1 /*****************************************************************************
      2 * | File      	:	EPD_7IN5B_HD.c
      3 * | Author      :   Waveshare team
      4 * | Function    :   Electronic paper driver
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2020-04-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_7in5b_HD.h"
     31 #include "Debug.h"
     32 
     33 /******************************************************************************
     34 function :	Software reset
     35 parameter:
     36 ******************************************************************************/
     37 static void EPD_7IN5B_HD_Reset(void)
     38 {
     39     DEV_Digital_Write(EPD_RST_PIN, 1);
     40     DEV_Delay_ms(200);
     41     DEV_Digital_Write(EPD_RST_PIN, 0);
     42     DEV_Delay_ms(10);
     43     DEV_Digital_Write(EPD_RST_PIN, 1);
     44     DEV_Delay_ms(200);
     45 }
     46 
     47 /******************************************************************************
     48 function :	send command
     49 parameter:
     50      Reg : Command register
     51 ******************************************************************************/
     52 static void EPD_7IN5B_HD_SendCommand(UBYTE Reg)
     53 {
     54     DEV_Digital_Write(EPD_DC_PIN, 0);
     55     DEV_Digital_Write(EPD_CS_PIN, 0);
     56     DEV_SPI_WriteByte(Reg);
     57     DEV_Digital_Write(EPD_CS_PIN, 1);
     58 }
     59 
     60 /******************************************************************************
     61 function :	send data
     62 parameter:
     63     Data : Write data
     64 ******************************************************************************/
     65 static void EPD_7IN5B_HD_SendData(UBYTE Data)
     66 {
     67     
     68     DEV_Digital_Write(EPD_CS_PIN, 0);
     69     DEV_Digital_Write(EPD_DC_PIN, 1);
     70     DEV_SPI_WriteByte(Data);
     71     DEV_Digital_Write(EPD_CS_PIN, 1);
     72 }
     73 
     74 /******************************************************************************
     75 function :	Wait until the busy_pin goes LOW
     76 parameter:
     77 ******************************************************************************/
     78 void EPD_7IN5B_HD_WaitUntilIdle(void)
     79 {
     80     Debug("e-Paper busy\r\n");
     81     while(DEV_Digital_Read(EPD_BUSY_PIN)){
     82         DEV_Delay_ms(10);
     83     }
     84     DEV_Delay_ms(200);      
     85     Debug("e-Paper busy release\r\n");
     86 }
     87 
     88 
     89 /******************************************************************************
     90 function :	Turn On Display
     91 parameter:
     92 ******************************************************************************/
     93 static void EPD_7IN5B_HD_TurnOnDisplay(void)
     94 {
     95     EPD_7IN5B_HD_SendCommand(0x22);
     96     EPD_7IN5B_HD_SendData(0xC7);    //Load LUT from MCU(0x32)
     97     EPD_7IN5B_HD_SendCommand(0x20);
     98     DEV_Delay_ms(200);      //!!!The delay here is necessary, 200uS at least!!!     
     99     EPD_7IN5B_HD_WaitUntilIdle();       //waiting for the electronic paper IC to release the idle signal
    100 }
    101 
    102 /******************************************************************************
    103 function :	Initialize the e-Paper register
    104 parameter:
    105 ******************************************************************************/
    106 UBYTE EPD_7IN5B_HD_Init(void)
    107 {
    108     EPD_7IN5B_HD_Reset();
    109 
    110     EPD_7IN5B_HD_SendCommand(0x12); 		  //SWRESET
    111     EPD_7IN5B_HD_WaitUntilIdle();        //waiting for the electronic paper IC to release the idle signal
    112 
    113     EPD_7IN5B_HD_SendCommand(0x46);  // Auto Write RAM
    114     EPD_7IN5B_HD_SendData(0xF7);
    115     EPD_7IN5B_HD_WaitUntilIdle();        //waiting for the electronic paper IC to release the idle signal
    116 
    117     EPD_7IN5B_HD_SendCommand(0x47);  // Auto Write RAM
    118     EPD_7IN5B_HD_SendData(0xF7);
    119     EPD_7IN5B_HD_WaitUntilIdle();        //waiting for the electronic paper IC to release the idle signal
    120 
    121     EPD_7IN5B_HD_SendCommand(0x0C);  // Soft start setting
    122     EPD_7IN5B_HD_SendData(0xAE);
    123     EPD_7IN5B_HD_SendData(0xC7);
    124     EPD_7IN5B_HD_SendData(0xC3);
    125     EPD_7IN5B_HD_SendData(0xC0);
    126     EPD_7IN5B_HD_SendData(0x40);   
    127 
    128     EPD_7IN5B_HD_SendCommand(0x01);  // Set MUX as 527
    129     EPD_7IN5B_HD_SendData(0xAF);
    130     EPD_7IN5B_HD_SendData(0x02);
    131     EPD_7IN5B_HD_SendData(0x01);
    132 
    133     EPD_7IN5B_HD_SendCommand(0x11);  // Data entry mode
    134     EPD_7IN5B_HD_SendData(0x01);
    135 
    136     EPD_7IN5B_HD_SendCommand(0x44);
    137     EPD_7IN5B_HD_SendData(0x00); // RAM x address start at 0
    138     EPD_7IN5B_HD_SendData(0x00);
    139     EPD_7IN5B_HD_SendData(0x6F); // RAM x address end at 36Fh -> 879
    140     EPD_7IN5B_HD_SendData(0x03);
    141     EPD_7IN5B_HD_SendCommand(0x45);
    142     EPD_7IN5B_HD_SendData(0xAF); // RAM y address start at 20Fh;
    143     EPD_7IN5B_HD_SendData(0x02);
    144     EPD_7IN5B_HD_SendData(0x00); // RAM y address end at 00h;
    145     EPD_7IN5B_HD_SendData(0x00);
    146 
    147     EPD_7IN5B_HD_SendCommand(0x3C); // VBD
    148     EPD_7IN5B_HD_SendData(0x01); // LUT1, for white
    149 
    150     EPD_7IN5B_HD_SendCommand(0x18);
    151     EPD_7IN5B_HD_SendData(0X80);
    152     EPD_7IN5B_HD_SendCommand(0x22);
    153     EPD_7IN5B_HD_SendData(0XB1);	//Load Temperature and waveform setting.
    154     EPD_7IN5B_HD_SendCommand(0x20);
    155     EPD_7IN5B_HD_WaitUntilIdle();        //waiting for the electronic paper IC to release the idle signal
    156 
    157     EPD_7IN5B_HD_SendCommand(0x4E); 
    158     EPD_7IN5B_HD_SendData(0x00);
    159     EPD_7IN5B_HD_SendData(0x00);
    160     EPD_7IN5B_HD_SendCommand(0x4F); 
    161     EPD_7IN5B_HD_SendData(0xAF);
    162     EPD_7IN5B_HD_SendData(0x02);
    163     return 0;
    164 }
    165 
    166 /******************************************************************************
    167 function :	Clear screen
    168 parameter:
    169 ******************************************************************************/
    170 void EPD_7IN5B_HD_Clear(void)
    171 {
    172     UWORD Width, Height;
    173     Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
    174     Height = EPD_7IN5B_HD_HEIGHT;
    175     EPD_7IN5B_HD_SendCommand(0x4F); 
    176     EPD_7IN5B_HD_SendData(0xAf);
    177     EPD_7IN5B_HD_SendData(0x02);
    178     UWORD i;
    179     EPD_7IN5B_HD_SendCommand(0x24);
    180     for(i=0; i<Width*Height; i++) {
    181         EPD_7IN5B_HD_SendData(0xff);
    182 
    183     }
    184     EPD_7IN5B_HD_SendCommand(0x26);
    185     for(i=0; i<Width*Height; i++)	{
    186         EPD_7IN5B_HD_SendData(0x00);
    187 
    188     }
    189     EPD_7IN5B_HD_TurnOnDisplay();
    190 }
    191 
    192 void EPD_7IN5B_HD_ClearRed(void)
    193 {
    194     UWORD Width, Height;
    195     Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
    196     Height = EPD_7IN5B_HD_HEIGHT;
    197 
    198     UWORD i;
    199     EPD_7IN5B_HD_SendCommand(0x4F); 
    200     EPD_7IN5B_HD_SendData(0xAF);
    201     EPD_7IN5B_HD_SendData(0x02);
    202     
    203     EPD_7IN5B_HD_SendCommand(0x24);
    204     for(i=0; i<Width*Height; i++) {
    205         EPD_7IN5B_HD_SendData(0xff);
    206 
    207     }
    208     EPD_7IN5B_HD_SendCommand(0x26);
    209     for(i=0; i<Width*Height; i++)	{
    210         EPD_7IN5B_HD_SendData(0xff);
    211 
    212     }
    213     EPD_7IN5B_HD_TurnOnDisplay();
    214 }
    215 
    216 void EPD_7IN5B_HD_ClearBlack(void)
    217 {
    218     UWORD Width, Height;
    219     Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
    220     Height = EPD_7IN5B_HD_HEIGHT;
    221 
    222     UWORD i;
    223     EPD_7IN5B_HD_SendCommand(0x4F); 
    224     EPD_7IN5B_HD_SendData(0xAF);
    225     EPD_7IN5B_HD_SendData(0x02);
    226     
    227     EPD_7IN5B_HD_SendCommand(0x24);
    228     for(i=0; i<Width*Height; i++) {
    229         EPD_7IN5B_HD_SendData(0x00);
    230 
    231     }
    232     EPD_7IN5B_HD_SendCommand(0x26);
    233     for(i=0; i<Width*Height; i++)	{
    234         EPD_7IN5B_HD_SendData(0x00);
    235 
    236     }
    237     EPD_7IN5B_HD_TurnOnDisplay();
    238 }
    239 
    240 /******************************************************************************
    241 function :	Sends the image buffer in RAM to e-Paper and displays
    242 parameter:
    243 ******************************************************************************/
    244 void EPD_7IN5B_HD_Display(const UBYTE *blackimage, const UBYTE *ryimage)
    245 {
    246     UDOUBLE Width, Height;
    247     Width =(EPD_7IN5B_HD_WIDTH % 8 == 0)?(EPD_7IN5B_HD_WIDTH / 8 ):(EPD_7IN5B_HD_WIDTH / 8 + 1);
    248     Height = EPD_7IN5B_HD_HEIGHT;
    249     EPD_7IN5B_HD_SendCommand(0x4F); 
    250     EPD_7IN5B_HD_SendData(0xAF);
    251     EPD_7IN5B_HD_SendData(0x02);
    252  //send black data
    253     EPD_7IN5B_HD_SendCommand(0x24);
    254     for (UDOUBLE j = 0; j < Height; j++) {
    255         for (UDOUBLE i = 0; i < Width; i++) {
    256             EPD_7IN5B_HD_SendData(blackimage[i + j * Width]);
    257         }
    258     }
    259  
    260 
    261     //send red data
    262     EPD_7IN5B_HD_SendCommand(0x26);
    263     for (UDOUBLE j = 0; j < Height; j++) {
    264         for (UDOUBLE i = 0; i < Width; i++) {
    265             EPD_7IN5B_HD_SendData(~ryimage[i + j * Width]);
    266         }
    267     }
    268     EPD_7IN5B_HD_TurnOnDisplay();
    269 }
    270 
    271 /******************************************************************************
    272 function :	Enter sleep mode
    273 parameter:
    274 ******************************************************************************/
    275 void EPD_7IN5B_HD_Sleep(void)
    276 {
    277     EPD_7IN5B_HD_SendCommand(0x10);  	//deep sleep
    278     EPD_7IN5B_HD_SendData(0x01);
    279 }