waveshare_epaper

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

EPD_1in54b_V2.c (7023B)


      1 /*****************************************************************************
      2 * | File      	:   EPD_1in54b_V2.h
      3 * | Author      :   Waveshare team
      4 * | Function    :   1.54inch e-paper b V2
      5 * | Info        :
      6 *----------------
      7 * |	This version:   V1.0
      8 * | Date        :   2020-04-02
      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_1in54b_V2.h"
     32 #include "Debug.h"
     33 
     34 /******************************************************************************
     35 function :	Software reset
     36 parameter:
     37 ******************************************************************************/
     38 static void EPD_1IN54B_V2_Reset(void)
     39 {
     40     DEV_Digital_Write(EPD_RST_PIN, 1);
     41     DEV_Delay_ms(100);
     42     DEV_Digital_Write(EPD_RST_PIN, 0);
     43     DEV_Delay_ms(10);
     44     DEV_Digital_Write(EPD_RST_PIN, 1);
     45     DEV_Delay_ms(100);
     46 }
     47 
     48 /******************************************************************************
     49 function :	send command
     50 parameter:
     51      Reg : Command register
     52 ******************************************************************************/
     53 static void EPD_1IN54B_V2_SendCommand(UBYTE Reg)
     54 {
     55     DEV_Digital_Write(EPD_DC_PIN, 0);
     56     DEV_Digital_Write(EPD_CS_PIN, 0);
     57     DEV_SPI_WriteByte(Reg);
     58     DEV_Digital_Write(EPD_CS_PIN, 1);
     59 }
     60 
     61 /******************************************************************************
     62 function :	send data
     63 parameter:
     64     Data : Write data
     65 ******************************************************************************/
     66 static void EPD_1IN54B_V2_SendData(UBYTE Data)
     67 {
     68     DEV_Digital_Write(EPD_DC_PIN, 1);
     69     DEV_Digital_Write(EPD_CS_PIN, 0);
     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 static void EPD_1IN54B_V2_ReadBusy(void)
     79 {
     80     Debug("e-Paper busy\r\n");
     81     while(1) {
     82         if(DEV_Digital_Read(EPD_BUSY_PIN) == 0)
     83             break;
     84     }
     85     DEV_Delay_ms(200);
     86     Debug("e-Paper busy release\r\n");
     87 }
     88 
     89 
     90 /******************************************************************************
     91 function :	Initialize the e-Paper register
     92 parameter:
     93 ******************************************************************************/
     94 void EPD_1IN54B_V2_Init(void)
     95 {
     96     EPD_1IN54B_V2_Reset();
     97 
     98     EPD_1IN54B_V2_ReadBusy();   
     99     EPD_1IN54B_V2_SendCommand(0x12);  //SWRESET
    100     EPD_1IN54B_V2_ReadBusy();   
    101 
    102     EPD_1IN54B_V2_SendCommand(0x01); //Driver output control      
    103     EPD_1IN54B_V2_SendData(0xC7);
    104     EPD_1IN54B_V2_SendData(0x00);
    105     EPD_1IN54B_V2_SendData(0x01);
    106 
    107     EPD_1IN54B_V2_SendCommand(0x11); //data entry mode       
    108     EPD_1IN54B_V2_SendData(0x01);
    109 
    110     EPD_1IN54B_V2_SendCommand(0x44); //set Ram-X address start/end position   
    111     EPD_1IN54B_V2_SendData(0x00);
    112     EPD_1IN54B_V2_SendData(0x18);    //0x18-->(24+1)*8=200
    113 
    114     EPD_1IN54B_V2_SendCommand(0x45); //set Ram-Y address start/end position          
    115     EPD_1IN54B_V2_SendData(0xC7);    //0xC7-->(199+1)=200
    116     EPD_1IN54B_V2_SendData(0x00);
    117     EPD_1IN54B_V2_SendData(0x00);
    118     EPD_1IN54B_V2_SendData(0x00); 
    119 
    120     EPD_1IN54B_V2_SendCommand(0x3C); //BorderWavefrom
    121     EPD_1IN54B_V2_SendData(0x05);
    122 
    123     EPD_1IN54B_V2_SendCommand(0x18); //Read built-in temperature sensor
    124     EPD_1IN54B_V2_SendData(0x80);
    125 
    126     EPD_1IN54B_V2_SendCommand(0x4E);   // set RAM x address count to 0;
    127     EPD_1IN54B_V2_SendData(0x00);
    128     EPD_1IN54B_V2_SendCommand(0x4F);   // set RAM y address count to 0X199;    
    129     EPD_1IN54B_V2_SendData(0xC7);
    130     EPD_1IN54B_V2_SendData(0x00);
    131     EPD_1IN54B_V2_ReadBusy();
    132 }
    133 
    134 /******************************************************************************
    135 function :	Clear screen
    136 parameter:
    137 ******************************************************************************/
    138 void EPD_1IN54B_V2_Clear(void)
    139 {
    140     
    141     unsigned int i;	
    142     EPD_1IN54B_V2_SendCommand(0x24);   //write RAM for black(0)/white (1)
    143     for(i=0;i<5000;i++)
    144     {               
    145         EPD_1IN54B_V2_SendData(0xff);
    146     }
    147     EPD_1IN54B_V2_SendCommand(0x26);   //write RAM for black(0)/white (1)
    148     for(i=0;i<5000;i++)
    149     {               
    150         EPD_1IN54B_V2_SendData(0x00);
    151     }
    152     EPD_1IN54B_V2_SendCommand(0x22); //Display Update Control
    153     EPD_1IN54B_V2_SendData(0xF7);   
    154     EPD_1IN54B_V2_SendCommand(0x20);  //Activate Display Update Sequence
    155     EPD_1IN54B_V2_ReadBusy();   
    156 }
    157 
    158 /******************************************************************************
    159 function :	Sends the image buffer in RAM to e-Paper and displays
    160 parameter:
    161 ******************************************************************************/
    162 void EPD_1IN54B_V2_Display(const UBYTE *blackimage, const UBYTE *redimage)
    163 {
    164     UWORD Width, Height;
    165     Width = (EPD_1IN54B_V2_WIDTH % 8 == 0)? (EPD_1IN54B_V2_WIDTH / 8 ): (EPD_1IN54B_V2_WIDTH / 8 + 1);
    166     Height = EPD_1IN54B_V2_HEIGHT;
    167 
    168      unsigned int i;	
    169     EPD_1IN54B_V2_SendCommand(0x24);   //write RAM for black(0)/white (1)
    170     for(i=0;i<Width*Height;i++)
    171     {               
    172         EPD_1IN54B_V2_SendData(blackimage[i]);
    173     }
    174     EPD_1IN54B_V2_SendCommand(0x26);   //write RAM for black(0)/white (1)
    175     for(i=0;i<Width*Height;i++)
    176     {               
    177         EPD_1IN54B_V2_SendData(~redimage[i]);
    178     }
    179     EPD_1IN54B_V2_SendCommand(0x22); //Display Update Control
    180     EPD_1IN54B_V2_SendData(0xF7);   
    181     EPD_1IN54B_V2_SendCommand(0x20);  //Activate Display Update Sequence
    182     EPD_1IN54B_V2_ReadBusy();   
    183 }
    184 
    185 /******************************************************************************
    186 function :	Enter sleep mode
    187 parameter:
    188 ******************************************************************************/
    189 void EPD_1IN54B_V2_Sleep(void)
    190 {
    191     EPD_1IN54B_V2_SendCommand(0x10); //enter deep sleep
    192     EPD_1IN54B_V2_SendData(0x01); 
    193     DEV_Delay_ms(100);
    194 }